Yokan / rebuild
Course 1 8624561

CreateExtensionViewModel.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/extensions/CreateExtensionViewModel.kt

implementation28 lines
1package eu.friedcutlet.yokan.ui.screens.extensions23import androidx.lifecycle.ViewModel4import eu.friedcutlet.yokan.data.toDomain5import eu.friedcutlet.yokan.database.DbSource6import eu.friedcutlet.yokan.database.YokanDatabase7import eu.friedcutlet.yokan.domain.Source8import kotlinx.coroutines.flow.MutableStateFlow9import kotlinx.coroutines.flow.StateFlow10import kotlinx.coroutines.flow.asStateFlow11import kotlinx.coroutines.flow.update1213internal data class CreateExtensionUiState(14    val created: Source? = null,15    val error: String? = null,16)1718/** Saves a locally authored extension source. */19internal class CreateExtensionViewModel(private val database: YokanDatabase) : ViewModel() {20    private val _state = MutableStateFlow(CreateExtensionUiState())21    val state: StateFlow<CreateExtensionUiState> = _state.asStateFlow()2223    fun create(source: DbSource) {24        runCatching { database.upsertSource(source) }25            .onSuccess { saved -> _state.value = CreateExtensionUiState(created = saved.toDomain()) }26            .onFailure { err -> _state.update { it.copy(error = err.message ?: "Could not create extension") } }27    }28}29

Search chapters, reference and source filenames.