fix(import): expire the sticky import error state

A failed local import left the error in the state flow forever, so the
dictionaries section replayed it as a snackbar on every recomposition
and screens created later picked it up long after the failure. Terminal
import errors now decay to idle after the same grace period as
downloads, unless a newer state has replaced them.
This commit is contained in:
OneWay committed 2026-11-01 21:01:43 +08:00
1 parent f2664e608e
commit c440fc5024
1 file changed
+16
@@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.*
import kotlinx.coroutines.ensureActive import kotlinx.coroutines.ensureActive
import java.io.File import java.io.File
import java.util.Collections import java.util.Collections
import kotlin.time.Duration.Companion.seconds
class DictionaryImportManager( class DictionaryImportManager(
private val application: Application private val application: Application
@@ -19,6 +20,10 @@ class DictionaryImportManager(
private val mutableImportState = MutableStateFlow<ImportState>(ImportState.Idle) private val mutableImportState = MutableStateFlow<ImportState>(ImportState.Idle)
val importState: StateFlow<ImportState> = mutableImportState.asStateFlow() val importState: StateFlow<ImportState> = mutableImportState.asStateFlow()
companion object {
private val TERMINAL_STATE_DURATION = 2.seconds
}
private val managerScope = CoroutineScope(SupervisorJob() + Dispatchers.Main) private val managerScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
private var importJob: Job? = null private var importJob: Job? = null
private val importedFiles = Collections.synchronizedList(mutableListOf<File>()) private val importedFiles = Collections.synchronizedList(mutableListOf<File>())
@@ -130,6 +135,9 @@ class DictionaryImportManager(
) )
mutableImportState.value = terminalState mutableImportState.value = terminalState
onTerminal?.invoke(terminalState) onTerminal?.invoke(terminalState)
if (terminalState is ImportState.Error) {
expireErrorStateToIdle(terminalState)
}
} catch (e: CancellationException) { } catch (e: CancellationException) {
cleanupImportedFiles() cleanupImportedFiles()
@@ -141,9 +149,17 @@ class DictionaryImportManager(
) )
mutableImportState.value = errorState mutableImportState.value = errorState
onTerminal?.invoke(errorState) onTerminal?.invoke(errorState)
expireErrorStateToIdle(errorState)
} }
} }
private suspend fun expireErrorStateToIdle(errorState: ImportState.Error) {
delay(TERMINAL_STATE_DURATION)
if (mutableImportState.value === errorState) {
mutableImportState.value = ImportState.Idle
}
}
private fun getFileName(uri: Uri): String? { private fun getFileName(uri: Uri): String? {
var fileName: String? = null var fileName: String? = null
application.contentResolver.query(uri, null, null, null, null)?.use { cursor -> application.contentResolver.query(uri, null, null, null, null)?.use { cursor ->