Skip to content

A library to simplify the implementation of the MVI architecture in Android.

License

Notifications You must be signed in to change notification settings

Lime-blur/ModelViewEvent

Repository files navigation

ModelViewEvent

release yoptava license

  1. About
  2. Installation
  3. Examples

About

The library simplifies the implementation of the MVI architecture by imposing certain implementation rules. Pay attention to the implementation diagram. In order to implement the architecture, it is necessary:

  • First of all, create sealed classes ViewEvent and ViewState, which implement the BaseViewEvent and BaseViewState interfaces, respectively. This is necessary to ensure that all ViewEvents and ViewStates are of type BaseViewEvent and BaseViewState respectively
  • After that, create a ViewModel class that inherits from the abstract MVEViewModel class. The ViewModel class must implement a method for listening to ViewEvents coming from the UI. The ViewModel also has ready-made methods that get and change the current ViewState
    • If you need your own implementation of ViewModel, or you don't use ViewModel at all, you can inherit from the abstract class MVECustomViewModel
    • Please note that the MVEViewModel has exactly one _viewState field, which is of type SingleLiveEvent. This limitation ensures that the UI is unique and easy to update. No need to track a large number of different data sources
  • Next, you need to create an abstract class that inherits from the Fragment class and implements the MVEUserInterface interface. This abstract class will listen to ViewStates coming from the ViewModel and call the corresponding abstract methods. This class also performs the role of sending ViewEvents to the ViewModel. This class should not perform any other functions!

Thus, when creating a new Fragment, we do not need to create unnecessary methods for listening to ViewStates and sending ViewEvents, which pollute the current logic.

The library also has utility methods:

  • MVEViewModel.setState — changes the current ViewState. This is a shortened variant of calling method MVECustomViewModel.setViewState
  • MVEUserInterface.triggerEvent — used to send ViewEvent to ViewModel. This is a shortened variant of calling method MVEUserInterface.triggerViewEvent

Installation

Add this dependency to your module's build.gradle file:

dependencies {
  ..
  implementation 'com.github.Lime-blur:ModelViewEvent:latest_version'
}

Add it in your root build.gradle at the end of repositories (or in settings.gradle):

dependencyResolutionManagement {
  repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
  repositories {
    mavenCentral()
    maven { url 'https://jitpack.io' }
  }
}

Examples

See the sample module: https://github.com/Lime-blur/ModelViewEvent/blob/main/sample