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: [database.staff] dbflow to room #2311

Merged
merged 4 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -12,8 +12,9 @@ package com.mifos.core.data.repository
import com.mifos.core.entity.client.Client
import com.mifos.core.entity.client.ClientPayload
import com.mifos.core.entity.organisation.Office
import com.mifos.core.entity.organisation.Staff
import com.mifos.core.entity.templates.clients.ClientsTemplate
import com.mifos.room.entities.organisation.Staff
import kotlinx.coroutines.flow.Flow
import okhttp3.MultipartBody
import okhttp3.ResponseBody
import rx.Observable
Expand All @@ -27,7 +28,7 @@ interface CreateNewClientRepository {

suspend fun offices(): List<Office>

suspend fun getStaffInOffice(officeId: Int): List<Staff>
fun getStaffInOffice(officeId: Int): Flow<List<Staff>>

fun createClient(clientPayload: ClientPayload): Observable<Client>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import com.mifos.core.data.repository.CreateNewClientRepository
import com.mifos.core.entity.client.Client
import com.mifos.core.entity.client.ClientPayload
import com.mifos.core.entity.organisation.Office
import com.mifos.core.entity.organisation.Staff
import com.mifos.core.entity.templates.clients.ClientsTemplate
import com.mifos.core.network.datamanager.DataManagerClient
import com.mifos.core.network.datamanager.DataManagerOffices
import com.mifos.core.network.datamanager.DataManagerStaff
import com.mifos.room.entities.organisation.Staff
import kotlinx.coroutines.flow.Flow
import okhttp3.MultipartBody
import okhttp3.ResponseBody
import rx.Observable
Expand All @@ -40,7 +41,7 @@ class CreateNewClientRepositoryImp @Inject constructor(
return dataManagerOffices.offices()
}

override suspend fun getStaffInOffice(officeId: Int): List<Staff> {
override fun getStaffInOffice(officeId: Int): Flow<List<Staff>> {
return dataManagerStaff.getStaffInOffice(officeId)
}

Expand Down
30 changes: 30 additions & 0 deletions core/database/src/main/java/com/mifos/room/dao/StaffDao.kt
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.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.mifos.room.entities.organisation.Staff
import kotlinx.coroutines.flow.Flow

/**
* Created by Pronay Sarker on 14/02/2025 (1:54 AM)
*/
@Dao
interface StaffDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertStaff(staff: Staff)

@Query("SELECT * FROM Staff WHERE officeId = :officeId")
fun readAllStaff(officeId: Int): Flow<List<Staff>>
}
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.StaffDao
import com.mifos.room.dao.SurveyDao
import com.mifos.room.entities.PaymentTypeOption
import com.mifos.room.entities.accounts.loans.ActualDisbursementDate
Expand All @@ -23,6 +24,7 @@ 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.organisation.Staff
import com.mifos.room.entities.survey.QuestionDatas
import com.mifos.room.entities.survey.ResponseDatas
import com.mifos.room.entities.survey.Survey
Expand Down Expand Up @@ -50,6 +52,7 @@ import com.mifos.room.utils.typeconverters.SurveyTypeConverters
Survey::class,
QuestionDatas::class,
ResponseDatas::class,
Staff::class,
],
version = MifosDatabase.VERSION,
exportSchema = true,
Expand All @@ -68,6 +71,7 @@ abstract class MifosDatabase : RoomDatabase() {
abstract fun columnValueDao(): ColumnValueDao
abstract fun loanDao(): LoanDao
abstract fun surveyDao(): SurveyDao
abstract fun staffDao(): StaffDao

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.StaffDao
import com.mifos.room.dao.SurveyDao
import com.mifos.room.db.MifosDatabase
import dagger.Module
Expand All @@ -35,4 +36,9 @@ object DaoModule {
fun providesSurveyDao(database: MifosDatabase): SurveyDao {
return database.surveyDao()
}

@Provides
fun providesStaffDao(database: MifosDatabase): StaffDao {
return database.staffDao()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
package com.mifos.room.entities.organisation

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

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

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

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

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

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

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

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

@ColumnInfo(name = "isActive")
var isActive: Boolean? = null,
val isActive: Boolean? = null,
) : Parcelable
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.helper

import com.mifos.room.dao.StaffDao
import com.mifos.room.entities.organisation.Staff
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

/**
* Created by Pronay Sarker on 14/02/2025 (2:17 AM)
*/
class StaffDaoHelper @Inject constructor(
private val staffDao: StaffDao,
) {
suspend fun saveAllStaffOfOffices(staffs: List<Staff>) {
for (staff in staffs) {
staffDao.insertStaff(staff)
}
}

fun readAllStaffOffices(officeId: Int): Flow<List<Staff>> {
return staffDao.readAllStaff(officeId)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
*/
package com.mifos.core.network.datamanager

import com.mifos.core.entity.organisation.Staff
import com.mifos.core.datastore.PrefManager
import com.mifos.core.network.BaseApiManager
import com.mifos.core.network.mappers.staffs.StaffMapper
import com.mifos.room.entities.organisation.Staff
import com.mifos.room.helper.StaffDaoHelper
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -23,15 +27,30 @@ class DataManagerStaff @Inject constructor(
val mBaseApiManager: BaseApiManager,
// private val mDatabaseHelperStaff: DatabaseHelperStaff,
private val baseApiManager: org.mifos.core.apimanager.BaseApiManager,
// private val prefManager: com.mifos.core.datastore.PrefManager,
private val prefManager: PrefManager,
private val staffDaoHelper: StaffDaoHelper,
) {
/**
* @param officeId
* @return
*/
suspend fun getStaffInOffice(officeId: Int): List<Staff> {
return baseApiManager.getStaffApi().retrieveAll16(officeId.toLong(), null, null, null)
.map(StaffMapper::mapFromEntity)
fun getStaffInOffice(officeId: Int): Flow<List<Staff>> {
return when (prefManager.userStatus) {
false -> flow {
baseApiManager.getStaffApi().retrieveAll16(
officeId.toLong(),
null,
null,
null,
)
.map(StaffMapper::mapFromEntity)
}

/**
* return all List of Staffs of Office from DatabaseHelperOffices
*/
true -> staffDaoHelper.readAllStaffOffices(officeId)
}
}
// fun getStaffInOffice(officeId: Int): Observable<List<Staff>> {
// return when (prefManager.userStatus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
*/
package com.mifos.core.network.mappers.staffs

import com.mifos.core.entity.organisation.Staff
import com.mifos.room.entities.organisation.Staff
import org.mifos.core.data.AbstractMapper
import org.openapitools.client.models.RetrieveOneResponse

object StaffMapper : AbstractMapper<RetrieveOneResponse, Staff>() {
override fun mapFromEntity(entity: RetrieveOneResponse): Staff {
return Staff().apply {
id = entity.id!!.toInt()
firstname = entity.firstname
lastname = entity.lastname
displayName = entity.displayName
officeId = entity.officeId!!.toInt()
officeName = entity.officeName
isLoanOfficer = entity.isLoanOfficer
isActive = entity.isActive
}
return Staff(
id = entity.id!!.toInt(),
firstname = entity.firstname,
lastname = entity.lastname,
displayName = entity.displayName,
officeId = entity.officeId!!.toInt(),
officeName = entity.officeName,
isLoanOfficer = entity.isLoanOfficer,
isActive = entity.isActive,
)
}

override fun mapToEntity(domainModel: Staff): RetrieveOneResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import androidx.lifecycle.viewModelScope
import com.mifos.core.common.utils.Resource
import com.mifos.core.data.repository.CreateNewClientRepository
import com.mifos.core.domain.useCases.GetOfficeListUseCase
import com.mifos.core.domain.useCases.GetStaffInOfficeForCreateNewClientUseCase
import com.mifos.core.entity.client.Client
import com.mifos.core.entity.client.ClientPayload
import com.mifos.core.entity.organisation.Office
import com.mifos.core.entity.organisation.Staff
import com.mifos.feature.client.R
import com.mifos.room.entities.organisation.Staff
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
Expand All @@ -45,7 +45,6 @@ import javax.inject.Inject
class CreateNewClientViewModel @Inject constructor(
private val repository: CreateNewClientRepository,
// private val clientTemplateUseCase: ClientTemplateUseCase,
private val getStaffInOffice: GetStaffInOfficeForCreateNewClientUseCase,
private val getOfficeListUseCase: GetOfficeListUseCase,
) : ViewModel() {

Expand Down Expand Up @@ -98,20 +97,17 @@ class CreateNewClientViewModel @Inject constructor(
}
}

fun loadStaffInOffices(officeId: Int) =
viewModelScope.launch(Dispatchers.IO) {
getStaffInOffice(officeId).collect { result ->
when (result) {
is Resource.Error ->
_createNewClientUiState.value =
CreateNewClientUiState.ShowError(R.string.feature_client_failed_to_fetch_staffs)

is Resource.Loading -> Unit

is Resource.Success -> _staffInOffices.value = result.data ?: emptyList()
fun loadStaffInOffices(officeId: Int) {
viewModelScope.launch {
repository.getStaffInOffice(officeId)
.catch {
_createNewClientUiState.value =
CreateNewClientUiState.ShowError(R.string.feature_client_failed_to_fetch_staffs)
}.collect { staffs ->
_staffInOffices.value = staffs
}
}
}
}

fun createClient(clientPayload: ClientPayload) {
_createNewClientUiState.value = CreateNewClientUiState.ShowProgressbar
Expand Down
Loading