Skip to content

Commit e317d84

Browse files
committed
add koin to multiplatform shared code
1 parent 1e2c2ea commit e317d84

File tree

10 files changed

+39
-12
lines changed

10 files changed

+39
-12
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.surrus.peopleinspace
22

33
import android.app.Application
4+
import com.surrus.common.di.commonModule
5+
import com.surrus.common.di.initKoin
46
import com.surrus.common.repository.appContext
57
import com.surrus.peopleinspace.di.appModule
68
import org.koin.android.ext.koin.androidContext
79
import org.koin.android.ext.koin.androidLogger
8-
import org.koin.core.context.startKoin
910

1011
class PeopleInSpaceApplication : Application() {
1112

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

1516
appContext = this
1617

17-
startKoin {
18+
initKoin {
1819
androidLogger()
1920
androidContext(this@PeopleInSpaceApplication)
20-
modules(appModule)
21+
modules(appModule, commonModule)
2122
}
2223
}
2324
}
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package com.surrus.peopleinspace.di
22

3-
import com.surrus.common.repository.PeopleInSpaceRepository
43
import com.surrus.peopleinspace.ui.PeopleInSpaceViewModel
54
import org.koin.android.viewmodel.dsl.viewModel
65
import org.koin.dsl.module
76

87
val appModule = module {
9-
108
viewModel { PeopleInSpaceViewModel(get()) }
11-
12-
single { PeopleInSpaceRepository() }
139
}

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ allprojects {
1919
google()
2020
mavenCentral()
2121
jcenter()
22+
maven( "https://dl.bintray.com/ekito/koin")
2223
maven("https://kotlin.bintray.com/kotlin-js-wrappers/")
2324
}
2425
}

buildSrc/src/main/java/Dependencies.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ object Versions {
44

55
const val kotlin = "1.3.71"
66
const val kotlinCoroutines = "1.3.5-native-mt"
7-
const val koin = "2.1.2"
7+
const val koin = "3.0.0-alpha-2"
88
const val ktor = "1.3.2"
99
const val kotlinxSerialization = "0.20.0"
1010
const val sqlDelight = "1.3.0"

common/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ kotlin {
8484
implementation("com.squareup.sqldelight:runtime:${Versions.sqlDelight}")
8585
implementation("com.squareup.sqldelight:coroutines-extensions:${Versions.sqlDelight}")
8686

87+
// koin
88+
implementation("org.koin:koin-core:${Versions.koin}")
8789
}
8890
}
8991

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.surrus.common.di
2+
3+
import com.surrus.common.remote.PeopleInSpaceApi
4+
import com.surrus.common.repository.PeopleInSpaceRepository
5+
import org.koin.core.context.startKoin
6+
import org.koin.dsl.KoinAppDeclaration
7+
import org.koin.dsl.module
8+
9+
fun initKoin(appDeclaration: KoinAppDeclaration = {}) = startKoin {
10+
appDeclaration()
11+
modules(commonModule)
12+
}
13+
14+
// called by iOS etc
15+
fun initKoin() = initKoin{}
16+
17+
val commonModule = module {
18+
single { PeopleInSpaceRepository() }
19+
single { PeopleInSpaceApi() }
20+
}

common/src/commonMain/kotlin/com/surrus/common/repository/PeopleInSpaceRepository.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ import com.surrus.common.remote.PeopleInSpaceApi
88
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
99
import kotlinx.coroutines.*
1010
import kotlinx.coroutines.flow.collect
11+
import org.koin.core.KoinComponent
12+
import org.koin.core.inject
1113

1214
expect fun createDb() : PeopleInSpaceDatabase?
1315

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

1719

18-
class PeopleInSpaceRepository {
19-
private val peopleInSpaceApi = PeopleInSpaceApi()
20+
class PeopleInSpaceRepository() : KoinComponent {
21+
private val peopleInSpaceApi: PeopleInSpaceApi by inject()
2022
private val peopleInSpaceDatabase = createDb()
2123
private val peopleInSpaceQueries = peopleInSpaceDatabase?.peopleInSpaceQueries
2224

ios/PeopleInSpaceSwiftUI/PeopleInSpaceSwiftUI/AppDelegate.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import UIKit
2+
import common
23

34
@UIApplicationMain
45
class AppDelegate: UIResponder, UIApplicationDelegate {
56

67

78

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

macOS/PeopleInSpace/PeopleInSpace/AppDelegate.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import Cocoa
1010
import SwiftUI
11+
import common
1112

1213
@NSApplicationMain
1314
class AppDelegate: NSObject, NSApplicationDelegate {
@@ -16,6 +17,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
1617

1718

1819
func applicationDidFinishLaunching(_ aNotification: Notification) {
20+
KoinKt.doInitKoin()
21+
1922
// Create the SwiftUI view that provides the window contents.
2023
let contentView = ContentView()
2124

watchos/PeopleInSpaceWatch/PeopleInSpaceWatch WatchKit Extension/ExtensionDelegate.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
//
55

66
import WatchKit
7+
import common
78

89
class ExtensionDelegate: NSObject, WKExtensionDelegate {
910

1011
func applicationDidFinishLaunching() {
11-
// Perform any final initialization of your application.
12+
KoinKt.doInitKoin()
1213
}
1314

1415
func applicationDidBecomeActive() {

0 commit comments

Comments
 (0)