Skip to content

Commit

Permalink
replace the Switch on the downloaded languages UI with a custom progr…
Browse files Browse the repository at this point in the history
…es indicator
  • Loading branch information
frett committed Nov 16, 2023
1 parent a6c1a99 commit 879f2bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down

0 comments on commit 879f2bb

Please sign in to comment.