Yokan / rebuild
Course 1 8624561

MassMigrationRunnerViewModel.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/migration/MassMigrationRunnerViewModel.kt

implementation42 lines
1package eu.friedcutlet.yokan.ui.screens.migration23import androidx.lifecycle.ViewModel4import androidx.lifecycle.viewModelScope5import eu.friedcutlet.yokan.services.migration.*6import kotlinx.coroutines.flow.MutableStateFlow7import kotlinx.coroutines.flow.asStateFlow8import kotlinx.coroutines.flow.update9import kotlinx.coroutines.launch1011internal data class MassMigrationRunnerState(12    val progress: MassMigrationProgress? = null,13    val busyWithAnotherRun: Boolean = false,14)1516/** Mirrors the run owned by this repository/session; route recreation does not restart it. */17internal class MassMigrationRunnerViewModel(18    sourceId: Long,19    destinationIds: List<Long>,20    mangaIds: List<Long>,21    private val migration: MigrationService,22) : ViewModel() {23    private val _state = MutableStateFlow(MassMigrationRunnerState())24    val state = _state.asStateFlow()2526    init {27        val accepted = migration.start(sourceId, destinationIds, mangaIds)28        _state.update { it.copy(busyWithAnotherRun = !accepted) }29        if (accepted) viewModelScope.launch {30            migration.progress.collect { progress ->31                if (progress?.sourceId == sourceId && progress.destinationIds == destinationIds.distinct() && progress.mangaIds == mangaIds.distinct()) {32                    _state.update { it.copy(progress = progress) }33                }34            }35        }36    }3738    fun select(id: Long, key: MigrationCandidateKey?) = migration.select(id, key)39    fun retry(id: Long) = migration.retry(id)40    fun commit() = migration.commitSelected()41    fun cancel() = migration.cancel()42}43

Search chapters, reference and source filenames.