added platform-specific koin providers

TODO(fix nullable inject for web)
This commit is contained in:
thelumiereguy 2021-04-12 00:59:36 +05:30
parent a7e8482c1b
commit 9917d2beb9
10 changed files with 46 additions and 59 deletions

View file

@ -1,20 +1,17 @@
package com.surrus.common.repository
import android.content.Context
import co.touchlab.kermit.LogcatLogger
import co.touchlab.kermit.Logger
import com.squareup.sqldelight.android.AndroidSqliteDriver
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.koin.dsl.module
lateinit var appContext: Context
actual fun createDb(): PeopleInSpaceDatabase? {
val driver = AndroidSqliteDriver(PeopleInSpaceDatabase.Schema, appContext, "peopleinspace.db")
return PeopleInSpaceDatabase(driver)
actual fun platformModule() = module {
single {
val driver =
AndroidSqliteDriver(PeopleInSpaceDatabase.Schema,get(), "peopleinspace.db")
PeopleInSpaceDatabase(driver)
}
}
actual fun getLogger(): Logger = LogcatLogger()

View file

@ -4,6 +4,7 @@ import co.touchlab.kermit.Kermit
import com.surrus.common.remote.PeopleInSpaceApi
import com.surrus.common.repository.PeopleInSpaceRepository
import com.surrus.common.repository.getLogger
import com.surrus.common.repository.platformModule
import io.ktor.client.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
@ -16,7 +17,7 @@ import org.koin.dsl.module
fun initKoin(enableNetworkLogs: Boolean = false, appDeclaration: KoinAppDeclaration = {}) =
startKoin {
appDeclaration()
modules(commonModule(enableNetworkLogs = enableNetworkLogs))
modules(commonModule(enableNetworkLogs = enableNetworkLogs), platformModule())
}
// called by iOS etc

View file

@ -1,8 +1,8 @@
package com.surrus.common.repository
import co.touchlab.kermit.Logger
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
import org.koin.core.module.Module
expect fun createDb() : PeopleInSpaceDatabase?
expect fun platformModule(): Module
expect fun getLogger(): Logger

View file

@ -4,13 +4,13 @@ import co.touchlab.kermit.Logger
import co.touchlab.kermit.NSLogLogger
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.koin.dsl.module
actual fun createDb(): PeopleInSpaceDatabase? {
val driver = NativeSqliteDriver(PeopleInSpaceDatabase.Schema, "peopleinspace.db")
return PeopleInSpaceDatabase(driver)
actual fun platformModule() = module {
single {
val driver = NativeSqliteDriver(PeopleInSpaceDatabase.Schema, "peopleinspace.db")
PeopleInSpaceDatabase(driver)
}
}
actual fun getLogger(): Logger = NSLogLogger()

View file

@ -3,13 +3,12 @@ package com.surrus.common.repository
import co.touchlab.kermit.CommonLogger
import co.touchlab.kermit.Logger
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.koin.dsl.module
actual fun createDb(): PeopleInSpaceDatabase? {
return null
actual fun platformModule() = module {
single<PeopleInSpaceDatabase?> {
null
}
}
actual fun getLogger(): Logger = CommonLogger()

View file

@ -1,18 +1,12 @@
package com.surrus
import com.surrus.common.di.createDbClient
import com.surrus.common.di.initKoin
import com.surrus.common.remote.PeopleInSpaceApi
import kotlinx.coroutines.runBlocking
import org.koin.dsl.module
fun main() {
runBlocking {
val koin = initKoin(enableNetworkLogs = true){
modules(module {
single { createDbClient() }
})
}.koin
val koin = initKoin(enableNetworkLogs = true).koin
val api = koin.get<PeopleInSpaceApi>()
println(api.fetchPeople())
}

View file

@ -4,12 +4,14 @@ import co.touchlab.kermit.CommonLogger
import co.touchlab.kermit.Logger
import com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
import org.koin.dsl.module
actual fun getLogger(): Logger = CommonLogger()
actual fun createDb(): PeopleInSpaceDatabase? {
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
.also { PeopleInSpaceDatabase.Schema.create(it) }
return PeopleInSpaceDatabase(driver)
}
actual fun getLogger(): Logger = CommonLogger()
actual fun platformModule() = module {
single {
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
.also { PeopleInSpaceDatabase.Schema.create(it) }
PeopleInSpaceDatabase(driver)
}
}

View file

@ -1,17 +1,16 @@
package com.surrus.common.repository
import co.touchlab.kermit.CommonLogger
import co.touchlab.kermit.Logger
import co.touchlab.kermit.NSLogLogger
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.koin.dsl.module
actual fun createDb(): PeopleInSpaceDatabase? {
val driver = NativeSqliteDriver(PeopleInSpaceDatabase.Schema, "peopleinspace.db")
return PeopleInSpaceDatabase(driver)
actual fun platformModule() = module {
single {
val driver = NativeSqliteDriver(PeopleInSpaceDatabase.Schema, "peopleinspace.db")
PeopleInSpaceDatabase(driver)
}
}
actual fun getLogger(): Logger = NSLogLogger()

View file

@ -4,13 +4,14 @@ import co.touchlab.kermit.Logger
import co.touchlab.kermit.NSLogLogger
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.koin.dsl.module
actual fun createDb(): PeopleInSpaceDatabase? {
val driver = NativeSqliteDriver(PeopleInSpaceDatabase.Schema, "peopleinspace.db")
return PeopleInSpaceDatabase(driver)
actual fun platformModule() = module {
single {
val driver = NativeSqliteDriver(PeopleInSpaceDatabase.Schema, "peopleinspace.db")
PeopleInSpaceDatabase(driver)
}
}
actual fun getLogger(): Logger = NSLogLogger()

View file

@ -18,7 +18,6 @@ import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.surrus.common.di.createDbClient
import com.surrus.common.di.initKoin
import com.surrus.common.model.personBios
import com.surrus.common.model.personImages
@ -27,7 +26,6 @@ import com.surrus.common.remote.PeopleInSpaceApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.skija.Image
import org.koin.dsl.module
import java.awt.image.BufferedImage
import java.io.ByteArrayOutputStream
import java.io.InputStream
@ -35,11 +33,7 @@ import java.net.HttpURLConnection
import java.net.URL
import javax.imageio.ImageIO
private val koin = initKoin(enableNetworkLogs = true){
modules(module {
createDbClient()
})
}.koin
private val koin = initKoin(enableNetworkLogs = true).koin
fun main() = Window {
var peopleState by remember { mutableStateOf(emptyList<Assignment>()) }