belinwu / D-KMP-sample

D-KMP Architecture official sample

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

D-KMP architecture - official sample

This is the official sample of the D-KMP architecture, presenting a simple master/detail app, for both Android and iOS.

For more info on the D-KMP Architecture, please read the relevant Medium article.

Key features of the D-KMP architecture:

  • it uses the latest declarative UI toolkits: JetpackCompose for Android and SwiftUI for iOS
  • it fully shares the ViewModel (and the DataLayer) via Kotlin MultiPlatform
  • coroutine scopes are cancelled/reinitialized automatically, based on the current active screens and the app lifecycle (using LifecycleObserver on Android and the SwiftUI lifecycle on iOS)
  • it implements the MVI pattern and the unidirectional data flow
  • it implements the CQRS pattern, by providing 2 types of functions to the UI layer: Events and StateProviders
  • it uses Kotlin's StateFlow to trigger UI layer recompositions

Data sources used by this sample:

other popular KMP libraries for connecting to different data sources:

Instructions to write your own D-KMP app:

If you want to create your own app using the D-KMP Architecture, here are the instructions you need:

shared code:

View Model

  • in the viewmodel/screens folder: create a folder for each screen of the app, containing these 4 files (as shown in the sample app structure above):
    • screenEvents.kt, where the event functions for that screen are defined
    • screenState.kt, where the data class of the state for that screen is defined
    • screenStateProvider.kt, where the state provider function for that screen is defined
    • screenStateReducers.kt, where the state reducers functions (called by the events) for that screen are defined
  • the 5 files in the viewmodel folder (DKMPViewModel.kt, Events.kt, StateManager.kt, StateProviders.kt, StateReducers.kt) don't need to be modified
  • also DKMPViewModelForAndroid.kt in androidMain and DKMPViewModelForIos.kt in iosMain don't need to be modified

Data Layer

  • in the datalayer/functions folder: create a file for each repository function to be called by the ViewModel's StateReducers
  • in the datalayer/objects folder: create a file for each data class used by the repository functions
  • in the datalayer/sources folder: create a folder for each datasource, where the datasource-specific functions (called by the repository functions) are defined
  • the datalayer/Repository.kt file should be modified only in case you want to add an extra datasource

platform-specific code:

androidApp

  • in the screens folder: create a folder for each screen of the app, containing all JetpackCompose composables for that screen
  • the DKMPApp.kt file doesn't need to be modified
  • the MainActivity.kt file doesn't need to be modified
  • the Navigation.kt file should be modified to define the screens navigation in the app

iosApp

  • in the screens folder: create a folder for each screen of the app, containing all SwiftUI views for that screen
  • the AppObservableObject.swift file doesn't need to be modified
  • the ContentView.swift file should be modified to define which is the startup screen of the app
  • the DKMPApp.swift file doesn't need to be modified

webApp (coming soon!)

  • waiting for Compose for Web to become available (it recently hit a great milestone)

About

D-KMP Architecture official sample

License:Apache License 2.0


Languages

Language:Kotlin 81.5%Language:Swift 18.5%