Feature module structure
Each feature module contains non-layer components and 3 layers with distinct set of responsibilities.
Presentation layer
This layer is closest to what the user sees on the screen. The presentationlayer is a mix of MVVM(Jetpack ViewModelused to preserve data across activity restart) and MVI (actionsmodify the common stateof the view and then new state is edited to a view via LiveData to be rendered).
Components:
·
View (Fragment) - presents data on the screen and pass user interactions to View Model. Views are hard to test, so they should be as simple as possible.
·
ViewModel- dispatches (through LiveData) state changes to the view and deals with user interactions (these view models are not simply <u>POJO classes</u>).
·
ViewState - common state for a single view
·
NavManager- singleton that facilitates handling all navigation events inside NavHostActivity (instead of separately, inside each view)
·
Domain layer
This is the core layer of the application. Notice that the domainlayer is independent of any other layers. This allows to make domain models and business logic independent from other layers. In other words, changes in other layers will have no effect on domain layer eg. changing database (data layer) or screen UI (presentationlayer) ideally will not result in any code change withing domain layer.
Components:
·
UseCase - contains business logic
·
DomainModel - defies the core structure of the data that will be used within the application. This is the source of truth for application data.
·
Repository interface- required to keep the domainlayer independent from the data layer (<u>Dependency inversion</u>).
·
Data layer
Manages application data and exposes these data sources as repositories to the domain layer. Typical responsibilities of this layer would be to retrieve data from the internet and optionally cache this data locally.
Components:
·
Repositoryis exposing data to the domainlayer. Depending on application structure and quality of the external APIs repository can also merge, filter, and transform the data. The intention of these operations is to create high-quality data source for the domain layer, not to perform any business logic (domainlayer use case responsibility).
·
·
Mapper- maps data modelto domain model(to keep domainlayer independent from the data layer).
·
·
RetrofitService - defines a set of API endpoints.
·
·
DataModel - defines the structure of the data retrieved from the network and contains annotations, so Retrofit (Moshi) understands how to parse this network data (XML, JSON, Binary...) this data into objects.
·
Data flow
Below diagram presents application data flow when a user interacts with album list screen:
MVVM:
ViewModel维护多个LiveData,View层可以使用DataBinding的响应式或者订阅后的命令式 方式更新UI显示。
但是会带来麻烦:多个LiveData的维护将会变得复杂,View层与ViewModel的交互比较分散,不成体系。
View与ViewModel通过ViewModel暴露的方法交互,比较凌乱难以维护。
MVI:
ViewModel依据Repository的返回数据,集中管理一个ViewState,View层只需要订阅一个ViewState便可获取页面的所有状态。
ViewModel通过ViewState与Action和View层通信,通过浏览ViewState和Action定义就可以理清ViewModel的职责,可以直接拿来作为接口文档使用。