From c440fc5024f778c0f063d7651018f3c3b5ffce77 Mon Sep 17 00:00:00 2001 From: OneWay Date: Sun, 1 Nov 2026 21:01:43 +0800 Subject: [PATCH] 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. --- .../feature/import/DictionaryImportManager.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/src/main/java/com/example/research/feature/import/DictionaryImportManager.kt b/app/src/main/java/com/example/research/feature/import/DictionaryImportManager.kt index 28f781a..bb43a0b 100644 --- a/app/src/main/java/com/example/research/feature/import/DictionaryImportManager.kt +++ b/app/src/main/java/com/example/research/feature/import/DictionaryImportManager.kt @@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.* import kotlinx.coroutines.ensureActive import java.io.File import java.util.Collections +import kotlin.time.Duration.Companion.seconds class DictionaryImportManager( private val application: Application @@ -19,6 +20,10 @@ class DictionaryImportManager( private val mutableImportState = MutableStateFlow(ImportState.Idle) val importState: StateFlow = mutableImportState.asStateFlow() + companion object { + private val TERMINAL_STATE_DURATION = 2.seconds + } + private val managerScope = CoroutineScope(SupervisorJob() + Dispatchers.Main) private var importJob: Job? = null private val importedFiles = Collections.synchronizedList(mutableListOf()) @@ -130,6 +135,9 @@ class DictionaryImportManager( ) mutableImportState.value = terminalState onTerminal?.invoke(terminalState) + if (terminalState is ImportState.Error) { + expireErrorStateToIdle(terminalState) + } } catch (e: CancellationException) { cleanupImportedFiles() @@ -141,9 +149,17 @@ class DictionaryImportManager( ) mutableImportState.value = 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? { var fileName: String? = null application.contentResolver.query(uri, null, null, null, null)?.use { cursor ->