Template (pure) for KMM application with DI support

Overview

KMM di template

Template (pure) for KMM application with DI support. Uses Multiplatform-DI for Dependency Injection

Features

  • Common architecture (VIP)
  • Common dependency injection
  • Coroutines for concurrency

Next

  • Support of Kotlin 1.5.0
  • Suport Coroutine Flows

Architecture and common concurrency

This template is for app with VIP architecture (sort of Clean architecture):

  • Interactor to communicate with common BL (IInteractor + BaseInteractor)
  • Presenter to process data from interactor and send to bound view (IPresenter + BasePresenter)
  • View (hold a link to interactor) (IView)

Also every VIP-module contains specific configurator to create all components and connect them together.

E.g the code from sample app:

class MoviesListConfigurator : IConfigurator {
    companion object {
        val instance = MoviesListConfigurator()
    }

    override fun create(view: IView): IInteractor? {
        val interactor: IMoviesListInteractor = MoviesListInteractor()
        val presenter = MoviesListPresenter()
        interactor.presenter = presenter
        presenter.view = view as? IMoviesListView
        return interactor
    }
}

Every interactor implements interface:

interface IInteractor {
    fun setup(di: DIManager)

    fun attachView()

    fun detachView()
}

Also it derives BaseInteractor with incapsulated common concurrency:

abstract class BaseInteractor<T : IView>(private val coroutineContext: CoroutineContext) {
    protected lateinit var scope: ModuleCoroutineScope

    fun attachView() {
        scope = ModuleCoroutineScope(coroutineContext)
    }

    fun detachView() {
        scope.viewDetached()
    }
}

class ModuleCoroutineScope(
    context: CoroutineContext
) : CoroutineScope {

    private var onViewDetachJob = Job()
    override val coroutineContext: CoroutineContext = context + onViewDetachJob

    fun viewDetached() {
        onViewDetachJob.cancel()
    }
}

All dependencies should be resolved in setup method of interactor:

class MoviesListInteractor :
    BaseInteractor<IMoviesListView>(uiDispatcher),
    IMoviesListInteractor {
    private var moviesService: MoviesService? = null
    override var presenter: IMoviesListPresenter? = null

    private var moviesList: ArrayList<MoviesItem> = arrayListOf()

    override fun setup(di: DIManager) {
        this.moviesService = di.resolve<MoviesService>(MoviesService::class) as? MoviesService
    }

The connection between view and interactor should be setup on native side:

// iOS MoviesListVC.swift

 @InjectedInView  var interactor: IMoviesListInteractor?

 override func viewDidLoad() {
        super.viewDidLoad()
        
        $interactor.view = self
   }
//Android MoviesListFragment.kt
val interactor: IMoviesListInteractor? by interactors(this)

//or 

var interactor: IMoviesListInteractor? = null

 override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.movies_list_fragment, container, false)?.also {
            this.interactor = App.container.resolve(this) as IMoviesListInteractor?
        }
    }

More info about property wrappers and delegates with DI you can find in WIKI (soon)

You might also like...
A pure Kotlin/Multiplatform implementation of group operations on Curve25519.

curve25519-kotlin A pure Kotlin/Multiplatform implementation of group operations on Curve25519. Gradle Kotlin DSL: dependencies { implementation("

AndroidappTemplate - A GitHub template repository intended to kickstart development on an Android application

Android App Template This is a GitHub template repository intended to kickstart

An Android template project (in Kotlin) with boilerplate and current patterns.

android-starter-v4 An Android template project (in Kotlin) with boilerplate and plumbing, exploring current architecture patterns. A bit too much for

Kotlin multiplatform library template.

template-kmp-library Kotlin multiplatform library template. Has a baseline setup for a multiplatform library supporting all kotlin targets except andr

Kotlin Multiplatform Mobile App Template

KMMT : Kotlin Multiplatform Mobile Template Kotlin Multiplatform Mobile Development Simplified KMMT is a KMM based project template designed to simpli

Improved project template for Android Studio

Improved project template for Android Studio Unfortunately the default Android Studio template has a lot of unnecessary stuff that has to be removed e

Spring-Boot Kotlin template for new microservices

kotlin-ms-template Spring-Boot Kotlin template for new microservices REST and GRPC ready Kafka producer/consumer ready Logs in JSON Format Base ready

A Kotlin Multiplatform and Compose template that allows you to easily set up your project targeting: Android, Desktop, and Web

A Kotlin Multiplatform and Compose template that allows you to easily set up your project targeting: Android, Desktop, and Web

🔨 Template for easy hosting of your Java/Kotlin libraries on GitHub

🔨 kotlin-jvm-library-template Another template for easy hosting your Java/Kotlin libraries on GitHub. Features boilerplate for Kotlin/Java projects w

Releases(v1.0)
Owner
Anna Zharkova
Anna Zharkova
KMM RSS Reader: an open-source, mobile, cross-platform application built with Kotlin Multiplatform Mobile.

KMM RSS Reader This is an open-source, mobile, cross-platform application built with Kotlin Multiplatform Mobile. It's a simple RSS reader, and you ca

Kotlin 1.4k Jan 4, 2023
An awesome list that curates the best KMM libraries, tools and more.

Awesome KMM Kotlin Multiplatform Mobile (KMM) is an SDK designed to simplify creating cross-platform mobile applications. With the help of KMM, you ca

Konstantin 994 Dec 28, 2022
Ricky and Morty episode guide using KMM and Apollo GraphQL Native

Ricky And Morty Episodes - Kmm Ricky and Morty episode guide using KMM and Apollo GraphQL Native This is a simple guide on how to create an KMM projec

Julio Ribeiro 2 Apr 15, 2022
use kmm to write a flutter plugin

use KMM to write a flutter plugin The reference plugin_codelab example plugin that accompanies the How to write a Flutter plugin codelab. I changed pl

libill 8 Nov 9, 2022
An unofficial Mangadex app using KMM

Mochi An unofficial Mangadex app using KMM. The app uses Mangadex new Public API, which may change at any time. ☢️ !!!Currently in Production. Not int

Michael24884 2 Jan 25, 2022
Kamper - a small KMM/KMP library that provides performance monitoring for your app.

?? Kamper Kamper is a KMP/KMM library that implements a unified way to track application performances. The solution is based on plugin design patterns

S. Mellouk 31 Jun 10, 2022
Example KMM app for showing about layered architecture

Layered Architecture in a Kotlin Multiplatform project This project was created by a series of posts you can find on my blog https://jflavio.com The d

Jose Flavio Quispe Irrazábal 13 Oct 10, 2022
A simple KMM app

Getting Ready Install latest Android Studio version (I’m using RC Dolphin). Make sure your Kotlin Plugin has 1.7.x version. Make sure you have Ruby ge

Lena Stepanova 2 Sep 20, 2022
Demo for Jetbrains webinar on "How to share data layer in KMM"

RealmDemo Demo application demostrating how to share data layer in an KMM project using Realm Kotlin SDK and Atlas App Service. Webinar Link : https:/

MongoDB Developer Relations 9 Dec 15, 2022
A pure-Kotlin library for bots to interface with Revolt

RevoltKt A pure-Kotlin library for bots to interface with Revolt Sample Usage import me.maya.revolt.defaultClientBuilder import me.maya.revolt.events.

Maya 8 May 20, 2022