AppIconViewModel.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/appearance/AppIconViewModel.kt
1package eu.friedcutlet.yokan.ui.screens.settings.appearance23import androidx.lifecycle.ViewModel4import androidx.lifecycle.viewModelScope5import eu.friedcutlet.yokan.services.appicons.AppIconDesign6import eu.friedcutlet.yokan.services.appicons.AppIconCapabilities7import eu.friedcutlet.yokan.services.appicons.AppIconSelection8import eu.friedcutlet.yokan.services.appicons.AppIconService9import kotlinx.coroutines.CancellationException10import kotlinx.coroutines.NonCancellable11import kotlinx.coroutines.flow.MutableStateFlow12import kotlinx.coroutines.flow.asStateFlow13import kotlinx.coroutines.flow.update14import kotlinx.coroutines.launch15import kotlinx.coroutines.withContext1617internal data class AppIconState(18 val capabilities: AppIconCapabilities,19 val selection: AppIconSelection? = null,20 val busy: Boolean = false,21 val pickerOpen: Boolean = false,22 val draft: AppIconSelection = AppIconSelection.Default,23 val failed: Boolean = false,24)2526internal class AppIconViewModel(private val service: AppIconService) : ViewModel() {27 private val _state = MutableStateFlow(AppIconState(service.capabilities))28 val state = _state.asStateFlow()2930 init { refresh() }3132 fun refresh() {33 if (_state.value.busy) return34 _state.update { it.copy(busy = true, failed = false) }35 viewModelScope.launch {36 try {37 val current = service.current()38 _state.update { it.copy(selection = current, failed = current == null) }39 } catch (error: CancellationException) {40 throw error41 } catch (_: Exception) {42 _state.update { it.copy(selection = null, failed = true) }43 } finally {44 _state.update { it.copy(busy = false) }45 }46 }47 }4849 fun openPicker() {50 if (_state.value.busy) return51 _state.update { it.copy(pickerOpen = true, draft = it.selection ?: AppIconSelection.Default) }52 }53 fun dismissPicker() { _state.update { it.copy(pickerOpen = false) } }54 fun chooseDesign(design: AppIconDesign) {55 if (_state.value.busy) return56 _state.update { state ->57 val choices = state.capabilities.choices.filter { it.design == design }58 val next = choices.firstOrNull { it.appearance == state.draft.appearance } ?: choices.firstOrNull()59 if (next == null) state else state.copy(draft = next)60 }61 }62 fun chooseAppearance(selection: AppIconSelection) {63 if (_state.value.busy || selection !in _state.value.capabilities.choices) return64 _state.update { if (selection.design == it.draft.design) it.copy(draft = selection) else it }65 }66 fun applyDraft() { select(_state.value.draft) }6768 fun select(selection: AppIconSelection) {69 if (_state.value.busy || selection !in service.capabilities.choices) return70 _state.update { it.copy(busy = true, failed = false) }71 viewModelScope.launch {72 // OS operations cannot be withdrawn when this settings route closes. Join completion73 // and read the installation's real state rather than leaving a stale optimistic choice.74 withContext(NonCancellable) {75 var failed = false76 try { service.apply(selection) } catch (_: Exception) { failed = true }77 val actual = try { service.current() } catch (_: Exception) { null }78 _state.update { it.copy(selection = actual, busy = false, pickerOpen = it.pickerOpen && (failed || actual != selection), failed = failed || actual != selection) }79 }80 }81 }82}83