-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move LessonToolCard to it's own source file
- Loading branch information
Showing
12 changed files
with
182 additions
and
53 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
app/src/main/kotlin/org/cru/godtools/ui/tools/LessonToolCard.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...sonToolCardPaparazziTest_LessonToolCard() - Default[NIGHT,NO_ACCESSIBILITY].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...sonToolCardPaparazziTest_LessonToolCard() - Default[NOTNIGHT,ACCESSIBILITY].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...ToolCardPaparazziTest_LessonToolCard() - Default[NOTNIGHT,NO_ACCESSIBILITY].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...ToolCardPaparazziTest_LessonToolCard() - Long Title[NIGHT,NO_ACCESSIBILITY].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...ToolCardPaparazziTest_LessonToolCard() - Long Title[NOTNIGHT,ACCESSIBILITY].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...lCardPaparazziTest_LessonToolCard() - Long Title[NOTNIGHT,NO_ACCESSIBILITY].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...lCardPaparazziTest_LessonToolCard() - Not Available[NIGHT,NO_ACCESSIBILITY].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...lCardPaparazziTest_LessonToolCard() - Not Available[NOTNIGHT,ACCESSIBILITY].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...rdPaparazziTest_LessonToolCard() - Not Available[NOTNIGHT,NO_ACCESSIBILITY].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions
73
app/src/testDebug/kotlin/org/cru/godtools/ui/tools/LessonToolCardPaparazziTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
} |