Skip to content

Commit

Permalink
update ToolCardPresenter to more directly use coroutines flows to loa…
Browse files Browse the repository at this point in the history
…d the translation
  • Loading branch information
frett committed May 3, 2024
1 parent 1c5cdcd commit 9366353
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions app/src/main/kotlin/org/cru/godtools/ui/tools/ToolCardPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
Expand Down Expand Up @@ -40,6 +44,7 @@ class ToolCardPresenter @Inject constructor(
private val translationsRepository: TranslationsRepository,
) {
@Composable
@OptIn(ExperimentalCoroutinesApi::class)
fun present(
tool: Tool,
loadAppLanguage: Boolean = false,
Expand All @@ -48,26 +53,30 @@ class ToolCardPresenter @Inject constructor(
eventSink: (ToolCard.Event) -> Unit = {},
): ToolCard.State {
val coroutineScope = rememberCoroutineScope()
val toolCode = tool.code
val toolCode by rememberUpdatedState(tool.code)
val appLocale by settings.produceAppLocaleState()
val defaultLocale by rememberUpdatedState(tool.defaultLocale)

// App Translation
val appLocale by settings.produceAppLocaleState()
var appTranslation: Translation? by remember { mutableStateOf(null) }
val appTranslationFlow = remember(toolCode, appLocale) {
translationsRepository.findLatestTranslationFlow(toolCode, appLocale)
.onEach { appTranslation = it }
}
val appLanguageAvailable by remember { derivedStateOf { appTranslation != null } }

// Translation
val defaultLocale = tool.defaultLocale
val defaultTranslationFlow = remember(toolCode, defaultLocale) {
translationsRepository.findLatestTranslationFlow(toolCode, defaultLocale)
.onStart { emit(null) }
}
var isLoaded by remember { mutableStateOf(false) }
val translation by remember(appTranslationFlow, defaultTranslationFlow) {
combine(appTranslationFlow, defaultTranslationFlow) { t1, t2 -> t1 ?: t2 }
val translation by remember {
val toolCodeFlow = snapshotFlow { toolCode }
toolCodeFlow
.combine(snapshotFlow { appLocale }) { t, l -> translationsRepository.findLatestTranslationFlow(t, l) }
.flatMapLatest { it }
.onEach { appTranslation = it }
.combine(
toolCodeFlow
.combine(snapshotFlow { defaultLocale }) { t, l ->
translationsRepository.findLatestTranslationFlow(t, l)
}
.flatMapLatest { it }
.onStart { emit(null) }
) { t1, t2 -> t1 ?: t2 }
.onEach { isLoaded = true }
}.collectAsState(null)

Expand All @@ -80,9 +89,9 @@ class ToolCardPresenter @Inject constructor(
{
when (it) {
ToolCard.Event.PinTool ->
coroutineScope.launch(NonCancellable) { toolCode?.let { toolsRepository.pinTool(toolCode) } }
coroutineScope.launch(NonCancellable) { toolCode?.let { toolsRepository.pinTool(it) } }
ToolCard.Event.UnpinTool ->
coroutineScope.launch(NonCancellable) { toolCode?.let { toolsRepository.unpinTool(toolCode) } }
coroutineScope.launch(NonCancellable) { toolCode?.let { toolsRepository.unpinTool(it) } }
else -> eventSink(it)
}
}
Expand All @@ -100,8 +109,7 @@ class ToolCardPresenter @Inject constructor(
secondLanguageAvailable = secondLanguageAvailable,
availableLanguages = when {
!loadAvailableLanguages -> 0
toolCode == null -> 0
else -> rememberAvailableLanguages(toolCode)
else -> toolCode?.let { rememberAvailableLanguages(it) } ?: 0
},
eventSink = interceptingEventSink,
)
Expand Down

0 comments on commit 9366353

Please sign in to comment.