Android MVVM Library

Additional

Language
Kotlin
Version
v1.1.1 (May 25, 2019)
Created
Apr 1, 2019
Updated
Jun 24, 2019
Owner
Arthur Ivanets (arthur3486)
Contributor
Arthur Ivanets (arthur3486)
1
Activity
Badge
Generate
Download
Source code
APK file

Promotion

Android MVVM Library + Comprehensive Demo Application

The library is accompanied by a comprehensive Demo Application that is built around MVVM and Clean Architecture concepts. The Demo Application utilizes such popular libraries as: RxJava, Dagger2, Android Navigation Architecture Component, OkHttp, Retrofit, Room, Glide.
Android library designed to greatly simplify the implementation process of an MVVM-based application by providing all the means necessary to solve the common problems and avoid the annoying boilerplate code.

Android MVVM Library will make the implementation of your MVVM-based application a trivial task thus allowing you to spend more time focusing on other important things.

Contents

Demo

Marvel Universe Application is an application built around MVVM and Clean Architecture concepts, as well as the data provided by the Marvel API. The application allows you to browse through the Marvel comics, events and characters; each of the aforementioned entities is accompanied by a corresponding detailed overview screen, which gives you even more insight into the Marvel Universe.

Screenshots

Application Architecture

Getting Started

Prerequisites

1. Make sure that you've added the jcenter() repository to your top-level build.gradle file.

buildscript {
    //...
    repositories {
        //...
        jcenter()
    }
    //...
}

2. Enable the jetifier and androidX support in the top-level gradle.properties file.

//...
android.enableJetifier=true
android.useAndroidX=true
//....

3. Update your compileSdkVersion in the module-level build.gradle file to 28+.

//...
android {
    //...
    compileSdkVersion 28
    //...
}
//...

4. Enable the Data Binding in the module-level build.gradle file.

//...
android {
    //...
    dataBinding {
        enabled true
    }
    //...
}
//...

5. Replace your com.android.support.appcompat.* dependency with the new androidx.appcompat.* alternative.

//...
dependencies {
    //...
    implementation "androidx.appcompat:appcompat:1.0.2"
    //...
}
//...

6. Add the Android Lifecycle (ViewModel), RxJava and RxBus dependencies to the module-level build.gradle file.

//...
dependencies {
    //...
    implementation "androidx.lifecycle:lifecycle-viewmodel:2.0.0"
    implementation "io.reactivex.rxjava2:rxjava:2.2.7"
    implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
    implementation "com.arthurivanets.rxbus:rxbus:1.0.1"
    implementation "com.arthurivanets.rxbus:rxbus-android:1.0.1"
    //...
}
//...

Android MVVM Dependencies

The basic implementation must include the core module

Latest version:

implementation "com.arthurivanets.mvvm:mvvm-core:X.Y.Z"

Which should be added to your module-level build.gradle file.

ext {
    //...
    androidMvvmLibraryVersion = "1.0.0"
}

dependencies {
    //...
    implementation "com.arthurivanets.mvvm:mvvm-core:$androidMvvmLibraryVersion"
}

After that you can proceed with further implementation.

See: Basic Implementation, Dagger Based Implementation, Navigation Component Based Implementation, Navigation Component and Dagger Based Implementation

Modules

The library is comprised of several modules, namely:

The mvvm-core module is a core module the other modules depend on. It provides all the means necessary to create the MVVM-based Fragments and Activities, as well the corresponding ViewModels. (See: MvvmFragment, MvvmActivity, BaseViewModel, AbstractViewModel, ViewModelEvent)

The mvvm-dagger module is a module that provides the MvvmActivity and MvvmFragment implementations that automatically handle the injection of the Dagger DI dependencies.

The mvvm-navigation module is a module that provides the MvvmActivity and MvvmFragment implementations with built-in support for the Android Navigation Component based navigation.

mvvm-navigation-dagger module is a module that provides the MvvmActivity and MvvmFragment implementations that have both the built-in support for the Android Navigation Component based navigation and automatic handling of the injection of the Dagger DI dependencies.

Basic Implementation

The basic implementation consists of 4 simple steps, namely:

  1. Creation of the ViewModel
  2. Creation of the ViewModel-specific events
  3. Creation of the layout.xml for the Activity/Fragment
  4. Implementation of the Activity/Fragment

So, let's start with the creation of the ViewModel for our Activity and/or Fragment.

SimpleViewModel.kt [contract] (click to expand)

interfaceSimpleViewModel:BaseViewModel {

    val loadingStateHolder :ObservableField<Boolean>

    // The rest of your observable fields and event propagation methods should be defined here

}



The ViewModel contract should implement the BaseViewModel interface.

SimpleViewModelImpl.kt [concrete implementation] (click to expand)

class SimpleViewModelImpl : AbstractViewModel(), SimpleViewModel {

    val loadingStateHolder = ObservableField(false)

    // Your concrete implementation...

}

The concrete implementation of the ViewModel should extend the AbstractViewModel class and implement the corresponding contract interface.

Then, create the ViewModel-specific events.

SimpleViewModelEvents.kt (click to expand)

sealedclassSimpleViewModelEvents<T>(payload :T? =null) :ViewModelEvent<T>(payload) { classSimpleEvent:SimpleViewModelEvents<Unit>() // The rest of your ViewModel Events go here... }

The implementation of the ViewModel-specific events should be based upon the

After that, let's create the

layout.xml files for both our Activity and Fragment.activity_simple_mvvm.xml + fragment_simple_mvvm.xml (click to expand)

<?xml version="1.0" encoding="utf-8"?> <layoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"> <!-- Data-binding-related --> <data> <variablename="viewModel"type="com.yourapplication.sample.SimpleViewModel"/> </data> <!-- The Content Layout --><!-- Your content layout goes here... --> </layout>

Finally, let's implement the MVVM-based Activity and Fragment.

SimpleMvvmActivity.kt (click to expand)

importcom.arthurivanets.mvvm.MvvmActivityclassSimpleMvvmActivity:MvvmActivity<ActivitySimpleMvvmBinding, SimpleViewModel>() { private lateinit var localViewModel :SimpleViewModeloverridefuninjectDependencies() { // Initialize your View Model here... localViewModel =SimpleViewModelImpl() } // The rest of the Activity Initialization goes here...overridefunonRegisterObservables() { // Register your ViewModel's observable fields here... localViewModel.loadingStateHolder.register { /* Handle the field state change */ } } overridefunonViewModelEvent(event :ViewModelEvent<*>) { super.onViewModelEvent(event) // handle the events emitted by the ViewModel here...when(event) { isSimpleViewModelEvents.SimpleEvent-> { /* Do something... */ } } } overridefungetLayoutId() :Int { returnR.layout.activity_simple_mvvm } overridefungetBindingVariable() :Int { returnBR.viewModel } overridefungetViewModel() :SimpleViewModel { return localViewModel } }

The implementation of the MVVM Activity should be based upon the Core

MvvmActivity class.SimpleMvvmFragment.kt (click to expand)


VideModelEvent class.