RepositoriesViewModel.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/browse/RepositoriesViewModel.kt
1package eu.friedcutlet.yokan.ui.screens.settings.browse23import androidx.lifecycle.ViewModel4import androidx.lifecycle.viewModelScope5import eu.friedcutlet.yokan.data.YokanRepository6import eu.friedcutlet.yokan.database.DbRepo7import eu.friedcutlet.yokan.domain.ItemType8import eu.friedcutlet.yokan.services.catchingNonCancellation9import eu.friedcutlet.yokan.services.extensions.SourceInstallService10import kotlinx.coroutines.CoroutineDispatcher11import kotlinx.coroutines.Dispatchers12import kotlinx.coroutines.flow.MutableStateFlow13import kotlinx.coroutines.flow.StateFlow14import kotlinx.coroutines.flow.asStateFlow15import kotlinx.coroutines.flow.distinctUntilChangedBy16import kotlinx.coroutines.flow.update17import kotlinx.coroutines.launch18import kotlinx.coroutines.withContext1920internal data class RepositoriesState(21 val itemType: ItemType,22 val repositories: List<DbRepo>,23 /** JSON URL of the repository being refreshed; one refresh at a time. */24 val loadingRepoUrl: String? = null,25 val message: String? = null,26)2728internal class RepositoriesViewModel(29 initialType: ItemType,30 private val repository: YokanRepository,31 private val sourceInstall: SourceInstallService,32 private val workDispatcher: CoroutineDispatcher = Dispatchers.Default,33) : ViewModel() {34 private val _state = MutableStateFlow(RepositoriesState(initialType, repository.repositories(initialType)))35 val state: StateFlow<RepositoriesState> = _state.asStateFlow()3637 init {38 // Repositories live in the settings row; sync and deep links write it too.39 viewModelScope.launch { repository.snapshots.distinctUntilChangedBy { it.settings }.collect { reload() } }40 }4142 fun selectType(type: ItemType) = _state.update { it.copy(itemType = type, repositories = repository.repositories(type)) }4344 fun upsert(repo: DbRepo) {45 repository.upsertRepository(_state.value.itemType, repo)46 reload()47 }4849 fun remove(jsonUrl: String) {50 repository.removeRepository(_state.value.itemType, jsonUrl)51 reload()52 }5354 fun showMessage(message: String) = _state.update { it.copy(message = message) }5556 fun refresh(repo: DbRepo) {57 val jsonUrl = repo.jsonUrl.orEmpty()58 val current = _state.value59 if (jsonUrl.isBlank() || current.loadingRepoUrl != null) return60 val type = current.itemType61 _state.update {62 it.copy(loadingRepoUrl = jsonUrl, message = "Refreshing ${repo.name ?: jsonUrl.substringAfterLast('/')}...")63 }64 viewModelScope.launch {65 val result = withContext(workDispatcher) {66 catchingNonCancellation { sourceInstall.refreshRepository(type, repo) }67 }68 val message = result.fold(69 onSuccess = { summary ->70 "${repo.name ?: "Repository"}: ${summary.fetched} fetched, ${summary.added} added, ${summary.updateAvailable} updates, ${summary.updated} auto-updated, ${summary.obsolete} obsolete."71 },72 onFailure = { error ->73 "${repo.name ?: "Repository"} refresh failed: ${error.message.orEmpty().ifBlank { error::class.simpleName.orEmpty() }}"74 },75 )76 _state.update { it.copy(loadingRepoUrl = null, message = message) }77 }78 }7980 private fun reload() = _state.update { it.copy(repositories = repository.repositories(it.itemType)) }81}82