Skip to content

Commit

Permalink
create a ToolViewModel.toState() composable to simplify migration
Browse files Browse the repository at this point in the history
  • Loading branch information
frett committed Dec 15, 2023
1 parent 5d51f67 commit 81b610d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 58 deletions.
70 changes: 12 additions & 58 deletions app/src/main/kotlin/org/cru/godtools/ui/tools/ToolCardLayouts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -182,7 +184,7 @@ fun ToolCard(
) {
Box(modifier = Modifier.fillMaxWidth()) {
ToolBanner(
viewModel,
state,
modifier = Modifier
.fillMaxWidth()
.aspectRatio(335f / 87f)
Expand All @@ -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()
Expand All @@ -222,10 +224,7 @@ fun ToolCard(
}
}
}
ToolCategory(
viewModel,
modifier = Modifier.fillMaxWidth()
)
ToolCategory(state, modifier = Modifier.fillMaxWidth())

if (showActions) {
ToolCardActions(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand All @@ -393,7 +384,7 @@ internal fun VariantToolCard(
modifier = modifier
) {
ToolBanner(
viewModel,
state,
modifier = Modifier
.fillMaxWidth()
.aspectRatio(335f / 87f)
Expand All @@ -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(),
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/kotlin/org/cru/godtools/ui/tools/ToolViewModels.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<Tool?>.attachmentFileFlow(transform: (value: Tool?) -> Long?) = this
Expand Down

0 comments on commit 81b610d

Please sign in to comment.