BrowseClearViewModel.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/browse/BrowseClearViewModel.kt
1package eu.friedcutlet.yokan.ui.screens.settings.browse23import androidx.lifecycle.ViewModel4import androidx.lifecycle.viewModelScope5import eu.friedcutlet.yokan.domain.ItemType6import eu.friedcutlet.yokan.services.library.ClearResult7import eu.friedcutlet.yokan.services.library.SelectiveDataClearService8import kotlinx.coroutines.CancellationException9import kotlinx.coroutines.CoroutineDispatcher10import kotlinx.coroutines.Dispatchers11import kotlinx.coroutines.flow.MutableStateFlow12import kotlinx.coroutines.flow.StateFlow13import kotlinx.coroutines.flow.asStateFlow14import kotlinx.coroutines.flow.update15import kotlinx.coroutines.launch16import kotlinx.coroutines.withContext1718internal data class BrowseClearState(19 val busy: Boolean = false,20 val error: String? = null,21 val success: String? = null,22)2324private data class ClearOutcome(val result: ClearResult, val hubConfigured: Boolean, val sources: Boolean)2526/** Runs the selective database clear away from the UI thread and reports only committed results. */27internal class BrowseClearViewModel(28 private val service: SelectiveDataClearService,29 private val hasConfiguredHub: () -> Boolean,30 private val workDispatcher: CoroutineDispatcher = Dispatchers.Default,31) : ViewModel() {32 private val _state = MutableStateFlow(BrowseClearState())33 val state: StateFlow<BrowseClearState> = _state.asStateFlow()3435 fun clearLibrary(types: Set<ItemType>) = clear {36 val (result, hubConfigured) = withContext(workDispatcher) {37 service.clearLibrary(types) to hasConfiguredHub()38 }39 ClearOutcome(result, hubConfigured, sources = false)40 }4142 fun clearSources(types: Set<ItemType>) = clear {43 withContext(workDispatcher) { ClearOutcome(service.clearSources(types), hubConfigured = false, sources = true) }44 }4546 fun clearFeedback() = _state.update { it.copy(error = null, success = null) }4748 private fun clear(operation: suspend () -> ClearOutcome) {49 if (_state.value.busy) return50 _state.update { it.copy(busy = true, error = null, success = null) }51 viewModelScope.launch {52 try {53 val outcome = operation()54 val result = outcome.result55 _state.update {56 if (!result.cleared) {57 it.copy(busy = false, error = result.error ?: "Nothing was cleared. Check the selected types and try again.")58 } else {59 it.copy(60 busy = false,61 success = when {62 result.deliveryPending -> "Saved locally; hub delivery pending."63 outcome.sources -> "${result.sourcesRemoved} source definitions removed locally. Library entries and media files were kept."64 outcome.hubConfigured -> "Library records cleared; hub delivery is up to date."65 else -> "Library records cleared locally; no hub is configured."66 },67 )68 }69 }70 } catch (error: CancellationException) {71 _state.update { it.copy(busy = false) }72 throw error73 } catch (error: Throwable) {74 _state.update {75 it.copy(busy = false, error = error.message ?: "Could not clear the selected data. Nothing was confirmed as cleared.")76 }77 }78 }79 }80}81