From b979752d87c74c28a84707515e4f7b5fc4e307e5 Mon Sep 17 00:00:00 2001 From: Daniel Frett Date: Fri, 3 May 2024 08:38:46 -0600 Subject: [PATCH] move LessonToolCard to it's own source file --- .../cru/godtools/ui/tools/LessonToolCard.kt | 82 +++++++++++++++++++ .../cru/godtools/ui/tools/ToolCardLayouts.kt | 53 ------------ ...rd() - Default[NIGHT,NO_ACCESSIBILITY].png | 3 + ...rd() - Default[NOTNIGHT,ACCESSIBILITY].png | 3 + ...) - Default[NOTNIGHT,NO_ACCESSIBILITY].png | 3 + ...) - Long Title[NIGHT,NO_ACCESSIBILITY].png | 3 + ...) - Long Title[NOTNIGHT,ACCESSIBILITY].png | 3 + ... Long Title[NOTNIGHT,NO_ACCESSIBILITY].png | 3 + ... Not Available[NIGHT,NO_ACCESSIBILITY].png | 3 + ... Not Available[NOTNIGHT,ACCESSIBILITY].png | 3 + ...t Available[NOTNIGHT,NO_ACCESSIBILITY].png | 3 + .../ui/tools/LessonToolCardPaparazziTest.kt | 73 +++++++++++++++++ 12 files changed, 182 insertions(+), 53 deletions(-) create mode 100644 app/src/main/kotlin/org/cru/godtools/ui/tools/LessonToolCard.kt create mode 100644 app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Default[NIGHT,NO_ACCESSIBILITY].png create mode 100644 app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Default[NOTNIGHT,ACCESSIBILITY].png create mode 100644 app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Default[NOTNIGHT,NO_ACCESSIBILITY].png create mode 100644 app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Long Title[NIGHT,NO_ACCESSIBILITY].png create mode 100644 app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Long Title[NOTNIGHT,ACCESSIBILITY].png create mode 100644 app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Long Title[NOTNIGHT,NO_ACCESSIBILITY].png create mode 100644 app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Not Available[NIGHT,NO_ACCESSIBILITY].png create mode 100644 app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Not Available[NOTNIGHT,ACCESSIBILITY].png create mode 100644 app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Not Available[NOTNIGHT,NO_ACCESSIBILITY].png create mode 100644 app/src/testDebug/kotlin/org/cru/godtools/ui/tools/LessonToolCardPaparazziTest.kt diff --git a/app/src/main/kotlin/org/cru/godtools/ui/tools/LessonToolCard.kt b/app/src/main/kotlin/org/cru/godtools/ui/tools/LessonToolCard.kt new file mode 100644 index 0000000000..01893535ae --- /dev/null +++ b/app/src/main/kotlin/org/cru/godtools/ui/tools/LessonToolCard.kt @@ -0,0 +1,82 @@ +package org.cru.godtools.ui.tools + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.aspectRatio +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.ElevatedCard +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberUpdatedState +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import org.ccci.gto.android.common.androidx.compose.ui.draw.invisibleIf +import org.cru.godtools.base.ui.util.ProvideLayoutDirectionFromLocale +import org.cru.godtools.model.Language + +@Composable +fun LessonToolCard( + toolCode: String, + modifier: Modifier = Modifier, + selectedLanguage: Language? = null, + viewModel: ToolViewModels.ToolViewModel = toolViewModels[toolCode], + onEvent: (ToolCardEvent) -> Unit = {}, +) { + val state = viewModel.toState(language = selectedLanguage) + val tool by rememberUpdatedState(state.tool) + val translation by rememberUpdatedState(state.translation) + + LessonToolCard( + state = remember(state) { + state.copy(eventSink = { + when (it) { + ToolCard.Event.Click -> + onEvent(ToolCardEvent.Click(tool?.code, tool?.type, translation?.languageCode)) + else -> TODO() + } + }) + }, + modifier = modifier, + ) +} + +@Composable +fun LessonToolCard(state: ToolCard.State, modifier: Modifier = Modifier) { + val isLoaded by rememberUpdatedState(state.isLoaded) + val translation by rememberUpdatedState(state.translation) + val language by rememberUpdatedState(state.language) + val languageAvailable by rememberUpdatedState(state.languageAvailable) + val eventSink by rememberUpdatedState(state.eventSink) + + ProvideLayoutDirectionFromLocale(locale = { translation?.languageCode }) { + ElevatedCard( + onClick = { eventSink(ToolCard.Event.Click) }, + elevation = toolCardElevation, + modifier = modifier.fillMaxWidth() + ) { + ToolBanner(state, modifier = Modifier.aspectRatio(335f / 80f)) + + Column( + modifier = Modifier + .fillMaxWidth() + .padding(16.dp) + ) { + ToolName(state, minLines = 2, modifier = Modifier.fillMaxWidth()) + + ToolCardInfoContent { + AvailableInLanguage( + language = language, + available = languageAvailable, + horizontalArrangement = Arrangement.End, + modifier = Modifier + .align(Alignment.End) + .invisibleIf { !isLoaded || language == null } + ) + } + } + } + } +} 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 d9f1b82abf..a43fc8fb0f 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 @@ -38,7 +38,6 @@ import coil.compose.AsyncImage import java.util.Locale import org.ccci.gto.android.common.androidx.compose.foundation.layout.widthIn import org.ccci.gto.android.common.androidx.compose.foundation.text.minLinesHeight -import org.ccci.gto.android.common.androidx.compose.ui.draw.invisibleIf import org.cru.godtools.base.ui.theme.GodToolsTheme import org.cru.godtools.base.ui.util.ProvideLayoutDirectionFromLocale import org.cru.godtools.base.ui.util.getCategory @@ -94,58 +93,6 @@ fun PreloadTool(tool: Tool) { toolViewModels[code, tool] } -@Composable -fun LessonToolCard( - toolCode: String, - modifier: Modifier = Modifier, - selectedLanguage: Language? = null, - viewModel: ToolViewModels.ToolViewModel = toolViewModels[toolCode], - onEvent: (ToolCardEvent) -> Unit = {}, -) { - val state = viewModel.toState(language = selectedLanguage) - val tool by rememberUpdatedState(state.tool) - val language by rememberUpdatedState(state.language) - val languageAvailable by rememberUpdatedState(state.languageAvailable) - val translation by rememberUpdatedState(state.translation) - - ProvideLayoutDirectionFromLocale(locale = { translation?.languageCode }) { - ElevatedCard( - onClick = { - onEvent( - ToolCardEvent.Click( - tool?.code, - tool?.type, - translation?.languageCode - ) - ) - }, - elevation = toolCardElevation, - modifier = modifier.fillMaxWidth() - ) { - ToolBanner(state, modifier = Modifier.aspectRatio(335f / 80f)) - - Column( - modifier = Modifier - .fillMaxWidth() - .padding(16.dp) - ) { - ToolName(state, minLines = 2, modifier = Modifier.fillMaxWidth()) - - ToolCardInfoContent { - AvailableInLanguage( - language = language, - available = languageAvailable, - horizontalArrangement = Arrangement.End, - modifier = Modifier - .align(Alignment.End) - .invisibleIf { !state.isLoaded || !languageAvailable } - ) - } - } - } - } -} - @Composable fun ToolCard( viewModel: ToolViewModels.ToolViewModel, diff --git a/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Default[NIGHT,NO_ACCESSIBILITY].png b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Default[NIGHT,NO_ACCESSIBILITY].png new file mode 100644 index 0000000000..14fc8cf57f --- /dev/null +++ b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Default[NIGHT,NO_ACCESSIBILITY].png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d86b9c9db4e77f39ac8468f659ca2a0094f54ca9183ba2aff2dd0a52db764a0f +size 38392 diff --git a/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Default[NOTNIGHT,ACCESSIBILITY].png b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Default[NOTNIGHT,ACCESSIBILITY].png new file mode 100644 index 0000000000..9af0c6c3d9 --- /dev/null +++ b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Default[NOTNIGHT,ACCESSIBILITY].png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2fbe0ad938598b83e44f3e82a87a8743942131b693075254a1a696e8de3c3d27 +size 42394 diff --git a/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Default[NOTNIGHT,NO_ACCESSIBILITY].png b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Default[NOTNIGHT,NO_ACCESSIBILITY].png new file mode 100644 index 0000000000..d7423d13d7 --- /dev/null +++ b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Default[NOTNIGHT,NO_ACCESSIBILITY].png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:71f51d5b6b6b62f17fa86f34d155feaef5fe6cd435db07d3d051ba77fb58d31d +size 38508 diff --git a/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Long Title[NIGHT,NO_ACCESSIBILITY].png b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Long Title[NIGHT,NO_ACCESSIBILITY].png new file mode 100644 index 0000000000..d0e09026cf --- /dev/null +++ b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Long Title[NIGHT,NO_ACCESSIBILITY].png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b491b9a04a3245d1aaa9aaac569c1c674562b1977d599486c93cd8c02c3c3f77 +size 49706 diff --git a/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Long Title[NOTNIGHT,ACCESSIBILITY].png b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Long Title[NOTNIGHT,ACCESSIBILITY].png new file mode 100644 index 0000000000..65d7f3a92d --- /dev/null +++ b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Long Title[NOTNIGHT,ACCESSIBILITY].png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e3c46944c45e581bba453b641e24289cffa063b56917799a3f6c18aa873c85eb +size 62728 diff --git a/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Long Title[NOTNIGHT,NO_ACCESSIBILITY].png b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Long Title[NOTNIGHT,NO_ACCESSIBILITY].png new file mode 100644 index 0000000000..136a8b309e --- /dev/null +++ b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Long Title[NOTNIGHT,NO_ACCESSIBILITY].png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:92265f8d5f42dac798d300d2abea17cfe9439f9348fcd8f6d66cface8eed22ae +size 49057 diff --git a/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Not Available[NIGHT,NO_ACCESSIBILITY].png b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Not Available[NIGHT,NO_ACCESSIBILITY].png new file mode 100644 index 0000000000..892322c486 --- /dev/null +++ b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Not Available[NIGHT,NO_ACCESSIBILITY].png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7a867105b4120bd3f0c4883b49085af3e3dd0fb89612bd3447e4e35ed812c57e +size 40241 diff --git a/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Not Available[NOTNIGHT,ACCESSIBILITY].png b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Not Available[NOTNIGHT,ACCESSIBILITY].png new file mode 100644 index 0000000000..3ae13cd325 --- /dev/null +++ b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Not Available[NOTNIGHT,ACCESSIBILITY].png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5d97fff42d9449a85d4c7fe2b6fe0f6370f50078259fb0b014ffd76157ba45aa +size 48899 diff --git a/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Not Available[NOTNIGHT,NO_ACCESSIBILITY].png b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Not Available[NOTNIGHT,NO_ACCESSIBILITY].png new file mode 100644 index 0000000000..b99568a05f --- /dev/null +++ b/app/src/test/snapshots/images/org.cru.godtools.ui.tools_LessonToolCardPaparazziTest_LessonToolCard() - Not Available[NOTNIGHT,NO_ACCESSIBILITY].png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8b12c596110feea75e2f8e947e804f6f32dc5418fdf73e90edcd1df9a6ace0b7 +size 40153 diff --git a/app/src/testDebug/kotlin/org/cru/godtools/ui/tools/LessonToolCardPaparazziTest.kt b/app/src/testDebug/kotlin/org/cru/godtools/ui/tools/LessonToolCardPaparazziTest.kt new file mode 100644 index 0000000000..49db77e643 --- /dev/null +++ b/app/src/testDebug/kotlin/org/cru/godtools/ui/tools/LessonToolCardPaparazziTest.kt @@ -0,0 +1,73 @@ +package org.cru.godtools.ui.tools + +import android.graphics.drawable.Drawable +import coil.Coil +import coil.ImageLoader +import coil.annotation.ExperimentalCoilApi +import coil.test.FakeImageLoaderEngine +import com.android.resources.NightMode +import com.google.testing.junit.testparameterinjector.TestParameter +import com.google.testing.junit.testparameterinjector.TestParameterInjector +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.UnconfinedTestDispatcher +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.setMain +import org.cru.godtools.model.randomTranslation +import org.cru.godtools.ui.BasePaparazziTest +import org.junit.runner.RunWith + +@RunWith(TestParameterInjector::class) +class LessonToolCardPaparazziTest( + @TestParameter nightMode: NightMode, + @TestParameter accessibilityMode: AccessibilityMode, +) : BasePaparazziTest(nightMode = nightMode, accessibilityMode = accessibilityMode) { + private val toolState = ToolCardStateTestData.tool + + @BeforeTest + @OptIn(ExperimentalCoilApi::class, ExperimentalCoroutinesApi::class) + fun setup() { + Dispatchers.setMain(UnconfinedTestDispatcher()) + val file = Drawable.createFromStream(javaClass.getResourceAsStream("banner.jpg"), "banner.jpg")!! + Coil.setImageLoader( + ImageLoader.Builder(paparazzi.context) + .components { + add( + FakeImageLoaderEngine.Builder() + .intercept(ToolCardStateTestData.banner, file) + .build() + ) + } + .build() + ) + } + + @AfterTest + @OptIn(ExperimentalCoroutinesApi::class) + fun cleanup() { + Coil.reset() + Dispatchers.resetMain() + } + + @Test + fun `LessonToolCard() - Default`() = centerInSnapshot { LessonToolCard(toolState) } + + @Test + fun `LessonToolCard() - Long Title`() = centerInSnapshot { + LessonToolCard( + toolState.copy( + translation = randomTranslation( + name = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt" + ) + ) + ) + } + + @Test + fun `LessonToolCard() - Not Available`() = centerInSnapshot { + LessonToolCard(toolState.copy(appLanguageAvailable = false)) + } +}