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

refactor: DBFlow Surveys migration to Room #2310

Merged
merged 3 commits into from
Feb 13, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
*/
package com.mifos.core.data.repository

import com.mifos.core.entity.survey.QuestionDatas
import com.mifos.core.entity.survey.ResponseDatas
import com.mifos.core.entity.survey.Survey
import rx.Observable
import com.mifos.room.entities.survey.QuestionDatas
import com.mifos.room.entities.survey.ResponseDatas
import com.mifos.room.entities.survey.Survey
import kotlinx.coroutines.flow.Flow

/**
* Created by Aditya Gupta on 08/08/23.
*/
interface SurveyListRepository {

fun allSurvey(): Observable<List<Survey>>
fun allSurvey(): Flow<List<Survey>>

fun databaseSurveys(): Observable<List<Survey>>
fun databaseSurveys(): Flow<List<Survey>>

fun getDatabaseQuestionData(surveyId: Int): Observable<List<QuestionDatas>>
fun getDatabaseQuestionData(surveyId: Int): Flow<List<QuestionDatas>>

fun getDatabaseResponseDatas(questionId: Int): Observable<List<ResponseDatas>>
fun getDatabaseResponseDatas(questionId: Int): Flow<List<ResponseDatas>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
package com.mifos.core.data.repositoryImp

import com.mifos.core.data.repository.SurveyListRepository
import com.mifos.core.entity.survey.QuestionDatas
import com.mifos.core.entity.survey.ResponseDatas
import com.mifos.core.entity.survey.Survey
import com.mifos.core.network.datamanager.DataManagerSurveys
import rx.Observable
import com.mifos.room.entities.survey.QuestionDatas
import com.mifos.room.entities.survey.ResponseDatas
import com.mifos.room.entities.survey.Survey
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

/**
Expand All @@ -23,19 +23,19 @@ import javax.inject.Inject
class SurveyListRepositoryImp @Inject constructor(private val dataManagerSurveys: DataManagerSurveys) :
SurveyListRepository {

override fun allSurvey(): Observable<List<Survey>> {
override fun allSurvey(): Flow<List<Survey>> {
return dataManagerSurveys.allSurvey
}

override fun databaseSurveys(): Observable<List<Survey>> {
override fun databaseSurveys(): Flow<List<Survey>> {
return dataManagerSurveys.databaseSurveys
}

override fun getDatabaseQuestionData(surveyId: Int): Observable<List<QuestionDatas>> {
override fun getDatabaseQuestionData(surveyId: Int): Flow<List<QuestionDatas>> {
return dataManagerSurveys.getDatabaseQuestionData(surveyId)
}

override fun getDatabaseResponseDatas(questionId: Int): Observable<List<ResponseDatas>> {
override fun getDatabaseResponseDatas(questionId: Int): Flow<List<ResponseDatas>> {
return dataManagerSurveys.getDatabaseResponseDatas(questionId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package com.mifos.core.entity.survey
import android.os.Parcelable
import com.mifos.core.database.MifosDatabase
import com.mifos.core.model.MifosBaseModel
import com.mifos.room.entities.survey.ResponseDatas
import com.raizlabs.android.dbflow.annotation.Column
import com.raizlabs.android.dbflow.annotation.PrimaryKey
import com.raizlabs.android.dbflow.annotation.Table
Expand Down
43 changes: 43 additions & 0 deletions core/database/src/main/java/com/mifos/room/dao/SurveyDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.Insert
import androidx.room.Query
import com.mifos.room.entities.survey.QuestionDatas
import com.mifos.room.entities.survey.ResponseDatas
import com.mifos.room.entities.survey.Survey
import kotlinx.coroutines.flow.Flow

/**
* Created by Pronay Sarker on 12/02/2025 (9:33 PM)
*/
@Dao
interface SurveyDao {

@Insert
suspend fun insertSurvey(survey: Survey)

@Insert
suspend fun insertQuestionData(questionData: QuestionDatas)

@Insert
suspend fun insertResponseData(responseDatas: ResponseDatas)

@Query("SELECT * FROM Survey")
fun getAllSurveys(): Flow<List<Survey>>

@Query("SELECT * FROM QuestionDatas WHERE surveyId = :surveyId ORDER BY sequenceNo ASC")
fun getQuestionDatas(surveyId: Int): Flow<List<QuestionDatas>>

@Query("SELECT * FROM ResponseDatas WHERE questionId = :questionId ORDER BY sequenceNo ASC")
fun getResponseDatas(questionId: Int): Flow<List<ResponseDatas>>
}
11 changes: 11 additions & 0 deletions core/database/src/main/java/com/mifos/room/db/MifosDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.mifos.room.dao.ColumnValueDao
import com.mifos.room.dao.LoanDao
import com.mifos.room.dao.SurveyDao
import com.mifos.room.entities.PaymentTypeOption
import com.mifos.room.entities.accounts.loans.ActualDisbursementDate
import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest
Expand All @@ -22,11 +23,15 @@ import com.mifos.room.entities.accounts.loans.Status
import com.mifos.room.entities.accounts.loans.Summary
import com.mifos.room.entities.accounts.loans.Timeline
import com.mifos.room.entities.noncore.ColumnValue
import com.mifos.room.entities.survey.QuestionDatas
import com.mifos.room.entities.survey.ResponseDatas
import com.mifos.room.entities.survey.Survey
import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate
import com.mifos.room.utils.typeconverters.DueDateConverter
import com.mifos.room.utils.typeconverters.ListTypeConverters
import com.mifos.room.utils.typeconverters.LoanTypeConverters
import com.mifos.room.utils.typeconverters.ServerTypesConverters
import com.mifos.room.utils.typeconverters.SurveyTypeConverters

@Database(
// [TODO -> add other entities ]
Expand All @@ -41,6 +46,10 @@ import com.mifos.room.utils.typeconverters.ServerTypesConverters
Timeline::class,
Status::class,
Summary::class,
// survey
Survey::class,
QuestionDatas::class,
ResponseDatas::class,
],
version = MifosDatabase.VERSION,
exportSchema = true,
Expand All @@ -51,12 +60,14 @@ import com.mifos.room.utils.typeconverters.ServerTypesConverters
ServerTypesConverters::class,
DueDateConverter::class,
LoanTypeConverters::class,
SurveyTypeConverters::class,
)
// ( TODO -> add type converters here )

abstract class MifosDatabase : RoomDatabase() {
abstract fun columnValueDao(): ColumnValueDao
abstract fun loanDao(): LoanDao
abstract fun surveyDao(): SurveyDao

companion object {
const val VERSION = 1
Expand Down
6 changes: 6 additions & 0 deletions core/database/src/main/java/com/mifos/room/di/DaoModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package com.mifos.room.di

import com.mifos.room.dao.ColumnValueDao
import com.mifos.room.dao.LoanDao
import com.mifos.room.dao.SurveyDao
import com.mifos.room.db.MifosDatabase
import dagger.Module
import dagger.Provides
Expand All @@ -29,4 +30,9 @@ object DaoModule {
fun providesLoanDao(database: MifosDatabase): LoanDao {
return database.loanDao()
}

@Provides
fun providesSurveyDao(database: MifosDatabase): SurveyDao {
return database.surveyDao()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
package com.mifos.room.entities.survey

import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.parcelize.Parcelize
Expand All @@ -19,18 +18,13 @@ import kotlinx.parcelize.Parcelize
@Entity(tableName = "ComponentDatas")
data class ComponentDatas(
@PrimaryKey
@ColumnInfo(name = "id")
var id: Int? = null,

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

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

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

@ColumnInfo(name = "sequenceNo")
var sequenceNo: Int = 0,
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,30 @@
package com.mifos.room.entities.survey

import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.mifos.core.entity.survey.ResponseDatas
import kotlinx.parcelize.Parcelize

@Parcelize
@Entity(tableName = "QuestionDatas")
data class QuestionDatas(
@PrimaryKey
@ColumnInfo(name = "id")
var id: Int = 0,

@ColumnInfo(name = "surveyId")
@Transient
var surveyId: Int = 0,
val surveyId: Int = 0,

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

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

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

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

@ColumnInfo(name = "sequenceNo")
var sequenceNo: Int = 0,
val sequenceNo: Int = 0,

@ColumnInfo(name = "responseDatas")
var responseDatas: List<ResponseDatas> = ArrayList(),
val responseDatas: List<ResponseDatas> = emptyList(),
) : Parcelable {

var questionId: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
package com.mifos.room.entities.survey

import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.parcelize.Parcelize
Expand All @@ -19,19 +18,14 @@ import kotlinx.parcelize.Parcelize
@Entity(tableName = "ResponseDatas")
data class ResponseDatas(
@PrimaryKey
@ColumnInfo(name = "responseId")
var responseId: Int = 0,
val responseId: Int = 0,

@ColumnInfo(name = "questionId")
@Transient
var questionId: Int = 0,
val questionId: Int = 0,

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

@ColumnInfo(name = "sequenceNo")
var sequenceNo: Int = 0,
val sequenceNo: Int = 0,

@ColumnInfo(name = "value")
var value: Int = 0,
val value: Int = 0,
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
package com.mifos.room.entities.survey

import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.parcelize.Parcelize
Expand All @@ -19,28 +18,20 @@ import kotlinx.parcelize.Parcelize
@Entity(tableName = "Survey")
data class Survey(
@PrimaryKey
@ColumnInfo(name = "id")
var id: Int = 0,
val id: Int = 0,

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

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

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

@ColumnInfo(name = "isSync")
@Transient
var isSync: Boolean = false,
val isSync: Boolean = false,

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

@ColumnInfo(name = "questionDatas")
var questionDatas: List<QuestionDatas> = ArrayList(),
val questionDatas: List<QuestionDatas> = emptyList(),

@ColumnInfo(name = "componentDatas")
var componentDatas: List<ComponentDatas> = ArrayList(),
val componentDatas: List<ComponentDatas> = emptyList(),
) : Parcelable
Loading
Loading