Skip to content

Commit e889975

Browse files
authored
Fix app cold start jank (meshtastic#3492)
1 parent 9f91b73 commit e889975

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

core/datastore/src/main/kotlin/org/meshtastic/core/datastore/UiPreferencesDataSource.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ class UiPreferencesDataSource @Inject constructor(private val dataStore: DataSto
4848

4949
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
5050

51-
val appIntroCompleted: StateFlow<Boolean> = dataStore.prefStateFlow(key = APP_INTRO_COMPLETED, default = false)
51+
// Start this flow eagerly, so app intro doesn't flash (when disabled) on cold app start.
52+
val appIntroCompleted: StateFlow<Boolean> =
53+
dataStore.prefStateFlow(key = APP_INTRO_COMPLETED, default = false, started = SharingStarted.Eagerly)
5254

5355
// Default value for AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
5456
val theme: StateFlow<Int> = dataStore.prefStateFlow(key = THEME, default = -1)
@@ -87,8 +89,11 @@ class UiPreferencesDataSource @Inject constructor(private val dataStore: DataSto
8789
dataStore.setPref(key = SHOW_IGNORED, value = value)
8890
}
8991

90-
private fun <T : Any> DataStore<Preferences>.prefStateFlow(key: Preferences.Key<T>, default: T): StateFlow<T> =
91-
data.map { it[key] ?: default }.stateIn(scope = scope, started = SharingStarted.Lazily, initialValue = default)
92+
private fun <T : Any> DataStore<Preferences>.prefStateFlow(
93+
key: Preferences.Key<T>,
94+
default: T,
95+
started: SharingStarted = SharingStarted.Lazily,
96+
): StateFlow<T> = data.map { it[key] ?: default }.stateIn(scope = scope, started = started, initialValue = default)
9297

9398
private fun <T : Any> DataStore<Preferences>.setPref(key: Preferences.Key<T>, value: T) {
9499
scope.launch { edit { it[key] = value } }

0 commit comments

Comments
 (0)