From 81b610dcdf0c27dcaee34a79213d2a66ad45912d Mon Sep 17 00:00:00 2001 From: Daniel Frett Date: Thu, 14 Dec 2023 18:14:25 -0700 Subject: [PATCH] create a ToolViewModel.toState() composable to simplify migration --- .../cru/godtools/ui/tools/ToolCardLayouts.kt | 70 ++++--------------- .../cru/godtools/ui/tools/ToolViewModels.kt | 13 ++++ 2 files changed, 25 insertions(+), 58 deletions(-) diff --git a/app/src/main/kotlin/org/cru/godtools/ui/tools/ToolCardLayouts.kt b/app/src/main/kotlin/org/cru/godtools/ui/tools/ToolCardLayouts.kt index 1a848baf68..0d2b712fc0 100644 --- a/app/src/main/kotlin/org/cru/godtools/ui/tools/ToolCardLayouts.kt +++ b/app/src/main/kotlin/org/cru/godtools/ui/tools/ToolCardLayouts.kt @@ -114,6 +114,7 @@ fun LessonToolCard( viewModel: ToolViewModels.ToolViewModel = toolViewModels[toolCode], onEvent: (ToolCardEvent) -> Unit = {}, ) { + val state = viewModel.toState() val tool by viewModel.tool.collectAsState() val translation by viewModel.firstTranslation.collectAsState() @@ -123,14 +124,14 @@ fun LessonToolCard( onClick = { onEvent(ToolCardEvent.Click(tool, translation.value?.languageCode)) }, modifier = modifier.fillMaxWidth() ) { - ToolBanner(viewModel, modifier = Modifier.aspectRatio(335f / 80f)) + ToolBanner(state, modifier = Modifier.aspectRatio(335f / 80f)) Column( modifier = Modifier .fillMaxWidth() .padding(16.dp) ) { - ToolName(viewModel, minLines = 2, modifier = Modifier.fillMaxWidth()) + ToolName(state, minLines = 2, modifier = Modifier.fillMaxWidth()) val appLanguage by viewModel.appLanguage.collectAsState() val appTranslation by viewModel.appTranslation.collectAsState() @@ -161,9 +162,10 @@ fun ToolCard( interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, onEvent: (ToolCardEvent) -> Unit = {}, ) { + val state = viewModel.toState() val tool by viewModel.tool.collectAsState() val firstTranslation by viewModel.firstTranslation.collectAsState() - val downloadProgress by viewModel.downloadProgress.collectAsState() + val downloadProgress by rememberUpdatedState(state.downloadProgress) ProvideLayoutDirectionFromLocale(locale = { firstTranslation.value?.languageCode }) { ElevatedCard( @@ -182,7 +184,7 @@ fun ToolCard( ) { Box(modifier = Modifier.fillMaxWidth()) { ToolBanner( - viewModel, + state, modifier = Modifier .fillMaxWidth() .aspectRatio(335f / 87f) @@ -205,7 +207,7 @@ fun ToolCard( modifier = Modifier.fillMaxWidth() ) { ToolName( - viewModel, + state, modifier = Modifier .run { if (additionalLanguage != null) widthIn(max = { it - 70.dp }) else this } .alignByBaseline() @@ -222,10 +224,7 @@ fun ToolCard( } } } - ToolCategory( - viewModel, - modifier = Modifier.fillMaxWidth() - ) + ToolCategory(state, modifier = Modifier.fillMaxWidth()) if (showActions) { ToolCardActions( @@ -274,18 +273,9 @@ fun SquareToolCard( } } } - val state = ToolCard.State( - tool = tool, - banner = viewModel.bannerFile.collectAsState().value, - translation = firstTranslation.value, - secondLanguage = viewModel.secondLanguage.collectAsState().value, - secondTranslation = secondTranslation, - downloadProgress = viewModel.downloadProgress.collectAsState().value, - eventSink = eventSink, - ) SquareToolCard( - state = state, + state = viewModel.toState(eventSink), modifier = modifier, showCategory = showCategory, showSecondLanguage = showSecondLanguage, @@ -383,6 +373,7 @@ internal fun VariantToolCard( modifier: Modifier = Modifier, onEvent: (ToolCardEvent) -> Unit = {}, ) { + val state = viewModel.toState() val tool by viewModel.tool.collectAsState() val firstTranslation by viewModel.firstTranslation.collectAsState() @@ -393,7 +384,7 @@ internal fun VariantToolCard( modifier = modifier ) { ToolBanner( - viewModel, + state, modifier = Modifier .fillMaxWidth() .aspectRatio(335f / 87f) @@ -402,7 +393,7 @@ internal fun VariantToolCard( RadioButton(selected = isSelected, onClick = null) Column(modifier = Modifier.padding(start = 16.dp)) { - ToolName(viewModel) + ToolName(state) Text( firstTranslation.value.getTagline(tool).orEmpty(), fontFamily = firstTranslation.value?.getFontFamilyOrNull(), @@ -443,10 +434,6 @@ internal fun VariantToolCard( } } -@Composable -private fun ToolBanner(viewModel: ToolViewModels.ToolViewModel, modifier: Modifier = Modifier) = - ToolBanner(state = ToolCard.State(banner = viewModel.bannerFile.collectAsState().value), modifier = modifier) - @Composable private fun ToolBanner(state: ToolCard.State, modifier: Modifier = Modifier) = AsyncImage( model = state.banner, @@ -455,26 +442,6 @@ private fun ToolBanner(state: ToolCard.State, modifier: Modifier = Modifier) = A modifier = modifier.background(GodToolsTheme.GRAY_E6) ) -@Composable -private fun ToolName( - viewModel: ToolViewModels.ToolViewModel, - modifier: Modifier = Modifier, - minLines: Int = 1, - maxLines: Int = Int.MAX_VALUE, -) { - val translation by viewModel.firstTranslation.collectAsState() - - ToolName( - state = ToolCard.State( - tool = viewModel.tool.collectAsState().value, - translation = translation.value - ), - modifier = modifier.invisibleIf { translation.isInitial }, - minLines = minLines, - maxLines = maxLines, - ) -} - @Composable private fun ToolName( state: ToolCard.State, @@ -493,19 +460,6 @@ private fun ToolName( ) } -@Composable -private fun ToolCategory(viewModel: ToolViewModels.ToolViewModel, modifier: Modifier = Modifier) { - val translation by viewModel.firstTranslation.collectAsState() - - ToolCategory( - ToolCard.State( - tool = viewModel.tool.collectAsState().value, - translation = translation.value, - ), - modifier = modifier.invisibleIf { translation.isInitial }, - ) -} - @Composable private fun ToolCategory(state: ToolCard.State, modifier: Modifier = Modifier) { val context = LocalContext.current diff --git a/app/src/main/kotlin/org/cru/godtools/ui/tools/ToolViewModels.kt b/app/src/main/kotlin/org/cru/godtools/ui/tools/ToolViewModels.kt index f2049c1d3f..0ff6c51874 100644 --- a/app/src/main/kotlin/org/cru/godtools/ui/tools/ToolViewModels.kt +++ b/app/src/main/kotlin/org/cru/godtools/ui/tools/ToolViewModels.kt @@ -1,5 +1,7 @@ package org.cru.godtools.ui.tools +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope @@ -135,6 +137,17 @@ class ToolViewModels @Inject constructor( toolsRepository.unpinTool(code) syncService.syncDirtyFavoriteTools() } + + @Composable + fun toState(eventSink: (ToolCard.Event) -> Unit = {}) = ToolCard.State( + tool = tool.collectAsState().value, + banner = bannerFile.collectAsState().value, + translation = firstTranslation.collectAsState().value.value, + secondLanguage = secondLanguage.collectAsState().value, + secondTranslation = secondTranslation.collectAsState().value, + downloadProgress = downloadProgress.collectAsState().value, + eventSink = eventSink, + ) } private fun Flow.attachmentFileFlow(transform: (value: Tool?) -> Long?) = this