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

Setup: initial setup for room db #2278

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/database/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plugins {
alias(libs.plugins.mifos.android.library)
alias(libs.plugins.mifos.android.hilt)
alias(libs.plugins.mifos.android.library.jacoco)
alias(libs.plugins.ksp)
id(libs.plugins.kotlin.parcelize.get().pluginId)
}

Expand Down Expand Up @@ -38,6 +39,12 @@ dependencies {
implementation(libs.dbflow)
kapt(libs.github.dbflow.processor)

//room
implementation(libs.androidx.room.runtime)
implementation(libs.androidx.room.ktx)
ksp(libs.androidx.room.compiler)
kspTest(libs.androidx.room.compiler)

// Hilt dependency
implementation(libs.hilt.android)
kapt(libs.hilt.compiler)
Expand Down
33 changes: 33 additions & 0 deletions core/database/src/main/java/com/mifos/room/dao/ColumnValueDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2025 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.room.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.mifos.room.entities.noncore.ColumnValue

@Dao
interface ColumnValueDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(columnValue: ColumnValue)

@Update
suspend fun update(columnValue: ColumnValue)

@Delete
suspend fun delete(columnValue: ColumnValue)

@Query("SELECT * FROM ColumnValue WHERE id = :id")
suspend fun getColumnValue(id: Int): ColumnValue?
}
59 changes: 59 additions & 0 deletions core/database/src/main/java/com/mifos/room/db/MifosDatabase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2025 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.room.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.mifos.room.dao.ColumnValueDao
import com.mifos.room.entities.noncore.ColumnValue
import com.mifos.room.utils.typeconverters.StringListConverter

@Database(
// [TODO -> add other entities ]
entities = [ColumnValue::class],
version = 1,
exportSchema = false,
)
@TypeConverters(StringListConverter::class)
// ( TODO -> add type converters here )

abstract class MifosDatabase : RoomDatabase() {
abstract fun columnValueDao(): ColumnValueDao

companion object {

private const val NAME: String = "Mifos"

@Volatile
private var instance: MifosDatabase? = null

fun getDatabase(context: Context): MifosDatabase {
return instance ?: synchronized(this) {
val currentInstance = Room.databaseBuilder(
context.applicationContext,
MifosDatabase::class.java,
NAME,
)
.addMigrations(
MIGRATION_1_2,
MIGRATION_2_3,
MIGRATION_3_4,
)
.build()
instance = currentInstance

currentInstance
}
}
}
}
36 changes: 36 additions & 0 deletions core/database/src/main/java/com/mifos/room/db/MigrationVersion.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2025 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.room.db

import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE LoanAccount ADD COLUMN centerId INTEGER")
}
}

val MIGRATION_2_3 = object : Migration(2, 3) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE SavingsAccount ADD COLUMN centerId INTEGER")
}
}

val MIGRATION_3_4 = object : Migration(3, 4) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE Center ADD COLUMN sync INTEGER")
db.execSQL("ALTER TABLE Center ADD COLUMN centerDate_centerId INTEGER")
db.execSQL("ALTER TABLE Center ADD COLUMN centerDate_chargeId INTEGER")
db.execSQL("ALTER TABLE Center ADD COLUMN centerDate_day INTEGER")
db.execSQL("ALTER TABLE Center ADD COLUMN centerDate_month INTEGER")
db.execSQL("ALTER TABLE Center ADD COLUMN centerDate_year INTEGER")
}
}
39 changes: 39 additions & 0 deletions core/database/src/main/java/com/mifos/room/di/DbModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2025 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.room.di

import android.content.Context
import com.mifos.room.dao.ColumnValueDao
import com.mifos.room.db.MifosDatabase
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

object DbModule {
@Module
@InstallIn(SingletonComponent::class)
object DatabaseModule {

@Provides
@Singleton
fun providesDatabase(@ApplicationContext context: Context): MifosDatabase {
return MifosDatabase.getDatabase(context)
}

@Provides
@Singleton
fun providesColumnValueDao(database: MifosDatabase): ColumnValueDao {
return database.columnValueDao()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2025 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.room.entities.noncore

import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.parcelize.Parcelize

@Parcelize
@Entity(tableName = "ColumnValue")
data class ColumnValue(
@PrimaryKey
@ColumnInfo(name = "id")
var id: Int? = null,

@ColumnInfo(name = "value")
var value: String? = null,

@ColumnInfo(name = "score")
var score: Int? = null,

@ColumnInfo(name = "registeredTableName")
var registeredTableName: String? = null,
) : Parcelable
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2025 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.room.utils.typeconverters

import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class StringListConverter {
@TypeConverter
fun fromList(list: List<String>?): String {
val gson = Gson()
val type = object : TypeToken<List<String>>() {}.type
return gson.toJson(list, type)
}

@TypeConverter
fun toList(listString: String): List<String>? {
val gson = Gson()
val type = object : TypeToken<List<String>>() {}.type
return gson.fromJson(listString, type)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2025 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.room.utils.typeconverters

import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class TypeConverters {

@TypeConverter
fun fromList(list: List<Int>?): String {
val gson = Gson()
val type = object : TypeToken<List<Int>>() {}.type
return gson.toJson(list, type)
}

@TypeConverter
fun fromIntList(value: String): ArrayList<Int?> {
val listType = object : TypeToken<ArrayList<Int?>>() {}.type
return Gson().fromJson(value, listType)
}

@TypeConverter
fun toIntList(list: ArrayList<Int?>): String {
return Gson().toJson(list)
}
}
10 changes: 9 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ androidDesugarJdkLibs = "2.0.4"
androidIconifyMaterial = "2.2.2"
androidJob = "1.2.6"
androidMapsUtils = "0.4.2"
androidGradlePlugin = "8.5.0"
androidGradlePlugin = "8.7.3"
androidTools = "31.6.0"
androidxActivity = "1.8.2"
androidxAppCompat = "1.6.1"
Expand Down Expand Up @@ -109,6 +109,10 @@ playService = "18.3.0"
realmVersion = "1.13.0"
recyclerview = "1.3.2"
room = "2.6.1"
roomCompilerVersion = "2.6.1"
roomKtxVersion = "2.6.1"
roomRuntimeVersion = "2.6.1"
roomTestingVersion = "2.6.1"
retrofit = "2.9.0"
rxandroidVersion = "1.1.0"
rxjava = "1.3.8"
Expand Down Expand Up @@ -137,6 +141,10 @@ accompanist-drawablepainter = { module = "com.google.accompanist:accompanist-dra
accompanist-swiperefresh = { module = "com.google.accompanist:accompanist-swiperefresh", version.ref = "accompanistSwiperefresh" }
accompanist-permission = { module = "com.google.accompanist:accompanist-permissions", version.ref = "accompanistPermission" }
android-desugarJdkLibs = { group = "com.android.tools", name = "desugar_jdk_libs", version.ref = "androidDesugarJdkLibs" }
androidx-room-compiler = { module = "androidx.room:room-compiler", version.ref = "roomCompilerVersion" }
androidx-room-ktx = { module = "androidx.room:room-ktx", version.ref = "roomKtxVersion" }
androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "roomRuntimeVersion" }
androidx-room-testing = { module = "androidx.room:room-testing", version.ref = "roomTestingVersion" }

# Androidx Activity Compose
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "androidxActivity" }
Expand Down
2 changes: 1 addition & 1 deletion mifosng-android/dependencies/demoDebugCompileClasspath.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ androidx.core:core-splashscreen:1.0.1
androidx.core:core:1.13.0
androidx.cursoradapter:cursoradapter:1.0.0
androidx.customview:customview:1.1.0
androidx.databinding:viewbinding:8.5.0
androidx.databinding:viewbinding:8.7.3
androidx.documentfile:documentfile:1.0.0
androidx.drawerlayout:drawerlayout:1.1.1
androidx.dynamicanimation:dynamicanimation:1.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ androidx.core:core-splashscreen:1.0.1
androidx.core:core:1.13.0
androidx.cursoradapter:cursoradapter:1.0.0
androidx.customview:customview:1.1.0
androidx.databinding:viewbinding:8.5.0
androidx.databinding:viewbinding:8.7.3
androidx.documentfile:documentfile:1.0.0
androidx.drawerlayout:drawerlayout:1.1.1
androidx.dynamicanimation:dynamicanimation:1.0.0
Expand Down
2 changes: 1 addition & 1 deletion mifosng-android/dependencies/prodDebugCompileClasspath.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ androidx.core:core-splashscreen:1.0.1
androidx.core:core:1.13.0
androidx.cursoradapter:cursoradapter:1.0.0
androidx.customview:customview:1.1.0
androidx.databinding:viewbinding:8.5.0
androidx.databinding:viewbinding:8.7.3
androidx.documentfile:documentfile:1.0.0
androidx.drawerlayout:drawerlayout:1.1.1
androidx.dynamicanimation:dynamicanimation:1.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ androidx.core:core-splashscreen:1.0.1
androidx.core:core:1.13.0
androidx.cursoradapter:cursoradapter:1.0.0
androidx.customview:customview:1.1.0
androidx.databinding:viewbinding:8.5.0
androidx.databinding:viewbinding:8.7.3
androidx.documentfile:documentfile:1.0.0
androidx.drawerlayout:drawerlayout:1.1.1
androidx.dynamicanimation:dynamicanimation:1.0.0
Expand Down
Loading