removed static context and created platform-specific implementations for database-related dependencies

This commit is contained in:
thelumiereguy 2021-04-10 22:02:53 +05:30
parent aad690a580
commit 3222efc433
14 changed files with 57 additions and 37 deletions

View file

@ -2,9 +2,7 @@ package com.surrus.peopleinspace
import android.app.Application
import co.touchlab.kermit.Kermit
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
@ -17,8 +15,6 @@ class PeopleInSpaceApplication : Application(), KoinComponent {
override fun onCreate() {
super.onCreate()
appContext = this
initKoin {
androidLogger()
androidContext(this@PeopleInSpaceApplication)

View file

@ -1,9 +1,12 @@
package com.surrus.peopleinspace.di
import com.surrus.common.di.createDbClient
import com.surrus.common.repository.AndroidDatabaseDependencyProvider
import com.surrus.peopleinspace.ui.PeopleInSpaceViewModel
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module
val appModule = module {
viewModel { PeopleInSpaceViewModel(get(),get()) }
single { createDbClient(AndroidDatabaseDependencyProvider(get())) }
}

View file

@ -4,17 +4,18 @@ import android.content.Context
import co.touchlab.kermit.LogcatLogger
import co.touchlab.kermit.Logger
import com.squareup.sqldelight.android.AndroidSqliteDriver
import com.surrus.common.di.IDatabaseDependencyProvider
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
lateinit var appContext: Context
actual fun createDb(): PeopleInSpaceDatabase? {
val driver = AndroidSqliteDriver(PeopleInSpaceDatabase.Schema, appContext, "peopleinspace.db")
return PeopleInSpaceDatabase(driver)
actual fun createDb(dependencyProvider: IDatabaseDependencyProvider): PeopleInSpaceDatabase? {
return if(dependencyProvider is AndroidDatabaseDependencyProvider) {
val driver =
AndroidSqliteDriver(PeopleInSpaceDatabase.Schema, dependencyProvider.context, "peopleinspace.db")
PeopleInSpaceDatabase(driver)
} else null
}
actual fun getLogger(): Logger = LogcatLogger()
actual fun getLogger(): Logger = LogcatLogger()
class AndroidDatabaseDependencyProvider(val context: Context):IDatabaseDependencyProvider

View file

@ -0,0 +1,9 @@
package com.surrus.common.di
import com.surrus.common.repository.createDb
interface IDatabaseDependencyProvider
fun createDbClient(
dependencyProvider: IDatabaseDependencyProvider = object : IDatabaseDependencyProvider {}
) = createDb(dependencyProvider)

View file

@ -1,8 +1,8 @@
package com.surrus.common.repository
import co.touchlab.kermit.Logger
import com.surrus.common.di.IDatabaseDependencyProvider
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
expect fun createDb() : PeopleInSpaceDatabase?
expect fun createDb(dependencyProvider: IDatabaseDependencyProvider) : PeopleInSpaceDatabase?
expect fun getLogger(): Logger

View file

@ -8,6 +8,7 @@ import com.surrus.common.model.personImages
import com.surrus.common.remote.Assignment
import com.surrus.common.remote.IssPosition
import com.surrus.common.remote.PeopleInSpaceApi
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
@ -23,7 +24,7 @@ class PeopleInSpaceRepository : KoinComponent {
private val logger: Kermit by inject()
private val coroutineScope: CoroutineScope = MainScope()
private val peopleInSpaceDatabase = createDb()
private val peopleInSpaceDatabase : PeopleInSpaceDatabase? by inject()
private val peopleInSpaceQueries = peopleInSpaceDatabase?.peopleInSpaceQueries
var peopleJob: Job? = null

View file

@ -3,12 +3,10 @@ package com.surrus.common.repository
import co.touchlab.kermit.Logger
import co.touchlab.kermit.NSLogLogger
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver
import com.surrus.common.di.IDatabaseDependencyProvider
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
actual fun createDb(): PeopleInSpaceDatabase? {
actual fun createDb(dependencyProvider: IDatabaseDependencyProvider): PeopleInSpaceDatabase? {
val driver = NativeSqliteDriver(PeopleInSpaceDatabase.Schema, "peopleinspace.db")
return PeopleInSpaceDatabase(driver)
}

View file

@ -2,13 +2,11 @@ package com.surrus.common.repository
import co.touchlab.kermit.CommonLogger
import co.touchlab.kermit.Logger
import com.surrus.common.di.IDatabaseDependencyProvider
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
actual fun createDb(): PeopleInSpaceDatabase? {
actual fun createDb(dependencyProvider: IDatabaseDependencyProvider): PeopleInSpaceDatabase? {
return null
}

View file

@ -1,12 +1,18 @@
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).koin
val koin = initKoin(enableNetworkLogs = true){
modules(module {
createDbClient()
})
}.koin
val api = koin.get<PeopleInSpaceApi>()
println(api.fetchPeople())
}

View file

@ -3,10 +3,11 @@ package com.surrus.common.repository
import co.touchlab.kermit.CommonLogger
import co.touchlab.kermit.Logger
import com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver
import com.surrus.common.di.IDatabaseDependencyProvider
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
actual fun createDb(): PeopleInSpaceDatabase? {
actual fun createDb(dependencyProvider: IDatabaseDependencyProvider): PeopleInSpaceDatabase? {
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
.also { PeopleInSpaceDatabase.Schema.create(it) }
return PeopleInSpaceDatabase(driver)

View file

@ -1,15 +1,12 @@
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.common.di.IDatabaseDependencyProvider
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
actual fun createDb(): PeopleInSpaceDatabase? {
actual fun createDb(dependencyProvider: IDatabaseDependencyProvider): PeopleInSpaceDatabase? {
val driver = NativeSqliteDriver(PeopleInSpaceDatabase.Schema, "peopleinspace.db")
return PeopleInSpaceDatabase(driver)
}

View file

@ -3,12 +3,10 @@ package com.surrus.common.repository
import co.touchlab.kermit.Logger
import co.touchlab.kermit.NSLogLogger
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver
import com.surrus.common.di.IDatabaseDependencyProvider
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
actual fun createDb(): PeopleInSpaceDatabase? {
actual fun createDb(dependencyProvider: IDatabaseDependencyProvider): PeopleInSpaceDatabase? {
val driver = NativeSqliteDriver(PeopleInSpaceDatabase.Schema, "peopleinspace.db")
return PeopleInSpaceDatabase(driver)
}

View file

@ -18,6 +18,7 @@ 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
@ -26,6 +27,7 @@ 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
@ -33,7 +35,11 @@ import java.net.HttpURLConnection
import java.net.URL
import javax.imageio.ImageIO
private val koin = initKoin(enableNetworkLogs = true).koin
private val koin = initKoin(enableNetworkLogs = true){
modules(module {
createDbClient()
})
}.koin
fun main() = Window {
var peopleState by remember { mutableStateOf(emptyList<Assignment>()) }

View file

@ -1,9 +1,11 @@
import co.touchlab.kermit.Kermit
import com.surrus.common.di.createDbClient
import com.surrus.common.di.initKoin
import com.surrus.common.repository.PeopleInSpaceRepository
import kotlinx.coroutines.InternalCoroutinesApi
import org.koin.core.component.KoinComponent
import org.koin.core.component.get
import org.koin.dsl.module
import react.child
import react.createContext
import react.dom.render
@ -13,7 +15,11 @@ object AppDependencies : KoinComponent {
val logger: Kermit
init {
initKoin()
initKoin{
modules(module {
createDbClient()
})
}
repository = get()
logger = get()
}