From 879f2bb3b345da2528b35d762cd6786fa62d5a98 Mon Sep 17 00:00:00 2001 From: Daniel Frett Date: Wed, 15 Nov 2023 16:56:50 -0700 Subject: [PATCH] replace the Switch on the downloaded languages UI with a custom progres indicator --- .../downloadable/DownloadableLanguagesLayout.kt | 17 ++++++++++++----- .../downloadable/LanguageViewModels.kt | 5 +++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/DownloadableLanguagesLayout.kt b/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/DownloadableLanguagesLayout.kt index d2a1be5f91..e4ef05b33c 100644 --- a/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/DownloadableLanguagesLayout.kt +++ b/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/DownloadableLanguagesLayout.kt @@ -2,6 +2,7 @@ package org.cru.godtools.ui.languages.downloadable import androidx.activity.compose.BackHandler import androidx.compose.foundation.ExperimentalFoundationApi +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.wrapContentWidth @@ -18,7 +19,6 @@ import androidx.compose.material3.IconButton import androidx.compose.material3.ListItem import androidx.compose.material3.Scaffold import androidx.compose.material3.SearchBar -import androidx.compose.material3.Switch import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState @@ -129,11 +129,18 @@ private fun LanguageListItem(viewModel: LanguageViewModels.LanguageViewModel, mo Text(pluralStringResource(R.plurals.language_settings_downloadable_languages_available_tools, tools, tools)) }, trailingContent = { - Switch(language.isAdded, onCheckedChange = { - scope.launch(NonCancellable) { - if (it) viewModel.pin() else viewModel.unpin() + val toolsDownloaded by viewModel.toolsDownloaded.collectAsState() + + LanguageDownloadProgressIndicator( + language.isAdded, + downloaded = toolsDownloaded, + total = toolsAvailable, + modifier = Modifier.clickable { + scope.launch(NonCancellable) { + if (language.isAdded) viewModel.unpin() else viewModel.pin() + } } - }) + ) }, modifier = modifier ) diff --git a/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageViewModels.kt b/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageViewModels.kt index e21ceb29ac..ccece87f4c 100644 --- a/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageViewModels.kt +++ b/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageViewModels.kt @@ -14,6 +14,7 @@ import kotlinx.coroutines.flow.stateIn import org.cru.godtools.db.repository.LanguagesRepository import org.cru.godtools.db.repository.ToolsRepository import org.cru.godtools.model.Language +import org.cru.godtools.model.Tool @HiltViewModel class LanguageViewModels @Inject constructor( @@ -32,6 +33,10 @@ class LanguageViewModels @Inject constructor( .map { it.size } .flowOn(Dispatchers.Default) .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), 0) + val toolsDownloaded = toolsRepository.getDownloadedToolsFlowByTypesAndLanguage(Tool.Type.NORMAL_TYPES, code) + .map { it.size } + .flowOn(Dispatchers.Default) + .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), 0) suspend fun pin() = languagesRepository.pinLanguage(code) suspend fun unpin() = languagesRepository.unpinLanguage(code)