Skip to content

Commit

Permalink
Rewrite database with SQLDelight
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsvanvelzen committed Jan 28, 2024
1 parent 8836e2c commit 7b1d632
Show file tree
Hide file tree
Showing 30 changed files with 459 additions and 354 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ charset = utf-8
indent_style = tab
tab_width = 4

[*.sq]
charset = utf-8
indent_style = tab
tab_width = 4

[*.md]
trim_trailing_whitespace = false
14 changes: 11 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
alias(libs.plugins.android.app)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.ksp)
alias(libs.plugins.sqldelight)
}

java.toolchain.languageVersion.set(JavaLanguageVersion.of(17))
Expand Down Expand Up @@ -34,6 +34,14 @@ android {
}
}

sqldelight {
databases {
create("Database") {
packageName.set("nl.ndat.tvlauncher.data.sqldelight")
}
}
}

dependencies {
// System
implementation(libs.androidx.core)
Expand All @@ -44,8 +52,8 @@ dependencies {
implementation(libs.timber)

// Data
implementation(libs.androidx.room.ktx)
ksp(libs.androidx.room.compiler)
implementation(libs.sqldelight.android)
implementation(libs.sqldelight.coroutines)

// UI
implementation(libs.androidx.appcompat)
Expand Down
15 changes: 6 additions & 9 deletions app/src/main/kotlin/nl/ndat/tvlauncher/LauncherApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.app.Application
import coil.ImageLoader
import coil.ImageLoaderFactory
import coil.util.DebugLogger
import nl.ndat.tvlauncher.data.SharedDatabase
import nl.ndat.tvlauncher.data.DatabaseContainer
import nl.ndat.tvlauncher.data.repository.AppRepository
import nl.ndat.tvlauncher.data.repository.ChannelRepository
import nl.ndat.tvlauncher.data.repository.InputRepository
Expand All @@ -23,27 +23,24 @@ import timber.log.Timber
private val launcherModule = module {
single { DefaultLauncherHelper(get()) }

single { AppRepository(get(), get(), get(), get()) }
single { AppRepository(get(), get(), get()) }
single { AppResolver() }

single { ChannelRepository(get(), get(), get(), get(), get()) }
single { ChannelRepository(get(), get(), get()) }
single { ChannelResolver() }

single { InputRepository(get(), get(), get(), get()) }
single { InputRepository(get(), get(), get()) }
single { InputResolver() }

single { PreferenceRepository() }
}

private val databaseModule = module {
// Create database(s)
single { SharedDatabase.build(get()) }
single { DatabaseContainer(get()) }

// Add DAOs for easy access
single { get<SharedDatabase>().appDao() }
single { get<SharedDatabase>().channelDao() }
single { get<SharedDatabase>().channelProgramDao() }
single { get<SharedDatabase>().inputDao() }

}

class LauncherApplication : Application(), ImageLoaderFactory {
Expand Down
53 changes: 53 additions & 0 deletions app/src/main/kotlin/nl/ndat/tvlauncher/data/DatabaseContainer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package nl.ndat.tvlauncher.data

import android.content.Context
import app.cash.sqldelight.ColumnAdapter
import app.cash.sqldelight.EnumColumnAdapter
import app.cash.sqldelight.TransactionWithReturn
import app.cash.sqldelight.TransactionWithoutReturn
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
import nl.ndat.tvlauncher.data.sqldelight.Channel
import nl.ndat.tvlauncher.data.sqldelight.ChannelProgram
import nl.ndat.tvlauncher.data.sqldelight.Database
import nl.ndat.tvlauncher.data.sqldelight.Input

class DatabaseContainer(
context: Context
) {
companion object {
const val DB_FILE = "data.db"
}

private val driver = AndroidSqliteDriver(Database.Schema, context, DB_FILE)
private val database = Database(
driver = driver,
ChannelAdapter = Channel.Adapter(
typeAdapter = EnumColumnAdapter(),
),
ChannelProgramAdapter = ChannelProgram.Adapter(
weightAdapter = IntColumnAdapter(),
typeAdapter = EnumColumnAdapter(),
posterArtAspectRatioAdapter = EnumColumnAdapter(),
lastPlaybackPositionMillisAdapter = IntColumnAdapter(),
durationMillisAdapter = IntColumnAdapter(),
itemCountAdapter = IntColumnAdapter(),
interactionTypeAdapter = EnumColumnAdapter(),
),
InputAdapter = Input.Adapter(
typeAdapter = EnumColumnAdapter()
),
)

val apps = database.appQueries
val inputs = database.inputQueries
val channels = database.channelQueries
val channelPrograms = database.channelProgramQueries

fun transaction(body: TransactionWithoutReturn.() -> Unit) = database.transaction { body() }
fun <T> transactionForResult(body: TransactionWithReturn<T>.() -> T) = database.transactionWithResult { body() }
}

class IntColumnAdapter : ColumnAdapter<Int, Long> {
override fun decode(databaseValue: Long): Int = databaseValue.toInt()
override fun encode(value: Int): Long = value.toLong()
}
43 changes: 0 additions & 43 deletions app/src/main/kotlin/nl/ndat/tvlauncher/data/SharedDatabase.kt

This file was deleted.

32 changes: 0 additions & 32 deletions app/src/main/kotlin/nl/ndat/tvlauncher/data/dao/AppDao.kt

This file was deleted.

27 changes: 0 additions & 27 deletions app/src/main/kotlin/nl/ndat/tvlauncher/data/dao/ChannelDao.kt

This file was deleted.

This file was deleted.

29 changes: 0 additions & 29 deletions app/src/main/kotlin/nl/ndat/tvlauncher/data/dao/InputDao.kt

This file was deleted.

18 changes: 0 additions & 18 deletions app/src/main/kotlin/nl/ndat/tvlauncher/data/entity/App.kt

This file was deleted.

19 changes: 0 additions & 19 deletions app/src/main/kotlin/nl/ndat/tvlauncher/data/entity/Channel.kt

This file was deleted.

This file was deleted.

Loading

0 comments on commit 7b1d632

Please sign in to comment.