Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLDelight database migration #151

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
47 changes: 42 additions & 5 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.jetbrains.kotlin.android)
id("app.cash.sqldelight")
}

android {
Expand Down Expand Up @@ -40,7 +41,7 @@ android {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
kotlinCompilerExtensionVersion = "1.5.13"
}
packaging {
resources {
Expand All @@ -56,24 +57,60 @@ android {
}

dependencies {

// Versions
val sqlDelightVersion = "2.0.2"
val koinVersion = "4.0.0-RC1"

// AndroidX Core Libraries
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.navigation.common.ktx)
implementation(libs.androidx.navigation.runtime.ktx)

// AndroidX Compose Libraries
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.window)
implementation(libs.androidx.appcompat)
implementation(libs.androidx.navigation.runtime.ktx)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.navigation.common.ktx)

//SQLDelight
implementation("app.cash.sqldelight:android-driver:$sqlDelightVersion")
implementation("app.cash.sqldelight:coroutines-extensions-jvm:$sqlDelightVersion")
implementation("app.cash.sqldelight:primitive-adapters:$sqlDelightVersion")

// GSON
implementation("com.google.code.gson:gson:2.11.0")


// Koin for Android
implementation(platform("io.insert-koin:koin-bom:$koinVersion"))
implementation("io.insert-koin:koin-core")
implementation (libs.koin.android)
implementation("io.insert-koin:koin-androidx-compose:$koinVersion")

// Testing
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)

debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)

runtimeOnly("org.jetbrains.kotlin:kotlin-reflect:2.0.21-RC")
}

sqldelight {
databases {
create("MensinatorDB") {
packageName.set("com.mensinator.app.database")
}
}
}
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Mensinator"
tools:targetApi="31">
tools:targetApi="31"
android:name=".MensinatorApplication"
>
<activity
android:name=".MainActivity"
android:exported="true"
Expand Down
52 changes: 52 additions & 0 deletions app/src/main/java/com/mensinator/app/AppModules.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.mensinator.app
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
import com.mensinator.app.database.MensinatorDB
import com.mensinator.app.database.repositories.AppSettingRepository
import com.mensinator.app.database.repositories.CycleRepository
import com.mensinator.app.database.repositories.NoteRepository
import com.mensinator.app.database.repositories.OvulationRepository
import com.mensinator.app.database.repositories.PeriodRepository
import com.mensinator.app.database.repositories.PredictionRepository
import com.mensinator.app.database.viewmodels.AppSettingViewModel
import com.mensinator.app.database.viewmodels.CycleViewModel
import com.mensinator.app.database.viewmodels.NoteViewModel
import com.mensinator.app.database.viewmodels.OvulationViewModel
import com.mensinator.app.database.viewmodels.PeriodViewModel
import com.mensinator.app.database.viewmodels.PredictionViewModel
import org.koin.core.module.dsl.viewModelOf
import org.koin.dsl.module

val databaseModule = module {
single<SqlDriver> {
AndroidSqliteDriver(
schema = MensinatorDB.Schema,
context = get(),
name = "mensinator.db"
)
}

single {
MensinatorDB(get())
}
}

val dataModule = module {
single { AppSettingRepository(get()) }
viewModelOf(::AppSettingViewModel)

single { CycleRepository(get()) }
viewModelOf(::CycleViewModel)

single { NoteRepository(get()) }
viewModelOf(::NoteViewModel)

single { OvulationRepository(get()) }
viewModelOf(::OvulationViewModel)

single { PeriodRepository(get()) }
viewModelOf(::PeriodViewModel)

single { PredictionRepository(get()) }
viewModelOf(::PredictionViewModel)
}
14 changes: 10 additions & 4 deletions app/src/main/java/com/mensinator/app/ExportImport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import android.database.sqlite.SQLiteDatabase
import android.icu.text.SimpleDateFormat
import android.os.Environment
import android.util.Log
import org.json.JSONObject
import com.mensinator.app.database.ImportExportManager
import org.json.JSONArray
import org.json.JSONObject
import java.io.BufferedReader
import java.io.File
import java.io.FileOutputStream
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.InputStreamReader
import java.io.BufferedReader
import java.util.Date
import java.util.Locale



class ExportImport {

fun getDocumentsExportFilePath(): String {
Expand Down Expand Up @@ -87,6 +87,9 @@ class ExportImport {
fileOutputStream.close()

db.close()

// Export function - to implement it better
ImportExportManager().exportDatabase("/storage/self/primary/Documents/")
}

private fun cursorToJsonArray(cursor: Cursor): JSONArray {
Expand All @@ -102,6 +105,8 @@ class ExportImport {
}

fun importDatabase(context: Context, filePath: String) {
ImportExportManager().importDatabase(filePath)

val dbHelper = PeriodDatabaseHelper(context)
val db = dbHelper.writableDatabase

Expand Down Expand Up @@ -157,6 +162,7 @@ class ExportImport {
}

db.close()

}

// This function will delete all period, ovulation, symptoms and symptomdates before importing the file
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/mensinator/app/MensinatorApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mensinator.app
import android.app.Application
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin

class MensinatorApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@MensinatorApplication)
androidLogger()
modules(
databaseModule,
dataModule
)
}
}
}
33 changes: 33 additions & 0 deletions app/src/main/java/com/mensinator/app/database/DatabaseEnums.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mensinator.app.database

enum class AppCategory(val value: String) {
COLORS("Colors"),
REMINDERS("Reminders"),
OTHERS("Other settings")
}

enum class NoteCategory(val value: String){
SYMPTOMS("Symptoms"),
MOODS("Moods"),
SEX("Sex")
}

enum class PredictionType(val value: String){
PERIOD("Period"),
OVULATION("Ovulation")
}

enum class Colors(val value: String) {
RED("Red"),
LIGHT_GRAY("LightGray"),
DARK_GRAY("DarkGray"),
YELLOW("Yellow"),
BLUE("Blue"),
MAGENTA("Magenta")
}

enum class PredefinedSymptoms(val value: String){
HEAVY_FLOW("Heavy flow"),
MEDIUM_FLOW("Medium flow"),
LIGHT_FLOW("Light flow"),
}
Loading