Skip to content

Modern Architecture sample with MVI using SqlDelight & Ktor with modularization πŸ“¦

License

Notifications You must be signed in to change notification settings

shahzadansari/RickAndMorty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Rick & Morty App ✨

This project demonstrates Modern App Architecture best practices in a Multi-Module Android application using MVI architecture pattern. This app is made entirely in Kotlin and consumes pure Kotlin libraries to manage application data (SqlDelight, Ktor) and UI (Jetpack Compose). Dependency Injection is managed via Koin.

πŸ“· Screenshots (Light & Dark Color Schemes)

characters-list character-details

characters-list character-details

Dynamic Colors - Android 12+

Dynamic Color enables users to personalize their devices to align tonally with the color scheme of their personal wallpaper or through a selected color in the wallpaper picker. You can read more about Dynamic Colors here.

dynamic-colors.mov

πŸ“ Architecture Overview

The app architecture has three layers: a data layer, a domain layer and a UI layer. The architecture follows a reactive programming model with unidirectional data flow. With the data layer at the bottom, the key concepts are:

  • Higher layers react to changes in lower layers.
  • Events flow down.
  • Data flows up.

The data flow is achieved using streams, implemented using Kotlin Flows.

architecture-overview

UI Layer

The UI layer is the pipeline that converts application data changes to a form that the UI can present and then displays it. The state is managed using a Unidirectional Data Flow (UDF) which aligns with the MVI (Model View Intent) architecture pattern's event-based nature.

ui-layer-arch

Data Layer

While the UI layer contains UI-related state and UI logic, the data layer contains application data and business logic. The business logic is what gives value to your appβ€”it's made of real-world business rules that determine how application data must be created, stored, and changed. The data layer is implemented as an offline-first source of app data and business logic. It is the source of truth for all data in the app.

data-layer-arch

Domain Layer

The domain layer is an optional layer that sits between the UI layer and the data layer. The domain layer is responsible for encapsulating complex business logic, or simple business logic that is reused by multiple ViewModels. This layer contains Usecases/Interactors which interact with data sources inside Data Layer.

domain-layer-arch


Rick & Morty App is an offline-first app and the app flow works as:

  1. Characters are fetched from Rick & Morty API using Ktor-client for Android.
  2. Characters are then stored in a SqlDelight Database. Our database inside Data layer is our single source of truth.
  3. The stored characters in cache are then requested by the Usecase in our domain layer.
  4. Characters from the cache are then exposed to the UI layer where they can be accessed as observable data streams i.e. Kotlin Flows.

This app has two screens:

  1. Characters List Screen
  2. Character Details Screen

Characters List Screen Architecture

characters-list-arch

Character Details Screen Architecture

character-details-arch

πŸ“¦ Modularization Overview

We have following modules in our project. Among these modules, we have Android Application module, Android Library modules & pure Kotlin Library modules. I have preferred using pure Kotlin libraries in domain & data layers as they can easily be reused in Kotlin Multiplatform projects. Therefore, SqlDelight is preferred over Room & Ktor client is used instead of Retrofit.

Module name Type Description
app Android Application Brings everything together required for the app to function correctly. This includes UI scaffolding and navigation.
core Kotlin Library Core business models and classes (such as DataState/Result wrapper classes) that are used by multiple modules.
character-datasource Kotlin Library Contains data-sources and private models such as CharacterDto & CharacterEntity (network and cache) for the characters module.
character-domain Kotlin Library Domain models and classes for the characters module i.e. data class Character
character-interactors Kotlin Library Use-cases for the characters Module.
ui-character-list Android Library UI components for the CharactersList screen.
ui-character-details Android Library UI components for the CharacterDetails screen.
components Android Library Common Composables.

modules modularization-overview

πŸ—ƒοΈ Tech Stack

Get Started

There are a few ways to open this project.

Android Studio

  1. Android Studio -> File -> New -> From Version control -> Git
  2. Enter https://github.com/shahzadansari/RickAndMorty.git into URL field and press Clone button

Command-line And Android Studio

  1. Run git clone https://github.com/shahzadansari/RickAndMorty.git command to clone the project
  2. Open Android Studio and select File | Open... from the menu. Select the cloned directory and press Open button

⭐ Find this repository useful? Leave a Star

Add this repository to your watchlist to get notified about the latest updates in the repository.

⏭️ Future Work

  • v1.4 Unit Tests
  • v1.5 UI Tests
  • v1.6 Pagination

🀝 Want to Contribute?

All contributions are welcomed. This project is still in development. If you encounter any problems, please create an issue here & if you want to contribute to this project, PRs are welcomed! πŸ™‚

πŸ‘¨β€πŸ’» Developed By

Shahzad Ansari

Twitter Linkedin