refactor(main): expose cold-start preferences as an InitialLaunch flow
Read the theme, locale, empty-library seed and section expansion once in the application and collect them as state, instead of blocking the UI thread with synchronous reads before setContent.
This commit is contained in:
1 parent
32349e2358
commit
af147ada50
2 files changed
+47
-77
No files matched your search
@@ -3,7 +3,9 @@ package com.example.research
|
||||
import android.app.Application
|
||||
import com.example.research.common.progress.DictionaryProgressStateHolder
|
||||
import com.example.research.common.util.NotificationHelper
|
||||
import com.example.research.core.domain.model.AppTheme
|
||||
import com.example.research.data.local.preferences.PreferencesManager
|
||||
import com.example.research.data.local.preferences.SectionExpansionStates
|
||||
import com.example.research.data.repository.LocalDictionaryRepository
|
||||
import com.example.research.feature.download.DownloadManager
|
||||
import com.example.research.feature.download.repository.DictionaryRepository
|
||||
@@ -12,10 +14,25 @@ import com.example.research.feature.import.DictionaryImportManager
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Protocol
|
||||
|
||||
/**
|
||||
* Values a cold start needs before the first composed frame: the initial
|
||||
* theme, locale, empty-library navigation seed, and settings expansion state.
|
||||
*/
|
||||
data class InitialLaunch(
|
||||
val theme: AppTheme,
|
||||
val language: String,
|
||||
val seedNoDictionaries: Boolean,
|
||||
val sectionExpansion: SectionExpansionStates,
|
||||
)
|
||||
|
||||
class ReSearchApplication : Application() {
|
||||
|
||||
lateinit var preferencesManager: PreferencesManager
|
||||
@@ -43,6 +60,9 @@ class ReSearchApplication : Application() {
|
||||
private set
|
||||
private val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
||||
|
||||
private val _initialLaunch = MutableStateFlow<InitialLaunch?>(null)
|
||||
val initialLaunch: StateFlow<InitialLaunch?> = _initialLaunch.asStateFlow()
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
@@ -53,6 +73,9 @@ class ReSearchApplication : Application() {
|
||||
applicationScope.launch(Dispatchers.IO) {
|
||||
preferencesManager.sanitizeDictionarySources()
|
||||
}
|
||||
applicationScope.launch(Dispatchers.IO) {
|
||||
_initialLaunch.value = loadInitialLaunch()
|
||||
}
|
||||
localDictionaryRepository = LocalDictionaryRepository(this, preferencesManager = preferencesManager)
|
||||
|
||||
okHttpClient = OkHttpClient.Builder()
|
||||
@@ -121,6 +144,23 @@ class ReSearchApplication : Application() {
|
||||
downloadManager.onTerminal = dictionaryPipelineCoordinator::onDownloadTerminal
|
||||
}
|
||||
|
||||
private suspend fun loadInitialLaunch(): InitialLaunch {
|
||||
val defaultExpansions = SectionExpansionStates(
|
||||
isThemeExpanded = false,
|
||||
isLanguageExpanded = false,
|
||||
isDictionariesExpanded = true,
|
||||
)
|
||||
return InitialLaunch(
|
||||
theme = runCatching { AppTheme.valueOf(preferencesManager.themeMode.first()) }
|
||||
.getOrDefault(AppTheme.SYSTEM),
|
||||
language = runCatching { preferencesManager.language.first() }.getOrDefault("system"),
|
||||
seedNoDictionaries = runCatching { preferencesManager.hadNoDictionaries.first() }
|
||||
.getOrDefault(true),
|
||||
sectionExpansion = runCatching { preferencesManager.sectionExpansionStates() }
|
||||
.getOrDefault(defaultExpansions),
|
||||
)
|
||||
}
|
||||
|
||||
override fun onTrimMemory(level: Int) {
|
||||
super.onTrimMemory(level)
|
||||
if (::localDictionaryRepository.isInitialized) {
|
||||
|
||||
Reference in new issue
Block a user