Yokan / rebuild
Course 1 8624561

DownloadsSettingsViewModel.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/downloads/DownloadsSettingsViewModel.kt

implementation87 lines
1package eu.friedcutlet.yokan.ui.screens.settings.downloads23import androidx.lifecycle.ViewModel4import androidx.lifecycle.viewModelScope5import eu.friedcutlet.yokan.data.YokanRepository6import eu.friedcutlet.yokan.database.DbCategory7import eu.friedcutlet.yokan.database.DbSource8import eu.friedcutlet.yokan.services.download.DownloadAutomation9import eu.friedcutlet.yokan.services.download.DownloadCleanupItem10import eu.friedcutlet.yokan.services.catchingNonCancellation11import eu.friedcutlet.yokan.services.library.LocalLibraryController12import eu.friedcutlet.yokan.services.library.LocalLibraryScanState13import kotlinx.coroutines.flow.StateFlow14import kotlinx.coroutines.flow.MutableStateFlow15import kotlinx.coroutines.flow.asStateFlow16import kotlinx.coroutines.flow.map17import kotlinx.coroutines.flow.distinctUntilChanged18import kotlinx.coroutines.flow.update19import kotlinx.coroutines.CoroutineDispatcher20import kotlinx.coroutines.Dispatchers21import kotlinx.coroutines.withContext22import kotlinx.coroutines.CancellationException23import kotlinx.coroutines.launch2425/** Local-folder rescans for the Downloads page; the scan state is app-wide, owned by [LocalLibraryController]. */26internal class DownloadsSettingsViewModel(27    private val localLibrary: LocalLibraryController,28    val defaultDownloadRoot: String,29    repository: YokanRepository,30    private val automation: DownloadAutomation,31    private val workDispatcher: CoroutineDispatcher = Dispatchers.Default,32) : ViewModel() {33    val scan: StateFlow<LocalLibraryScanState> = localLibrary.scan34    private val _policy = MutableStateFlow(DownloadPolicyUiState())35    val policy = _policy.asStateFlow()3637    init {38        viewModelScope.launch {39            repository.snapshots.map { it.categories to it.sources }.distinctUntilChanged().collect { (categories, sources) ->40                _policy.update { it.copy(categories = categories.sortedWith(compareBy({ it.forItemType.ordinal }, { it.position ?: 0 }, { it.name })), sources = sources.sortedBy { source -> source.name }) }41            }42        }43    }4445    fun previewCleanup() {46        if (_policy.value.busy) return47        _policy.update { it.copy(busy = true, deletedCount = null, failed = false) }48        viewModelScope.launch {49            try {50                val preview = withContext(workDispatcher) { automation.previewReadCleanup() }51                _policy.update { it.copy(preview = preview) }52            } catch (cancel: CancellationException) { throw cancel }53            catch (_: Exception) { _policy.update { it.copy(failed = true) } }54            finally { _policy.update { it.copy(busy = false) } }55        }56    }5758    fun dismissPreview() { _policy.update { it.copy(preview = null) } }5960    fun confirmCleanup() {61        val ids = _policy.value.preview?.mapTo(mutableSetOf()) { it.chapterId } ?: return62        if (_policy.value.busy) return63        _policy.update { it.copy(busy = true, preview = null, failed = false) }64        viewModelScope.launch {65            try {66                val count = withContext(workDispatcher) { automation.cleanupReadDownloads(ids) }67                _policy.update { it.copy(deletedCount = count) }68            } catch (cancel: CancellationException) { throw cancel }69            catch (_: Exception) { _policy.update { it.copy(failed = true) } }70            finally { _policy.update { it.copy(busy = false) } }71        }72    }7374    // A failed scan is recorded in [scan] by the controller.75    fun rescan() {76        viewModelScope.launch { catchingNonCancellation { localLibrary.rescan() } }77    }78}7980internal data class DownloadPolicyUiState(81    val categories: List<DbCategory> = emptyList(),82    val sources: List<DbSource> = emptyList(),83    val preview: List<DownloadCleanupItem>? = null,84    val busy: Boolean = false,85    val deletedCount: Int? = null,86    val failed: Boolean = false,87)88

Search chapters, reference and source filenames.