A sample application demoing some of the latest and greatest Android patterns, practices, and tools such as Android Jetpack.
Star Wars data provided by SWAPI.
This app follows Android's guide to app architecture by implementing strong separation of concerns and a model-driven UI.
More specifically, the UI (view) is separated from the data (model) layer with an Android ViewModel
.
As an example, the following diagram outlines how the people list screen functions:
PeopleListFragment
|
v
PeopleListViewModel
|
v
-----PeopleRepository-----
| |
v v
Room/SQLite SWAPI
PeopleListFragment
listens for UI state updates emitted by PeopleListViewModel
via a StateFlow
.
PeopleListFragment
listens for these updates in a coroutine tied to it's lifecycle to ensure updates are only processed when the UI is visible (LifecycleScope).
PeopleListViewModel
listens for data updates emitted by PeopleRepository
via a Flow
.
It does this in a coroutine tied the ViewModel ensuring the coroutine will be canceled when the ViewModel is no longer in use (ViewModelScope).
As the interface to our data sources, PeopleRepository
is responsible for fetching the underlying people data.
Our data sources are a SQLite DB and an HTTP web service. As described previously, PeopleRepository
will return a Flow
to the ViewModel.
This Flow will come directly from our database.
This pattern allows us to treat our database as the single source of truth where the ViewModel will listen for changes to our database which may be updated asynchronously with data fetched from the network.
Additionally, this application uses a single-activity/multiple fragment architecture with navigation managed by the Navigation component.
- DataStore (key-value storage)
- Gson (JSON serial/deserialization)
- Hilt (dependency injection)
- Lifecycle-aware components (component lifecycle changes)
- Navigation component (in-app navigation)
- Paging (load and display paginated data)
- Retrofit / OkHttp (REST/HTTP client)
- Room (SQLite abstraction)
This project uses a Gradle
buildSrc
directory to simplify our dependency management and versioning. (seebuildSrc
in the root of the project)