MAJORITY assignment solution in Kotlin via MVVM Repository Pattern.
REST countries sample app that loads information from REST countries API V3 to show an approach to using some of the best practices in Android Development.
Cache Strategy
On the very first launch app send the request to the API and fetch the data,after successfully fetch save all the data in the local cache DataStoredPreference. On the next launch app will check need to fetch the data from the api or show the local storage data. it's depends on the fetch interval defined in FETCH_INTERVAL_IN_SEC
in Constants.kt
. Current interval is 24 Hours.You can change it from Constants.kt
.
Libraries Used
- Foundation - Components for core system capabilities, Kotlin extensions and support for multidex and automated testing.
- Android KTX - Write more concise, idiomatic Kotlin code.
- Test - An Android testing framework for unit and runtime UI tests.
- Architecture - A collection of libraries that help you design robust, testable, and maintainable apps. Start with classes for managing your UI component lifecycle and handling data persistence.
- Data Binding - Declaratively bind observable data to UI elements.
- Lifecycles - Create a UI that automatically responds to lifecycle events.
- LiveData - Build data objects that notify views when the underlying database changes.
- Navigation - Handle everything needed for in-app navigation.
- Room - Access your app's SQLite database with in-app objects and compile-time checks.
- ViewModel - Store UI-related data that isn't destroyed on app rotations. Easily schedule asynchronous tasks for optimal execution.
- UI - Details on why and how to use UI Components in your apps - together or separate
- Animations & Transitions - Move widgets and transition between screens.
- Fragment - A basic unit of composable UI.
- Layout - Lay out widgets using different algorithms.
- Third party and miscellaneous libraries
- Picasso for image loading
- Hilt: for dependency injection
- Kotlin Coroutines for managing background threads with simplified code and reducing needs for callbacks
- SSP An android lib that provides a new size unit - ssp (scalable sp). This size unit scales with the screen size based on the sp size unit (for texts). It can help Android developers with supporting multiple screens.
- SDP An android lib that provides a new size unit - sdp (scalable dp). This size unit scales with the screen size. It can help Android developers with supporting multiple screens.
Upcoming features
Updates will include incorporating additional Jetpack components and updating existing components as the component libraries evolve.
TestCases are pending due to very tight work schedule these days.
API Response
Countries API return alot of data but currently I am using this data. Sample JSON.
[
{
"name": {
"common": "Malaysia",
"official": "Malaysia"
},
"unMember": true,
"currencies": {
"MYR": {
"name": "Malaysian ringgit",
"symbol": "RM"
}
},
"capital": [
"Kuala Lumpur"
],
"region": "Asia",
"subregion": "South-Eastern Asia",
"languages": {
"eng": "English",
"msa": "Malay"
},
"population": 32365998,
"timezones": [
"UTC+08:00"
],
"continents": [
"Asia"
],
"flags": {
"png": "https://flagcdn.com/w320/my.png"
},
"startOfWeek": "sunday"
}
]
MVVM Repository Pattern
The repository pattern is a structural design pattern. It’s instrumental for organizing how you access data. It also helps divide concerns into smaller parts.
Repository operations delegate to a relevant datasource. Datasources can be remote or local. The repository operation has logic that determines the relevance of a given datasource. For instance, the repository can provide a value from a local datasource or ask a remote datasource to load from the network.
Android Studio IDE setup
For development, the latest version of Android Studio is required. The latest version can be downloaded from here.
All the dependencies are declared in the build.gradle
file. No need to take any extra measure. Just import this project via Android Studio :)