- Notifications
You must be signed in to change notification settings - Fork91
D-KMP Architecture official sample: it uses a shared KMP ViewModel and Navigation for Compose and SwiftUI apps.
License
NotificationsYou must be signed in to change notification settings
dbaroncelli/D-KMP-sample
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This is the official sample of theD-KMP architecture, presenting a simple master/detail app, forAndroid,iOS andDesktop.
(theWeb version will be added at a later stage, when "Compose for Web" and "Kotlin/Wasm" becomemore mature)
- it uses the latestdeclarative UI toolkits:Compose forAndroid andSwiftUI foriOS
- itfully shares the ViewModel (includingnavigation logic anddata layer) viaKotlin MultiPlatform
- coroutine scopes arecancelled/reinitialized automatically, based on the current active screens and the app lifecycle (using LifecycleObserver onAndroid and the SwiftUI lifecycle oniOS)
- it implements theMVI pattern and theunidirectional data flow
- it implements theCQRS pattern, by providingCommand functions (viaEvents andNavigation) andQuery functions (viaStateProviders)
- it uses Kotlin'sStateFlow to trigger UI layer recompositions
- thenavigation state is processed in theshared code, and then exposed to theUI layer:
- onSwiftUI it seamlessly integrates with the new iOS 16 navigation patterns (NavigationStack and/orNavigationSplitView)
- onCompose it's a "remembered" data class which works on any platform (unlikeJetpack Navigation, which only works on Android)
you can find more info on these articles:
- D-KMP sample now leverages iOS 16 navigation (february 2023)
- The future of apps:Declarative UIs with Kotlin MultiPlatform (D-KMP) (november 2020)
- webservices (usingKtor Http Client)
- local db (usingSqlDelight)
- local settings (usingMultiplaformSettings)
- realtime db (usingFirestore)
- graphQL (usingApollo GraphQL)
- device bluetooth (usingKable)
- etc...
If you want to create your own app using the D-KMP Architecture, here are the instructions you need:
- 🛠️ in theviewmodel/screens folder: create a folder for each screen of the app, containing these3 files (as shown in the sample app structure above):
- screenEvents.kt, where the event functions for that screen are defined
- screenInit.kt, where the initialization settings for that screen are defined
- screenState.kt, where the data class of the state for that screen is defined
- 🛠️ in theNavigationSettings.kt file in thescreens folder, you should define your level 1 navigation and other settings
- 🛠️ in theScreenEnum.kt file in thescreens folder, you should define the enum with all screens in your app
- ✅ theScreenInitSettings.kt file in thescreens folder doesn't need to be modified
- ✅ the6 files in theviewmodel folder (DKMPViewModel.kt,Events.kt,Navigation.kt,ScreenIdentifier.kt,StateManager.kt,StateProviders.kt) don't need to be modified
- ✅ alsoDKMPViewModelForAndroid.kt inandroidMain andDKMPViewModelForIos.kt iniosMain don't need to be modified
- 🛠️ in thedatalayer/functions folder: create a file for each repository function to be called by the ViewModel's StateReducers
- 🛠️ in thedatalayer/objects folder: create a file for each data class used by the repository functions
- 🛠️ in thedatalayer/sources folder: create a folder for each datasource, where the datasource-specific functions (called by the repository functions) are defined
- ✅ thedatalayer/Repository.kt file should be modified only in case you want to add an extra datasource
- ✅ theApp.kt file doesn't need to be modified
- ✅ theMainActivity.kt file doesn't need to be modified
- The composables are used by both Android and Desktop apps:
- 🛠️ theLevel1BottomBar.kt andLevel1NavigationRail.kt files in thenavigation/bars folder should be modified to custom the Navigation bars items
- ✅ theTopBar.kt file in thenavigation/bars folder doesn't need to be modified
- ✅ theOnePane.kt andTwoPane.kt files in thenavigation/templates folder don't need to be modified
- ✅ theHandleBackButton.kt file in thenavigation folder doesn't need to be modified
- ✅ theRouter.kt file in thenavigation folder doesn't need to be modified
- 🛠️ in theScreenPicker.kt file in thenavigation folder, you should define the screen composables in your app
- 🛠️ in thescreens folder: create a folder for each screen of the app, containing all composables for that screen
- ✅ theMainComposable.kt file doesn't need to be modified
- 🛠️ theLevel1BottomBar.swift andLevel1NavigationRail.swift files in thecomposables/navigation/bars folder should be modified to custom the Navigation bars items
- ✅ theTopBar.swift file in thecomposables/navigation/bars folder doesn't need to be modified
- ✅ theOnePane.swift andTwoPane.swift files in thecomposables/navigation/templates folder don't need to be modified
- ✅ theRouter.swift file in thecomposables/navigation folder doesn't need to be modified
- 🛠️ in theScreenPicker.swift file in theviews/navigation folder, you should define the screen composables in your app
- 🛠️ in theviews/screens folder: create a folder for each screen of the app, containing all SwiftUI views for that screen
- ✅ theApp.swift file doesn't need to be modified
- ✅ theAppObservableObject.swift file doesn't need to be modified
- ✅ themain.kt file doesn't need to be modified
- The composables are used by both Android and Desktop apps:
- look at the description on theAndroid section above
- The best technology to implement a Web App in Kotlin will beCompose for Web backed byKotlin/Wasm. HoweverKotlin/Wasm is still at a very early stage: the first version was just released in February 2023, withKotlin 1.8.20.
- Compose For Web andKotlin/Wasm will allow us to build Compose projects seamlessly for the Web, using the same composables we are already using for the Desktop and Android.