Android Architecture¶
Why there is need to change the architecture, is it right time to rewrite whole app or change it incrementally, what goals to be achieved with new architecture… There are lot of questions out there if we want to change. So why we decided to change our architecture? Its simple
- Clean Architecture
- Separation of Resposibilities
- Common development environment for andorid and ios
There are lot of degrees to consider. After lot of trial and error we came up with MVVM-C Architecture for both android and ios.
Base - MVVM-C¶
The MVVM - C pattern departs from commonly known MVVM by removing responsibility to navigate from view to coordinator, as this is part of business logic.
In android
- Model: As name suggest is responsible to get data from database or network
- View: Handles user interactions and delivers this knowledge to view model
- View Model(VM): Core part of app, it has business logic
- Coordinator: Manages how user navigates from one view to another.
Here, We will discuss Coordinator, View and VM as Model is discussed in detail in Network and Database sections.
View¶
Fragment and xml handles responsibility of view. They hold reference to vm, providing it user actions and listening to event from vm. We have used rx and databinding to achieve this. Our all observer is added to composite disposable object to prevent multiple initializing multiple observer.
1 2 3 4 5 6 | lateinit var navigator: Contract.Navigator
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
setViewModel(V::class.java, viewModelFactory)
return setAndBindContentView(inflater, container, savedInstanceState, R.layout.fragment_sample)
}
|
In above snippet, our fragment holds reference to navigator as well as vm (property with BaseFragment class).
View model- VM¶
View model is core of our android app which has responsibility to implement business logic. It achieves it by holding observable(eg. mutable live data or subjects or observable) and reference to data layer. Our view observes them. Since, our vm observes event from model it also has composite disposable attached to it.
Coordinator¶
Our activity take the responsibility of creating view i.e. fragment and navigating among views. It holds code for creating new activity and view. It also have implementation for all navigations that view i.e. fragment supports generated by the activity.
Network¶