ExternalDownloadsViewModel.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/downloads/ExternalDownloadsViewModel.kt
1package eu.friedcutlet.yokan.ui.screens.settings.downloads23import androidx.lifecycle.ViewModel4import androidx.lifecycle.viewModelScope5import eu.friedcutlet.yokan.database.DbRemoteDownloadJob6import eu.friedcutlet.yokan.database.RemoteDownloadIntent7import eu.friedcutlet.yokan.services.download.ExternalDownloads8import kotlinx.coroutines.CancellationException9import kotlinx.coroutines.flow.MutableStateFlow10import kotlinx.coroutines.flow.asStateFlow11import kotlinx.coroutines.flow.update12import kotlinx.coroutines.flow.map13import kotlinx.coroutines.flow.distinctUntilChanged14import kotlinx.coroutines.flow.collectLatest15import kotlinx.coroutines.launch1617internal enum class RemoteDownloadAction { Pause, Resume, Cancel, Retry, Refresh, Forget }18internal data class RemoteDownloadsUiState(19 val jobs: List<DbRemoteDownloadJob> = emptyList(),20 val busyIds: Set<String> = emptySet(),21 val testing: Boolean = false,22 val version: String? = null,23 val failed: Boolean = false,24)2526internal class ExternalDownloadsViewModel(private val downloads: ExternalDownloads) : ViewModel() {27 private val _state = MutableStateFlow(RemoteDownloadsUiState(downloads.jobs.value))28 val state = _state.asStateFlow()29 init {30 viewModelScope.launch {31 _state.subscriptionCount.map { it > 0 }.distinctUntilChanged().collectLatest { visible ->32 if (visible) downloads.jobs.collect { jobs -> _state.update { it.copy(jobs = jobs) } }33 }34 }35 }3637 fun clearConnection() { _state.update { it.copy(version = null, failed = false) } }38 fun testConnection() {39 if (_state.value.testing) return40 _state.update { it.copy(testing = true, failed = false, version = null) }41 viewModelScope.launch {42 try { val version = downloads.testConnection(); _state.update { it.copy(version = version) } }43 catch (cancel: CancellationException) { throw cancel }44 catch (_: Exception) { _state.update { it.copy(failed = true) } }45 finally { _state.update { it.copy(testing = false) } }46 }47 }4849 fun act(id: String, action: RemoteDownloadAction) {50 if (id in _state.value.busyIds) return51 _state.update { it.copy(busyIds = it.busyIds + id, failed = false) }52 viewModelScope.launch {53 try {54 when (action) {55 RemoteDownloadAction.Pause -> downloads.setIntent(id, RemoteDownloadIntent.Pause)56 RemoteDownloadAction.Resume -> downloads.setIntent(id, RemoteDownloadIntent.Run)57 RemoteDownloadAction.Cancel -> downloads.setIntent(id, RemoteDownloadIntent.Cancel)58 RemoteDownloadAction.Retry -> downloads.retry(id)59 RemoteDownloadAction.Forget -> downloads.remove(id, forgetUnverified = true)60 RemoteDownloadAction.Refresh -> Unit61 }62 if (action != RemoteDownloadAction.Forget) downloads.reconcile(id)63 } catch (cancel: CancellationException) { throw cancel }64 catch (_: Exception) { _state.update { it.copy(failed = true) } }65 finally { _state.update { it.copy(busyIds = it.busyIds - id) } }66 }67 }68}69