-
Notifications
You must be signed in to change notification settings - Fork 0
Feature Screen Pattern
Andrés Pedraza Míguez edited this page Dec 6, 2025
·
2 revisions
To maintain clean code and enable Jetpack Compose @Preview, we separate the "Logic" from the "UI".
- Responsibility: Pure UI rendering.
- Dependencies: None. It relies on simple data classes or primitives.
- State: Stateless (receives state as parameters).
-
Events: Exposes lambdas (e.g.,
onClick: () -> Unit). - Benefit: Can be previewed easily in Android Studio with dummy data.
@Composable
fun DefaultCurrencyScreen(
state: CurrencyUiState, // Pure data
onSelect: (String) -> Unit // Event
) { ... }- Responsibility: wiring the logic.
-
Dependencies:
ViewModel,NavController(via Koin). -
State: Collects
StateFlowfrom ViewModel. - Events: Handles navigation or calls ViewModel functions.
- Benefit: Isolates dependencies from the rendering logic.
@Composable
fun DefaultCurrencyFeature(
viewModel: CurrencyViewModel = koinViewModel()
) {
val state by viewModel.state.collectAsState()
// Wraps the screen
FeatureScaffold(currentRoute = Routes.CURRENCY) {
DefaultCurrencyScreen(
state = state,
onSelect = viewModel::selectCurrency
)
}
}- Never pass a
ViewModelinto aScreen. - Never pass a
NavControllerinto aScreen. - Pass them into the
Feature, which orchestrates theScreen.