Skip to content

Architecture (EN)

Thanh-Son-Philippe Lam edited this page Apr 25, 2019 · 3 revisions

This application uses an architecture based on the Android Architecture Components (AAC). Specifically, it uses the following components:

Modules Responsibilities
Activity/Fragment Activities and fragments represent the user interface (UI). They present data obtained from a LiveData object and they communicate to each other by using the observer pattern. When created, the Activity or the Fragment subscribes to one or multiple LiveData provided by a ViewModel. When a change occurs, the LiveData will notify the observer who can then update the UI.
LiveData LiveData is a data holder. LiveData objects can be observed by a LifecycleOwner such as Activity and Fragment. LiveData respects the lifecycle of its observers and informs them of changes only if they are active.
ViewModel ViewModel provides data for activities or fragments. They hold LiveData objects and allow them to persist over configuration changes such as screen rotations. The LiveData objects are obtained from Repository modules.
UseCase Use cases define the operations that the app needs. To do so, they retrieve data from Repository modules, process it and return it as LiveData to ViewModels.
Repository Repository modules are responsible for retrieving data and returning it as LiveData to UseCases. To do this, they act as a mediator between multiple sources such as a database or API's. First, they fetch the data from the database. The data is returned if it exists. This allows the UI to display them while new data is being retrieved from a given API. After obtaining the new data, the Repository updates the database and the new data is routed over to the UI.
Resource Data sent to the UI are wrapped by the Resource class. Resource is responsible for describing the data with a status. This allows observers to know if the loading has succeded, is in progress or has failed.
DB The database module uses the Room persistence library to persist the app's data. Requested data is returned as LiveDatas.
API API modules are responsible for sending requests to a webservice to retrieve data. To do this, they use the Retrofit HTTP library. It returns an object of the interface Retrofit2.Call. This object is converted to a LiveData before being returned.
ApiResponse An ApiResponse represents a response returned by a web service. ApiResponse allows the conversion of Retrofit2.Call into a LiveData.