Skip to content

Commit b0de2c7

Browse files
frettclaude
andcommitted
make createToolIntent a Context extension rather than a Tool extension
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent e64ee51 commit b0de2c7

10 files changed

Lines changed: 80 additions & 59 deletions

File tree

app/src/main/kotlin/org/cru/godtools/ui/dashboard/home/AllFavoritesPresenter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class AllFavoritesPresenter @AssistedInject constructor(
5858
when (it) {
5959
ToolCard.Event.Click,
6060
ToolCard.Event.OpenTool -> {
61-
val intent = tool.createToolIntent(
62-
context = context,
61+
val intent = context.createToolIntent(
62+
tool = tool,
6363
languages = listOfNotNull(
6464
tool.primaryLocale ?: state.translation?.languageCode,
6565
tool.parallelLocale

app/src/main/kotlin/org/cru/godtools/ui/dashboard/home/HomePresenter.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ class HomePresenter @AssistedInject constructor(
9393
lessonState = toolCardPresenter.present(lesson) {
9494
when (it) {
9595
ToolCard.Event.Click -> {
96-
val intent = lesson.createToolIntent(
97-
context = context,
96+
val intent = context.createToolIntent(
97+
tool = lesson,
9898
languages = listOfNotNull(lessonState.translation?.languageCode),
9999
resumeProgress = true,
100100
)
@@ -133,8 +133,8 @@ class HomePresenter @AssistedInject constructor(
133133
when (it) {
134134
ToolCard.Event.Click,
135135
ToolCard.Event.OpenTool -> {
136-
val intent = tool.createToolIntent(
137-
context = context,
136+
val intent = context.createToolIntent(
137+
tool = tool,
138138
languages = listOfNotNull(
139139
tool.primaryLocale ?: state.translation?.languageCode,
140140
tool.parallelLocale

app/src/main/kotlin/org/cru/godtools/ui/dashboard/lessons/LessonsPresenter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ class LessonsPresenter @AssistedInject constructor(
157157
eventBus.post(OpenAnalyticsActionEvent(ACTION_OPEN_LESSON, tool.code, SOURCE_LESSONS))
158158
navigator.goTo(
159159
IntentScreen(
160-
tool.createToolIntent(
161-
context = context,
160+
context.createToolIntent(
161+
tool = tool,
162162
languages = listOfNotNull(toolState.translation?.languageCode),
163163
resumeProgress = true
164164
) ?: return@present

app/src/main/kotlin/org/cru/godtools/ui/tooldetails/ToolDetailsPresenter.kt

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import dagger.assisted.AssistedFactory
2121
import dagger.assisted.AssistedInject
2222
import dagger.hilt.android.qualifiers.ApplicationContext
2323
import dagger.hilt.components.SingletonComponent
24+
import java.util.Locale
2425
import javax.inject.Named
2526
import kotlinx.collections.immutable.ImmutableList
2627
import kotlinx.collections.immutable.persistentListOf
@@ -143,7 +144,12 @@ class ToolDetailsPresenter @AssistedInject constructor(
143144
when (it) {
144145
UiEvent.NavigateUp -> navigator.pop()
145146

146-
UiEvent.OpenTool -> openTool(tool, translation, secondTranslation)
147+
UiEvent.OpenTool -> openTool(
148+
toolCode = toolCode,
149+
toolType = tool?.type ?: Tool.Type.UNKNOWN,
150+
firstLanguage = translation?.languageCode,
151+
secondLanguage = secondTranslation?.languageCode
152+
)
147153

148154
UiEvent.OpenToolTraining -> tool?.let {
149155
// TODO: handle opening training tips and optionally showing the tutorial locally once the
@@ -240,30 +246,25 @@ class ToolDetailsPresenter @AssistedInject constructor(
240246
}
241247

242248
private fun openTool(
243-
tool: Tool?,
244-
translation: Translation?,
245-
secondTranslation: Translation?,
249+
toolCode: String?,
250+
toolType: Tool.Type,
251+
firstLanguage: Locale?,
252+
secondLanguage: Locale?,
246253
showTips: Boolean = false,
247254
) {
248-
tool?.let { tool ->
249-
val intent = tool.createToolIntent(
250-
context = context,
251-
languages = when (tool.type) {
252-
Tool.Type.ARTICLE -> listOfNotNull(
253-
secondTranslation?.languageCode ?: translation?.languageCode
254-
)
255-
256-
else -> listOfNotNull(translation?.languageCode, secondTranslation?.languageCode)
257-
},
258-
activeLocale = secondTranslation?.languageCode,
259-
showTips = showTips
260-
)
255+
val intent = context.createToolIntent(
256+
type = toolType,
257+
toolCode = toolCode,
258+
languages = when (toolType) {
259+
Tool.Type.ARTICLE -> listOfNotNull(secondLanguage ?: firstLanguage)
260+
else -> listOfNotNull(firstLanguage, secondLanguage)
261+
},
262+
activeLocale = secondLanguage,
263+
showTips = showTips
264+
) ?: return
261265

262-
if (intent != null) {
263-
eventBus.post(OpenAnalyticsActionEvent(ACTION_OPEN_TOOL, tool.code, SOURCE_TOOL_DETAILS))
264-
navigator.goTo(IntentScreen(intent))
265-
}
266-
}
266+
eventBus.post(OpenAnalyticsActionEvent(ACTION_OPEN_TOOL, toolCode, SOURCE_TOOL_DETAILS))
267+
navigator.goTo(IntentScreen(intent))
267268
}
268269

269270
@AssistedFactory

app/src/main/kotlin/org/cru/godtools/util/ActivityUtils.kt

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,54 @@ fun Activity.openToolActivity(code: String, type: Type, vararg languages: Locale
2424
Type.META, Type.UNKNOWN -> Unit
2525
}
2626

27-
fun Tool.createToolIntent(
28-
context: Context,
27+
fun Context.createToolIntent(
28+
tool: Tool,
2929
languages: List<Locale>,
3030
activeLocale: Locale? = null,
3131
showTips: Boolean = false,
3232
saveLanguageSettings: Boolean = false,
33-
resumeProgress: Boolean = false
33+
resumeProgress: Boolean = false,
34+
) = createToolIntent(
35+
type = tool.type,
36+
toolCode = tool.code,
37+
languages = languages,
38+
activeLocale = activeLocale,
39+
showTips = showTips,
40+
saveLanguageSettings = saveLanguageSettings,
41+
resumeProgress = resumeProgress,
42+
progressLastPageId = tool.progressLastPageId,
43+
)
44+
45+
fun Context.createToolIntent(
46+
type: Type,
47+
toolCode: String?,
48+
languages: List<Locale>,
49+
activeLocale: Locale? = null,
50+
showTips: Boolean = false,
51+
saveLanguageSettings: Boolean = false,
52+
resumeProgress: Boolean = false,
53+
progressLastPageId: String? = null,
3454
): Intent? {
35-
val code = code ?: return null
55+
val toolCode = toolCode ?: return null
3656
if (languages.isEmpty()) return null
3757

3858
return when (type) {
39-
Type.ARTICLE -> context.createArticlesIntent(code, languages[0])
59+
Type.ARTICLE -> createArticlesIntent(toolCode, languages[0])
4060

41-
Type.CYOA -> context.createCyoaActivityIntent(
42-
code,
61+
Type.CYOA -> createCyoaActivityIntent(
62+
toolCode,
4363
*languages.toTypedArray(),
4464
saveLanguageSettings = saveLanguageSettings
4565
)
4666

47-
Type.LESSON -> context.createLessonActivityIntent(
48-
code,
67+
Type.LESSON -> createLessonActivityIntent(
68+
toolCode,
4969
languages[0],
5070
resumePageId = progressLastPageId.takeIf { resumeProgress }
5171
)
5272

53-
Type.TRACT -> context.createTractActivityIntent(
54-
code,
73+
Type.TRACT -> createTractActivityIntent(
74+
toolCode,
5575
*languages.toTypedArray(),
5676
activeLocale = activeLocale,
5777
showTips = showTips,

app/src/test/kotlin/org/cru/godtools/util/ActivityUtilsTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ class ActivityUtilsTest {
1919

2020
@Test
2121
fun `createToolIntent() - Invalid - code=null`() {
22-
assertNull(Tool(code = null).createToolIntent(context, listOf(Locale.ENGLISH)))
22+
assertNull(context.createToolIntent(Tool(code = null), listOf(Locale.ENGLISH)))
2323
}
2424

2525
@Test
2626
fun `createToolIntent() - Invalid - no languages`() {
27-
assertNull(randomTool().createToolIntent(context, emptyList()))
27+
assertNull(context.createToolIntent(randomTool(), emptyList()))
2828
}
2929
}

app/src/testDebug/kotlin/org/cru/godtools/ui/dashboard/home/AllFavoritesPresenterTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ class AllFavoritesPresenterTest {
157157
presenter.test {
158158
expectMostRecentItem().tools[0].eventSink(ToolCard.Event.Click)
159159

160-
val expected = tool.createToolIntent(
161-
context,
160+
val expected = context.createToolIntent(
161+
tool,
162162
languages = listOf(Locale.ENGLISH),
163163
saveLanguageSettings = true
164164
)
@@ -183,8 +183,8 @@ class AllFavoritesPresenterTest {
183183
presenter.test {
184184
expectMostRecentItem().tools[0].eventSink(ToolCard.Event.Click)
185185

186-
val expected = tool.createToolIntent(
187-
context,
186+
val expected = context.createToolIntent(
187+
tool,
188188
languages = listOf(Locale.FRENCH, Locale.GERMAN),
189189
saveLanguageSettings = true
190190
)

app/src/testDebug/kotlin/org/cru/godtools/ui/dashboard/home/HomePresenterTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class HomePresenterTest {
189189
expectMostRecentItem().spotlightLessons[0].eventSink(ToolCard.Event.Click)
190190

191191
assertIs<IntentScreen>(navigator.awaitNextScreen()).let {
192-
val expected = lesson.createToolIntent(context, listOf(translation.languageCode), resumeProgress = true)
192+
val expected = context.createToolIntent(lesson, listOf(translation.languageCode), resumeProgress = true)
193193
assertTrue(expected equalsIntent it.intent)
194194
}
195195
}
@@ -231,8 +231,8 @@ class HomePresenterTest {
231231
assertNotNull(expectMostRecentItem().favoriteTools[0]) { toolState ->
232232
toolState.eventSink(ToolCard.Event.Click)
233233

234-
val expected = tool.createToolIntent(
235-
context,
234+
val expected = context.createToolIntent(
235+
tool,
236236
listOf(toolState.translation!!.languageCode),
237237
saveLanguageSettings = true
238238
)
@@ -250,8 +250,8 @@ class HomePresenterTest {
250250
assertNotNull(expectMostRecentItem().favoriteTools[0]) { toolState ->
251251
toolState.eventSink(ToolCard.Event.OpenTool)
252252

253-
val expected = tool.createToolIntent(
254-
context,
253+
val expected = context.createToolIntent(
254+
tool,
255255
listOf(toolState.translation!!.languageCode),
256256
saveLanguageSettings = true
257257
)
@@ -269,8 +269,8 @@ class HomePresenterTest {
269269
assertNotNull(expectMostRecentItem().favoriteTools[0]) { toolState ->
270270
toolState.eventSink(ToolCard.Event.OpenTool)
271271

272-
val expected = tool.createToolIntent(
273-
context,
272+
val expected = context.createToolIntent(
273+
tool,
274274
listOf(Locale.GERMAN, Locale.FRENCH),
275275
saveLanguageSettings = true
276276
)

app/src/testDebug/kotlin/org/cru/godtools/ui/dashboard/lessons/LessonsPresenterTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ class LessonsPresenterTest {
384384
presenter.test {
385385
expectMostRecentItem().lessons[1].eventSink(ToolCard.Event.Click)
386386

387-
val expectedIntent = enLessonsFlow.value[1].createToolIntent(
388-
context,
387+
val expectedIntent = context.createToolIntent(
388+
enLessonsFlow.value[1],
389389
languages = listOf(Locale.ENGLISH),
390390
resumeProgress = true
391391
)

app/src/testDebug/kotlin/org/cru/godtools/ui/tooldetails/ToolDetailsPresenterTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ class ToolDetailsPresenterTest {
404404

405405
with(navigator.awaitNextScreen()) {
406406
assertTrue(this is IntentScreen)
407-
val expected = toolFlow.value?.createToolIntent(context, listOf(Locale.ENGLISH), showTips = false)!!
407+
val expected = context.createToolIntent(toolFlow.value!!, listOf(Locale.ENGLISH), showTips = false)!!
408408
assertTrue(expected equalsIntent intent)
409409
}
410410

@@ -428,8 +428,8 @@ class ToolDetailsPresenterTest {
428428

429429
with(navigator.awaitNextScreen()) {
430430
assertIs<IntentScreen>(this)
431-
val expected = toolFlow.value?.createToolIntent(
432-
context,
431+
val expected = context.createToolIntent(
432+
toolFlow.value!!,
433433
listOf(Locale.ENGLISH, Locale.FRENCH),
434434
activeLocale = Locale.FRENCH,
435435
showTips = false

0 commit comments

Comments
 (0)