Last active
July 24, 2025 15:33
-
-
Save thedroiddiv/b5f88c252437de5249565613177991ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun DashboardScreen( | |
| viewModel: DashboardScreenVM, | |
| navigateToPayments: () -> Unit, | |
| navigateToTasks: (taskId: String, workerId: String) -> Unit, | |
| navigateToHelp: (screenshot: Uri?) -> Unit, | |
| ) { | |
| val tutorialVideoNotAvailable = stringResource(id = R.string.tutorial_video_not_available) | |
| val noInternet = stringResource(id = R.string.no_internet) | |
| val snackbarHostState = remember { SnackbarHostState() } | |
| val uiState by viewModel.uiState.collectAsStateWithLifecycle() | |
| // I can improve by observing with lifecycle, haven't included here for simplicity | |
| LaunchedEffect(Unit) { | |
| viewModel.vmEvents.collectLatest { event -> | |
| when (event) { | |
| // When payments icon is clicked, event to sent to VM | |
| // VM fetches payment information from backend, and depending on some business logic, | |
| // user is either navigated to payments or shown a Modal Dialog (uiState.xyzDialogVisible) | |
| is DashboardVmEvent.NavigateToPayments -> navigateToPayments() | |
| // can perform navigation directly on click, but to be consistent here in navigation workflow | |
| // event is first sent to VM, then VM emits this event | |
| is DashboardVmEvent.NavigateToTask -> navigateToTasks(event.taskId, event.workerId) | |
| // on clicking help icon, a bitmap from composed screen is sent to ViewModel. | |
| // VM then saves that bitmap on disk (IO) with help of repo, then returns the Uri/Path | |
| is DashboardVmEvent.NavigateToHelp -> navigateToHelp(event.screenshotUri) | |
| // Need String Resources here, so I cannot show snackbar from VM directly | |
| is DashboardVmEvent.ShowSnackBar -> { | |
| when (event.snack) { | |
| DashboardSnack.TUTORIAL_VIDEO_NOT_AVAILABLE -> snackbarHostState | |
| .showSnackbar(tutorialVideoNotAvailable) | |
| DashboardSnack.NETWORK_UNAVAILABLE -> snackbarHostState | |
| .showSnackbar(noInternet) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| DashboardScreen( | |
| uiState = uiState, | |
| onUiEvent = viewModel::onUiEvent | |
| ) | |
| } | |
| @Composable | |
| private fun DashboardScreen( | |
| uiState: DashboardScreenUiState, | |
| onUiEvent: (DashboardScreenUiEvent) -> Unit | |
| ) { | |
| // Implementation | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment