Regarding Android Jetpack-Compose it is common practice to put your logic-parts inside your view-model.
Thats why I am using UI-States to abstract my businesslogic from what I display.
But where should I put necessary “visual-decision-logic” at.
This question is intended as a generic question of architecture best-practice in android compose.
visual-decision-logic := what is to be displayed depending on UI-State
Example – allowing simple logic inside nested composables
/**
* this is the topmost Composable which is called by the Activity
*/
@Composable
fun DashboardScreen(dashboardViewModel: DashboardViewModel = hiltViewModel()) {
val dashboardLayoutState: DashboardLayoutState? by
dashboardViewModel.dashboardLayoutState.collectAsStateWithLifecycle()
val isLongpressHintDisplayed: Boolean by
dashboardViewModel.isLongpressHintDisplayed.collectAsStateWithLifecycle(
initialValue = false
)
DashboardComposable(
isLongpressHintDisplayed,
dashboardLayoutState
)
}
@Composable
private fun DashboardComposable(
isHintDisplayed: Boolean,
dashboardLayoutState: DashboardLayoutState?
) {
val pagerState = rememberPagerState(pageCount = { dashboardLayoutState.pages.size })
HorizontalPager(
state = pagerState
) { index ->
// question is about this if statement/logic
// also a longer when-statement could be here instead
if (isHintDisplayed) {
HintComposable()
} else {
DashboardPageComposable(
pageIndex = index
)
}
}
}
pro: straight forward coding, local-variables like pagerState are stored and used where needed
con: nested components are not “good” testable because of “hidden” paths and may get worst in bigger compose-trees