Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

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)

Key features of the D-KMP architecture:

  • 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:

Data sources used by this sample:

these are other data sources, not used by this sample, for which popular KMP libraries exist:

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 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

Data Layer

  • 🛠️ 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



PLATFORM-SPECIFIC CODE:

Android

Schermata 2021-06-26 alle 16 54 32

Schermata 2021-06-26 alle 17 03 13

  • ✅ 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

iOS

ios-files

  • 🛠️ 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

Desktop

Schermata 2021-06-26 alle 16 54 15

Schermata 2021-06-26 alle 17 03 13

  • ✅ themain.kt file doesn't need to be modified
  • The composables are used by both Android and Desktop apps:

Web (not yet implemented)

  • 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.

[8]ページ先頭

©2009-2025 Movatter.jp