diff --git a/app/src/main/java/com/example/research/data/local/preferences/PreferencesManager.kt b/app/src/main/java/com/example/research/data/local/preferences/PreferencesManager.kt index 7981f4f..0ba31ec 100644 --- a/app/src/main/java/com/example/research/data/local/preferences/PreferencesManager.kt +++ b/app/src/main/java/com/example/research/data/local/preferences/PreferencesManager.kt @@ -33,6 +33,7 @@ class PreferencesManager(private val context: Context) { val IS_DICTIONARIES_EXPANDED = booleanPreferencesKey("is_dictionaries_expanded") val DISABLED_DICTIONARY_PATHS = stringSetPreferencesKey("disabled_dictionary_paths") val HAD_NO_DICTIONARIES = booleanPreferencesKey("had_no_dictionaries") + val DICTIONARY_STATUS = stringPreferencesKey("dictionary_status") } private val json = Json { ignoreUnknownKeys = true } @@ -86,6 +87,15 @@ class PreferencesManager(private val context: Context) { } } + suspend fun dictionaryStatus(): String? = + context.dataStore.data.first()[PreferencesKeys.DICTIONARY_STATUS] + + suspend fun saveDictionaryStatus(status: String) { + context.dataStore.edit { preferences -> + preferences[PreferencesKeys.DICTIONARY_STATUS] = status + } + } + suspend fun sectionExpansionStates(): SectionExpansionStates = context.dataStore.data.first().let { preferences -> SectionExpansionStates( diff --git a/app/src/main/java/com/example/research/ui/settings/SettingsViewModel.kt b/app/src/main/java/com/example/research/ui/settings/SettingsViewModel.kt index 59e1813..25e90a7 100644 --- a/app/src/main/java/com/example/research/ui/settings/SettingsViewModel.kt +++ b/app/src/main/java/com/example/research/ui/settings/SettingsViewModel.kt @@ -7,6 +7,7 @@ import androidx.lifecycle.viewModelScope import com.example.research.DictionaryStatus import com.example.research.R import com.example.research.common.progress.DictionaryProgressStateHolder +import com.example.research.common.util.DateUtils import com.example.research.core.domain.model.AppTheme import com.example.research.core.domain.model.Dictionary import com.example.research.core.domain.model.DictionarySource @@ -100,9 +101,26 @@ class SettingsViewModel( init { setupStateObservation() observeDictionariesForSeedFlag() + persistDictionaryStatus() initialize() } + private fun persistDictionaryStatus() { + viewModelScope.launch { + dictionaryStatus.collect { status -> + val persistedName = when (status) { + DictionaryStatus.UpToDate -> "UpToDate" + DictionaryStatus.NeedsUpdate -> "NeedsUpdate" + DictionaryStatus.Empty -> "Empty" + else -> null + } + if (persistedName != null) { + preferencesManager.saveDictionaryStatus("$persistedName|${DateUtils.getCurrentDateString()}") + } + } + } + } + private fun observeDictionariesForSeedFlag() { viewModelScope.launch { localDictionaryRepository.dictionaries.collect { dictionaries -> @@ -161,7 +179,9 @@ class SettingsViewModel( if (wasIndexing && !isIndexing) { if (isDownloadInProgress) { isDownloadInProgress = false - dictionaryStatus.value = DictionaryStatus.UpToDate + dictionaryStatus.value = withContext(Dispatchers.IO) { + evaluateDictionaryStatus() + } } } wasIndexing = isIndexing @@ -561,9 +581,21 @@ class SettingsViewModel( dictionaryStatus.value = DictionaryStatus.UpToDate isDownloadInProgress = false } else { - dictionaryStatus.value = DictionaryStatus.Checking - val status = withContext(Dispatchers.IO) { evaluateDictionaryStatus() } - dictionaryStatus.value = status + val savedStatus = preferencesManager.dictionaryStatus() + val savedStatusName = savedStatus?.substringBefore('|') + val savedStatusDate = savedStatus?.substringAfter('|', "") + val restoredStatus = when (savedStatusName) { + "UpToDate" -> DictionaryStatus.UpToDate + "NeedsUpdate" -> DictionaryStatus.NeedsUpdate + else -> null + } + if (restoredStatus != null && savedStatusDate == DateUtils.getCurrentDateString()) { + dictionaryStatus.value = restoredStatus + } else { + dictionaryStatus.value = DictionaryStatus.Checking + val status = withContext(Dispatchers.IO) { evaluateDictionaryStatus() } + dictionaryStatus.value = status + } } if (result.data > 0) {