Skip to content

Commit

Permalink
add koin to multiplatform shared code
Browse files Browse the repository at this point in the history
  • Loading branch information
joreilly committed May 15, 2020
1 parent 1e2c2ea commit e317d84
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.surrus.peopleinspace

import android.app.Application
import com.surrus.common.di.commonModule
import com.surrus.common.di.initKoin
import com.surrus.common.repository.appContext
import com.surrus.peopleinspace.di.appModule
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin

class PeopleInSpaceApplication : Application() {

Expand All @@ -14,10 +15,10 @@ class PeopleInSpaceApplication : Application() {

appContext = this

startKoin {
initKoin {
androidLogger()
androidContext(this@PeopleInSpaceApplication)
modules(appModule)
modules(appModule, commonModule)
}
}
}
4 changes: 0 additions & 4 deletions app/src/main/java/com/surrus/peopleinspace/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.surrus.peopleinspace.di

import com.surrus.common.repository.PeopleInSpaceRepository
import com.surrus.peopleinspace.ui.PeopleInSpaceViewModel
import org.koin.android.viewmodel.dsl.viewModel
import org.koin.dsl.module

val appModule = module {

viewModel { PeopleInSpaceViewModel(get()) }

single { PeopleInSpaceRepository() }
}
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ allprojects {
google()
mavenCentral()
jcenter()
maven( "https://dl.bintray.com/ekito/koin")
maven("https://kotlin.bintray.com/kotlin-js-wrappers/")
}
}
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object Versions {

const val kotlin = "1.3.71"
const val kotlinCoroutines = "1.3.5-native-mt"
const val koin = "2.1.2"
const val koin = "3.0.0-alpha-2"
const val ktor = "1.3.2"
const val kotlinxSerialization = "0.20.0"
const val sqlDelight = "1.3.0"
Expand Down
2 changes: 2 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ kotlin {
implementation("com.squareup.sqldelight:runtime:${Versions.sqlDelight}")
implementation("com.squareup.sqldelight:coroutines-extensions:${Versions.sqlDelight}")

// koin
implementation("org.koin:koin-core:${Versions.koin}")
}
}

Expand Down
20 changes: 20 additions & 0 deletions common/src/commonMain/kotlin/com/surrus/common/di/Koin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.surrus.common.di

import com.surrus.common.remote.PeopleInSpaceApi
import com.surrus.common.repository.PeopleInSpaceRepository
import org.koin.core.context.startKoin
import org.koin.dsl.KoinAppDeclaration
import org.koin.dsl.module

fun initKoin(appDeclaration: KoinAppDeclaration = {}) = startKoin {
appDeclaration()
modules(commonModule)
}

// called by iOS etc
fun initKoin() = initKoin{}

val commonModule = module {
single { PeopleInSpaceRepository() }
single { PeopleInSpaceApi() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import com.surrus.common.remote.PeopleInSpaceApi
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.collect
import org.koin.core.KoinComponent
import org.koin.core.inject

expect fun createDb() : PeopleInSpaceDatabase?

// TEMP until following is resolved https://github.com/ktorio/ktor/issues/1622
expect fun ktorScope(block: suspend () -> Unit)


class PeopleInSpaceRepository {
private val peopleInSpaceApi = PeopleInSpaceApi()
class PeopleInSpaceRepository() : KoinComponent {
private val peopleInSpaceApi: PeopleInSpaceApi by inject()
private val peopleInSpaceDatabase = createDb()
private val peopleInSpaceQueries = peopleInSpaceDatabase?.peopleInSpaceQueries

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import UIKit
import common

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {



func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
KoinKt.doInitKoin()
return true
}

Expand Down
3 changes: 3 additions & 0 deletions macOS/PeopleInSpace/PeopleInSpace/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Cocoa
import SwiftUI
import common

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
Expand All @@ -16,6 +17,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {


func applicationDidFinishLaunching(_ aNotification: Notification) {
KoinKt.doInitKoin()

// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
//

import WatchKit
import common

class ExtensionDelegate: NSObject, WKExtensionDelegate {

func applicationDidFinishLaunching() {
// Perform any final initialization of your application.
KoinKt.doInitKoin()
}

func applicationDidBecomeActive() {
Expand Down

0 comments on commit e317d84

Please sign in to comment.