Skip to content

Commit

Permalink
switch secondTranslation to secondLanguageAvailable
Browse files Browse the repository at this point in the history
  • Loading branch information
frett committed Mar 15, 2024
1 parent 1730f4e commit 5503e33
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.compose.foundation.layout.width
import androidx.compose.material3.ElevatedCard
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
Expand Down Expand Up @@ -156,8 +155,7 @@ fun SquareToolCard(

@Composable
private fun SquareToolCardSecondLanguage(state: ToolCard.State) = ToolCardInfoContent {
val secondTranslation by rememberUpdatedState(state.secondTranslation)
val available by remember { derivedStateOf { secondTranslation != null } }
val available by rememberUpdatedState(state.secondLanguageAvailable)

AvailableInLanguage(
state.secondLanguage,
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/org/cru/godtools/ui/tools/ToolCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object ToolCard {
val appLanguage: Language? = null,
val appTranslation: Translation? = null,
val secondLanguage: Language? = null,
val secondTranslation: Translation? = null,
val secondLanguageAvailable: Boolean = false,
val availableLanguages: Int = 0,
val downloadProgress: DownloadProgress? = null,
val eventSink: (Event) -> Unit = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ class ToolCardPresenter @Inject constructor(
appLanguage = if (loadAppLanguage) languagesRepository.rememberLanguage(appLocale) else null,
appTranslation = appTranslation,
secondLanguage = secondLanguage,
secondTranslation = when (secondLanguage?.code) {
translation.value?.languageCode -> null
else -> secondTranslation
secondLanguageAvailable = when (secondLanguage?.code) {
translation.value?.languageCode -> false
else -> secondTranslation != null
},
availableLanguages = when {
!loadAvailableLanguages -> 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class ToolViewModels @Inject constructor(
appLanguage = appLanguage.collectAsState().value,
appTranslation = appTranslation.collectAsState().value.value,
secondLanguage = secondLanguage,
secondTranslation = secondTranslation.collectAsState().value,
secondLanguageAvailable = secondTranslation.collectAsState().value != null,
availableLanguages = availableLanguages.collectAsState().value.size,
downloadProgress = downloadProgress.collectAsState().value,
eventSink = eventSink,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal fun VariantToolCard(state: ToolCard.State, modifier: Modifier = Modifie
val appLanguage by rememberUpdatedState(state.appLanguage)
val appTranslation by rememberUpdatedState(state.appTranslation)
val secondLanguage by rememberUpdatedState(state.secondLanguage)
val secondTranslation by rememberUpdatedState(state.secondTranslation)
val secondLanguageAvailable by rememberUpdatedState(state.secondLanguageAvailable)
val languageCount by rememberUpdatedState(state.availableLanguages)

val eventSink by rememberUpdatedState(state.eventSink)
Expand Down Expand Up @@ -73,7 +73,7 @@ internal fun VariantToolCard(state: ToolCard.State, modifier: Modifier = Modifie

if (secondLanguage != null) {
// TODO: I believe we need to suppress the "Unavailable in" prefix for this phrase
AvailableInLanguage(secondLanguage, available = secondTranslation != null)
AvailableInLanguage(secondLanguage, available = secondLanguageAvailable)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import org.cru.godtools.downloadmanager.DownloadProgress
import org.cru.godtools.model.Language
import org.cru.godtools.model.Tool
import org.cru.godtools.model.randomTool
import org.cru.godtools.model.randomTranslation
import org.junit.Rule

class SquareToolCardPaparazziTest {
Expand All @@ -38,7 +37,7 @@ class SquareToolCardPaparazziTest {
),
banner = mockk(),
secondLanguage = Language(Locale.FRENCH),
secondTranslation = randomTranslation()
secondLanguageAvailable = true,
)

@BeforeTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ class ToolCardPresenterTest {
}
// endregion ToolCard.State.secondLanguage

// region ToolCard.State.secondTranslation
// region ToolCard.State.secondLanguageAvailable
@Test
fun `ToolCardState - secondTranslation`() = runTest {
fun `ToolCardState - secondLanguageAvailable`() = runTest {
toolFlow.value = randomTool(TOOL)
val language = Language(Locale.FRENCH)
val translation = randomTranslation(TOOL, Locale.FRENCH)
Expand All @@ -298,12 +298,12 @@ class ToolCardPresenterTest {
presentFunction = { presenter.present(tool = toolFlow.collectAsState().value, secondLanguage = language) }
) {
frTranslationFlow.emit(translation)
assertEquals(translation, expectMostRecentItem().secondTranslation)
assertTrue(expectMostRecentItem().secondLanguageAvailable)
}
}

@Test
fun `ToolCardState - secondTranslation - Doesn't match the language for the main translation`() = runTest {
fun `ToolCardState - secondLanguageAvailable - Doesn't match the language for the main translation`() = runTest {
toolFlow.value = randomTool(TOOL)
val translation = randomTranslation(TOOL, Locale.ENGLISH)

Expand All @@ -318,12 +318,12 @@ class ToolCardPresenterTest {
enTranslationFlow.emit(translation)

assertNotNull(expectMostRecentItem()) { state ->
assertNull(state.secondTranslation)
assertFalse(state.secondLanguageAvailable)
assertEquals(translation, state.translation)
}
}
}
// endregion ToolCard.State.secondTranslation
// endregion ToolCard.State.secondLanguageAvailable

// region ToolCard.State.availableLanguages
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class VariantToolCardTest {
appLanguage = Language(Locale.ENGLISH),
appTranslation = null,
secondLanguage = Language(Locale.FRENCH),
secondTranslation = randomTranslation(),
secondLanguageAvailable = true,
),
)
}
Expand All @@ -153,7 +153,7 @@ class VariantToolCardTest {
appLanguage = Language(Locale.ENGLISH),
appTranslation = randomTranslation(),
secondLanguage = Language(Locale.FRENCH),
secondTranslation = null,
secondLanguageAvailable = false,
),
)
}
Expand Down

0 comments on commit 5503e33

Please sign in to comment.