Skip to content

Commit

Permalink
use Circuit to render the AllFavoritesScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
frett committed Nov 19, 2024
1 parent 430923b commit 8fac326
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -54,17 +56,17 @@ 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
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)
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8fac326

Please sign in to comment.