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).

Google has made it easier to implement this architecture by using Android Architecture Components, which includes the LiveData and ViewModel components.

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

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:

Create a ViewModel class for your Activity to hold and manipulate the UI data. It should contain

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:

  1. Add a ProgressBar and a Button to the layout.
  2. Create a placeholder webclient class that can return a Note after a given period of time (simulating receiving data from the internet).
  3. Add appropriate members to the View, ViewModel and Repository.
  4. 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.