Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GT-2269 Open tool from tool details with filter language #3413

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/kotlin/org/cru/godtools/util/ActivityUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Locale>, showTips: Boolean = false) = code?.let { code ->
fun Tool.createToolIntent(
context: Context,
languages: List<Locale>,
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])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -265,19 +267,49 @@ 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)
}

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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Manifest.Type?>(null)
val toolCode = savedState.getMutableStateFlow<String?>(viewModelScope, EXTRA_TOOL, null)
val locale = savedState.getMutableStateFlow<Locale?>(viewModelScope, STATE_ACTIVE_LOCALE, null)
val locale = savedState.getMutableStateFlow<Locale?>(viewModelScope, EXTRA_ACTIVE_LOCALE, null)

val manifest = toolCode
.combineTransformLatest(locale) { tool, locale ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -143,7 +144,7 @@ class MultiLanguageToolActivityDataModel @Inject constructor(
// endregion Loading State

// region Active Tool
val activeLocale by savedState.livedata<Locale?>(STATE_ACTIVE_LOCALE)
val activeLocale by savedState.livedata<Locale?>(EXTRA_ACTIVE_LOCALE)

val activeLoadingState = distinctToolCode.switchCombineWith(activeLocale) { tool, l ->
combine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down