From 8fac326ded25ed42ed83b729061673744e34e54d Mon Sep 17 00:00:00 2001 From: Daniel Frett Date: Mon, 18 Nov 2024 16:48:44 -0700 Subject: [PATCH] use Circuit to render the AllFavoritesScreen --- .../ui/dashboard/DashboardActivity.kt | 1 + .../godtools/ui/dashboard/DashboardLayout.kt | 32 +++------ .../ui/dashboard/home/AllFavoritesLayout.kt | 71 +------------------ 3 files changed, 15 insertions(+), 89 deletions(-) diff --git a/app/src/main/kotlin/org/cru/godtools/ui/dashboard/DashboardActivity.kt b/app/src/main/kotlin/org/cru/godtools/ui/dashboard/DashboardActivity.kt index 082c8fe2ab..aa1cf7ad14 100644 --- a/app/src/main/kotlin/org/cru/godtools/ui/dashboard/DashboardActivity.kt +++ b/app/src/main/kotlin/org/cru/godtools/ui/dashboard/DashboardActivity.kt @@ -43,6 +43,7 @@ class DashboardActivity : BaseActivity() { DashboardLayout( onEvent = { e -> when (e) { + is DashboardEvent.OpenIntent -> startActivity(e.intent) is DashboardEvent.OpenTool -> openTool(e.tool, e.type, *listOfNotNull(e.lang1, e.lang2).toTypedArray()) is DashboardEvent.OpenToolDetails -> diff --git a/app/src/main/kotlin/org/cru/godtools/ui/dashboard/DashboardLayout.kt b/app/src/main/kotlin/org/cru/godtools/ui/dashboard/DashboardLayout.kt index 2826072baf..7f3ec28c70 100644 --- a/app/src/main/kotlin/org/cru/godtools/ui/dashboard/DashboardLayout.kt +++ b/app/src/main/kotlin/org/cru/godtools/ui/dashboard/DashboardLayout.kt @@ -1,5 +1,6 @@ package org.cru.godtools.ui.dashboard +import android.content.Intent import androidx.activity.compose.BackHandler import androidx.compose.animation.Crossfade import androidx.compose.foundation.layout.Box @@ -37,6 +38,7 @@ import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import com.slack.circuit.foundation.CircuitContent import com.slack.circuit.foundation.NavEvent +import com.slack.circuitx.android.IntentScreen import java.util.Locale import kotlinx.coroutines.launch import org.ccci.gto.android.common.androidx.compose.material3.ui.navigationdrawer.toggle @@ -54,7 +56,7 @@ import org.cru.godtools.base.ui.dashboard.Page import org.cru.godtools.base.ui.theme.GodToolsTheme import org.cru.godtools.model.Tool import org.cru.godtools.shared.analytics.AnalyticsScreenNames -import org.cru.godtools.ui.dashboard.home.AllFavoritesList +import org.cru.godtools.ui.dashboard.home.AllFavoritesScreen import org.cru.godtools.ui.dashboard.home.DashboardHomeEvent import org.cru.godtools.ui.dashboard.home.HomeContent import org.cru.godtools.ui.dashboard.lessons.DashboardLessonsEvent @@ -62,9 +64,9 @@ import org.cru.godtools.ui.dashboard.lessons.LessonsLayout import org.cru.godtools.ui.dashboard.tools.ToolsScreen import org.cru.godtools.ui.drawer.DrawerMenuLayout import org.cru.godtools.ui.tooldetails.ToolDetailsScreen -import org.cru.godtools.ui.tools.ToolCardEvent internal sealed interface DashboardEvent { + class OpenIntent(val intent: Intent) : DashboardEvent open class OpenTool(val tool: String?, val type: Tool.Type?, val lang1: Locale?, val lang2: Locale? = null) : DashboardEvent class OpenLesson(lesson: String?, lang: Locale?) : OpenTool(lesson, Tool.Type.LESSON, lang) @@ -144,30 +146,18 @@ internal fun DashboardLayout(onEvent: (DashboardEvent) -> Unit, viewModel: Dashb } ) - Page.FAVORITE_TOOLS -> AllFavoritesList( - onEvent = { - when (it) { - is ToolCardEvent.Click, - is ToolCardEvent.OpenTool, -> onEvent( - DashboardEvent.OpenTool( - tool = it.tool, - type = it.toolType, - lang1 = it.lang1, - lang2 = it.lang2, - ) - ) - is ToolCardEvent.OpenToolDetails -> - onEvent(DashboardEvent.OpenToolDetails(it.tool)) - } - }, - ) - + Page.FAVORITE_TOOLS, Page.ALL_TOOLS -> { CircuitContent( - screen = ToolsScreen, + screen = when (page) { + Page.FAVORITE_TOOLS -> AllFavoritesScreen + Page.ALL_TOOLS -> ToolsScreen + else -> error("Page $page is not converted to Circuit yet") + }, onNavEvent = { when (it) { is NavEvent.GoTo -> when (val screen = it.screen) { + is IntentScreen -> onEvent(DashboardEvent.OpenIntent(screen.intent)) is ToolDetailsScreen -> onEvent( DashboardEvent.OpenToolDetails( screen.initialTool, diff --git a/app/src/main/kotlin/org/cru/godtools/ui/dashboard/home/AllFavoritesLayout.kt b/app/src/main/kotlin/org/cru/godtools/ui/dashboard/home/AllFavoritesLayout.kt index 6ba9e1b2c4..12dee9ca48 100644 --- a/app/src/main/kotlin/org/cru/godtools/ui/dashboard/home/AllFavoritesLayout.kt +++ b/app/src/main/kotlin/org/cru/godtools/ui/dashboard/home/AllFavoritesLayout.kt @@ -11,90 +11,25 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue -import androidx.compose.runtime.key import androidx.compose.runtime.remember import androidx.compose.runtime.rememberUpdatedState import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp -import androidx.lifecycle.viewmodel.compose.viewModel +import com.slack.circuit.codegen.annotations.CircuitInject +import dagger.hilt.components.SingletonComponent import org.burnoutcrew.reorderable.ReorderableItem import org.burnoutcrew.reorderable.detectReorderAfterLongPress import org.burnoutcrew.reorderable.rememberReorderableLazyListState import org.burnoutcrew.reorderable.reorderable import org.cru.godtools.R -import org.cru.godtools.analytics.model.OpenAnalyticsActionEvent.Companion.ACTION_OPEN_TOOL -import org.cru.godtools.analytics.model.OpenAnalyticsActionEvent.Companion.ACTION_OPEN_TOOL_DETAILS -import org.cru.godtools.analytics.model.OpenAnalyticsActionEvent.Companion.SOURCE_FAVORITE import org.cru.godtools.ui.dashboard.home.AllFavoritesScreen.UiEvent import org.cru.godtools.ui.dashboard.home.AllFavoritesScreen.UiState import org.cru.godtools.ui.tools.ToolCard -import org.cru.godtools.ui.tools.ToolCardEvent -import org.cru.godtools.ui.tools.ToolViewModels - -@Composable -internal fun AllFavoritesList( - onEvent: (ToolCardEvent) -> Unit, - viewModel: HomeViewModel = viewModel(), - toolViewModels: ToolViewModels = viewModel(), -) { - val favoriteTools by viewModel.reorderableFavoriteTools.collectAsState() - - val state = UiState( - tools = favoriteTools.map { tool -> - key(tool.code) { - val toolViewModel = toolViewModels[tool.code.orEmpty(), tool] - val firstTranslation by toolViewModel.firstTranslation.collectAsState() - - toolViewModel.toState { - when (it) { - ToolCard.Event.Click -> { - viewModel.recordOpenClickInAnalytics(ACTION_OPEN_TOOL, tool.code, SOURCE_FAVORITE) - onEvent( - ToolCardEvent.Click( - tool = tool.code, - type = tool.type, - lang1 = firstTranslation.value?.languageCode, - ) - ) - } - - ToolCard.Event.OpenTool -> { - viewModel.recordOpenClickInAnalytics(ACTION_OPEN_TOOL, tool.code, SOURCE_FAVORITE) - onEvent( - ToolCardEvent.OpenTool( - tool = tool.code, - type = tool.type, - lang1 = firstTranslation.value?.languageCode, - ) - ) - } - - ToolCard.Event.OpenToolDetails -> { - viewModel.recordOpenClickInAnalytics(ACTION_OPEN_TOOL_DETAILS, tool.code, SOURCE_FAVORITE) - onEvent(ToolCardEvent.OpenToolDetails(tool.code)) - } - - ToolCard.Event.PinTool -> toolViewModel.pinTool() - ToolCard.Event.UnpinTool -> toolViewModel.unpinTool() - } - } - } - }, - eventSink = { - when (it) { - is UiEvent.MoveTool -> viewModel.moveFavoriteTool(it.from, it.to) - UiEvent.CommitToolOrder -> viewModel.commitFavoriteToolOrder() - } - } - ) - - AllFavoritesLayout(state) -} @Composable +@CircuitInject(AllFavoritesScreen::class, SingletonComponent::class) internal fun AllFavoritesLayout(state: UiState, modifier: Modifier = Modifier) { val tools by rememberUpdatedState(state.tools) val eventSink by rememberUpdatedState(state.eventSink)