Skip to content

Commit

Permalink
Merge pull request #125 from VictorKabata/room-db-migration
Browse files Browse the repository at this point in the history
Room DB migration
  • Loading branch information
VictorKabata committed Sep 19, 2024
2 parents 510ce8c + 4be3333 commit abd96db
Show file tree
Hide file tree
Showing 134 changed files with 911 additions and 573 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ appiOS/Pods/
appiOS/Podfile.lock
*.podspec
*.xcuserstate

notflix.preferences_pb
4 changes: 2 additions & 2 deletions appiOS/Notflix.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
isa = PBXNativeTarget;
buildConfigurationList = 1CE0FB2C2A6A8B5100B26932 /* Build configuration list for PBXNativeTarget "Notflix" */;
buildPhases = (
1C13F5F92C773DB900B1DA2E /* ShellScript */,
1CD2A8F32C9B405100B07805 /* ShellScript */,
1CE0FB042A6A8B4F00B26932 /* Sources */,
1CE0FB052A6A8B4F00B26932 /* Frameworks */,
1CE0FB062A6A8B4F00B26932 /* Resources */,
Expand Down Expand Up @@ -268,7 +268,7 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
1C13F5F92C773DB900B1DA2E /* ShellScript */ = {
1CD2A8F32C9B405100B07805 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
Expand Down
40 changes: 22 additions & 18 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ plugins {
alias(libs.plugins.kotlinX.serialization.plugin)
alias(libs.plugins.buildKonfig)
alias(libs.plugins.compose)

alias(libs.plugins.ksp)
alias(libs.plugins.room)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.sqlDelight)

alias(libs.plugins.android.application)
alias(libs.plugins.googleServices.plugin)
Expand All @@ -17,6 +19,10 @@ plugins {
alias(libs.plugins.firebase.performance.plugin)
}

kotlin {
jvmToolchain(17)
}

@OptIn(org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi::class)
kotlin {
kotlin.applyDefaultHierarchyTemplate()
Expand All @@ -30,7 +36,7 @@ kotlin {
).forEach {
it.binaries.framework {
baseName = "shared"
isStatic = false
isStatic = true
}
}

Expand All @@ -48,25 +54,24 @@ kotlin {

implementation(libs.bundles.ktor)

api(libs.koin.core)
implementation(libs.koin.core)
implementation(libs.koin.compose)
implementation(libs.koin.composeViewModel)

implementation(libs.kotlinX.serializationJson)

implementation(libs.kotlinX.dateTime)

api(libs.napier)
implementation(libs.napier)

implementation(libs.bundles.coil)

implementation(libs.navigation)

implementation(libs.sqlDelight.coroutine)

implementation(libs.datastore.preferences)

// implementation(libs.material.windowSizeClass)
implementation(libs.room.runtime)
implementation(libs.sqlite.bundled)
}

commonTest.dependencies {
Expand All @@ -78,25 +83,21 @@ kotlin {

androidMain.dependencies {
implementation(libs.ktor.android)
implementation(libs.sqlDelight.android)
implementation(libs.koin.android)
implementation(libs.koin.androidx.compose)
implementation(libs.androidX.activity)

// Firebase
implementation(libs.firebase.analytics)
implementation(libs.firebase.crashlytics)
implementation(libs.firebase.performance)
}

iosMain.dependencies {
implementation(libs.ktor.darwin)
implementation(libs.sqlDelight.native)
}

sourceSets["desktopMain"].dependencies {
implementation(libs.ktor.java)
implementation(libs.sqlDelight.jvm)
implementation(libs.coroutines.swing)
implementation(compose.desktop.currentOs)
}
Expand Down Expand Up @@ -184,13 +185,16 @@ buildkonfig {
}
}

sqldelight {
databases {
create("AppDatabase") {
packageName.set("com.vickbt.shared.data.cache.sqldelight")
srcDirs.setFrom("src/commonMain/kotlin")
}
}
room {
schemaDirectory("$projectDir/schemas")
}

dependencies {
add("kspAndroid", libs.room.compiler)
add("kspIosX64", libs.room.compiler)
add("kspIosArm64", libs.room.compiler)
add("kspIosSimulatorArm64", libs.room.compiler)
add("kspDesktop", libs.room.compiler)
}

compose.resources {
Expand Down
112 changes: 112 additions & 0 deletions composeApp/schemas/com.vickbt.composeApp.data.cache.AppDatabase/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "b735d958b317149fd8c12c6ffeccf7a6",
"entities": [
{
"tableName": "Favorite Movies",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `imdbId` TEXT, `backdropPath` TEXT NOT NULL, `posterPath` TEXT NOT NULL, `originalLanguage` TEXT, `originalTitle` TEXT NOT NULL, `overview` TEXT NOT NULL, `popularity` REAL, `releaseDate` TEXT, `runtime` INTEGER, `status` TEXT, `tagLine` TEXT, `title` TEXT NOT NULL, `voteAverage` REAL, `voteCount` INTEGER, `createdAt` TEXT NOT NULL, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "imdbId",
"columnName": "imdbId",
"affinity": "TEXT"
},
{
"fieldPath": "backdropPath",
"columnName": "backdropPath",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "posterPath",
"columnName": "posterPath",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "originalLanguage",
"columnName": "originalLanguage",
"affinity": "TEXT"
},
{
"fieldPath": "originalTitle",
"columnName": "originalTitle",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "overview",
"columnName": "overview",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "popularity",
"columnName": "popularity",
"affinity": "REAL"
},
{
"fieldPath": "releaseDate",
"columnName": "releaseDate",
"affinity": "TEXT"
},
{
"fieldPath": "runtime",
"columnName": "runtime",
"affinity": "INTEGER"
},
{
"fieldPath": "status",
"columnName": "status",
"affinity": "TEXT"
},
{
"fieldPath": "tagLine",
"columnName": "tagLine",
"affinity": "TEXT"
},
{
"fieldPath": "title",
"columnName": "title",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "voteAverage",
"columnName": "voteAverage",
"affinity": "REAL"
},
{
"fieldPath": "voteCount",
"columnName": "voteCount",
"affinity": "INTEGER"
},
{
"fieldPath": "createdAt",
"columnName": "createdAt",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"id"
]
}
}
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'b735d958b317149fd8c12c6ffeccf7a6')"
]
}
}
4 changes: 2 additions & 2 deletions composeApp/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
android:name="com.vickbt.shared.NotflixApplication"
android:name="com.vickbt.composeApp.NotflixApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand All @@ -14,7 +14,7 @@
android:theme="@android:style/Theme.Material.Light.NoActionBar">

<activity
android:name="com.vickbt.shared.ui.activity.MainActivity"
android:name="com.vickbt.composeApp.ui.activity.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.vickbt.shared
package com.vickbt.composeApp

import android.app.Application
import com.vickbt.shared.di.initKoin
import com.vickbt.shared.utils.ContextUtils
import com.vickbt.composeApp.di.initKoin
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.logger.Level
Expand All @@ -12,8 +11,6 @@ class NotflixApplication : Application() {
override fun onCreate() {
super.onCreate()

ContextUtils.setContext(context = this)

initKoin {
androidLogger(level = Level.NONE)
androidContext(androidContext = this@NotflixApplication)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.vickbt.composeApp.di

import com.vickbt.composeApp.utils.DatabaseFactory
import com.vickbt.composeApp.utils.DatastoreFactory
import org.koin.core.module.Module
import org.koin.dsl.module

actual fun platformModule(): Module = module {
single { DatastoreFactory(context = get()).createDatastore() }

single { DatabaseFactory(context = get()).createDatabase() }
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.vickbt.shared.ui.activity
package com.vickbt.composeApp.ui.activity

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.ExperimentalAnimationApi
import com.vickbt.shared.ui.screens.main.MainScreen
import com.vickbt.composeApp.ui.screens.main.MainScreen

@ExperimentalAnimationApi
class MainActivity : ComponentActivity() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.vickbt.shared.utils
package com.vickbt.composeApp.utils

import android.content.Context

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.vickbt.composeApp.utils

import android.content.Context
import androidx.room.Room
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
import com.vickbt.composeApp.data.cache.AppDatabase
import kotlinx.coroutines.Dispatchers

actual class DatabaseFactory(private val context: Context) {
actual fun createDatabase(): AppDatabase {
val appContext = context.applicationContext

val dbFile = appContext.getDatabasePath("notflix.db")
return Room.databaseBuilder<AppDatabase>(
context = appContext,
name = dbFile.absolutePath
).setQueryCoroutineContext(Dispatchers.IO).fallbackToDestructiveMigration(true)
.setDriver(BundledSQLiteDriver()).build()
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.vickbt.shared.utils
package com.vickbt.composeApp.utils

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import com.vickbt.shared.domain.utils.Constants
import com.vickbt.composeApp.domain.utils.Constants

actual class DatastoreFactory(private val context: Context) {
actual fun createDatastore(): DataStore<Preferences> {
return initDataStore {
context.filesDir.resolve(Constants.dataStoreFileName).absolutePath
context.filesDir.resolve(Constants.DATASTORE_FILE_NAME).absolutePath
}
}
}
11 changes: 0 additions & 11 deletions composeApp/src/androidMain/kotlin/com/vickbt/shared/di/Module.kt

This file was deleted.

This file was deleted.

Loading

0 comments on commit abd96db

Please sign in to comment.