Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MVVM on Android - DevFest Pisa

MVVM on Android - DevFest Pisa

MVVM is an architecture pattern for architecting applications that has become popular on Android.

In this workshop we will look at what is MVVM and how to use Architecture Components with them, and share my experience on how I build robust, maintainable and testable apps using them.

- What is MVVM
- How it differs from other design patterns
- Leveraging Architecture Components
- Brief look at testing
- Hands on to building a solid and testable app which you can take home ;)

Akshay Chordiya

April 13, 2019
Tweet

More Decks by Akshay Chordiya

Other Decks in Programming

Transcript

  1. 3 MV* Model represents state, structure, and behaviour of the

    user’s mental model View presents information that it retrieves from one or more model objects Model View
  2. 4 MVVM Model represents state, structure, and behaviour of the

    user’s mental model View presents information that it observes from one or more ViewModel objects Model View ViewModel Interacts with model and on change notifies the view to update
  3. Lifecycle LifecycleObserver [e.g. LiveData] LifecycleOwner [Activity / Fragment] LifecycleOwners are

    ViewController with their own lifecycle LifecycleObserver observe LifecycleOwners and are notified about lifecycle changes
  4. 12 class LifecycleAwareLogging : LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) fun listeningToCreate() {

    Timber.d("onCreate()") } @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) fun listeningToDestroy() { Timber.d("onDestroy()") } }
  5. 15 LiveData value updates Observe for updates Is STARTED or

    RESUMED Notify the observer Update the value
  6. 17

  7. 20 class CounterViewModel : ViewModel() { var counter: MutableLiveData<Int> =

    MutableLiveData() init { counter.value = 0 } } Emits data
  8. 21 class CounterViewModel : ViewModel() { var counter: MutableLiveData<Int> =

    MutableLiveData() init { counter.value = 0 } fun increment() { counter.value = counter.value?.inc() } } Emits data Emits data
  9. 22 // Get the ViewModel in [Activity, Fragment] val counterViewModel

    = ViewModelProviders.of(this) .get(CounterViewModel::class.java)
  10. 23 // Get the ViewModel in [Activity, Fragment] val counterViewModel

    = ViewModelProviders.of(this) .get(CounterViewModel::class.java) // Observe the `LiveData` for changes counterViewModel.counter.observe(this, Observer { count.text = counterViewModel.counter.toString() })
  11. 24 // Get the ViewModel in [Activity, Fragment] val counterViewModel

    = ViewModelProviders.of(this) .get(CounterViewModel::class.java) // Observe the `LiveData` for changes counterViewModel.counter.observe(this, Observer { count.text = counterViewModel.counter.toString() }) // Increment the count increment.setOnClickListener { counterViewModel.increment() }
  12. onCleared() ViewModel Scope Activity created Activity rotated finish() Finished onCreate

    onStart onResume onPause onStop onCreate onDestroy onStart onResume onPause onStop onDestroy
  13. onCleared() Activity created Activity rotated finish() Finished onCreate onStart onStop

    onCreate onDestroy onStart onResume onPause onStop onDestroy 0 0 1 1 2 onPause onResume 3 3
  14. 32 UI Controller [Activity / Fragment] ViewModel Repository Model Remote

    Data Source SQLite webservice All things related to View like rendering Holds data required by UI
  15. 33 UI Controller [Activity / Fragment] Repository Model Remote Data

    Source SQLite webservice ViewModel LiveData LiveData LiveData Room Retrofit
  16. Testing Component Tests Mock UI Espresso ViewModel ViewModel JUnit Repository

    Repository JUnit DAO and WebService DAO Instrumented - WebService Instrumented MockWebServer