Coder Social home page Coder Social logo

famzy / d-kmp-sample Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dbaroncelli/d-kmp-sample

0.0 0.0 0.0 3.2 MB

D-KMP Architecture official sample: it uses a shared KMP ViewModel for JetpackCompose and SwiftUI.

License: Apache License 2.0

Kotlin 80.27% Swift 19.73%

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:

May 13, 2021: UPDATE! There is an ongoing rewriting of the architecture, which should be completed by the end of May. So, the advise is not to go any deep into the current sample, as soon there will be major improvements, such as shared-code navigation.

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
  • ๐Ÿ› ๏ธ in the ScreenEnum.kt file in the viewmodel folder, you should define the enum with all screens in your app
  • โœ… the other 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 DKMPNavigation.kt file doesn't need to be modified
  • โœ… the MainActivity.kt file doesn't need to be modified
  • โœ… the MainComposable.kt file doesn't need to be modified
  • ๐Ÿ› ๏ธ the ScreenComposables.kt file should be modified to define the screen composables 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 DKMPApp.swift file doesn't need to be modified
  • โœ… the DKMPNavigation.swift file doesn't need to be modified
  • โœ… the MainView.swift file doesn't need to be modified
  • ๐Ÿ› ๏ธ the ScreenViews.swift file should be modified to define the screen views in the app

webApp (coming soon!)

d-kmp-sample's People

Contributors

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