ProfilesViewModel.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/profiles/ProfilesViewModel.kt
1package eu.friedcutlet.yokan.ui.screens.settings.profiles23import androidx.lifecycle.ViewModel4import androidx.lifecycle.viewModelScope5import eu.friedcutlet.yokan.app.profiles.*6import eu.friedcutlet.yokan.services.catchingNonCancellation7import kotlinx.coroutines.CoroutineDispatcher8import kotlinx.coroutines.Dispatchers9import kotlinx.coroutines.flow.MutableStateFlow10import kotlinx.coroutines.flow.asStateFlow11import kotlinx.coroutines.flow.update12import kotlinx.coroutines.launch13import kotlinx.coroutines.withContext14import eu.friedcutlet.yokan.domain.ProfileId1516internal sealed interface ProfileDialog {17 data class Name(val id: ProfileId? = null, val draft: String = "") : ProfileDialog18 data class Delete(val profile: ProfileRecord) : ProfileDialog19}20internal data class ProfilesState(21 val registry: ProfileRegistrySnapshot = ProfileRegistrySnapshot(),22 val activeId: ProfileId = ProfileId.Default,23 val available: Boolean = false,24 val busy: Boolean = false,25 val error: String? = null,26 val dialog: ProfileDialog? = null,27)2829/** Device-owned: activation must never run in a navigator ViewModel that it is about to retire. */30internal class ProfilesViewModel(31 private val registry: ProfileRegistry,32 private val coordinator: ProfileCoordinator,33 private val workDispatcher: CoroutineDispatcher = Dispatchers.Default,34) : ViewModel() {35 private val _state = MutableStateFlow(ProfilesState(activeId = coordinator.currentSession.id))36 val state = _state.asStateFlow()37 private var resumed = false38 init {39 viewModelScope.launch {40 registry.state.collect { update -> _state.update {41 when (update) {42 is ProfileRegistryState.Ready -> it.copy(registry = update.snapshot, available = true)43 else -> it.copy(available = false)44 }45 } }46 }47 viewModelScope.launch {48 coordinator.state.collect { update ->49 if (update is ProfileActivationState.Ready) _state.update {50 it.copy(activeId = update.session.id, error = update.recoveryMessage ?: it.error)51 }52 }53 }54 }55 fun resumeLastProfile() {56 if (resumed) return57 resumed = true58 command {59 val snapshot = registry.refresh()60 if (snapshot.lastActive != coordinator.currentSession.id) coordinator.switchTo(snapshot.lastActive)61 }62 }63 fun refresh() = command { registry.refresh() }64 fun switchTo(id: ProfileId) = command { coordinator.switchTo(id) }65 fun recover() = command { coordinator.recover() }66 fun retryProvisioning(id: ProfileId) = command { coordinator.resumeProvisioning(id) }67 fun showCreate() = _state.update { it.copy(dialog = ProfileDialog.Name(), error = null) }68 fun showRename(record: ProfileRecord) = _state.update { it.copy(dialog = ProfileDialog.Name(record.id, record.name), error = null) }69 fun showDelete(record: ProfileRecord) = _state.update { it.copy(dialog = ProfileDialog.Delete(record), error = null) }70 fun dismissDialog() = _state.update { it.copy(dialog = null) }71 fun changeName(draft: String) = _state.update {72 val dialog = it.dialog as? ProfileDialog.Name ?: return@update it73 it.copy(dialog = dialog.copy(draft = draft.take(80)))74 }75 fun confirm() {76 when (val dialog = _state.value.dialog) {77 is ProfileDialog.Name -> command {78 if (dialog.id == null) coordinator.create(dialog.draft) else coordinator.rename(dialog.id, dialog.draft)79 dismissDialog()80 }81 is ProfileDialog.Delete -> command { coordinator.delete(dialog.profile.id); dismissDialog() }82 null -> Unit83 }84 }85 private fun command(action: suspend () -> Unit) {86 if (_state.value.busy) return87 _state.update { it.copy(busy = true, error = null) }88 viewModelScope.launch {89 try {90 catchingNonCancellation { withContext(workDispatcher) { action() } }91 .onFailure { error -> _state.update { it.copy(error = error.message ?: "Profile operation failed") } }92 } finally { _state.update { it.copy(busy = false) } }93 }94 }95}96