Android MVVM library
It's small architectural library, inspired by Android ViewModel, ReactJS and Clean Architecture. It has not too many code and you easily can gasp the main idea of this projects just within 15-30 min.
Rationale
Application architecture with clear separation of concern shines most obviously on big projects or projects with constant changes. Should you use it for small projects or prototypes? I think, yes. At least, you definitely can get benefits in application stability, what is important for projects of any kind.
Pros:
- You have clean and manageable components
- It's easy to detect and isolate a failed part
- With practice you will write code faster
- It definitely boosts re-usability of your code
- App becomes more open for changes
- Writing tests is really easy task
Cons:
- Even a small screen requires a pile of code for infrastructure (but it's not too big)
- You need some time to get used to it
- From now on you have no excuse for abandoning unit-tests
This project tries to reduce amount of infrastructure's code, which you need to write by yourself. It also enforces some limitations, which helps to follow SRP principle.
Usage
Artifacts are published in JCenter and Maven Central repository. Make sure that one of them is listed in the repository
section of build.gradle
file in your root project folder:
buildscript {
repositories {
jcenter()
}
}
or
buildscript {
repositories {
mavenCentral()
}
}
A single module project
If you have just a single module, then in build.gradle
file of this module add these dependencies:
dependencies {
implementation "com.github.asuslennikov:mvvm-domain:x.y.z"
implementation "com.github.asuslennikov:mvvm-presentation:x.y.z"
}
Please replace the 'x.y.z' by the latest available version (check the JCenter badge at the top of file).
A multi-module project
if you follow the clean architecture guideline and have separate modules for your business rules (a domain
module) and presentation (a presentation
module), then:
- in your
domain
module add this dependency inbuild.gradle
file:dependencies { api "com.github.asuslennikov:mvvm-domain:x.y.z" }
- and in
presentation
module:dependencies { implementation "com.github.asuslennikov:mvvm-presentation:x.y.z" }
Please replace the 'x.y.z' by the latest available version (check the JCenter badge at the top of file).
Library components
Data flow diagram
UML overview
On this image you can see an overview of main library components:
Components details
Presentation layer:
Screen
State
Effect
ViewModel
Domain layer:
UseCaseInput
UseCaseOutput
UseCase
Data layer:
Manager
Name | Manager |
---|---|
Synonyms | Gateway, Repository |
Component layer | Data |
Responsibility | It provides a platform specific implementation for interfaces, defined in domain layer |
UML diagram | No specific component diagram |
Notes | - It doesn't know anything about presentation layer. It depends only on domain layer. - It uses platform-specific tests (for example, you can use Robolectric). |
Example link | TBD |
Additional reading:
- Clean Architecture:
- presentation by Uncle Bob: https://www.youtube.com/watch?v=Nsjsiz2A9mg
- article: https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
- usage in real application (e-book, about 40 pages): https://five.agency/android-architecture-part-1-every-new-beginning-is-hard/
- ViewModel
- React Properties: https://www.w3schools.com/react/react_props.asp
- Reactive:
- Andre Staltz - "What if the user was a function?", link: https://www.youtube.com/watch?v=1zj7M1LnJV4
- Jake Wharton - "Managing The Reactive World with RxJava". Presentation: https://jakewharton.com/managing-the-reactive-world-with-rxjava/, video: https://www.youtube.com/watch?v=0IKHxjkgop4
- MVI pattern:
- Blog articles by Hannes Dorfmann:
- http://hannesdorfmann.com/android/model-view-intent
- http://hannesdorfmann.com/android/mosby3-mvi-1
- http://hannesdorfmann.com/android/mosby3-mvi-2
- http://hannesdorfmann.com/android/mosby3-mvi-3
- http://hannesdorfmann.com/android/mosby3-mvi-4
- http://hannesdorfmann.com/android/mosby3-mvi-5
- http://hannesdorfmann.com/android/mosby3-mvi-6
- http://hannesdorfmann.com/android/mosby3-mvi-7
- http://hannesdorfmann.com/android/mosby3-mvi-8
- Android Fragmented podcast: https://fragmentedpodcast.com/episodes/148/
- Blog articles by Hannes Dorfmann: