fix(settings): evaluate update availability on operations and startup

Re-evaluate after indexing completes, persist the last status with its check date, and restore it on startup. When the saved date is older than today the status is re-evaluated once, so new dictionary updates stay visible without a server request on every launch.
This commit is contained in:
OneWay committed 2026-09-15 16:55:00 +08:00
1 parent 830f99578a
commit 2394c7cf5e
2 files changed
+46 -4

No files matched your search

@@ -33,6 +33,7 @@ class PreferencesManager(private val context: Context) {
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") val HAD_NO_DICTIONARIES = booleanPreferencesKey("had_no_dictionaries")
val DICTIONARY_STATUS = stringPreferencesKey("dictionary_status")
} }
private val json = Json { ignoreUnknownKeys = true } 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 = suspend fun sectionExpansionStates(): SectionExpansionStates =
context.dataStore.data.first().let { preferences -> context.dataStore.data.first().let { preferences ->
SectionExpansionStates( SectionExpansionStates(
@@ -7,6 +7,7 @@ import androidx.lifecycle.viewModelScope
import com.example.research.DictionaryStatus import com.example.research.DictionaryStatus
import com.example.research.R import com.example.research.R
import com.example.research.common.progress.DictionaryProgressStateHolder 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.AppTheme
import com.example.research.core.domain.model.Dictionary import com.example.research.core.domain.model.Dictionary
import com.example.research.core.domain.model.DictionarySource import com.example.research.core.domain.model.DictionarySource
@@ -100,9 +101,26 @@ class SettingsViewModel(
init { init {
setupStateObservation() setupStateObservation()
observeDictionariesForSeedFlag() observeDictionariesForSeedFlag()
persistDictionaryStatus()
initialize() 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() { private fun observeDictionariesForSeedFlag() {
viewModelScope.launch { viewModelScope.launch {
localDictionaryRepository.dictionaries.collect { dictionaries -> localDictionaryRepository.dictionaries.collect { dictionaries ->
@@ -161,7 +179,9 @@ class SettingsViewModel(
if (wasIndexing && !isIndexing) { if (wasIndexing && !isIndexing) {
if (isDownloadInProgress) { if (isDownloadInProgress) {
isDownloadInProgress = false isDownloadInProgress = false
dictionaryStatus.value = DictionaryStatus.UpToDate dictionaryStatus.value = withContext(Dispatchers.IO) {
evaluateDictionaryStatus()
}
} }
} }
wasIndexing = isIndexing wasIndexing = isIndexing
@@ -561,9 +581,21 @@ class SettingsViewModel(
dictionaryStatus.value = DictionaryStatus.UpToDate dictionaryStatus.value = DictionaryStatus.UpToDate
isDownloadInProgress = false isDownloadInProgress = false
} else { } else {
dictionaryStatus.value = DictionaryStatus.Checking val savedStatus = preferencesManager.dictionaryStatus()
val status = withContext(Dispatchers.IO) { evaluateDictionaryStatus() } val savedStatusName = savedStatus?.substringBefore('|')
dictionaryStatus.value = status 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) { if (result.data > 0) {