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 ->