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:
1 parent
830f99578a
commit
2394c7cf5e
2 files changed
+43
-1
No files matched your search
@@ -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(
|
||||
|
||||
@@ -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
|
||||
@@ -560,11 +580,23 @@ class SettingsViewModel(
|
||||
} else if (isDownloadInProgress) {
|
||||
dictionaryStatus.value = DictionaryStatus.UpToDate
|
||||
isDownloadInProgress = false
|
||||
} else {
|
||||
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) {
|
||||
viewModelScope.launch(Dispatchers.Default) {
|
||||
|
||||
Reference in new issue
Block a user