You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
7 Hrs
36 Min
10 Seg
Curso de Jetpack Compose en Android

Curso de Jetpack Compose en Android

Amazon Web Services (AWS)

Amazon Web Services (AWS)

Arquitectura de Acci贸n - Estado - Evento en ViewModel

10/19
Resources

How to improve your screen interaction with ViewModel on Android?

It is fascinating to see how an application can respond to user interactions, allowing you to mark tasks as completed, delete them, or update them. When approaching programming in Android, the ViewModel is a key tool that makes it easy to organize and manage these interactive actions. Here are the essential steps to get your application to respond appropriately to these actions.

How to start with the initial modifications to your DataSource?

To enable the visibility of the information on the screen, some modifications are made to the FakeDataLocalDataSource class. Adding an init block after the task flow is essential. The goal is to load a first batch of completed and pending task information, merging lists so that everything is ready for display in the user interface.

init { completed = completedTask + pendingTask}

How to improve organization with a data class for status?

Separating the status from the home in a separate class improves the organization of the project. Using a specific data class like HomeDataState allows to keep everything much more organized and in its proper place.

data class HomeDataState( val completedTasks: List, val pendingTasks: List)

What is the purpose of a Sealed Interface in screen actions?

A Sealed Interface in Android allows you to clearly describe the actions that the user can perform. These actions are specified through separate data classes for each type of action. This allows to manage different behaviors such as marking complete tasks or deleting a specific task.

sealed interface HomeScreenAction { data class OnToggleTask(val task: Task) : HomeScreenAction data class OnDeleteTask(val task: Task) : HomeScreenAction object OnDeleteAllTasks : HomeScreenAction object OnAddTask : HomeScreenAction}

How to handle events derived from actions with a sealed class?

The use of a sealed class to model events is fundamental. These classes help map specific events that are triggered by user actions, ensuring a detailed and organized handling of each event.

sealed class HomeScreenEvent { object OnTaskUpdated : HomeScreenEvent() object OnAllTasksDeleted : HomeScreenEvent() object OnTaskDeleted : HomeScreenEvent()}

How to structure a ViewModel to connect actions and events?

The ViewModel is used to manage state changes and actions on the screen. Implementing a ViewModel involves:

  • Maintaining the state of the ViewModel using an event channel.

  • Filtering completed and pending tasks.

  • Updating the status with each data issue.

  • Privately handle state changes and process actions derived from the task flow.

    class HomeScreenViewModel : ViewModel() { private val taskLocalDataSourceFake = TaskLocalDataSourceFake() private val _state = mutableStateOf(HomeDataState(listOf(), listOf())) val state: State = _state

    fun onAction(action: HomeScreenAction) { when (action) { is HomeScreenAction.OnDeleteAllTasks -> { // Handle delete all tasks action } is HomeScreenAction.OnToggleTask -> { // Handle toggle task action } }} } }

    }

How to integrate actions and events correctly in the user interface?

Integrating actions and events involves handling them in the interactive elements of the interface. Each action and event must be properly implemented in the Composables of the screen to ensure reactivity.

  • Delete All Action: When the icon is selected to delete all tasks, it must be interacted with the ViewModel.

  • Event Filter: Use collect to react to events such as displaying informative Toast messages or immediate UI changes.

    fun HomeScreen(onAction: (HomeScreenAction) -> Unit) { // UI composables... FloatingActionButton(onClick = { onAction(HomeScreenAction.OnDeleteAllTasks) }) { Text(text = "Delete All") }

    // Handle actions...

    }

By implementing these practices, you are charged with designing an Android application that not only organizes its business logic better, but also responds nimbly to user interactions, significantly improving the user experience.

Contributions 6

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

`completedTask` y `pendingTask` se generaron cuando copiamos el preview de la pantalla si se lo est谩n preguntando :)
![](https://static.platzi.com/media/user_upload/image-fc102099-d929-45be-a290-0d76a5adea7c.jpg) Va quedando genial 馃殌, cada vez odio m谩s xml
woww, increible clase. a煤n no me acostumbro a escribir el c贸digo de esa manera, pero que calidad!!!
![](https://static.platzi.com/media/user_upload/image-64fa66d9-904c-4f3d-b4c8-1b293cc9e43f.jpg)
![](https://static.platzi.com/media/user_upload/Screenshot_1738931808-029a3eec-0113-401a-b644-cf784051939c.jpg)
Hola hola una consulta. que arquitectura va mas acorde con compose, o cual es la recomendable?