fix: open Settings immediately on cold start when no dictionaries

Seed and synchronously read hadNoDictionaries before setContent so
Settings shows from the first frame until the startup scan confirms.
This commit is contained in:
2026-09-04 19:09:19 +08:00
parent 0f3d4b965c
commit c79be491cc
4 changed files with 43 additions and 3 deletions
@@ -83,6 +83,7 @@ class MainActivity : ComponentActivity() {
val app = application as ReSearchApplication val app = application as ReSearchApplication
val initialTheme = loadInitialTheme(app) val initialTheme = loadInitialTheme(app)
val initialLanguage = loadInitialLanguage(app) val initialLanguage = loadInitialLanguage(app)
val initialHadNoDictionaries = loadInitialHadNoDictionaries(app)
handleIntent(intent) handleIntent(intent)
setContent { setContent {
@@ -104,7 +105,8 @@ class MainActivity : ComponentActivity() {
) { ) {
AppNavigation( AppNavigation(
searchViewModel = searchViewModel, searchViewModel = searchViewModel,
settingsViewModel = settingsViewModel settingsViewModel = settingsViewModel,
seedNoDictionaries = initialHadNoDictionaries
) )
} }
} }
@@ -165,6 +167,16 @@ class MainActivity : ComponentActivity() {
} }
} }
private fun loadInitialHadNoDictionaries(app: ReSearchApplication): Boolean {
return try {
runBlocking {
app.preferencesManager.hadNoDictionaries.first()
}
} catch (e: Exception) {
true
}
}
override fun onNewIntent(intent: Intent) { override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent) super.onNewIntent(intent)
handleIntent(intent) handleIntent(intent)
@@ -25,6 +25,7 @@ class PreferencesManager(private val context: Context) {
val IS_LANGUAGE_EXPANDED = booleanPreferencesKey("is_language_expanded") val IS_LANGUAGE_EXPANDED = booleanPreferencesKey("is_language_expanded")
val IS_DICTIONARIES_EXPANDED = booleanPreferencesKey("is_dictionaries_expanded") val IS_DICTIONARIES_EXPANDED = booleanPreferencesKey("is_dictionaries_expanded")
val DISABLED_DICTIONARY_PATHS = stringSetPreferencesKey("disabled_dictionary_paths") val DISABLED_DICTIONARY_PATHS = stringSetPreferencesKey("disabled_dictionary_paths")
val HAD_NO_DICTIONARIES = booleanPreferencesKey("had_no_dictionaries")
} }
private val json = Json { ignoreUnknownKeys = true } private val json = Json { ignoreUnknownKeys = true }
@@ -67,6 +68,17 @@ class PreferencesManager(private val context: Context) {
preferences[PreferencesKeys.DISABLED_DICTIONARY_PATHS] ?: emptySet() preferences[PreferencesKeys.DISABLED_DICTIONARY_PATHS] ?: emptySet()
} }
val hadNoDictionaries: Flow<Boolean> = context.dataStore.data
.map { preferences ->
preferences[PreferencesKeys.HAD_NO_DICTIONARIES] ?: true
}
suspend fun setHadNoDictionaries(value: Boolean) {
context.dataStore.edit { preferences ->
preferences[PreferencesKeys.HAD_NO_DICTIONARIES] = value
}
}
suspend fun setDictionaryActive(path: String, isActive: Boolean) { suspend fun setDictionaryActive(path: String, isActive: Boolean) {
context.dataStore.edit { preferences -> context.dataStore.edit { preferences ->
val disabledPaths = preferences[PreferencesKeys.DISABLED_DICTIONARY_PATHS] val disabledPaths = preferences[PreferencesKeys.DISABLED_DICTIONARY_PATHS]
@@ -33,7 +33,8 @@ enum class Screen {
@Composable @Composable
fun AppNavigation( fun AppNavigation(
searchViewModel: SearchViewModel, searchViewModel: SearchViewModel,
settingsViewModel: SettingsViewModel settingsViewModel: SettingsViewModel,
seedNoDictionaries: Boolean = false
) { ) {
val screenStack = rememberSaveable( val screenStack = rememberSaveable(
saver = listSaver( saver = listSaver(
@@ -84,8 +85,9 @@ fun AppNavigation(
val isOperationActive = stickyPipelineActive val isOperationActive = stickyPipelineActive
val hasConfirmedNoDictionaries = val hasConfirmedNoDictionaries =
settingsState.hasCompletedStartupScan && settingsState.dictionaries.isEmpty() settingsState.hasCompletedStartupScan && settingsState.dictionaries.isEmpty()
val hasSeededNoDictionaries = seedNoDictionaries && !settingsState.hasCompletedStartupScan
NavigationState( NavigationState(
shouldShowSettings = hasConfirmedNoDictionaries || isOperationActive, shouldShowSettings = hasConfirmedNoDictionaries || hasSeededNoDictionaries || isOperationActive,
showBackButtonInSettings = settingsState.dictionaries.isNotEmpty() && !isOperationActive, showBackButtonInSettings = settingsState.dictionaries.isNotEmpty() && !isOperationActive,
isOperationActive = isOperationActive, isOperationActive = isOperationActive,
) )
@@ -89,9 +89,20 @@ class SettingsViewModel(
init { init {
setupStateObservation() setupStateObservation()
observeDictionariesForSeedFlag()
initialize() initialize()
} }
private fun observeDictionariesForSeedFlag() {
viewModelScope.launch {
localDictionaryRepository.dictionaries.collect { dictionaries ->
if (hasCompletedStartupScan.value) {
preferencesManager.setHadNoDictionaries(dictionaries.isEmpty())
}
}
}
}
private fun setupStateObservation() { private fun setupStateObservation() {
viewModelScope.launch { viewModelScope.launch {
var wasIndexing = false var wasIndexing = false
@@ -245,6 +256,9 @@ class SettingsViewModel(
android.util.Log.w("SettingsViewModel", "Failed to load language preference: ${e.message}") android.util.Log.w("SettingsViewModel", "Failed to load language preference: ${e.message}")
} }
hasCompletedStartupScan.value = true hasCompletedStartupScan.value = true
preferencesManager.setHadNoDictionaries(
localDictionaryRepository.dictionaries.value.isEmpty()
)
} }
} }
} }