Skip to content

Commit

Permalink
feat: flavor 毎に内部データベース名を変更
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsutakein committed Mar 11, 2024
1 parent 95d9d45 commit cf08d46
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import club.nito.app.shared.NitoApp
import org.koin.core.qualifier.named
import org.koin.dsl.module

class MainActivity : ComponentActivity() {
Expand Down Expand Up @@ -41,7 +42,14 @@ class MainActivity : ComponentActivity() {
initKoin = {
modules(
appModule,
module { single<Context> { this@MainActivity.applicationContext } },
module {
single<Context>(named(name = "application")) {
this@MainActivity.applicationContext
}
single<Context>(named(name = "activity")) {
this@MainActivity
}
},
)
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import android.content.Context
import androidx.sqlite.db.SupportSQLiteDatabase
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
import club.nito.core.model.BuildConfig

internal actual class DriverFactory(private val context: Context) {
internal actual class DriverFactory(
private val context: Context,
private val buildConfig: BuildConfig,
) {
actual fun createDriver(): SqlDriver {
val schema = Database.Schema

return AndroidSqliteDriver(
schema = schema,
context = context,
name = DATABASE_NAME,
name = databaseName(flavor = buildConfig.flavor),
callback = object : AndroidSqliteDriver.Callback(schema) {
override fun onConfigure(db: SupportSQLiteDatabase) {
db.enableWriteAheadLogging()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package club.nito.core.database.di

import club.nito.core.database.DriverFactory
import org.koin.core.qualifier.named
import org.koin.core.scope.Scope

internal actual fun Scope.createDriverFactory(): DriverFactory = DriverFactory(
context = get(),
context = get(named(name = "application")),
buildConfig = get(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package club.nito.core.database
import app.cash.sqldelight.EnumColumnAdapter
import app.cash.sqldelight.db.SqlDriver
import club.nito.core.database.adapter.InstantLongColumnAdapter
import club.nito.core.model.Flavor

internal expect class DriverFactory {
fun createDriver(): SqlDriver
}

internal const val DATABASE_NAME = "nito.db"
internal fun databaseName(flavor: Flavor): String = when (flavor) {
Flavor.Dev -> "nito-dev.db"
Flavor.Prod -> "nito.db"
}

internal fun createDatabase(driverFactory: DriverFactory): Database {
val driver = driverFactory.createDriver()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package club.nito.core.database

import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.native.NativeSqliteDriver
import club.nito.core.model.BuildConfig

internal actual class DriverFactory {
internal actual class DriverFactory(
private val buildConfig: BuildConfig,
) {
actual fun createDriver(): SqlDriver = NativeSqliteDriver(
schema = Database.Schema,
name = DATABASE_NAME,
name = databaseName(flavor = buildConfig.flavor),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ package club.nito.core.database.di
import club.nito.core.database.DriverFactory
import org.koin.core.scope.Scope

internal actual fun Scope.createDriverFactory(): DriverFactory = DriverFactory()
internal actual fun Scope.createDriverFactory(): DriverFactory = DriverFactory(
buildConfig = get(),
)

0 comments on commit cf08d46

Please sign in to comment.