Today you will learn how to structure your source code to get a scaleable, maintainable and reliable application. For this we will use the officially supported application architecture - Model View ViewModel (MVVM).
LiveData is an observable dataholder, that helps you manage the Android lifecycle.
ViewModel provides the data for your UI and can survive configuration changes.
What you'll learn
How to create a simple note taking application using the MVVM architecture
How to utilize the ViewModel class to hold and provide UI data
How to convert your data into LiveData and observe data changes
How to abstract the data backend with the repository pattern in conjunction with MVVM
Create a new Project with an empty Activity. Add LiveData and ViewModel dependencies to your project.
For your MainActivity, create the layout for a small note taking application. It should contain:
An EditText field to write a note
A Button to save the note
A Button to delete all notes
A TextView to display all notes
Create a ViewModel class for your Activity to hold and manipulate the UI data. It should contain
The list of notes
A method to retrieve all notes in the list
A method to add notes to the list
A method delete all notes from the list
Retrieve the instance of your ViewModel in your Activity and display its data in the TextView. You should not use
LiveData
yet.
Modify your solution from the previous exercise to use LiveData.
Expand your solution to include a repository and a database. The database should just be an in-memory placeholder for now - we will learn to persist data next week :)
For this exercise we will simulate interacting with a web service. This could take time. Therefore we need to show a progress bar when data is loading. Your task is the following:
Add a ProgressBar and a Button to the layout.
Create a placeholder webclient class that can return a Note after a given period of time (simulating receiving data from the internet).
Add appropriate members to the View, ViewModel and Repository.
Whenever you click the button, the progress bar must be shown for 5 seconds before the Note from the internet is added to the list of notes.
new Timer().schedule(new TimerTask() {
@Override
public void run() {
//Do something in a background thread after the delay
}
}, 5000);
Show the architecture of your app project in class diagram using Astah.
Afterwards, start implementing MVVM in your app project.