Code Monkey home page Code Monkey logo

d-kmp-sample's Introduction

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

mainly waiting for 2 big things to happen:

  • SqlDelight releasing version 1.5.0, which will have support for JS/Web
  • Compose for Web to become available (it recently hit a great milestone)

d-kmp-sample's People

Contributors

dbaroncelli avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.