From 85be945d24eb9aa9d76e53b1a55f2a33cbce7086 Mon Sep 17 00:00:00 2001 From: Daniel Frett Date: Mon, 11 Mar 2024 15:35:55 -0600 Subject: [PATCH] open tract tools taking the filter language into account --- .../ui/tooldetails/ToolDetailsPresenter.kt | 6 +-- .../org/cru/godtools/util/ActivityUtils.kt | 14 ++++++- .../tooldetails/ToolDetailsPresenterTest.kt | 42 ++++++++++++++++--- .../kotlin/org/cru/godtools/base/Constants.kt | 1 + .../activity/BaseToolRendererViewModel.kt | 7 +--- .../MultiLanguageToolActivityDataModel.kt | 3 +- .../org/cru/godtools/base/ui/Activities.kt | 3 ++ 7 files changed, 60 insertions(+), 16 deletions(-) diff --git a/app/src/main/kotlin/org/cru/godtools/ui/tooldetails/ToolDetailsPresenter.kt b/app/src/main/kotlin/org/cru/godtools/ui/tooldetails/ToolDetailsPresenter.kt index 510de36deb..18201a6971 100644 --- a/app/src/main/kotlin/org/cru/godtools/ui/tooldetails/ToolDetailsPresenter.kt +++ b/app/src/main/kotlin/org/cru/godtools/ui/tooldetails/ToolDetailsPresenter.kt @@ -89,8 +89,7 @@ class ToolDetailsPresenter @AssistedInject constructor( val tool by toolsRepository.produceToolState(toolCode) val translation by rememberUpdatedState(rememberPrimaryTranslation(tool, toolCode)) - val secondTranslation by translationsRepository - .produceLatestTranslationState(toolCode, screen.secondLanguage) + val secondTranslation by translationsRepository.produceLatestTranslationState(toolCode, screen.secondLanguage) val pendingShortcut by remember { derivedStateOf { shortcutManager.getPendingToolShortcut(toolCode) } } val eventSink: (Event) -> Unit = remember { @@ -100,7 +99,8 @@ class ToolDetailsPresenter @AssistedInject constructor( Event.OpenTool -> tool?.let { tool -> val intent = tool.createToolIntent( context = context, - languages = listOfNotNull(translation?.languageCode, secondTranslation?.languageCode) + languages = listOfNotNull(translation?.languageCode, secondTranslation?.languageCode), + activeLocale = if (secondTranslation != null) screen.secondLanguage else null ) if (intent != null) { diff --git a/app/src/main/kotlin/org/cru/godtools/util/ActivityUtils.kt b/app/src/main/kotlin/org/cru/godtools/util/ActivityUtils.kt index fb40230e8b..38806a39eb 100644 --- a/app/src/main/kotlin/org/cru/godtools/util/ActivityUtils.kt +++ b/app/src/main/kotlin/org/cru/godtools/util/ActivityUtils.kt @@ -23,9 +23,19 @@ fun Activity.openToolActivity(code: String, type: Type, vararg languages: Locale Type.META, Type.UNKNOWN -> Unit } -fun Tool.createToolIntent(context: Context, languages: List, showTips: Boolean = false) = code?.let { code -> +fun Tool.createToolIntent( + context: Context, + languages: List, + activeLocale: Locale? = null, + showTips: Boolean = false, +) = code?.let { code -> when (type) { - Type.TRACT -> context.createTractActivityIntent(code, *languages.toTypedArray(), showTips = showTips) + Type.TRACT -> context.createTractActivityIntent( + code, + *languages.toTypedArray(), + activeLocale = activeLocale, + showTips = showTips + ) Type.ARTICLE -> context.createArticlesIntent(code, languages[0]) Type.CYOA -> context.createCyoaActivityIntent(code, *languages.toTypedArray()) Type.LESSON -> context.createLessonActivityIntent(code, languages[0]) diff --git a/app/src/testDebug/kotlin/org/cru/godtools/ui/tooldetails/ToolDetailsPresenterTest.kt b/app/src/testDebug/kotlin/org/cru/godtools/ui/tooldetails/ToolDetailsPresenterTest.kt index 8fe769a403..77cc02e2a8 100644 --- a/app/src/testDebug/kotlin/org/cru/godtools/ui/tooldetails/ToolDetailsPresenterTest.kt +++ b/app/src/testDebug/kotlin/org/cru/godtools/ui/tooldetails/ToolDetailsPresenterTest.kt @@ -28,6 +28,7 @@ import kotlin.test.assertTrue import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.test.runTest +import org.ccci.gto.android.common.util.os.equalsBundle import org.cru.godtools.TestUtils.clearAndroidUiDispatcher import org.cru.godtools.analytics.model.OpenAnalyticsActionEvent import org.cru.godtools.analytics.model.OpenAnalyticsActionEvent.Companion.ACTION_OPEN_TOOL @@ -42,6 +43,7 @@ import org.cru.godtools.db.repository.TranslationsRepository import org.cru.godtools.downloadmanager.DownloadProgress import org.cru.godtools.downloadmanager.GodToolsDownloadManager import org.cru.godtools.model.Attachment +import org.cru.godtools.model.Language import org.cru.godtools.model.Tool import org.cru.godtools.model.randomTool import org.cru.godtools.model.randomTranslation @@ -265,9 +267,8 @@ class ToolDetailsPresenterTest { @Test fun `Event - OpenTool - Tract Tool`() = runTest { toolFlow.value = randomTool(TOOL, Tool.Type.TRACT) - every { - translationsRepository.findLatestTranslationFlow(TOOL, Locale.ENGLISH) - } returns flowOf(randomTranslation(TOOL, Locale.ENGLISH)) + every { translationsRepository.findLatestTranslationFlow(TOOL, Locale.ENGLISH) } + .returns(flowOf(randomTranslation(TOOL, Locale.ENGLISH))) createPresenter().test { expectMostRecentItem().eventSink(Event.OpenTool) @@ -275,9 +276,40 @@ class ToolDetailsPresenterTest { with(navigator.awaitNextScreen()) { assertTrue(this is IntentScreen) - val expected = toolFlow.value?.createToolIntent(context, listOf(Locale.ENGLISH), false)!! + val expected = toolFlow.value?.createToolIntent(context, listOf(Locale.ENGLISH), showTips = false)!! + assertEquals(expected.component, intent.component) + assertTrue(expected.extras equalsBundle intent.extras) + } + + navigator.assertIsEmpty() + verifyAll { + eventBus.post(OpenAnalyticsActionEvent(ACTION_OPEN_TOOL, TOOL, SOURCE_TOOL_DETAILS)) + } + } + + @Test + fun `Event - OpenTool - Tract Tool - With Second Language`() = runTest { + toolFlow.value = randomTool(TOOL, Tool.Type.TRACT) + every { languagesRepository.findLanguageFlow(Locale.FRENCH) } returns flowOf(Language(Locale.FRENCH)) + every { translationsRepository.findLatestTranslationFlow(TOOL, Locale.ENGLISH) } + .returns(flowOf(randomTranslation(TOOL, Locale.ENGLISH))) + every { translationsRepository.findLatestTranslationFlow(TOOL, Locale.FRENCH) } + .returns(flowOf(randomTranslation(TOOL, Locale.FRENCH))) + + createPresenter(ToolDetailsScreen(TOOL, secondLanguage = Locale.FRENCH)).test { + expectMostRecentItem().eventSink(Event.OpenTool) + } + + with(navigator.awaitNextScreen()) { + assertTrue(this is IntentScreen) + val expected = toolFlow.value?.createToolIntent( + context, + listOf(Locale.ENGLISH, Locale.FRENCH), + activeLocale = Locale.FRENCH, + showTips = false + )!! assertEquals(expected.component, intent.component) -// assertTrue(expected.extras equalsBundle intent.extras) + assertTrue(expected.extras equalsBundle intent.extras) } navigator.assertIsEmpty() diff --git a/library/base/src/main/kotlin/org/cru/godtools/base/Constants.kt b/library/base/src/main/kotlin/org/cru/godtools/base/Constants.kt index 88bd8099a5..e3b5e0bf74 100644 --- a/library/base/src/main/kotlin/org/cru/godtools/base/Constants.kt +++ b/library/base/src/main/kotlin/org/cru/godtools/base/Constants.kt @@ -7,6 +7,7 @@ const val EXTRA_TOOL = "tool" const val EXTRA_LANGUAGE = "language" const val EXTRA_LANGUAGES = "languages" const val EXTRA_PAGE = "page" +const val EXTRA_ACTIVE_LOCALE = "activeLocale" const val SCHEME_GODTOOLS = "godtools" const val HOST_GODTOOLSAPP_COM = "godtoolsapp.com" diff --git a/ui/base-tool/src/main/kotlin/org/cru/godtools/base/tool/activity/BaseToolRendererViewModel.kt b/ui/base-tool/src/main/kotlin/org/cru/godtools/base/tool/activity/BaseToolRendererViewModel.kt index b5b0f0d56e..efa1bde43f 100644 --- a/ui/base-tool/src/main/kotlin/org/cru/godtools/base/tool/activity/BaseToolRendererViewModel.kt +++ b/ui/base-tool/src/main/kotlin/org/cru/godtools/base/tool/activity/BaseToolRendererViewModel.kt @@ -15,6 +15,7 @@ import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.stateIn import org.ccci.gto.android.common.androidx.lifecycle.getMutableStateFlow import org.ccci.gto.android.common.kotlin.coroutines.flow.combineTransformLatest +import org.cru.godtools.base.EXTRA_ACTIVE_LOCALE import org.cru.godtools.base.EXTRA_TOOL import org.cru.godtools.base.tool.service.ManifestManager import org.cru.godtools.downloadmanager.GodToolsDownloadManager @@ -28,13 +29,9 @@ open class BaseToolRendererViewModel( userActivityManager: UserActivityManager, savedState: SavedStateHandle, ) : ViewModel() { - protected companion object { - internal const val STATE_ACTIVE_LOCALE = "activeLocale" - } - val supportedType = MutableStateFlow(null) val toolCode = savedState.getMutableStateFlow(viewModelScope, EXTRA_TOOL, null) - val locale = savedState.getMutableStateFlow(viewModelScope, STATE_ACTIVE_LOCALE, null) + val locale = savedState.getMutableStateFlow(viewModelScope, EXTRA_ACTIVE_LOCALE, null) val manifest = toolCode .combineTransformLatest(locale) { tool, locale -> diff --git a/ui/base-tool/src/main/kotlin/org/cru/godtools/base/tool/activity/MultiLanguageToolActivityDataModel.kt b/ui/base-tool/src/main/kotlin/org/cru/godtools/base/tool/activity/MultiLanguageToolActivityDataModel.kt index 15bbd41e6e..81d6c22c96 100644 --- a/ui/base-tool/src/main/kotlin/org/cru/godtools/base/tool/activity/MultiLanguageToolActivityDataModel.kt +++ b/ui/base-tool/src/main/kotlin/org/cru/godtools/base/tool/activity/MultiLanguageToolActivityDataModel.kt @@ -36,6 +36,7 @@ import org.ccci.gto.android.common.androidx.lifecycle.observeOnce import org.ccci.gto.android.common.androidx.lifecycle.switchCombineWith import org.ccci.gto.android.common.androidx.lifecycle.switchFold import org.ccci.gto.android.common.androidx.lifecycle.withInitialValue +import org.cru.godtools.base.EXTRA_ACTIVE_LOCALE import org.cru.godtools.base.EXTRA_TOOL import org.cru.godtools.base.tool.BaseToolRendererModule.Companion.IS_CONNECTED_LIVE_DATA import org.cru.godtools.base.tool.activity.BaseToolActivity.LoadingState @@ -143,7 +144,7 @@ class MultiLanguageToolActivityDataModel @Inject constructor( // endregion Loading State // region Active Tool - val activeLocale by savedState.livedata(STATE_ACTIVE_LOCALE) + val activeLocale by savedState.livedata(EXTRA_ACTIVE_LOCALE) val activeLoadingState = distinctToolCode.switchCombineWith(activeLocale) { tool, l -> combine( diff --git a/ui/base/src/main/kotlin/org/cru/godtools/base/ui/Activities.kt b/ui/base/src/main/kotlin/org/cru/godtools/base/ui/Activities.kt index 94d689bda7..6b20600c2b 100644 --- a/ui/base/src/main/kotlin/org/cru/godtools/base/ui/Activities.kt +++ b/ui/base/src/main/kotlin/org/cru/godtools/base/ui/Activities.kt @@ -7,6 +7,7 @@ import android.os.Bundle import java.util.Locale import org.ccci.gto.android.common.util.os.putLocale import org.ccci.gto.android.common.util.os.putLocaleArray +import org.cru.godtools.base.EXTRA_ACTIVE_LOCALE import org.cru.godtools.base.EXTRA_LANGUAGE import org.cru.godtools.base.EXTRA_LANGUAGES import org.cru.godtools.base.EXTRA_PAGE @@ -68,11 +69,13 @@ fun Activity.startTractActivity(toolCode: String, vararg languages: Locale?, sho fun Context.createTractActivityIntent( toolCode: String, vararg languages: Locale?, + activeLocale: Locale? = null, page: Int = 0, showTips: Boolean = false ) = Intent().setClassName(this, ACTIVITY_CLASS_TRACT) .putExtra(EXTRA_TOOL, toolCode) .putLanguagesExtra(*languages) + .putExtra(EXTRA_ACTIVE_LOCALE, activeLocale) .putExtra(EXTRA_PAGE, page) .putExtra(EXTRA_SHOW_TIPS, showTips) // endregion TractActivity