Hold the first frame until the stored appearance is read (#118)
This commit is contained in:
1 parent
c2712a7d99
commit
7b8de39fab
1 file changed
+40
-19
@@ -3,25 +3,29 @@ package de.jeanlucmakiola.agendula
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.os.SystemClock
|
||||
import android.text.format.DateFormat
|
||||
import android.view.View
|
||||
import android.view.ViewTreeObserver
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.foundation.isSystemInDarkTheme
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.runtime.SideEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.core.net.toUri
|
||||
import de.jeanlucmakiola.agendula.data.prefs.Settings
|
||||
import de.jeanlucmakiola.agendula.data.prefs.SettingsPrefs
|
||||
import de.jeanlucmakiola.agendula.data.prefs.is24Hour
|
||||
import de.jeanlucmakiola.agendula.ui.common.LocalFirstDayOfWeek
|
||||
import de.jeanlucmakiola.agendula.ui.common.LocalUse24HourFormat
|
||||
import de.jeanlucmakiola.agendula.ui.common.localeFirstDayOfWeek
|
||||
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
@@ -33,21 +37,18 @@ import de.jeanlucmakiola.agendula.ui.navigation.AppShortcuts
|
||||
import de.jeanlucmakiola.agendula.ui.RootScreen
|
||||
import de.jeanlucmakiola.agendula.ui.crash.CrashReportActivity
|
||||
import de.jeanlucmakiola.agendula.ui.navigation.NavRequest
|
||||
import de.jeanlucmakiola.agendula.ui.settings.SettingsViewModel
|
||||
import de.jeanlucmakiola.agendula.ui.theme.AgendulaTheme
|
||||
import de.jeanlucmakiola.agendula.ui.theme.SystemTheme
|
||||
import de.jeanlucmakiola.floret.crash.CrashReportDialog
|
||||
import de.jeanlucmakiola.floret.crash.CrashReporter
|
||||
import de.jeanlucmakiola.floret.crash.submitCrashReport
|
||||
import de.jeanlucmakiola.floret.prefs.ActionStyle
|
||||
import de.jeanlucmakiola.floret.prefs.LayoutDensity
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* Single activity. The theme follows [SettingsViewModel]; [RootScreen] is the
|
||||
* Single activity. The theme follows the stored [Settings]; [RootScreen] is the
|
||||
* (replaceable) functional scaffold over the real data layer. Notification taps
|
||||
* arrive as a [NavRequest] (see [navRequestOf]).
|
||||
*/
|
||||
@@ -66,6 +67,9 @@ class MainActivity : ComponentActivity() {
|
||||
// A notification tap's destination, handed to the nav host and cleared once taken.
|
||||
private var navRequest by mutableStateOf<NavRequest?>(null)
|
||||
|
||||
// Set once the stored settings reach the composition; see [holdFirstFrameForAppearance].
|
||||
private var appearanceReady = false
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
@@ -100,21 +104,17 @@ class MainActivity : ComponentActivity() {
|
||||
if (savedInstanceState == null) {
|
||||
launchHooks.forEach { hook -> lifecycleScope.launch { runCatching { hook.onLaunch(intent) } } }
|
||||
}
|
||||
holdFirstFrameForAppearance()
|
||||
setContent {
|
||||
val settingsViewModel: SettingsViewModel = hiltViewModel()
|
||||
val ui by settingsViewModel.state.collectAsStateWithLifecycle()
|
||||
val darkTheme = ui.settings.themeMode.isDark(isSystemInDarkTheme())
|
||||
// The stored settings, not a placeholder: until they're read the first
|
||||
// frame is held, so a compact or shaped layout never opens on the defaults.
|
||||
val stored by remember { settingsPrefs.settings }.collectAsStateWithLifecycle(initialValue = null)
|
||||
if (stored != null) SideEffect { appearanceReady = true }
|
||||
val settings = stored ?: Settings()
|
||||
val context = LocalContext.current
|
||||
val use24Hour = ui.settings.timeFormat.is24Hour(DateFormat.is24HourFormat(context))
|
||||
val firstDayOfWeek = ui.settings.weekStart ?: localeFirstDayOfWeek()
|
||||
AgendulaTheme(
|
||||
darkTheme = darkTheme,
|
||||
dynamicColor = ui.settings.dynamicColor,
|
||||
pureBlack = ui.settings.themeMode.isPureBlack,
|
||||
compactLayout = ui.settings.density == LayoutDensity.COMPACT,
|
||||
shapedActions = ui.settings.actionStyle == ActionStyle.SHAPES,
|
||||
reduceMotion = ui.settings.reduceMotion,
|
||||
) {
|
||||
val use24Hour = settings.timeFormat.is24Hour(DateFormat.is24HourFormat(context))
|
||||
val firstDayOfWeek = settings.weekStart ?: localeFirstDayOfWeek()
|
||||
AgendulaTheme(settings) {
|
||||
CompositionLocalProvider(
|
||||
LocalUse24HourFormat provides use24Hour,
|
||||
LocalFirstDayOfWeek provides firstDayOfWeek,
|
||||
@@ -144,6 +144,24 @@ class MainActivity : ComponentActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps the launch window up until the stored appearance is in the first
|
||||
* composition, at most [APPEARANCE_WAIT_MS], so a slow read can't hold the launch.
|
||||
*/
|
||||
private fun holdFirstFrameForAppearance() {
|
||||
val content = findViewById<View>(android.R.id.content)
|
||||
val since = SystemClock.uptimeMillis()
|
||||
content.viewTreeObserver.addOnPreDrawListener(
|
||||
object : ViewTreeObserver.OnPreDrawListener {
|
||||
override fun onPreDraw(): Boolean {
|
||||
val draw = appearanceReady || SystemClock.uptimeMillis() - since > APPEARANCE_WAIT_MS
|
||||
if (draw) content.viewTreeObserver.removeOnPreDrawListener(this)
|
||||
return draw
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
setIntent(intent)
|
||||
@@ -291,3 +309,6 @@ class MainActivity : ComponentActivity() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The longest the first frame waits for the stored appearance. */
|
||||
private const val APPEARANCE_WAIT_MS = 600L
|
||||
Reference in new issue
Block a user