diff --git a/core/data/src/main/java/com/mifos/core/data/repository/LoanAccountSummaryRepository.kt b/core/data/src/main/java/com/mifos/core/data/repository/LoanAccountSummaryRepository.kt index 7b54d7d3a6..725ef4b48c 100644 --- a/core/data/src/main/java/com/mifos/core/data/repository/LoanAccountSummaryRepository.kt +++ b/core/data/src/main/java/com/mifos/core/data/repository/LoanAccountSummaryRepository.kt @@ -9,13 +9,13 @@ */ package com.mifos.core.data.repository -import com.mifos.core.entity.accounts.loan.LoanWithAssociations -import rx.Observable +import com.mifos.room.entities.accounts.loans.LoanWithAssociations +import kotlinx.coroutines.flow.Flow /** * Created by Aditya Gupta on 08/08/23. */ interface LoanAccountSummaryRepository { - fun getLoanById(loanId: Int): Observable + fun getLoanById(loanId: Int): Flow } diff --git a/core/data/src/main/java/com/mifos/core/data/repository/LoanRepaymentRepository.kt b/core/data/src/main/java/com/mifos/core/data/repository/LoanRepaymentRepository.kt index f52099b892..89e271e70d 100644 --- a/core/data/src/main/java/com/mifos/core/data/repository/LoanRepaymentRepository.kt +++ b/core/data/src/main/java/com/mifos/core/data/repository/LoanRepaymentRepository.kt @@ -9,22 +9,22 @@ */ package com.mifos.core.data.repository -import com.mifos.core.entity.accounts.loan.LoanRepaymentRequest -import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate -import com.mifos.core.objects.account.loan.LoanRepaymentResponse -import rx.Observable +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest +import com.mifos.room.entities.accounts.loans.LoanRepaymentResponse +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate +import kotlinx.coroutines.flow.Flow /** * Created by Aditya Gupta on 10/08/23. */ interface LoanRepaymentRepository { - fun getLoanRepayTemplate(loanId: Int): Observable + fun getLoanRepayTemplate(loanId: Int): Flow - fun submitPayment( + suspend fun submitPayment( loanId: Int, request: LoanRepaymentRequest, - ): Observable + ): LoanRepaymentResponse - fun getDatabaseLoanRepaymentByLoanId(loanId: Int): Observable + fun getDatabaseLoanRepaymentByLoanId(loanId: Int): Flow } diff --git a/core/data/src/main/java/com/mifos/core/data/repository/LoanRepaymentScheduleRepository.kt b/core/data/src/main/java/com/mifos/core/data/repository/LoanRepaymentScheduleRepository.kt index 7ec2704372..6a2a5e079a 100644 --- a/core/data/src/main/java/com/mifos/core/data/repository/LoanRepaymentScheduleRepository.kt +++ b/core/data/src/main/java/com/mifos/core/data/repository/LoanRepaymentScheduleRepository.kt @@ -9,7 +9,7 @@ */ package com.mifos.core.data.repository -import com.mifos.core.entity.accounts.loan.LoanWithAssociations +import com.mifos.room.entities.accounts.loans.LoanWithAssociations import rx.Observable /** diff --git a/core/data/src/main/java/com/mifos/core/data/repository/LoanTransactionsRepository.kt b/core/data/src/main/java/com/mifos/core/data/repository/LoanTransactionsRepository.kt index 2b3dcee3b0..ef8b4ee2a4 100644 --- a/core/data/src/main/java/com/mifos/core/data/repository/LoanTransactionsRepository.kt +++ b/core/data/src/main/java/com/mifos/core/data/repository/LoanTransactionsRepository.kt @@ -9,7 +9,7 @@ */ package com.mifos.core.data.repository -import com.mifos.core.entity.accounts.loan.LoanWithAssociations +import com.mifos.room.entities.accounts.loans.LoanWithAssociations import rx.Observable /** diff --git a/core/data/src/main/java/com/mifos/core/data/repository/OfflineDashboardRepository.kt b/core/data/src/main/java/com/mifos/core/data/repository/OfflineDashboardRepository.kt index 74b3bd01c2..917e1491dd 100644 --- a/core/data/src/main/java/com/mifos/core/data/repository/OfflineDashboardRepository.kt +++ b/core/data/src/main/java/com/mifos/core/data/repository/OfflineDashboardRepository.kt @@ -9,11 +9,12 @@ */ package com.mifos.core.data.repository -import com.mifos.core.entity.accounts.loan.LoanRepaymentRequest import com.mifos.core.entity.accounts.savings.SavingsAccountTransactionRequest import com.mifos.core.entity.center.CenterPayload import com.mifos.core.entity.client.ClientPayload import com.mifos.core.entity.group.GroupPayload +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest +import kotlinx.coroutines.flow.Flow import rx.Observable /** @@ -27,7 +28,7 @@ interface OfflineDashboardRepository { fun allDatabaseCenterPayload(): Observable> - fun databaseLoanRepayments(): Observable> + fun databaseLoanRepayments(): Flow> fun allSavingsAccountTransactions(): Observable> } diff --git a/core/data/src/main/java/com/mifos/core/data/repository/SyncCentersDialogRepository.kt b/core/data/src/main/java/com/mifos/core/data/repository/SyncCentersDialogRepository.kt index cbd0ecd498..47a4cbf4be 100644 --- a/core/data/src/main/java/com/mifos/core/data/repository/SyncCentersDialogRepository.kt +++ b/core/data/src/main/java/com/mifos/core/data/repository/SyncCentersDialogRepository.kt @@ -9,18 +9,19 @@ */ package com.mifos.core.data.repository -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.entity.accounts.savings.SavingsAccountWithAssociations import com.mifos.core.entity.client.Client import com.mifos.core.entity.group.Center import com.mifos.core.entity.group.Group -import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate import com.mifos.core.entity.templates.savings.SavingsAccountTransactionTemplate import com.mifos.room.entities.accounts.CenterAccounts import com.mifos.room.entities.accounts.ClientAccounts import com.mifos.room.entities.accounts.GroupAccounts +import com.mifos.room.entities.accounts.loans.LoanWithAssociations import com.mifos.room.entities.group.CenterWithAssociations import com.mifos.room.entities.group.GroupWithAssociations +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate +import kotlinx.coroutines.flow.Flow import rx.Observable /** @@ -30,9 +31,9 @@ interface SyncCentersDialogRepository { fun syncCenterAccounts(centerId: Int): Observable - fun syncLoanById(loanId: Int): Observable + fun syncLoanById(loanId: Int): Flow - fun syncLoanRepaymentTemplate(loanId: Int): Observable + fun syncLoanRepaymentTemplate(loanId: Int): Flow fun getCenterWithAssociations(centerId: Int): Observable diff --git a/core/data/src/main/java/com/mifos/core/data/repository/SyncClientsDialogRepository.kt b/core/data/src/main/java/com/mifos/core/data/repository/SyncClientsDialogRepository.kt index 095cdfb491..6d219ad437 100644 --- a/core/data/src/main/java/com/mifos/core/data/repository/SyncClientsDialogRepository.kt +++ b/core/data/src/main/java/com/mifos/core/data/repository/SyncClientsDialogRepository.kt @@ -9,12 +9,13 @@ */ package com.mifos.core.data.repository -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.entity.accounts.savings.SavingsAccountWithAssociations import com.mifos.core.entity.client.Client -import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate import com.mifos.core.entity.templates.savings.SavingsAccountTransactionTemplate import com.mifos.room.entities.accounts.ClientAccounts +import com.mifos.room.entities.accounts.loans.LoanWithAssociations +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate +import kotlinx.coroutines.flow.Flow import rx.Observable /** @@ -24,9 +25,9 @@ interface SyncClientsDialogRepository { suspend fun syncClientAccounts(clientId: Int): ClientAccounts - fun syncLoanById(loanId: Int): Observable + fun syncLoanById(loanId: Int): Flow - fun syncLoanRepaymentTemplate(loanId: Int): Observable + fun syncLoanRepaymentTemplate(loanId: Int): Flow fun syncSavingsAccount( type: String?, diff --git a/core/data/src/main/java/com/mifos/core/data/repository/SyncGroupsDialogRepository.kt b/core/data/src/main/java/com/mifos/core/data/repository/SyncGroupsDialogRepository.kt index 2a13d64728..2fa604ec10 100644 --- a/core/data/src/main/java/com/mifos/core/data/repository/SyncGroupsDialogRepository.kt +++ b/core/data/src/main/java/com/mifos/core/data/repository/SyncGroupsDialogRepository.kt @@ -9,15 +9,16 @@ */ package com.mifos.core.data.repository -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.entity.accounts.savings.SavingsAccountWithAssociations import com.mifos.core.entity.client.Client import com.mifos.core.entity.group.Group -import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate import com.mifos.core.entity.templates.savings.SavingsAccountTransactionTemplate import com.mifos.room.entities.accounts.ClientAccounts import com.mifos.room.entities.accounts.GroupAccounts +import com.mifos.room.entities.accounts.loans.LoanWithAssociations import com.mifos.room.entities.group.GroupWithAssociations +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate +import kotlinx.coroutines.flow.Flow import rx.Observable /** @@ -27,9 +28,9 @@ interface SyncGroupsDialogRepository { fun syncGroupAccounts(groupId: Int): Observable - fun syncLoanById(loanId: Int): Observable + fun syncLoanById(loanId: Int): Flow - fun syncLoanRepaymentTemplate(loanId: Int): Observable + fun syncLoanRepaymentTemplate(loanId: Int): Flow fun syncSavingsAccount( type: String?, diff --git a/core/data/src/main/java/com/mifos/core/data/repository/SyncLoanRepaymentTransactionRepository.kt b/core/data/src/main/java/com/mifos/core/data/repository/SyncLoanRepaymentTransactionRepository.kt index 550025478d..53890a797b 100644 --- a/core/data/src/main/java/com/mifos/core/data/repository/SyncLoanRepaymentTransactionRepository.kt +++ b/core/data/src/main/java/com/mifos/core/data/repository/SyncLoanRepaymentTransactionRepository.kt @@ -9,25 +9,25 @@ */ package com.mifos.core.data.repository -import com.mifos.core.entity.accounts.loan.LoanRepaymentRequest -import com.mifos.core.objects.account.loan.LoanRepaymentResponse import com.mifos.room.entities.PaymentTypeOption -import rx.Observable +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest +import com.mifos.room.entities.accounts.loans.LoanRepaymentResponse +import kotlinx.coroutines.flow.Flow interface SyncLoanRepaymentTransactionRepository { - fun databaseLoanRepayments(): Observable> + fun databaseLoanRepayments(): Flow> - fun paymentTypeOption(): Observable> + fun paymentTypeOption(): Flow> - fun submitPayment( + suspend fun submitPayment( loanId: Int, request: LoanRepaymentRequest, - ): Observable + ): LoanRepaymentResponse - fun deleteAndUpdateLoanRepayments(loanId: Int): Observable> + fun deleteAndUpdateLoanRepayments(loanId: Int): Flow> fun updateLoanRepaymentTransaction( loanRepaymentRequest: LoanRepaymentRequest, - ): Observable + ): Flow } diff --git a/core/data/src/main/java/com/mifos/core/data/repository/SyncSavingsAccountTransactionRepository.kt b/core/data/src/main/java/com/mifos/core/data/repository/SyncSavingsAccountTransactionRepository.kt index 437e57c617..d1bdd26390 100644 --- a/core/data/src/main/java/com/mifos/core/data/repository/SyncSavingsAccountTransactionRepository.kt +++ b/core/data/src/main/java/com/mifos/core/data/repository/SyncSavingsAccountTransactionRepository.kt @@ -12,6 +12,7 @@ package com.mifos.core.data.repository import com.mifos.core.entity.accounts.savings.SavingsAccountTransactionRequest import com.mifos.core.objects.account.saving.SavingsAccountTransactionResponse import com.mifos.room.entities.PaymentTypeOption +import kotlinx.coroutines.flow.Flow import rx.Observable /** @@ -21,7 +22,7 @@ interface SyncSavingsAccountTransactionRepository { fun allSavingsAccountTransactions(): Observable> - fun paymentTypeOption(): Observable> + fun paymentTypeOption(): Flow> fun processTransaction( savingsAccountType: String?, diff --git a/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanAccountSummaryRepositoryImp.kt b/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanAccountSummaryRepositoryImp.kt index 309194c7ad..860b535981 100644 --- a/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanAccountSummaryRepositoryImp.kt +++ b/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanAccountSummaryRepositoryImp.kt @@ -10,18 +10,19 @@ package com.mifos.core.data.repositoryImp import com.mifos.core.data.repository.LoanAccountSummaryRepository -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.network.datamanager.DataManagerLoan -import rx.Observable +import com.mifos.room.entities.accounts.loans.LoanWithAssociations +import kotlinx.coroutines.flow.Flow import javax.inject.Inject /** * Created by Aditya Gupta on 08/08/23. */ -class LoanAccountSummaryRepositoryImp @Inject constructor(private val dataManagerLoan: DataManagerLoan) : - LoanAccountSummaryRepository { +class LoanAccountSummaryRepositoryImp @Inject constructor( + private val dataManagerLoan: DataManagerLoan, +) : LoanAccountSummaryRepository { - override fun getLoanById(loanId: Int): Observable { + override fun getLoanById(loanId: Int): Flow { return dataManagerLoan.getLoanById(loanId) } } diff --git a/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanRepaymentRepositoryImp.kt b/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanRepaymentRepositoryImp.kt index 8741b01186..78e9a53cea 100644 --- a/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanRepaymentRepositoryImp.kt +++ b/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanRepaymentRepositoryImp.kt @@ -10,31 +10,32 @@ package com.mifos.core.data.repositoryImp import com.mifos.core.data.repository.LoanRepaymentRepository -import com.mifos.core.entity.accounts.loan.LoanRepaymentRequest -import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate import com.mifos.core.network.datamanager.DataManagerLoan -import com.mifos.core.objects.account.loan.LoanRepaymentResponse -import rx.Observable +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest +import com.mifos.room.entities.accounts.loans.LoanRepaymentResponse +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate +import kotlinx.coroutines.flow.Flow import javax.inject.Inject /** * Created by Aditya Gupta on 10/08/23. */ -class LoanRepaymentRepositoryImp @Inject constructor(private val dataManagerLoan: DataManagerLoan) : - LoanRepaymentRepository { +class LoanRepaymentRepositoryImp @Inject constructor( + private val dataManagerLoan: DataManagerLoan, +) : LoanRepaymentRepository { - override fun getLoanRepayTemplate(loanId: Int): Observable { + override fun getLoanRepayTemplate(loanId: Int): Flow { return dataManagerLoan.getLoanRepayTemplate(loanId) } - override fun submitPayment( + override suspend fun submitPayment( loanId: Int, request: LoanRepaymentRequest, - ): Observable { - return submitPayment(loanId, request) + ): LoanRepaymentResponse { + return dataManagerLoan.submitPayment(loanId, request) } - override fun getDatabaseLoanRepaymentByLoanId(loanId: Int): Observable { + override fun getDatabaseLoanRepaymentByLoanId(loanId: Int): Flow { return dataManagerLoan.getDatabaseLoanRepaymentByLoanId(loanId) } } diff --git a/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanRepaymentScheduleRepositoryImp.kt b/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanRepaymentScheduleRepositoryImp.kt index 2c5930a51b..d299eb579d 100644 --- a/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanRepaymentScheduleRepositoryImp.kt +++ b/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanRepaymentScheduleRepositoryImp.kt @@ -10,8 +10,8 @@ package com.mifos.core.data.repositoryImp import com.mifos.core.data.repository.LoanRepaymentScheduleRepository -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.network.DataManager +import com.mifos.room.entities.accounts.loans.LoanWithAssociations import rx.Observable import javax.inject.Inject diff --git a/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanTransactionsRepositoryImp.kt b/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanTransactionsRepositoryImp.kt index b743985696..d88e3f5a04 100644 --- a/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanTransactionsRepositoryImp.kt +++ b/core/data/src/main/java/com/mifos/core/data/repositoryImp/LoanTransactionsRepositoryImp.kt @@ -10,8 +10,8 @@ package com.mifos.core.data.repositoryImp import com.mifos.core.data.repository.LoanTransactionsRepository -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.network.DataManager +import com.mifos.room.entities.accounts.loans.LoanWithAssociations import rx.Observable import javax.inject.Inject diff --git a/core/data/src/main/java/com/mifos/core/data/repositoryImp/OfflineDashboardRepositoryImp.kt b/core/data/src/main/java/com/mifos/core/data/repositoryImp/OfflineDashboardRepositoryImp.kt index 7e213c8ade..bb3cb1000c 100644 --- a/core/data/src/main/java/com/mifos/core/data/repositoryImp/OfflineDashboardRepositoryImp.kt +++ b/core/data/src/main/java/com/mifos/core/data/repositoryImp/OfflineDashboardRepositoryImp.kt @@ -10,7 +10,6 @@ package com.mifos.core.data.repositoryImp import com.mifos.core.data.repository.OfflineDashboardRepository -import com.mifos.core.entity.accounts.loan.LoanRepaymentRequest import com.mifos.core.entity.accounts.savings.SavingsAccountTransactionRequest import com.mifos.core.entity.center.CenterPayload import com.mifos.core.entity.client.ClientPayload @@ -20,6 +19,8 @@ import com.mifos.core.network.datamanager.DataManagerClient import com.mifos.core.network.datamanager.DataManagerGroups import com.mifos.core.network.datamanager.DataManagerLoan import com.mifos.core.network.datamanager.DataManagerSavings +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest +import kotlinx.coroutines.flow.Flow import rx.Observable import javax.inject.Inject @@ -46,7 +47,7 @@ class OfflineDashboardRepositoryImp @Inject constructor( return dataManagerCenter.allDatabaseCenterPayload } - override fun databaseLoanRepayments(): Observable> { + override fun databaseLoanRepayments(): Flow> { return dataManagerLoan.databaseLoanRepayments } diff --git a/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncCentersDialogRepositoryImp.kt b/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncCentersDialogRepositoryImp.kt index 7cb66069ee..ea6e086592 100644 --- a/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncCentersDialogRepositoryImp.kt +++ b/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncCentersDialogRepositoryImp.kt @@ -10,12 +10,10 @@ package com.mifos.core.data.repositoryImp import com.mifos.core.data.repository.SyncCentersDialogRepository -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.entity.accounts.savings.SavingsAccountWithAssociations import com.mifos.core.entity.client.Client import com.mifos.core.entity.group.Center import com.mifos.core.entity.group.Group -import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate import com.mifos.core.entity.templates.savings.SavingsAccountTransactionTemplate import com.mifos.core.network.datamanager.DataManagerCenter import com.mifos.core.network.datamanager.DataManagerClient @@ -25,8 +23,11 @@ import com.mifos.core.network.datamanager.DataManagerSavings import com.mifos.room.entities.accounts.CenterAccounts import com.mifos.room.entities.accounts.ClientAccounts import com.mifos.room.entities.accounts.GroupAccounts +import com.mifos.room.entities.accounts.loans.LoanWithAssociations import com.mifos.room.entities.group.CenterWithAssociations import com.mifos.room.entities.group.GroupWithAssociations +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate +import kotlinx.coroutines.flow.Flow import rx.Observable import javax.inject.Inject @@ -45,11 +46,11 @@ class SyncCentersDialogRepositoryImp @Inject constructor( return dataManagerCenter.syncCenterAccounts(centerId) } - override fun syncLoanById(loanId: Int): Observable { + override fun syncLoanById(loanId: Int): Flow { return dataManagerLoan.syncLoanById(loanId) } - override fun syncLoanRepaymentTemplate(loanId: Int): Observable { + override fun syncLoanRepaymentTemplate(loanId: Int): Flow { return dataManagerLoan.syncLoanRepaymentTemplate(loanId) } diff --git a/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncClientsDialogRepositoryImp.kt b/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncClientsDialogRepositoryImp.kt index 6b0171ab3e..ce691dc6f6 100644 --- a/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncClientsDialogRepositoryImp.kt +++ b/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncClientsDialogRepositoryImp.kt @@ -10,15 +10,16 @@ package com.mifos.core.data.repositoryImp import com.mifos.core.data.repository.SyncClientsDialogRepository -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.entity.accounts.savings.SavingsAccountWithAssociations import com.mifos.core.entity.client.Client -import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate import com.mifos.core.entity.templates.savings.SavingsAccountTransactionTemplate import com.mifos.core.network.datamanager.DataManagerClient import com.mifos.core.network.datamanager.DataManagerLoan import com.mifos.core.network.datamanager.DataManagerSavings import com.mifos.room.entities.accounts.ClientAccounts +import com.mifos.room.entities.accounts.loans.LoanWithAssociations +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate +import kotlinx.coroutines.flow.Flow import rx.Observable import javax.inject.Inject @@ -35,11 +36,11 @@ class SyncClientsDialogRepositoryImp @Inject constructor( return dataManagerClient.getClientAccounts(clientId) } - override fun syncLoanById(loanId: Int): Observable { + override fun syncLoanById(loanId: Int): Flow { return dataManagerLoan.syncLoanById(loanId) } - override fun syncLoanRepaymentTemplate(loanId: Int): Observable { + override fun syncLoanRepaymentTemplate(loanId: Int): Flow { return dataManagerLoan.syncLoanRepaymentTemplate(loanId) } diff --git a/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncGroupsDialogRepositoryImp.kt b/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncGroupsDialogRepositoryImp.kt index cd0f3c2076..394e1071b9 100644 --- a/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncGroupsDialogRepositoryImp.kt +++ b/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncGroupsDialogRepositoryImp.kt @@ -10,11 +10,9 @@ package com.mifos.core.data.repositoryImp import com.mifos.core.data.repository.SyncGroupsDialogRepository -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.entity.accounts.savings.SavingsAccountWithAssociations import com.mifos.core.entity.client.Client import com.mifos.core.entity.group.Group -import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate import com.mifos.core.entity.templates.savings.SavingsAccountTransactionTemplate import com.mifos.core.network.datamanager.DataManagerClient import com.mifos.core.network.datamanager.DataManagerGroups @@ -22,7 +20,10 @@ import com.mifos.core.network.datamanager.DataManagerLoan import com.mifos.core.network.datamanager.DataManagerSavings import com.mifos.room.entities.accounts.ClientAccounts import com.mifos.room.entities.accounts.GroupAccounts +import com.mifos.room.entities.accounts.loans.LoanWithAssociations import com.mifos.room.entities.group.GroupWithAssociations +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate +import kotlinx.coroutines.flow.Flow import rx.Observable import javax.inject.Inject @@ -40,11 +41,11 @@ class SyncGroupsDialogRepositoryImp @Inject constructor( return dataManagerGroups.syncGroupAccounts(groupId) } - override fun syncLoanById(loanId: Int): Observable { + override fun syncLoanById(loanId: Int): Flow { return dataManagerLoan.syncLoanById(loanId) } - override fun syncLoanRepaymentTemplate(loanId: Int): Observable { + override fun syncLoanRepaymentTemplate(loanId: Int): Flow { return dataManagerLoan.syncLoanRepaymentTemplate(loanId) } diff --git a/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncLoanRepaymentTransactionRepositoryImp.kt b/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncLoanRepaymentTransactionRepositoryImp.kt index e5feaaa80f..3346735c74 100644 --- a/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncLoanRepaymentTransactionRepositoryImp.kt +++ b/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncLoanRepaymentTransactionRepositoryImp.kt @@ -10,36 +10,37 @@ package com.mifos.core.data.repositoryImp import com.mifos.core.data.repository.SyncLoanRepaymentTransactionRepository -import com.mifos.core.entity.accounts.loan.LoanRepaymentRequest import com.mifos.core.network.datamanager.DataManagerLoan -import com.mifos.core.objects.account.loan.LoanRepaymentResponse import com.mifos.room.entities.PaymentTypeOption -import rx.Observable +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest +import com.mifos.room.entities.accounts.loans.LoanRepaymentResponse +import kotlinx.coroutines.flow.Flow import javax.inject.Inject -class SyncLoanRepaymentTransactionRepositoryImp @Inject constructor(private val dataManagerLoan: DataManagerLoan) : - SyncLoanRepaymentTransactionRepository { +class SyncLoanRepaymentTransactionRepositoryImp @Inject constructor( + private val dataManagerLoan: DataManagerLoan, +) : SyncLoanRepaymentTransactionRepository { - override fun databaseLoanRepayments(): Observable> { + override fun databaseLoanRepayments(): Flow> { return dataManagerLoan.databaseLoanRepayments } - override fun paymentTypeOption(): Observable> { + override fun paymentTypeOption(): Flow> { return dataManagerLoan.paymentTypeOption } - override fun submitPayment( + override suspend fun submitPayment( loanId: Int, request: LoanRepaymentRequest, - ): Observable { + ): LoanRepaymentResponse { return dataManagerLoan.submitPayment(loanId, request) } - override fun deleteAndUpdateLoanRepayments(loanId: Int): Observable> { + override fun deleteAndUpdateLoanRepayments(loanId: Int): Flow> { return dataManagerLoan.deleteAndUpdateLoanRepayments(loanId) } - override fun updateLoanRepaymentTransaction(loanRepaymentRequest: LoanRepaymentRequest): Observable { + override fun updateLoanRepaymentTransaction(loanRepaymentRequest: LoanRepaymentRequest): Flow { return dataManagerLoan.updateLoanRepaymentTransaction(loanRepaymentRequest) } } diff --git a/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncSavingsAccountTransactionRepositoryImp.kt b/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncSavingsAccountTransactionRepositoryImp.kt index 237d890732..e40075368f 100644 --- a/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncSavingsAccountTransactionRepositoryImp.kt +++ b/core/data/src/main/java/com/mifos/core/data/repositoryImp/SyncSavingsAccountTransactionRepositoryImp.kt @@ -15,6 +15,7 @@ import com.mifos.core.network.datamanager.DataManagerLoan import com.mifos.core.network.datamanager.DataManagerSavings import com.mifos.core.objects.account.saving.SavingsAccountTransactionResponse import com.mifos.room.entities.PaymentTypeOption +import kotlinx.coroutines.flow.Flow import rx.Observable import javax.inject.Inject @@ -30,7 +31,7 @@ class SyncSavingsAccountTransactionRepositoryImp @Inject constructor( return dataManagerSavings.allSavingsAccountTransactions } - override fun paymentTypeOption(): Observable> { + override fun paymentTypeOption(): Flow> { return dataManagerLoan.paymentTypeOption } diff --git a/core/database/schemas/com.mifos.room.db.MifosDatabase/1.json b/core/database/schemas/com.mifos.room.db.MifosDatabase/1.json new file mode 100644 index 0000000000..f768669443 --- /dev/null +++ b/core/database/schemas/com.mifos.room.db.MifosDatabase/1.json @@ -0,0 +1,1178 @@ +{ + "formatVersion": 1, + "database": { + "version": 1, + "identityHash": "be50121d7afe08c6200f3cea98d17b8a", + "entities": [ + { + "tableName": "ColumnValue", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER, `value` TEXT, `score` INTEGER, `registeredTableName` TEXT, PRIMARY KEY(`id`))", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER" + }, + { + "fieldPath": "value", + "columnName": "value", + "affinity": "TEXT" + }, + { + "fieldPath": "score", + "columnName": "score", + "affinity": "INTEGER" + }, + { + "fieldPath": "registeredTableName", + "columnName": "registeredTableName", + "affinity": "TEXT" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + } + }, + { + "tableName": "LoanWithAssociations", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `accountNo` TEXT NOT NULL, `status` TEXT NOT NULL, `clientId` INTEGER NOT NULL, `clientName` TEXT NOT NULL, `clientOfficeId` INTEGER NOT NULL, `loanProductId` INTEGER NOT NULL, `loanProductName` TEXT NOT NULL, `loanProductDescription` TEXT NOT NULL, `fundId` INTEGER NOT NULL, `fundName` TEXT NOT NULL, `loanPurposeId` INTEGER NOT NULL, `loanPurposeName` TEXT NOT NULL, `loanOfficerId` INTEGER NOT NULL, `loanOfficerName` TEXT NOT NULL, `principal` REAL NOT NULL, `approvedPrincipal` REAL NOT NULL, `termFrequency` INTEGER NOT NULL, `numberOfRepayments` INTEGER NOT NULL, `repaymentEvery` INTEGER NOT NULL, `interestRatePerPeriod` REAL NOT NULL, `annualInterestRate` REAL NOT NULL, `transactionProcessingStrategyId` INTEGER NOT NULL, `transactionProcessingStrategyName` TEXT NOT NULL, `syncDisbursementWithMeeting` INTEGER NOT NULL, `timeline` TEXT NOT NULL, `summary` TEXT NOT NULL, `transactions` TEXT NOT NULL, `feeChargesAtDisbursementCharged` REAL NOT NULL, `totalOverpaid` REAL NOT NULL, `loanCounter` INTEGER NOT NULL, `loanProductCounter` INTEGER NOT NULL, `multiDisburseLoan` INTEGER NOT NULL, `canDisburse` INTEGER NOT NULL, `inArrears` INTEGER NOT NULL, `isNPA` INTEGER NOT NULL, `LoanType_id` INTEGER, `LoanType_code` TEXT, `LoanType_value` TEXT, `Currency_code` TEXT, `Currency_name` TEXT, `Currency_decimalPlaces` REAL, `Currency_inMultiplesOf` INTEGER, `Currency_displaySymbol` TEXT, `Currency_nameCode` TEXT, `Currency_displayLabel` TEXT, `termPeriodFrequencyType_id` INTEGER, `termPeriodFrequencyType_code` TEXT, `termPeriodFrequencyType_value` TEXT, `repaymentFrequencyType_id` INTEGER, `repaymentFrequencyType_code` TEXT, `repaymentFrequencyType_value` TEXT, `interestRateFrequencyType_id` INTEGER, `interestRateFrequencyType_code` TEXT, `interestRateFrequencyType_value` TEXT, `amortization_type_id` INTEGER, `amortization_type_code` TEXT, `amortization_type_value` TEXT, `interestType_id` INTEGER, `interestType_code` TEXT, `interestType_value` TEXT, `interestCalculationPeriodType_id` INTEGER, `interestCalculationPeriodType_code` TEXT, `interestCalculationPeriodType_value` TEXT, `repaymentSchedule_currency` TEXT, `repaymentSchedule_loanTermInDays` INTEGER, `repaymentSchedule_periods` TEXT, `repaymentSchedule_totalFeeChargesCharged` REAL, `repaymentSchedule_totalInterestCharged` REAL, `repaymentSchedule_totalOutstanding` REAL, `repaymentSchedule_totalPaidInAdvance` REAL, `repaymentSchedule_totalPaidLate` REAL, `repaymentSchedule_totalPenaltyChargesCharged` REAL, `repaymentSchedule_totalPrincipalDisbursed` REAL, `repaymentSchedule_totalPrincipalExpected` REAL, `repaymentSchedule_totalPrincipalPaid` REAL, `repaymentSchedule_totalRepayment` REAL, `repaymentSchedule_totalRepaymentExpected` REAL, `repaymentSchedule_totalWaived` REAL, `repaymentSchedule_totalWrittenOff` REAL, FOREIGN KEY(`status`) REFERENCES `LoanStatus`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE , FOREIGN KEY(`timeline`) REFERENCES `Timeline`(`loanId`) ON UPDATE NO ACTION ON DELETE CASCADE , FOREIGN KEY(`summary`) REFERENCES `LoansAccountSummary`(`loanId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accountNo", + "columnName": "accountNo", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "status", + "columnName": "status", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "clientId", + "columnName": "clientId", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "clientName", + "columnName": "clientName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "clientOfficeId", + "columnName": "clientOfficeId", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "loanProductId", + "columnName": "loanProductId", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "loanProductName", + "columnName": "loanProductName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "loanProductDescription", + "columnName": "loanProductDescription", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "fundId", + "columnName": "fundId", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "fundName", + "columnName": "fundName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "loanPurposeId", + "columnName": "loanPurposeId", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "loanPurposeName", + "columnName": "loanPurposeName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "loanOfficerId", + "columnName": "loanOfficerId", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "loanOfficerName", + "columnName": "loanOfficerName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "principal", + "columnName": "principal", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "approvedPrincipal", + "columnName": "approvedPrincipal", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "termFrequency", + "columnName": "termFrequency", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "numberOfRepayments", + "columnName": "numberOfRepayments", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "repaymentEvery", + "columnName": "repaymentEvery", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "interestRatePerPeriod", + "columnName": "interestRatePerPeriod", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "annualInterestRate", + "columnName": "annualInterestRate", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "transactionProcessingStrategyId", + "columnName": "transactionProcessingStrategyId", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "transactionProcessingStrategyName", + "columnName": "transactionProcessingStrategyName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "syncDisbursementWithMeeting", + "columnName": "syncDisbursementWithMeeting", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "timeline", + "columnName": "timeline", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "summary", + "columnName": "summary", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "transactions", + "columnName": "transactions", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "feeChargesAtDisbursementCharged", + "columnName": "feeChargesAtDisbursementCharged", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "totalOverpaid", + "columnName": "totalOverpaid", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "loanCounter", + "columnName": "loanCounter", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "loanProductCounter", + "columnName": "loanProductCounter", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "multiDisburseLoan", + "columnName": "multiDisburseLoan", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canDisburse", + "columnName": "canDisburse", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "inArrears", + "columnName": "inArrears", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isNPA", + "columnName": "isNPA", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "loanType.id", + "columnName": "LoanType_id", + "affinity": "INTEGER" + }, + { + "fieldPath": "loanType.code", + "columnName": "LoanType_code", + "affinity": "TEXT" + }, + { + "fieldPath": "loanType.value", + "columnName": "LoanType_value", + "affinity": "TEXT" + }, + { + "fieldPath": "currency.code", + "columnName": "Currency_code", + "affinity": "TEXT" + }, + { + "fieldPath": "currency.name", + "columnName": "Currency_name", + "affinity": "TEXT" + }, + { + "fieldPath": "currency.decimalPlaces", + "columnName": "Currency_decimalPlaces", + "affinity": "REAL" + }, + { + "fieldPath": "currency.inMultiplesOf", + "columnName": "Currency_inMultiplesOf", + "affinity": "INTEGER" + }, + { + "fieldPath": "currency.displaySymbol", + "columnName": "Currency_displaySymbol", + "affinity": "TEXT" + }, + { + "fieldPath": "currency.nameCode", + "columnName": "Currency_nameCode", + "affinity": "TEXT" + }, + { + "fieldPath": "currency.displayLabel", + "columnName": "Currency_displayLabel", + "affinity": "TEXT" + }, + { + "fieldPath": "termPeriodFrequencyType.id", + "columnName": "termPeriodFrequencyType_id", + "affinity": "INTEGER" + }, + { + "fieldPath": "termPeriodFrequencyType.code", + "columnName": "termPeriodFrequencyType_code", + "affinity": "TEXT" + }, + { + "fieldPath": "termPeriodFrequencyType.value", + "columnName": "termPeriodFrequencyType_value", + "affinity": "TEXT" + }, + { + "fieldPath": "repaymentFrequencyType.id", + "columnName": "repaymentFrequencyType_id", + "affinity": "INTEGER" + }, + { + "fieldPath": "repaymentFrequencyType.code", + "columnName": "repaymentFrequencyType_code", + "affinity": "TEXT" + }, + { + "fieldPath": "repaymentFrequencyType.value", + "columnName": "repaymentFrequencyType_value", + "affinity": "TEXT" + }, + { + "fieldPath": "interestRateFrequencyType.id", + "columnName": "interestRateFrequencyType_id", + "affinity": "INTEGER" + }, + { + "fieldPath": "interestRateFrequencyType.code", + "columnName": "interestRateFrequencyType_code", + "affinity": "TEXT" + }, + { + "fieldPath": "interestRateFrequencyType.value", + "columnName": "interestRateFrequencyType_value", + "affinity": "TEXT" + }, + { + "fieldPath": "amortizationType.id", + "columnName": "amortization_type_id", + "affinity": "INTEGER" + }, + { + "fieldPath": "amortizationType.code", + "columnName": "amortization_type_code", + "affinity": "TEXT" + }, + { + "fieldPath": "amortizationType.value", + "columnName": "amortization_type_value", + "affinity": "TEXT" + }, + { + "fieldPath": "interestType.id", + "columnName": "interestType_id", + "affinity": "INTEGER" + }, + { + "fieldPath": "interestType.code", + "columnName": "interestType_code", + "affinity": "TEXT" + }, + { + "fieldPath": "interestType.value", + "columnName": "interestType_value", + "affinity": "TEXT" + }, + { + "fieldPath": "interestCalculationPeriodType.id", + "columnName": "interestCalculationPeriodType_id", + "affinity": "INTEGER" + }, + { + "fieldPath": "interestCalculationPeriodType.code", + "columnName": "interestCalculationPeriodType_code", + "affinity": "TEXT" + }, + { + "fieldPath": "interestCalculationPeriodType.value", + "columnName": "interestCalculationPeriodType_value", + "affinity": "TEXT" + }, + { + "fieldPath": "repaymentSchedule.currency", + "columnName": "repaymentSchedule_currency", + "affinity": "TEXT" + }, + { + "fieldPath": "repaymentSchedule.loanTermInDays", + "columnName": "repaymentSchedule_loanTermInDays", + "affinity": "INTEGER" + }, + { + "fieldPath": "repaymentSchedule.periods", + "columnName": "repaymentSchedule_periods", + "affinity": "TEXT" + }, + { + "fieldPath": "repaymentSchedule.totalFeeChargesCharged", + "columnName": "repaymentSchedule_totalFeeChargesCharged", + "affinity": "REAL" + }, + { + "fieldPath": "repaymentSchedule.totalInterestCharged", + "columnName": "repaymentSchedule_totalInterestCharged", + "affinity": "REAL" + }, + { + "fieldPath": "repaymentSchedule.totalOutstanding", + "columnName": "repaymentSchedule_totalOutstanding", + "affinity": "REAL" + }, + { + "fieldPath": "repaymentSchedule.totalPaidInAdvance", + "columnName": "repaymentSchedule_totalPaidInAdvance", + "affinity": "REAL" + }, + { + "fieldPath": "repaymentSchedule.totalPaidLate", + "columnName": "repaymentSchedule_totalPaidLate", + "affinity": "REAL" + }, + { + "fieldPath": "repaymentSchedule.totalPenaltyChargesCharged", + "columnName": "repaymentSchedule_totalPenaltyChargesCharged", + "affinity": "REAL" + }, + { + "fieldPath": "repaymentSchedule.totalPrincipalDisbursed", + "columnName": "repaymentSchedule_totalPrincipalDisbursed", + "affinity": "REAL" + }, + { + "fieldPath": "repaymentSchedule.totalPrincipalExpected", + "columnName": "repaymentSchedule_totalPrincipalExpected", + "affinity": "REAL" + }, + { + "fieldPath": "repaymentSchedule.totalPrincipalPaid", + "columnName": "repaymentSchedule_totalPrincipalPaid", + "affinity": "REAL" + }, + { + "fieldPath": "repaymentSchedule.totalRepayment", + "columnName": "repaymentSchedule_totalRepayment", + "affinity": "REAL" + }, + { + "fieldPath": "repaymentSchedule.totalRepaymentExpected", + "columnName": "repaymentSchedule_totalRepaymentExpected", + "affinity": "REAL" + }, + { + "fieldPath": "repaymentSchedule.totalWaived", + "columnName": "repaymentSchedule_totalWaived", + "affinity": "REAL" + }, + { + "fieldPath": "repaymentSchedule.totalWrittenOff", + "columnName": "repaymentSchedule_totalWrittenOff", + "affinity": "REAL" + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_LoanWithAssociations_status", + "unique": false, + "columnNames": [ + "status" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_LoanWithAssociations_status` ON `${TABLE_NAME}` (`status`)" + }, + { + "name": "index_LoanWithAssociations_timeline", + "unique": false, + "columnNames": [ + "timeline" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_LoanWithAssociations_timeline` ON `${TABLE_NAME}` (`timeline`)" + }, + { + "name": "index_LoanWithAssociations_summary", + "unique": false, + "columnNames": [ + "summary" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_LoanWithAssociations_summary` ON `${TABLE_NAME}` (`summary`)" + } + ], + "foreignKeys": [ + { + "table": "LoanStatus", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "status" + ], + "referencedColumns": [ + "id" + ] + }, + { + "table": "Timeline", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "timeline" + ], + "referencedColumns": [ + "loanId" + ] + }, + { + "table": "LoansAccountSummary", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "summary" + ], + "referencedColumns": [ + "loanId" + ] + } + ] + }, + { + "tableName": "LoanRepaymentRequest", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`timeStamp` INTEGER NOT NULL, `loanId` INTEGER, `errorMessage` TEXT, `dateFormat` TEXT, `locale` TEXT, `transactionDate` TEXT, `transactionAmount` TEXT, `paymentTypeId` TEXT, `note` TEXT, `accountNumber` TEXT, `checkNumber` TEXT, `routingCode` TEXT, `receiptNumber` TEXT, `bankNumber` TEXT, PRIMARY KEY(`timeStamp`))", + "fields": [ + { + "fieldPath": "timeStamp", + "columnName": "timeStamp", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "loanId", + "columnName": "loanId", + "affinity": "INTEGER" + }, + { + "fieldPath": "errorMessage", + "columnName": "errorMessage", + "affinity": "TEXT" + }, + { + "fieldPath": "dateFormat", + "columnName": "dateFormat", + "affinity": "TEXT" + }, + { + "fieldPath": "locale", + "columnName": "locale", + "affinity": "TEXT" + }, + { + "fieldPath": "transactionDate", + "columnName": "transactionDate", + "affinity": "TEXT" + }, + { + "fieldPath": "transactionAmount", + "columnName": "transactionAmount", + "affinity": "TEXT" + }, + { + "fieldPath": "paymentTypeId", + "columnName": "paymentTypeId", + "affinity": "TEXT" + }, + { + "fieldPath": "note", + "columnName": "note", + "affinity": "TEXT" + }, + { + "fieldPath": "accountNumber", + "columnName": "accountNumber", + "affinity": "TEXT" + }, + { + "fieldPath": "checkNumber", + "columnName": "checkNumber", + "affinity": "TEXT" + }, + { + "fieldPath": "routingCode", + "columnName": "routingCode", + "affinity": "TEXT" + }, + { + "fieldPath": "receiptNumber", + "columnName": "receiptNumber", + "affinity": "TEXT" + }, + { + "fieldPath": "bankNumber", + "columnName": "bankNumber", + "affinity": "TEXT" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "timeStamp" + ] + } + }, + { + "tableName": "LoanRepaymentTemplate", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`loanId` INTEGER PRIMARY KEY AUTOINCREMENT, `type` TEXT, `date` TEXT, `currency` TEXT, `amount` REAL, `principalPortion` REAL, `interestPortion` REAL, `feeChargesPortion` REAL, `penaltyChargesPortion` REAL, `paymentTypeOptions` TEXT)", + "fields": [ + { + "fieldPath": "loanId", + "columnName": "loanId", + "affinity": "INTEGER" + }, + { + "fieldPath": "type", + "columnName": "type", + "affinity": "TEXT" + }, + { + "fieldPath": "date", + "columnName": "date", + "affinity": "TEXT" + }, + { + "fieldPath": "currency", + "columnName": "currency", + "affinity": "TEXT" + }, + { + "fieldPath": "amount", + "columnName": "amount", + "affinity": "REAL" + }, + { + "fieldPath": "principalPortion", + "columnName": "principalPortion", + "affinity": "REAL" + }, + { + "fieldPath": "interestPortion", + "columnName": "interestPortion", + "affinity": "REAL" + }, + { + "fieldPath": "feeChargesPortion", + "columnName": "feeChargesPortion", + "affinity": "REAL" + }, + { + "fieldPath": "penaltyChargesPortion", + "columnName": "penaltyChargesPortion", + "affinity": "REAL" + }, + { + "fieldPath": "paymentTypeOptions", + "columnName": "paymentTypeOptions", + "affinity": "TEXT" + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "loanId" + ] + } + }, + { + "tableName": "PaymentTypeOption", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `name` TEXT NOT NULL, `description` TEXT, `isCashPayment` INTEGER, `position` INTEGER, PRIMARY KEY(`id`))", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "description", + "columnName": "description", + "affinity": "TEXT" + }, + { + "fieldPath": "isCashPayment", + "columnName": "isCashPayment", + "affinity": "INTEGER" + }, + { + "fieldPath": "position", + "columnName": "position", + "affinity": "INTEGER" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + } + }, + { + "tableName": "ActualDisbursementDate", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`loanId` INTEGER, `year` INTEGER, `month` INTEGER, `date` INTEGER, PRIMARY KEY(`loanId`))", + "fields": [ + { + "fieldPath": "loanId", + "columnName": "loanId", + "affinity": "INTEGER" + }, + { + "fieldPath": "year", + "columnName": "year", + "affinity": "INTEGER" + }, + { + "fieldPath": "month", + "columnName": "month", + "affinity": "INTEGER" + }, + { + "fieldPath": "date", + "columnName": "date", + "affinity": "INTEGER" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "loanId" + ] + } + }, + { + "tableName": "Timeline", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`loanId` INTEGER, `submittedOnDate` TEXT, `submittedByUsername` TEXT, `submittedByFirstname` TEXT, `submittedByLastname` TEXT, `approvedOnDate` TEXT, `approvedByUsername` TEXT, `approvedByFirstname` TEXT, `approvedByLastname` TEXT, `expectedDisbursementDate` TEXT, `actualDisburseDate` TEXT, `actualDisbursementDate` TEXT, `disbursedByUsername` TEXT, `disbursedByFirstname` TEXT, `disbursedByLastname` TEXT, `closedOnDate` TEXT, `expectedMaturityDate` TEXT, PRIMARY KEY(`loanId`), FOREIGN KEY(`actualDisburseDate`) REFERENCES `ActualDisbursementDate`(`loanId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "loanId", + "columnName": "loanId", + "affinity": "INTEGER" + }, + { + "fieldPath": "submittedOnDate", + "columnName": "submittedOnDate", + "affinity": "TEXT" + }, + { + "fieldPath": "submittedByUsername", + "columnName": "submittedByUsername", + "affinity": "TEXT" + }, + { + "fieldPath": "submittedByFirstname", + "columnName": "submittedByFirstname", + "affinity": "TEXT" + }, + { + "fieldPath": "submittedByLastname", + "columnName": "submittedByLastname", + "affinity": "TEXT" + }, + { + "fieldPath": "approvedOnDate", + "columnName": "approvedOnDate", + "affinity": "TEXT" + }, + { + "fieldPath": "approvedByUsername", + "columnName": "approvedByUsername", + "affinity": "TEXT" + }, + { + "fieldPath": "approvedByFirstname", + "columnName": "approvedByFirstname", + "affinity": "TEXT" + }, + { + "fieldPath": "approvedByLastname", + "columnName": "approvedByLastname", + "affinity": "TEXT" + }, + { + "fieldPath": "expectedDisbursementDate", + "columnName": "expectedDisbursementDate", + "affinity": "TEXT" + }, + { + "fieldPath": "actualDisburseDate", + "columnName": "actualDisburseDate", + "affinity": "TEXT" + }, + { + "fieldPath": "actualDisbursementDate", + "columnName": "actualDisbursementDate", + "affinity": "TEXT" + }, + { + "fieldPath": "disbursedByUsername", + "columnName": "disbursedByUsername", + "affinity": "TEXT" + }, + { + "fieldPath": "disbursedByFirstname", + "columnName": "disbursedByFirstname", + "affinity": "TEXT" + }, + { + "fieldPath": "disbursedByLastname", + "columnName": "disbursedByLastname", + "affinity": "TEXT" + }, + { + "fieldPath": "closedOnDate", + "columnName": "closedOnDate", + "affinity": "TEXT" + }, + { + "fieldPath": "expectedMaturityDate", + "columnName": "expectedMaturityDate", + "affinity": "TEXT" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "loanId" + ] + }, + "indices": [ + { + "name": "index_Timeline_actualDisburseDate", + "unique": false, + "columnNames": [ + "actualDisburseDate" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_Timeline_actualDisburseDate` ON `${TABLE_NAME}` (`actualDisburseDate`)" + } + ], + "foreignKeys": [ + { + "table": "ActualDisbursementDate", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "actualDisburseDate" + ], + "referencedColumns": [ + "loanId" + ] + } + ] + }, + { + "tableName": "LoanStatus", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER, `code` TEXT, `value` TEXT, `pendingApproval` INTEGER, `waitingForDisbursal` INTEGER, `active` INTEGER, `closedObligationsMet` INTEGER, `closedWrittenOff` INTEGER, `closedRescheduled` INTEGER, `closed` INTEGER, `overpaid` INTEGER, PRIMARY KEY(`id`))", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER" + }, + { + "fieldPath": "code", + "columnName": "code", + "affinity": "TEXT" + }, + { + "fieldPath": "value", + "columnName": "value", + "affinity": "TEXT" + }, + { + "fieldPath": "pendingApproval", + "columnName": "pendingApproval", + "affinity": "INTEGER" + }, + { + "fieldPath": "waitingForDisbursal", + "columnName": "waitingForDisbursal", + "affinity": "INTEGER" + }, + { + "fieldPath": "active", + "columnName": "active", + "affinity": "INTEGER" + }, + { + "fieldPath": "closedObligationsMet", + "columnName": "closedObligationsMet", + "affinity": "INTEGER" + }, + { + "fieldPath": "closedWrittenOff", + "columnName": "closedWrittenOff", + "affinity": "INTEGER" + }, + { + "fieldPath": "closedRescheduled", + "columnName": "closedRescheduled", + "affinity": "INTEGER" + }, + { + "fieldPath": "closed", + "columnName": "closed", + "affinity": "INTEGER" + }, + { + "fieldPath": "overpaid", + "columnName": "overpaid", + "affinity": "INTEGER" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + } + }, + { + "tableName": "LoansAccountSummary", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`loanId` INTEGER, `principalDisbursed` REAL, `principalPaid` REAL, `principalWrittenOff` REAL, `principalOutstanding` REAL, `principalOverdue` REAL, `interestCharged` REAL, `interestPaid` REAL, `interestWaived` REAL, `interestWrittenOff` REAL, `interestOutstanding` REAL, `interestOverdue` REAL, `feeChargesCharged` REAL, `feeChargesDueAtDisbursementCharged` REAL, `feeChargesPaid` REAL, `feeChargesWaived` REAL, `feeChargesWrittenOff` REAL, `feeChargesOutstanding` REAL, `feeChargesOverdue` REAL, `penaltyChargesCharged` REAL, `penaltyChargesPaid` REAL, `penaltyChargesWaived` REAL, `penaltyChargesWrittenOff` REAL, `penaltyChargesOutstanding` REAL, `penaltyChargesOverdue` REAL, `totalExpectedRepayment` REAL, `totalRepayment` REAL, `totalExpectedCostOfLoan` REAL, `totalCostOfLoan` REAL, `totalWaived` REAL, `totalWrittenOff` REAL, `totalOutstanding` REAL, `totalOverdue` REAL, `overdueSinceDate` TEXT, `code` TEXT, `name` TEXT, `decimalPlaces` REAL, `inMultiplesOf` INTEGER, `displaySymbol` TEXT, `nameCode` TEXT, `displayLabel` TEXT, PRIMARY KEY(`loanId`))", + "fields": [ + { + "fieldPath": "loanId", + "columnName": "loanId", + "affinity": "INTEGER" + }, + { + "fieldPath": "principalDisbursed", + "columnName": "principalDisbursed", + "affinity": "REAL" + }, + { + "fieldPath": "principalPaid", + "columnName": "principalPaid", + "affinity": "REAL" + }, + { + "fieldPath": "principalWrittenOff", + "columnName": "principalWrittenOff", + "affinity": "REAL" + }, + { + "fieldPath": "principalOutstanding", + "columnName": "principalOutstanding", + "affinity": "REAL" + }, + { + "fieldPath": "principalOverdue", + "columnName": "principalOverdue", + "affinity": "REAL" + }, + { + "fieldPath": "interestCharged", + "columnName": "interestCharged", + "affinity": "REAL" + }, + { + "fieldPath": "interestPaid", + "columnName": "interestPaid", + "affinity": "REAL" + }, + { + "fieldPath": "interestWaived", + "columnName": "interestWaived", + "affinity": "REAL" + }, + { + "fieldPath": "interestWrittenOff", + "columnName": "interestWrittenOff", + "affinity": "REAL" + }, + { + "fieldPath": "interestOutstanding", + "columnName": "interestOutstanding", + "affinity": "REAL" + }, + { + "fieldPath": "interestOverdue", + "columnName": "interestOverdue", + "affinity": "REAL" + }, + { + "fieldPath": "feeChargesCharged", + "columnName": "feeChargesCharged", + "affinity": "REAL" + }, + { + "fieldPath": "feeChargesDueAtDisbursementCharged", + "columnName": "feeChargesDueAtDisbursementCharged", + "affinity": "REAL" + }, + { + "fieldPath": "feeChargesPaid", + "columnName": "feeChargesPaid", + "affinity": "REAL" + }, + { + "fieldPath": "feeChargesWaived", + "columnName": "feeChargesWaived", + "affinity": "REAL" + }, + { + "fieldPath": "feeChargesWrittenOff", + "columnName": "feeChargesWrittenOff", + "affinity": "REAL" + }, + { + "fieldPath": "feeChargesOutstanding", + "columnName": "feeChargesOutstanding", + "affinity": "REAL" + }, + { + "fieldPath": "feeChargesOverdue", + "columnName": "feeChargesOverdue", + "affinity": "REAL" + }, + { + "fieldPath": "penaltyChargesCharged", + "columnName": "penaltyChargesCharged", + "affinity": "REAL" + }, + { + "fieldPath": "penaltyChargesPaid", + "columnName": "penaltyChargesPaid", + "affinity": "REAL" + }, + { + "fieldPath": "penaltyChargesWaived", + "columnName": "penaltyChargesWaived", + "affinity": "REAL" + }, + { + "fieldPath": "penaltyChargesWrittenOff", + "columnName": "penaltyChargesWrittenOff", + "affinity": "REAL" + }, + { + "fieldPath": "penaltyChargesOutstanding", + "columnName": "penaltyChargesOutstanding", + "affinity": "REAL" + }, + { + "fieldPath": "penaltyChargesOverdue", + "columnName": "penaltyChargesOverdue", + "affinity": "REAL" + }, + { + "fieldPath": "totalExpectedRepayment", + "columnName": "totalExpectedRepayment", + "affinity": "REAL" + }, + { + "fieldPath": "totalRepayment", + "columnName": "totalRepayment", + "affinity": "REAL" + }, + { + "fieldPath": "totalExpectedCostOfLoan", + "columnName": "totalExpectedCostOfLoan", + "affinity": "REAL" + }, + { + "fieldPath": "totalCostOfLoan", + "columnName": "totalCostOfLoan", + "affinity": "REAL" + }, + { + "fieldPath": "totalWaived", + "columnName": "totalWaived", + "affinity": "REAL" + }, + { + "fieldPath": "totalWrittenOff", + "columnName": "totalWrittenOff", + "affinity": "REAL" + }, + { + "fieldPath": "totalOutstanding", + "columnName": "totalOutstanding", + "affinity": "REAL" + }, + { + "fieldPath": "totalOverdue", + "columnName": "totalOverdue", + "affinity": "REAL" + }, + { + "fieldPath": "overdueSinceDate", + "columnName": "overdueSinceDate", + "affinity": "TEXT" + }, + { + "fieldPath": "currency.code", + "columnName": "code", + "affinity": "TEXT" + }, + { + "fieldPath": "currency.name", + "columnName": "name", + "affinity": "TEXT" + }, + { + "fieldPath": "currency.decimalPlaces", + "columnName": "decimalPlaces", + "affinity": "REAL" + }, + { + "fieldPath": "currency.inMultiplesOf", + "columnName": "inMultiplesOf", + "affinity": "INTEGER" + }, + { + "fieldPath": "currency.displaySymbol", + "columnName": "displaySymbol", + "affinity": "TEXT" + }, + { + "fieldPath": "currency.nameCode", + "columnName": "nameCode", + "affinity": "TEXT" + }, + { + "fieldPath": "currency.displayLabel", + "columnName": "displayLabel", + "affinity": "TEXT" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "loanId" + ] + } + } + ], + "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, 'be50121d7afe08c6200f3cea98d17b8a')" + ] + } +} \ No newline at end of file diff --git a/core/database/src/main/java/com/mifos/core/databasehelper/DatabaseHelperLoan.kt b/core/database/src/main/java/com/mifos/core/databasehelper/DatabaseHelperLoan.kt index f38346323e..d58825e8cf 100644 --- a/core/database/src/main/java/com/mifos/core/databasehelper/DatabaseHelperLoan.kt +++ b/core/database/src/main/java/com/mifos/core/databasehelper/DatabaseHelperLoan.kt @@ -16,8 +16,8 @@ import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.entity.accounts.loan.LoanWithAssociations_Table import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate_Table -import com.mifos.core.objects.account.loan.LoanRepaymentResponse import com.mifos.room.entities.PaymentTypeOption +import com.mifos.room.entities.accounts.loans.LoanRepaymentResponse import com.raizlabs.android.dbflow.sql.language.Delete import com.raizlabs.android.dbflow.sql.language.SQLite import rx.Observable diff --git a/core/database/src/main/java/com/mifos/core/entity/accounts/loan/LoanApprovalData.kt b/core/database/src/main/java/com/mifos/core/entity/accounts/loan/LoanApprovalData.kt index c55ce68001..7d7def7ed3 100644 --- a/core/database/src/main/java/com/mifos/core/entity/accounts/loan/LoanApprovalData.kt +++ b/core/database/src/main/java/com/mifos/core/entity/accounts/loan/LoanApprovalData.kt @@ -10,6 +10,7 @@ package com.mifos.core.entity.accounts.loan import android.os.Parcelable +import com.mifos.room.entities.accounts.loans.LoanWithAssociations import kotlinx.parcelize.Parcelize /** diff --git a/core/database/src/main/java/com/mifos/room/dao/LoanDao.kt b/core/database/src/main/java/com/mifos/room/dao/LoanDao.kt new file mode 100644 index 0000000000..f126141fef --- /dev/null +++ b/core/database/src/main/java/com/mifos/room/dao/LoanDao.kt @@ -0,0 +1,58 @@ +/* + * 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 androidx.room.Update +import com.mifos.room.entities.PaymentTypeOption +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest +import com.mifos.room.entities.accounts.loans.LoanWithAssociations +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate +import kotlinx.coroutines.flow.Flow + +@Dao +interface LoanDao { + + @Insert(onConflict = OnConflictStrategy.REPLACE) + suspend fun saveLoanWithAssociations(loanWithAssociations: LoanWithAssociations) + + @Query("SELECT * FROM LoanWithAssociations WHERE id = :loanId LIMIT 1") + fun getLoanById(loanId: Int): Flow + + @Insert(onConflict = OnConflictStrategy.REPLACE) + suspend fun insertLoanRepaymentTransaction(request: LoanRepaymentRequest) + + @Insert(onConflict = OnConflictStrategy.REPLACE) + suspend fun insertPaymentTypeOption(paymentTypeOption: PaymentTypeOption) + + @Query("SELECT * FROM PaymentTypeOption") + fun getPaymentTypeOptions(): Flow> + + @Query("SELECT * FROM LoanRepaymentRequest WHERE loanId = :loanId LIMIT 1") + suspend fun getLoanRepaymentRequest(loanId: Int): LoanRepaymentRequest? + + @Update + suspend fun updateLoanRepaymentRequest(loanRepaymentRequest: LoanRepaymentRequest) + + @Query("SELECT * FROM LoanRepaymentRequest ORDER BY timeStamp ASC") + fun readAllLoanRepaymentTransaction(): Flow> + + @Insert(onConflict = OnConflictStrategy.REPLACE) + suspend fun insertLoanRepaymentTemplate(template: LoanRepaymentTemplate) + + @Query("SELECT * FROM LoanRepaymentTemplate WHERE loanId = :loanId LIMIT 1") + fun getLoanRepaymentTemplate(loanId: Int): LoanRepaymentTemplate? + + @Query("DELETE FROM LoanRepaymentTemplate WHERE loanId = :loanId") + suspend fun deleteLoanRepaymentByLoanId(loanId: Int) +} diff --git a/core/database/src/main/java/com/mifos/room/db/MifosDatabase.kt b/core/database/src/main/java/com/mifos/room/db/MifosDatabase.kt index 5169011f29..ee9fa0271d 100644 --- a/core/database/src/main/java/com/mifos/room/db/MifosDatabase.kt +++ b/core/database/src/main/java/com/mifos/room/db/MifosDatabase.kt @@ -13,23 +13,50 @@ import androidx.room.Database import androidx.room.RoomDatabase import androidx.room.TypeConverters import com.mifos.room.dao.ColumnValueDao +import com.mifos.room.dao.LoanDao +import com.mifos.room.entities.PaymentTypeOption +import com.mifos.room.entities.accounts.loans.ActualDisbursementDate +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest +import com.mifos.room.entities.accounts.loans.LoanWithAssociations +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.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 @Database( // [TODO -> add other entities ] - entities = [ColumnValue::class], + entities = [ + ColumnValue::class, + // loan + LoanWithAssociations::class, + LoanRepaymentRequest::class, + LoanRepaymentTemplate::class, + PaymentTypeOption::class, + ActualDisbursementDate::class, + Timeline::class, + Status::class, + Summary::class, + ], version = MifosDatabase.VERSION, exportSchema = true, autoMigrations = [], ) -@TypeConverters(ListTypeConverters::class, ServerTypesConverters::class, DueDateConverter::class) +@TypeConverters( + ListTypeConverters::class, + ServerTypesConverters::class, + DueDateConverter::class, + LoanTypeConverters::class, +) // ( TODO -> add type converters here ) abstract class MifosDatabase : RoomDatabase() { abstract fun columnValueDao(): ColumnValueDao + abstract fun loanDao(): LoanDao companion object { const val VERSION = 1 diff --git a/core/database/src/main/java/com/mifos/room/di/DaoModule.kt b/core/database/src/main/java/com/mifos/room/di/DaoModule.kt index ec8c9ade34..204fbedfbd 100644 --- a/core/database/src/main/java/com/mifos/room/di/DaoModule.kt +++ b/core/database/src/main/java/com/mifos/room/di/DaoModule.kt @@ -10,6 +10,7 @@ package com.mifos.room.di import com.mifos.room.dao.ColumnValueDao +import com.mifos.room.dao.LoanDao import com.mifos.room.db.MifosDatabase import dagger.Module import dagger.Provides @@ -23,4 +24,9 @@ object DaoModule { fun providesColumnValueDao(database: MifosDatabase): ColumnValueDao { return database.columnValueDao() } + + @Provides + fun providesLoanDao(database: MifosDatabase): LoanDao { + return database.loanDao() + } } diff --git a/core/database/src/main/java/com/mifos/room/entities/PaymentTypeOption.kt b/core/database/src/main/java/com/mifos/room/entities/PaymentTypeOption.kt index 553ff1db94..ed4222fd7f 100644 --- a/core/database/src/main/java/com/mifos/room/entities/PaymentTypeOption.kt +++ b/core/database/src/main/java/com/mifos/room/entities/PaymentTypeOption.kt @@ -11,40 +11,27 @@ package com.mifos.room.entities import android.os.Parcel import android.os.Parcelable -import com.google.gson.annotations.SerializedName -import com.mifos.core.database.MifosDatabase +import androidx.room.Entity +import androidx.room.PrimaryKey import com.mifos.core.model.MifosBaseModel -import com.raizlabs.android.dbflow.annotation.Column -import com.raizlabs.android.dbflow.annotation.ModelContainer -import com.raizlabs.android.dbflow.annotation.PrimaryKey -import com.raizlabs.android.dbflow.annotation.Table -@Table(database = MifosDatabase::class) -@ModelContainer +@Entity(tableName = "PaymentTypeOption") data class PaymentTypeOption( - @SerializedName("id") @PrimaryKey - var id: Int = 0, + val id: Int = 0, - @SerializedName("name") - @Column - var name: String = "", + val name: String = "", - @SerializedName("description") - @Column - var description: String? = null, + val description: String? = null, - @SerializedName("isCashPayment") - @Column - var isCashPayment: Boolean? = null, + val isCashPayment: Boolean? = null, + + val position: Int? = null, - @SerializedName("position") - @Column - var position: Int? = null, ) : MifosBaseModel(), Comparable, Parcelable { - override fun compareTo(another: PaymentTypeOption): Int { - return position?.compareTo(another.position ?: 0) ?: 0 + override fun compareTo(other: PaymentTypeOption): Int { + return position?.compareTo(other.position ?: 0) ?: 0 } override fun toString(): String { diff --git a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/ActualDisbursementDate.kt b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/ActualDisbursementDate.kt index 80d26995f8..df5cab4fc0 100644 --- a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/ActualDisbursementDate.kt +++ b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/ActualDisbursementDate.kt @@ -9,21 +9,17 @@ */ package com.mifos.room.entities.accounts.loans -import androidx.room.ColumnInfo +import android.os.Parcelable import androidx.room.Entity import androidx.room.PrimaryKey +import kotlinx.parcelize.Parcelize @Entity +@Parcelize data class ActualDisbursementDate( @PrimaryKey - var loanId: Int? = null, - - @ColumnInfo(name = "year") - var year: Int? = null, - - @ColumnInfo(name = "month") - var month: Int? = null, - - @ColumnInfo(name = "date") - var date: Int? = null, -) + val loanId: Int? = null, + val year: Int? = null, + val month: Int? = null, + val date: Int? = null, +) : Parcelable diff --git a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Loan.kt b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Loan.kt index c6abb28800..246abf8481 100644 --- a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Loan.kt +++ b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Loan.kt @@ -22,87 +22,87 @@ import com.mifos.core.objects.account.loan.RepaymentFrequencyType import com.mifos.core.objects.account.loan.TermPeriodFrequencyType data class Loan( - var id: Int? = null, + val id: Int? = null, - var accountNo: String? = null, + val accountNo: String? = null, - var status: Status? = null, + val status: Status? = null, - var clientId: Int? = null, + val clientId: Int? = null, - var clientName: String? = null, + val clientName: String? = null, - var clientOfficeId: Int? = null, + val clientOfficeId: Int? = null, - var loanProductId: Int? = null, + val loanProductId: Int? = null, - var loanProductName: String? = null, + val loanProductName: String? = null, - var loanProductDescription: String? = null, + val loanProductDescription: String? = null, - var fundId: Int? = null, + val fundId: Int? = null, - var fundName: String? = null, + val fundName: String? = null, - var loanOfficerId: Int? = null, + val loanOfficerId: Int? = null, - var loanOfficerName: String? = null, + val loanOfficerName: String? = null, - var loanType: LoanType? = null, + val loanType: LoanType? = null, - var currency: Currency? = null, + val currency: Currency? = null, - var principal: Double? = null, + val principal: Double? = null, - var approvedPrincipal: Double? = null, + val approvedPrincipal: Double? = null, - var termFrequency: Int? = null, + val termFrequency: Int? = null, - var termPeriodFrequencyType: TermPeriodFrequencyType? = null, + val termPeriodFrequencyType: TermPeriodFrequencyType? = null, - var numberOfRepayments: Int? = null, + val numberOfRepayments: Int? = null, - var repaymentEvery: Int? = null, + val repaymentEvery: Int? = null, - var repaymentFrequencyType: RepaymentFrequencyType? = null, + val repaymentFrequencyType: RepaymentFrequencyType? = null, - var interestRatePerPeriod: Double? = null, + val interestRatePerPeriod: Double? = null, - var interestRateFrequencyType: InterestRateFrequencyType? = null, + val interestRateFrequencyType: InterestRateFrequencyType? = null, - var annualInterestRate: Double? = null, + val annualInterestRate: Double? = null, - var amortizationType: AmortizationType? = null, + val amortizationType: AmortizationType? = null, - var interestType: InterestType? = null, + val interestType: InterestType? = null, - var interestCalculationPeriodType: InterestCalculationPeriodType? = null, + val interestCalculationPeriodType: InterestCalculationPeriodType? = null, - var transactionProcessingStrategyId: Int? = null, + val transactionProcessingStrategyId: Int? = null, - var transactionProcessingStrategyName: String? = null, + val transactionProcessingStrategyName: String? = null, - var syncDisbursementWithMeeting: Boolean? = null, + val syncDisbursementWithMeeting: Boolean? = null, - var timeline: Timeline? = null, + val timeline: Timeline? = null, - var summary: Summary? = null, + val summary: Summary? = null, - var feeChargesAtDisbursementCharged: Double? = null, + val feeChargesAtDisbursementCharged: Double? = null, - var loanCounter: Int? = null, + val loanCounter: Int? = null, - var loanProductCounter: Int? = null, + val loanProductCounter: Int? = null, - var multiDisburseLoan: Boolean? = null, + val multiDisburseLoan: Boolean? = null, - var canDisburse: Boolean? = null, + val canDisburse: Boolean? = null, - var inArrears: Boolean? = null, + val inArrears: Boolean? = null, - var isNPA: Boolean? = null, + val isNPA: Boolean? = null, - var overdueCharges: List = ArrayList(), + val overdueCharges: List = emptyList(), - private val additionalProperties: MutableMap = HashMap(), + private val additionalProperties: MutableMap = mutableMapOf(), ) diff --git a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanAccount.kt b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanAccount.kt index c51c60b92b..d4dda2049d 100644 --- a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanAccount.kt +++ b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanAccount.kt @@ -32,38 +32,29 @@ import androidx.room.PrimaryKey ) data class LoanAccount( @PrimaryKey - var id: Int? = null, + val id: Int? = null, - @ColumnInfo(name = "clientId") - var clientId: Long = 0, + val clientId: Long = 0, - @ColumnInfo(name = "groupId") - var groupId: Long = 0, + val groupId: Long = 0, - @ColumnInfo(name = "centerId") - var centerId: Long = 0, + val centerId: Long = 0, - @ColumnInfo(name = "accountNo") - var accountNo: String? = null, + val accountNo: String? = null, - @ColumnInfo(name = "externalId") - var externalId: String? = null, + val externalId: String? = null, - @ColumnInfo(name = "productId") - var productId: Int? = null, + val productId: Int? = null, - @ColumnInfo(name = "productName") - var productName: String? = null, + val productName: String? = null, - @ColumnInfo(name = "status", index = true) - var status: Status? = null, + @ColumnInfo(index = true) + val status: Status? = null, - @ColumnInfo(name = "loanType", index = true) - var loanType: LoanType? = null, + @ColumnInfo(index = true) + val loanType: LoanType? = null, - @ColumnInfo(name = "loanCycle") - var loanCycle: Int? = null, + val loanCycle: Int? = null, - @ColumnInfo(name = "inArrears") - var inArrears: Boolean? = null, + val inArrears: Boolean? = null, ) diff --git a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanRepaymentRequest.kt b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanRepaymentRequest.kt index 7498955242..b6a5109934 100644 --- a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanRepaymentRequest.kt +++ b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanRepaymentRequest.kt @@ -18,51 +18,51 @@ data class LoanRepaymentRequest( // TODO(check this out) // @PrimaryKey(autoGenerate = true) -// var id: Int = 0, +// val id: Int = 0, @ColumnInfo(name = "timeStamp") @PrimaryKey @Transient - var timeStamp: Long = 0, + val timeStamp: Long = 0, @ColumnInfo(name = "loanId") @Transient - var loanId: Int? = null, + val loanId: Int? = null, @ColumnInfo(name = "errorMessage") @Transient - var errorMessage: String? = null, + val errorMessage: String? = null, @ColumnInfo(name = "dateFormat") - var dateFormat: String? = null, + val dateFormat: String? = null, @ColumnInfo(name = "locale") - var locale: String? = null, + val locale: String? = null, @ColumnInfo(name = "transactionDate") - var transactionDate: String? = null, + val transactionDate: String? = null, @ColumnInfo(name = "transactionAmount") - var transactionAmount: String? = null, + val transactionAmount: String? = null, @ColumnInfo(name = "paymentTypeId") - var paymentTypeId: String? = null, + val paymentTypeId: String? = null, @ColumnInfo(name = "note") - var note: String? = null, + val note: String? = null, @ColumnInfo(name = "accountNumber") - var accountNumber: String? = null, + val accountNumber: String? = null, @ColumnInfo(name = "checkNumber") - var checkNumber: String? = null, + val checkNumber: String? = null, @ColumnInfo(name = "routingCode") - var routingCode: String? = null, + val routingCode: String? = null, @ColumnInfo(name = "receiptNumber") - var receiptNumber: String? = null, + val receiptNumber: String? = null, @ColumnInfo(name = "bankNumber") - var bankNumber: String? = null, + val bankNumber: String? = null, ) diff --git a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanRepaymentResponse.kt b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanRepaymentResponse.kt new file mode 100644 index 0000000000..87641bb4bc --- /dev/null +++ b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanRepaymentResponse.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2024 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.accounts.loans + +import android.os.Parcelable +import androidx.room.Entity +import androidx.room.PrimaryKey +import com.mifos.core.objects.Changes +import kotlinx.parcelize.Parcelize + +@Entity(tableName = "LoanRepaymentResponse") +@Parcelize +data class LoanRepaymentResponse( + @PrimaryKey(autoGenerate = true) + val id: Int = 0, + val officeId: Int? = null, + val clientId: Int? = null, + val loanId: Int? = null, + val resourceId: Int? = null, + val changes: Changes? = null, +) : Parcelable diff --git a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanType.kt b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanType.kt index 726a590d6d..ba766066a9 100644 --- a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanType.kt +++ b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanType.kt @@ -9,18 +9,16 @@ */ package com.mifos.room.entities.accounts.loans -import androidx.room.ColumnInfo +import android.os.Parcelable import androidx.room.Entity -import androidx.room.PrimaryKey +import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable @Entity(tableName = "LoanAccountLoanType") +@Serializable +@Parcelize data class LoanType( - @PrimaryKey - var id: Int? = null, - - @ColumnInfo("code") - var code: String? = null, - - @ColumnInfo("value") - var value: String? = null, -) + val id: Int? = null, + val code: String? = null, + val value: String? = null, +) : Parcelable diff --git a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanWithAssociations.kt b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanWithAssociations.kt index 1c39acae61..ba3d4a7973 100644 --- a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanWithAssociations.kt +++ b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/LoanWithAssociations.kt @@ -9,6 +9,7 @@ */ package com.mifos.room.entities.accounts.loans +import android.os.Parcelable import androidx.room.ColumnInfo import androidx.room.Embedded import androidx.room.Entity @@ -23,7 +24,23 @@ import com.mifos.core.objects.account.loan.RepaymentFrequencyType import com.mifos.core.objects.account.loan.RepaymentSchedule import com.mifos.core.objects.account.loan.TermPeriodFrequencyType import com.mifos.core.objects.account.loan.Transaction +import kotlinx.parcelize.Parcelize +// @TypeConverters( +// AmortizationTypeConverter::class, +// CurrencyTypeConverter::class, +// InterestCalculationPeriodTypeConverter::class, +// InterestRateFrequencyTypeConverter::class, +// InterestTypeConverter::class, +// LoanTypeConverter::class, +// RepaymentFrequencyTypeConverter::class, +// RepaymentScheduleTypeConverter::class, +// TermPeriodFrequencyTypeConverter::class, +// TimelineTypeConverter::class, +// TransactionListConverter::class, +// ) + +@Parcelize @Entity( tableName = "LoanWithAssociations", foreignKeys = [ @@ -41,158 +58,113 @@ import com.mifos.core.objects.account.loan.Transaction ), ForeignKey( entity = Summary::class, - parentColumns = ["savings"], + parentColumns = ["loanId"], childColumns = ["summary"], onDelete = ForeignKey.CASCADE, ), ], ) -// @TypeConverters( -// AmortizationTypeConverter::class, -// CurrencyTypeConverter::class, -// InterestCalculationPeriodTypeConverter::class, -// InterestRateFrequencyTypeConverter::class, -// InterestTypeConverter::class, -// LoanTypeConverter::class, -// RepaymentFrequencyTypeConverter::class, -// RepaymentScheduleTypeConverter::class, -// TermPeriodFrequencyTypeConverter::class, -// TimelineTypeConverter::class, -// TransactionListConverter::class, -// ) data class LoanWithAssociations( - @PrimaryKey - var id: Int = 0, + @PrimaryKey(autoGenerate = true) + val id: Int = 0, - @ColumnInfo(name = "accountNo") - var accountNo: String = "", + val accountNo: String = "", - @ColumnInfo(name = "status", index = true) - var status: Status = Status(), + @ColumnInfo(index = true) + val status: Status = Status(), - @ColumnInfo(name = "clientId") - var clientId: Int = 0, + val clientId: Int = 0, - @ColumnInfo(name = "clientName") - var clientName: String = "", + val clientName: String = "", - @ColumnInfo(name = "clientOfficeId") - var clientOfficeId: Int = 0, + val clientOfficeId: Int = 0, - @ColumnInfo(name = "loanProductId") - var loanProductId: Int = 0, + val loanProductId: Int = 0, - @ColumnInfo(name = "loanProductName") - var loanProductName: String = "", + val loanProductName: String = "", - @ColumnInfo(name = "loanProductDescription") - var loanProductDescription: String = "", + val loanProductDescription: String = "", - @ColumnInfo(name = "fundId") - var fundId: Int = 0, + val fundId: Int = 0, - @ColumnInfo(name = "fundName") - var fundName: String = "", + val fundName: String = "", - @ColumnInfo(name = "loanPurposeId") - var loanPurposeId: Int = 0, + val loanPurposeId: Int = 0, - @ColumnInfo(name = "loanPurposeName") - var loanPurposeName: String = "", + val loanPurposeName: String = "", - @ColumnInfo(name = "loanOfficerId") - var loanOfficerId: Int = 0, + val loanOfficerId: Int = 0, - @ColumnInfo(name = "loanOfficerName") - var loanOfficerName: String = "", + val loanOfficerName: String = "", - @Embedded - var loanType: LoanType = LoanType(), + @Embedded(prefix = "LoanType_") + val loanType: LoanType = LoanType(), - @Embedded - var currency: Currency = Currency(), + @Embedded(prefix = "Currency_") + val currency: Currency = Currency(), - @ColumnInfo(name = "principal") - var principal: Double = 0.0, + val principal: Double = 0.0, - @ColumnInfo(name = "approvedPrincipal") - var approvedPrincipal: Double = 0.0, + val approvedPrincipal: Double = 0.0, - @ColumnInfo(name = "termFrequency") - var termFrequency: Int = 0, + val termFrequency: Int = 0, - @Embedded - var termPeriodFrequencyType: TermPeriodFrequencyType = TermPeriodFrequencyType(), + @Embedded(prefix = "termPeriodFrequencyType_") + val termPeriodFrequencyType: TermPeriodFrequencyType = TermPeriodFrequencyType(), - @ColumnInfo(name = "numberOfRepayments") - var numberOfRepayments: Int = 0, + val numberOfRepayments: Int = 0, - @ColumnInfo(name = "repaymentEvery") - var repaymentEvery: Int = 0, + val repaymentEvery: Int = 0, - @Embedded - var repaymentFrequencyType: RepaymentFrequencyType = RepaymentFrequencyType(), + @Embedded(prefix = "repaymentFrequencyType_") + val repaymentFrequencyType: RepaymentFrequencyType = RepaymentFrequencyType(), - @ColumnInfo(name = "interestRatePerPeriod") - var interestRatePerPeriod: Double = 0.0, + val interestRatePerPeriod: Double = 0.0, - @Embedded - var interestRateFrequencyType: InterestRateFrequencyType = InterestRateFrequencyType(), + @Embedded(prefix = "interestRateFrequencyType_") + val interestRateFrequencyType: InterestRateFrequencyType = InterestRateFrequencyType(), - @ColumnInfo(name = "annualInterestRate") - var annualInterestRate: Double = 0.0, + val annualInterestRate: Double = 0.0, - @Embedded - var amortizationType: AmortizationType = AmortizationType(), + @Embedded(prefix = "amortization_type_") + val amortizationType: AmortizationType = AmortizationType(), - @Embedded - var interestType: InterestType = InterestType(), + @Embedded(prefix = "interestType_") + val interestType: InterestType = InterestType(), - @Embedded - var interestCalculationPeriodType: InterestCalculationPeriodType = InterestCalculationPeriodType(), + @Embedded(prefix = "interestCalculationPeriodType_") + val interestCalculationPeriodType: InterestCalculationPeriodType = InterestCalculationPeriodType(), - @ColumnInfo(name = "transactionProcessingStrategyId") - var transactionProcessingStrategyId: Int = 0, + val transactionProcessingStrategyId: Int = 0, - @ColumnInfo(name = "transactionProcessingStrategyName") - var transactionProcessingStrategyName: String = "", + val transactionProcessingStrategyName: String = "", - @ColumnInfo(name = "syncDisbursementWithMeeting") - var syncDisbursementWithMeeting: Boolean = false, + val syncDisbursementWithMeeting: Boolean = false, - @ColumnInfo(name = "timeline", index = true) - var timeline: Timeline = Timeline(), + @ColumnInfo(index = true) + val timeline: Timeline = Timeline(), - @ColumnInfo(name = "summary", index = true) - var summary: Summary = Summary(), + @ColumnInfo(index = true) + val summary: Summary = Summary(), - @Embedded - var repaymentSchedule: RepaymentSchedule = RepaymentSchedule(), + @Embedded(prefix = "repaymentSchedule_") + val repaymentSchedule: RepaymentSchedule = RepaymentSchedule(), - @ColumnInfo(name = "transactions") - var transactions: List = ArrayList(), + val transactions: List = emptyList(), - @ColumnInfo(name = "feeChargesAtDisbursementCharged") - var feeChargesAtDisbursementCharged: Double = 0.0, + val feeChargesAtDisbursementCharged: Double = 0.0, - @ColumnInfo(name = "totalOverpaid") - var totalOverpaid: Double = 0.0, + val totalOverpaid: Double = 0.0, - @ColumnInfo(name = "loanCounter") - var loanCounter: Int = 0, + val loanCounter: Int = 0, - @ColumnInfo(name = "loanProductCounter") - var loanProductCounter: Int = 0, + val loanProductCounter: Int = 0, - @ColumnInfo(name = "multiDisburseLoan") - var multiDisburseLoan: Boolean = false, + val multiDisburseLoan: Boolean = false, - @ColumnInfo(name = "canDisburse") - var canDisburse: Boolean = false, + val canDisburse: Boolean = false, - @ColumnInfo(name = "inArrears") - var inArrears: Boolean = false, + val inArrears: Boolean = false, - @ColumnInfo(name = "isNPA") - var isNPA: Boolean = false, -) + val isNPA: Boolean = false, +) : Parcelable diff --git a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Status.kt b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Status.kt index f6d427df46..a0d4a368ab 100644 --- a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Status.kt +++ b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Status.kt @@ -9,42 +9,36 @@ */ package com.mifos.room.entities.accounts.loans -import androidx.room.ColumnInfo +import android.os.Parcelable import androidx.room.Entity import androidx.room.PrimaryKey +import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable @Entity(tableName = "LoanStatus") +@Serializable +@Parcelize data class Status( @PrimaryKey - var id: Int? = null, + val id: Int? = null, - @ColumnInfo(name = "code") - var code: String? = null, + val code: String? = null, - @ColumnInfo(name = "value") - var value: String? = null, + val value: String? = null, - @ColumnInfo(name = "pendingApproval") - var pendingApproval: Boolean? = null, + val pendingApproval: Boolean? = null, - @ColumnInfo(name = "waitingForDisbursal") - var waitingForDisbursal: Boolean? = null, + val waitingForDisbursal: Boolean? = null, - @ColumnInfo(name = "active") - var active: Boolean? = null, + val active: Boolean? = null, - @ColumnInfo(name = "closedObligationsMet") - var closedObligationsMet: Boolean? = null, + val closedObligationsMet: Boolean? = null, - @ColumnInfo(name = "closedWrittenOff") - var closedWrittenOff: Boolean? = null, + val closedWrittenOff: Boolean? = null, - @ColumnInfo(name = "closedRescheduled") - var closedRescheduled: Boolean? = null, + val closedRescheduled: Boolean? = null, - @ColumnInfo(name = "closed") - var closed: Boolean? = null, + val closed: Boolean? = null, - @ColumnInfo(name = "overpaid") - var overpaid: Boolean? = null, -) + val overpaid: Boolean? = null, +) : Parcelable diff --git a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Summary.kt b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Summary.kt index 9db95a472b..344690000d 100644 --- a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Summary.kt +++ b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Summary.kt @@ -9,116 +9,121 @@ */ package com.mifos.room.entities.accounts.loans +import android.os.Parcelable import androidx.room.ColumnInfo import androidx.room.Embedded import androidx.room.Entity import androidx.room.PrimaryKey import com.mifos.core.objects.account.loan.Currency +import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable @Entity(tableName = "LoansAccountSummary") +@Serializable +@Parcelize data class Summary( @PrimaryKey - var loanId: Int? = null, + val loanId: Int? = null, @Embedded - var currency: Currency? = null, + val currency: Currency? = null, @ColumnInfo(name = "principalDisbursed") - var principalDisbursed: Double? = null, + val principalDisbursed: Double? = null, @ColumnInfo(name = "principalPaid") - var principalPaid: Double? = null, + val principalPaid: Double? = null, @ColumnInfo(name = "principalWrittenOff") - var principalWrittenOff: Double? = null, + val principalWrittenOff: Double? = null, @ColumnInfo(name = "principalOutstanding") - var principalOutstanding: Double? = null, + val principalOutstanding: Double? = null, @ColumnInfo(name = "principalOverdue") - var principalOverdue: Double? = null, + val principalOverdue: Double? = null, @ColumnInfo(name = "interestCharged") - var interestCharged: Double? = null, + val interestCharged: Double? = null, @ColumnInfo(name = "interestPaid") - var interestPaid: Double? = null, + val interestPaid: Double? = null, @ColumnInfo(name = "interestWaived") - var interestWaived: Double? = null, + val interestWaived: Double? = null, @ColumnInfo(name = "interestWrittenOff") - var interestWrittenOff: Double? = null, + val interestWrittenOff: Double? = null, @ColumnInfo(name = "interestOutstanding") - var interestOutstanding: Double? = null, + val interestOutstanding: Double? = null, @ColumnInfo(name = "interestOverdue") - var interestOverdue: Double? = null, + val interestOverdue: Double? = null, @ColumnInfo(name = "feeChargesCharged") - var feeChargesCharged: Double? = null, + val feeChargesCharged: Double? = null, @ColumnInfo(name = "feeChargesDueAtDisbursementCharged") - var feeChargesDueAtDisbursementCharged: Double? = null, + val feeChargesDueAtDisbursementCharged: Double? = null, @ColumnInfo(name = "feeChargesPaid") - var feeChargesPaid: Double? = null, + val feeChargesPaid: Double? = null, @ColumnInfo(name = "feeChargesWaived") - var feeChargesWaived: Double? = null, + val feeChargesWaived: Double? = null, @ColumnInfo(name = "feeChargesWrittenOff") - var feeChargesWrittenOff: Double? = null, + val feeChargesWrittenOff: Double? = null, @ColumnInfo(name = "feeChargesOutstanding") - var feeChargesOutstanding: Double? = null, + val feeChargesOutstanding: Double? = null, @ColumnInfo(name = "feeChargesOverdue") - var feeChargesOverdue: Double? = null, + val feeChargesOverdue: Double? = null, @ColumnInfo(name = "penaltyChargesCharged") - var penaltyChargesCharged: Double? = null, + val penaltyChargesCharged: Double? = null, @ColumnInfo(name = "penaltyChargesPaid") - var penaltyChargesPaid: Double? = null, + val penaltyChargesPaid: Double? = null, @ColumnInfo(name = "penaltyChargesWaived") - var penaltyChargesWaived: Double? = null, + val penaltyChargesWaived: Double? = null, @ColumnInfo(name = "penaltyChargesWrittenOff") - var penaltyChargesWrittenOff: Double? = null, + val penaltyChargesWrittenOff: Double? = null, @ColumnInfo(name = "penaltyChargesOutstanding") - var penaltyChargesOutstanding: Double? = null, + val penaltyChargesOutstanding: Double? = null, @ColumnInfo(name = "penaltyChargesOverdue") - var penaltyChargesOverdue: Double? = null, + val penaltyChargesOverdue: Double? = null, @ColumnInfo(name = "totalExpectedRepayment") - var totalExpectedRepayment: Double? = null, + val totalExpectedRepayment: Double? = null, @ColumnInfo(name = "totalRepayment") - var totalRepayment: Double? = null, + val totalRepayment: Double? = null, @ColumnInfo(name = "totalExpectedCostOfLoan") - var totalExpectedCostOfLoan: Double? = null, + val totalExpectedCostOfLoan: Double? = null, @ColumnInfo(name = "totalCostOfLoan") - var totalCostOfLoan: Double? = null, + val totalCostOfLoan: Double? = null, @ColumnInfo(name = "totalWaived") - var totalWaived: Double? = null, + val totalWaived: Double? = null, @ColumnInfo(name = "totalWrittenOff") - var totalWrittenOff: Double? = null, + val totalWrittenOff: Double? = null, @ColumnInfo(name = "totalOutstanding") - var totalOutstanding: Double? = null, + val totalOutstanding: Double? = null, @ColumnInfo(name = "totalOverdue") - var totalOverdue: Double? = null, + val totalOverdue: Double? = null, @ColumnInfo(name = "overdueSinceDate") - var overdueSinceDate: List? = null, -) + val overdueSinceDate: List? = null, +) : Parcelable diff --git a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Timeline.kt b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Timeline.kt index b5a90b8920..4867a02a97 100644 --- a/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Timeline.kt +++ b/core/database/src/main/java/com/mifos/room/entities/accounts/loans/Timeline.kt @@ -9,10 +9,13 @@ */ package com.mifos.room.entities.accounts.loans +import android.os.Parcelable import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.ForeignKey import androidx.room.PrimaryKey +import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable import kotlinx.serialization.Transient @Entity( @@ -26,58 +29,45 @@ import kotlinx.serialization.Transient ), ], ) +@Serializable +@Parcelize data class Timeline( @PrimaryKey @Transient - var loanId: Int? = null, + val loanId: Int? = null, - @ColumnInfo(name = "submittedOnDate") - var submittedOnDate: List? = null, + val submittedOnDate: List? = null, - @ColumnInfo(name = "submittedByUsername") - var submittedByUsername: String? = null, + val submittedByUsername: String? = null, - @ColumnInfo(name = "submittedByFirstname") - var submittedByFirstname: String? = null, + val submittedByFirstname: String? = null, - @ColumnInfo(name = "submittedByLastname") - var submittedByLastname: String? = null, + val submittedByLastname: String? = null, - @ColumnInfo(name = "approvedOnDate") - var approvedOnDate: List? = null, + val approvedOnDate: List? = null, - @ColumnInfo(name = "approvedByUsername") - var approvedByUsername: String? = null, + val approvedByUsername: String? = null, - @ColumnInfo(name = "approvedByFirstname") - var approvedByFirstname: String? = null, + val approvedByFirstname: String? = null, - @ColumnInfo(name = "approvedByLastname") - var approvedByLastname: String? = null, + val approvedByLastname: String? = null, - @ColumnInfo(name = "expectedDisbursementDate") - var expectedDisbursementDate: List? = null, + val expectedDisbursementDate: List? = null, // todo check if its int - @ColumnInfo(name = "actualDisburseDate", index = true) + @ColumnInfo(index = true) @Transient - var actualDisburseDate: ActualDisbursementDate? = null, + val actualDisburseDate: ActualDisbursementDate? = null, - @ColumnInfo(name = "actualDisbursementDate") - var actualDisbursementDate: List? = null, + val actualDisbursementDate: List? = null, - @ColumnInfo(name = "disbursedByUsername") - var disbursedByUsername: String? = null, + val disbursedByUsername: String? = null, - @ColumnInfo(name = "disbursedByFirstname") - var disbursedByFirstname: String? = null, + val disbursedByFirstname: String? = null, - @ColumnInfo(name = "disbursedByLastname") - var disbursedByLastname: String? = null, + val disbursedByLastname: String? = null, - @ColumnInfo(name = "closedOnDate") - var closedOnDate: List? = null, + val closedOnDate: List? = null, - @ColumnInfo(name = "expectedMaturityDate") - var expectedMaturityDate: List? = null, -) + val expectedMaturityDate: List? = null, +) : Parcelable diff --git a/core/database/src/main/java/com/mifos/room/entities/accounts/savings/Transaction.kt b/core/database/src/main/java/com/mifos/room/entities/accounts/savings/Transaction.kt index 35d9e366de..8ab674bcca 100644 --- a/core/database/src/main/java/com/mifos/room/entities/accounts/savings/Transaction.kt +++ b/core/database/src/main/java/com/mifos/room/entities/accounts/savings/Transaction.kt @@ -15,7 +15,7 @@ import androidx.room.ForeignKey import androidx.room.PrimaryKey @Entity( - tableName = "Transaction", + tableName = "TransactionTable", foreignKeys = [ ForeignKey( entity = TransactionType::class, diff --git a/core/database/src/main/java/com/mifos/room/entities/group/Group.kt b/core/database/src/main/java/com/mifos/room/entities/group/Group.kt index 0ac0729e30..77dd2ecfc8 100644 --- a/core/database/src/main/java/com/mifos/room/entities/group/Group.kt +++ b/core/database/src/main/java/com/mifos/room/entities/group/Group.kt @@ -20,7 +20,7 @@ import kotlinx.parcelize.Parcelize @Parcelize @Entity( - tableName = "Group", + tableName = "GroupTable", foreignKeys = [ ForeignKey( entity = GroupDate::class, diff --git a/core/database/src/main/java/com/mifos/room/entities/organisation/Office.kt b/core/database/src/main/java/com/mifos/room/entities/organisation/Office.kt index 1b13a63b22..cc58574c13 100644 --- a/core/database/src/main/java/com/mifos/room/entities/organisation/Office.kt +++ b/core/database/src/main/java/com/mifos/room/entities/organisation/Office.kt @@ -43,7 +43,7 @@ data class Office( var nameDecorated: String? = null, @ColumnInfo(name = "officeOpeningDate") - var officeOpeningDate: Int? = null, + var officeOpeningDate: OfficeOpeningDate? = null, @ColumnInfo(name = "openingDate") var openingDate: List = ArrayList(), diff --git a/core/database/src/main/java/com/mifos/room/entities/templates/loans/LoanRepaymentTemplate.kt b/core/database/src/main/java/com/mifos/room/entities/templates/loans/LoanRepaymentTemplate.kt index 5fb57c9b08..f3b6945a56 100644 --- a/core/database/src/main/java/com/mifos/room/entities/templates/loans/LoanRepaymentTemplate.kt +++ b/core/database/src/main/java/com/mifos/room/entities/templates/loans/LoanRepaymentTemplate.kt @@ -10,7 +10,6 @@ package com.mifos.room.entities.templates.loans import android.os.Parcelable -import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey import com.mifos.core.objects.template.loan.Currency @@ -22,33 +21,23 @@ import kotlinx.parcelize.Parcelize @Entity(tableName = "LoanRepaymentTemplate") data class LoanRepaymentTemplate( @PrimaryKey(autoGenerate = true) - @ColumnInfo(name = "loanId") - var loanId: Int? = null, + val loanId: Int? = null, - @ColumnInfo(name = "type") - var type: Type? = null, + val type: Type? = null, - @ColumnInfo(name = "date") - var date: MutableList? = null, + val date: MutableList? = null, - @ColumnInfo(name = "currency") - var currency: Currency? = null, + val currency: Currency? = null, - @ColumnInfo(name = "amount") - var amount: Double? = null, + val amount: Double? = null, - @ColumnInfo(name = "principalPortion") - var principalPortion: Double? = null, + val principalPortion: Double? = null, - @ColumnInfo(name = "interestPortion") - var interestPortion: Double? = null, + val interestPortion: Double? = null, - @ColumnInfo(name = "feeChargesPortion") - var feeChargesPortion: Double? = null, + val feeChargesPortion: Double? = null, - @ColumnInfo(name = "penaltyChargesPortion") - var penaltyChargesPortion: Double? = null, + val penaltyChargesPortion: Double? = null, - @ColumnInfo(name = "paymentTypeOptions") - var paymentTypeOptions: MutableList? = null, + val paymentTypeOptions: MutableList? = null, ) : Parcelable diff --git a/core/database/src/main/java/com/mifos/room/entities/templates/loans/LoanTemplate.kt b/core/database/src/main/java/com/mifos/room/entities/templates/loans/LoanTemplate.kt index 793a17df19..c84a4ab932 100644 --- a/core/database/src/main/java/com/mifos/room/entities/templates/loans/LoanTemplate.kt +++ b/core/database/src/main/java/com/mifos/room/entities/templates/loans/LoanTemplate.kt @@ -44,115 +44,115 @@ import kotlinx.parcelize.Parcelize */ @Parcelize data class LoanTemplate( - var clientId: Int? = null, + val clientId: Int? = null, - var clientAccountNo: String? = null, + val clientAccountNo: String? = null, - var clientName: String? = null, + val clientName: String? = null, - var clientOfficeId: Int? = null, + val clientOfficeId: Int? = null, - var loanProductId: Int? = null, + val loanProductId: Int? = null, - var loanProductName: String? = null, + val loanProductName: String? = null, - var isLoanProductLinkedToFloatingRate: Boolean? = null, + val isLoanProductLinkedToFloatingRate: Boolean? = null, - var fundId: Int? = null, + val fundId: Int? = null, - var fundName: String? = null, + val fundName: String? = null, - var currency: Currency? = null, + val currency: Currency? = null, - var principal: Double? = null, + val principal: Double? = null, - var approvedPrincipal: Double? = null, + val approvedPrincipal: Double? = null, - var proposedPrincipal: Double? = null, + val proposedPrincipal: Double? = null, - var termFrequency: Int? = null, + val termFrequency: Int? = null, - var termPeriodFrequencyType: TermPeriodFrequencyType? = null, + val termPeriodFrequencyType: TermPeriodFrequencyType? = null, - var numberOfRepayments: Int? = null, + val numberOfRepayments: Int? = null, - var repaymentEvery: Int? = null, + val repaymentEvery: Int? = null, - var repaymentFrequencyType: RepaymentFrequencyType? = null, + val repaymentFrequencyType: RepaymentFrequencyType? = null, - var interestRatePerPeriod: Double? = null, + val interestRatePerPeriod: Double? = null, - var interestRateFrequencyType: InterestRateFrequencyType? = null, + val interestRateFrequencyType: InterestRateFrequencyType? = null, - var annualInterestRate: Double? = null, + val annualInterestRate: Double? = null, - var isFloatingInterestRate: Boolean? = null, + val isFloatingInterestRate: Boolean? = null, - var amortizationType: AmortizationType? = null, + val amortizationType: AmortizationType? = null, - var interestType: InterestType? = null, + val interestType: InterestType? = null, - var interestCalculationPeriodType: InterestCalculationPeriodType? = null, + val interestCalculationPeriodType: InterestCalculationPeriodType? = null, - var allowPartialPeriodInterestCalcualtion: Boolean? = null, + val allowPartialPeriodInterestCalcualtion: Boolean? = null, - var transactionProcessingStrategyId: Int? = null, + val transactionProcessingStrategyId: Int? = null, - var graceOnArrearsAgeing: Int? = null, + val graceOnArrearsAgeing: Int? = null, - var timeline: Timeline? = null, + val timeline: Timeline? = null, - var productOptions: List = ArrayList(), + val productOptions: List = emptyList(), - var dataTables: ArrayList = ArrayList(), + val dataTables: ArrayList = ArrayList(), - var loanOfficerOptions: List = ArrayList(), + val loanOfficerOptions: List = emptyList(), - var loanPurposeOptions: List = ArrayList(), + val loanPurposeOptions: List = emptyList(), - var fundOptions: List = ArrayList(), + val fundOptions: List = emptyList(), - var termFrequencyTypeOptions: List = ArrayList(), + val termFrequencyTypeOptions: List = emptyList(), - var repaymentFrequencyTypeOptions: List = ArrayList(), + val repaymentFrequencyTypeOptions: List = emptyList(), - var repaymentFrequencyNthDayTypeOptions: List = ArrayList(), + val repaymentFrequencyNthDayTypeOptions: List = emptyList(), - var repaymentFrequencyDaysOfWeekTypeOptions: List = ArrayList(), + val repaymentFrequencyDaysOfWeekTypeOptions: List = emptyList(), - var interestRateFrequencyTypeOptions: List = ArrayList(), + val interestRateFrequencyTypeOptions: List = emptyList(), - var amortizationTypeOptions: List = ArrayList(), + val amortizationTypeOptions: List = emptyList(), - var interestTypeOptions: List = ArrayList(), + val interestTypeOptions: List = emptyList(), - var interestCalculationPeriodTypeOptions: List = ArrayList(), + val interestCalculationPeriodTypeOptions: List = emptyList(), - var transactionProcessingStrategyOptions: List = ArrayList(), + val transactionProcessingStrategyOptions: List = emptyList(), - var chargeOptions: List = ArrayList(), + val chargeOptions: List = emptyList(), - var loanCollateralOptions: List = ArrayList(), + val loanCollateralOptions: List = emptyList(), - var multiDisburseLoan: Boolean? = null, + val multiDisburseLoan: Boolean? = null, - var canDefineInstallmentAmount: Boolean? = null, + val canDefineInstallmentAmount: Boolean? = null, - var canDisburse: Boolean? = null, + val canDisburse: Boolean? = null, - var product: Product? = null, + val product: Product? = null, - var daysInMonthType: DaysInMonthType? = null, + val daysInMonthType: DaysInMonthType? = null, - var daysInYearType: DaysInYearType? = null, + val daysInYearType: DaysInYearType? = null, - var isInterestRecalculationEnabled: Boolean? = null, + val isInterestRecalculationEnabled: Boolean? = null, - var isVariableInstallmentsAllowed: Boolean? = null, + val isvaliableInstallmentsAllowed: Boolean? = null, - var minimumGap: Int? = null, + val minimumGap: Int? = null, - var maximumGap: Int? = null, + val maximumGap: Int? = null, - var accountLinkingOptions: List = ArrayList(), + val accountLinkingOptions: List = emptyList(), ) : Parcelable diff --git a/core/database/src/main/java/com/mifos/room/entities/templates/loans/LoanTransactionTemplate.kt b/core/database/src/main/java/com/mifos/room/entities/templates/loans/LoanTransactionTemplate.kt index e023244b05..375edb00c5 100644 --- a/core/database/src/main/java/com/mifos/room/entities/templates/loans/LoanTransactionTemplate.kt +++ b/core/database/src/main/java/com/mifos/room/entities/templates/loans/LoanTransactionTemplate.kt @@ -19,15 +19,15 @@ import kotlinx.parcelize.Parcelize */ @Parcelize data class LoanTransactionTemplate( - var type: Type? = null, + val type: Type? = null, - var date: List = ArrayList(), + val date: List = emptyList(), - var amount: Double? = null, + val amount: Double? = null, - var manuallyReversed: Boolean? = null, + val manuallyReversed: Boolean? = null, - var possibleNextRepaymentDate: List = ArrayList(), + val possibleNextRepaymentDate: List = emptyList(), - var paymentTypeOptions: List = ArrayList(), + val paymentTypeOptions: List = emptyList(), ) : Parcelable diff --git a/core/database/src/main/java/com/mifos/room/entities/zipmodels/LoanAndLoanRepayment.kt b/core/database/src/main/java/com/mifos/room/entities/zipmodels/LoanAndLoanRepayment.kt index 1b83f3d436..ca80e590ef 100644 --- a/core/database/src/main/java/com/mifos/room/entities/zipmodels/LoanAndLoanRepayment.kt +++ b/core/database/src/main/java/com/mifos/room/entities/zipmodels/LoanAndLoanRepayment.kt @@ -9,8 +9,8 @@ */ package com.mifos.room.entities.zipmodels -import com.mifos.core.entity.accounts.loan.LoanWithAssociations -import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate +import com.mifos.room.entities.accounts.loans.LoanWithAssociations +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate /** * diff --git a/core/database/src/main/java/com/mifos/room/helper/LoanDaoHelper.kt b/core/database/src/main/java/com/mifos/room/helper/LoanDaoHelper.kt new file mode 100644 index 0000000000..617388ac67 --- /dev/null +++ b/core/database/src/main/java/com/mifos/room/helper/LoanDaoHelper.kt @@ -0,0 +1,216 @@ +/* + * 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.LoanDao +import com.mifos.room.entities.PaymentTypeOption +import com.mifos.room.entities.accounts.loans.ActualDisbursementDate +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest +import com.mifos.room.entities.accounts.loans.LoanRepaymentResponse +import com.mifos.room.entities.accounts.loans.LoanWithAssociations +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.emitAll +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.firstOrNull +import kotlinx.coroutines.flow.flow +import javax.inject.Inject + +// TODO update docs +class LoanDaoHelper @Inject constructor( + private val loanDao: LoanDao, +) { + /** + * This Method Saving the Loan In Database table according to Loan Id + * + * @param loanWithAssociations + */ + fun saveLoanById(loanWithAssociations: LoanWithAssociations): Flow { + return flow { + // Setting Loan Id in Summary Table + val updatedSummary = loanWithAssociations.summary.copy( + loanId = loanWithAssociations.id, + ) + + val updatedTimeline = loanWithAssociations.timeline.copy( + loanId = loanWithAssociations.id, + actualDisburseDate = loanWithAssociations.timeline.actualDisbursementDate?.let { + ActualDisbursementDate( + loanId = loanWithAssociations.id, + year = it.getOrNull(0), + month = it.getOrNull(1), + date = it.getOrNull(2), + ) + }, + ) + + val updatedLoanWithAssociations = loanWithAssociations.copy( + summary = updatedSummary, + timeline = updatedTimeline, + ) + + loanDao.saveLoanWithAssociations(updatedLoanWithAssociations) + emit(updatedLoanWithAssociations) + } + } + + /** + * Retrieving LoanWithAssociation according to Loan Id from Database Table + * + * @param loanId + * @return LoanWithAssociation + */ + fun getLoanById(loanId: Int): Flow { + return flow { + val loan = loanDao.getLoanById(loanId).firstOrNull() + + val updatedLoan = loan?.let { + val updatedTimeline = it.timeline.copy( + actualDisbursementDate = listOf( + it.timeline.actualDisburseDate?.year, + it.timeline.actualDisburseDate?.month, + it.timeline.actualDisburseDate?.date, + ), + ) + it.copy(timeline = updatedTimeline) + } + + emit(updatedLoan) + } + } + + /** + * This Method Saving the Loan Transaction Offline in Database Table + * + * @param loanId Loan Id + * @param loanRepaymentRequest Request Body of Loan Transaction + * @return LoanRepaymentResponse + */ + suspend fun saveLoanRepaymentTransaction( + loanId: Int, + loanRepaymentRequest: LoanRepaymentRequest, + ): LoanRepaymentResponse { + // Setting Loan Id and Time Stamp + val updatedLoanRepaymentRequest = loanRepaymentRequest.copy( + loanId = loanId, + timeStamp = System.currentTimeMillis() / 1000, + ) + + // Saving Transaction In Database Table + loanDao.insertLoanRepaymentTransaction(updatedLoanRepaymentRequest) + return LoanRepaymentResponse() + } + + /** + * Read All LoanRepaymentRequest from Database ascending Order of TimeStamp + * + * @return Flow> + */ + fun readAllLoanRepaymentTransaction(): Flow> { + return loanDao.readAllLoanRepaymentTransaction() + } + + /** + * This Method send a query to Sqlite Database and get the LoanRepaymentRequest Where + * Loan Id is Loan Id, + * + * This method used to check that LoanRepayment in offline mode, + * Is already done with this loanId or not, If Yes then new Transaction can be made if + * old one will be sync to server. + * + * @param loanId Loan Id + * @return LoanRepaymentRequest by Loan Id + */ + suspend fun getDatabaseLoanRepaymentByLoanId(loanId: Int): LoanRepaymentRequest? { + return loanDao.getLoanRepaymentRequest(loanId) + } + + /** + * This Method updating the LoanRepayment to Database Table. this method will be called + * whenever error will come during sync the LoanRepayment. This method saving the Error + * message to the Table entry. + * + * @param loanRepaymentRequest LoanRepayment for update + * @return LoanRepaymentRequest + */ + suspend fun updateLoanRepaymentTransaction( + loanRepaymentRequest: LoanRepaymentRequest, + ) { + loanDao.updateLoanRepaymentRequest(loanRepaymentRequest) + } + + /** + * This Method Deleting the LoanRepayment with the loanId and loading the + * List from Database and return to the DataManagerLoan + * that synced LoanRepayment is deleted from Database and updated Database Table entries. + * + * @param loanId loan Id of the LoanRepayment + * @return List + */ + // TODO recheck logic in DatabaseHelperLoan.deleteAndUpdateLoanRepayments() + fun deleteAndUpdateLoanRepayments(loanId: Int): Flow> { + return flow { + loanDao.deleteLoanRepaymentByLoanId(loanId) + emitAll(loanDao.readAllLoanRepaymentTransaction()) + } + } + + /** + * This method saves the LoanRepaymentTemplate in Database for making Transaction In offline + * and As the Template is saved in the Database, its return the same LoanRepaymentTemplate. + * + * @param loanId Loan Id of the LoanTemplate + * @param loanRepaymentTemplate LoanRepaymentTemplate for saving in Database + * @return LoanRepaymentTemplate + */ + suspend fun saveLoanRepaymentTemplate( + loanId: Int, + loanRepaymentTemplate: LoanRepaymentTemplate, + ): LoanRepaymentTemplate { + val updatedLoanRepaymentTemplate = loanRepaymentTemplate.copy(loanId = loanId) + + updatedLoanRepaymentTemplate.paymentTypeOptions?.forEach { paymentTypeOption -> + loanDao.insertPaymentTypeOption(paymentTypeOption) + } + + loanDao.insertLoanRepaymentTemplate(updatedLoanRepaymentTemplate) + return updatedLoanRepaymentTemplate + } + + /** + * This Method request a query to Database in PaymentTypeOption_Table with argument paymentType + * and return the list of PaymentTypeOption + * + * @return List + */ + + val getAllPaymentTypeOption: Flow> = loanDao.getPaymentTypeOptions() + + /** + * This Method retrieve the LoanRepaymentTemplate from Database LoanRepaymentTemplate_Table + * according to Loan Id and retrieve the PaymentTypeOptions according to templateType + * LoanRepaymentTemplate + * + * @param loanId Loan Id of the LoanRepaymentTemplate. + * @return LoanRepaymentTemplate from Database Query. + */ + fun getLoanRepayTemplate(loanId: Int): Flow { + return flow { + val loanRepaymentTemplate = loanDao.getLoanRepaymentTemplate(loanId) + val paymentTypeOptions = loanDao.getPaymentTypeOptions().first() + + val updatedLoanRepaymentTemplate = loanRepaymentTemplate?.copy( + paymentTypeOptions = paymentTypeOptions.toMutableList(), + ) + + emit(updatedLoanRepaymentTemplate) + } + } +} diff --git a/core/database/src/main/java/com/mifos/room/utils/typeconverters/ListTypeConverters.kt b/core/database/src/main/java/com/mifos/room/utils/typeconverters/ListTypeConverters.kt index eb9c0fe2f8..9045ab361d 100644 --- a/core/database/src/main/java/com/mifos/room/utils/typeconverters/ListTypeConverters.kt +++ b/core/database/src/main/java/com/mifos/room/utils/typeconverters/ListTypeConverters.kt @@ -10,7 +10,7 @@ package com.mifos.room.utils.typeconverters import androidx.room.TypeConverter -import com.mifos.room.entities.accounts.savings.Currency +import com.mifos.room.entities.group.Center import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json @@ -27,12 +27,12 @@ class ListTypeConverters { } @TypeConverter - fun currencyToJson(currency: Currency?): String? { - return currency?.let { Json.encodeToString(it) } + fun fromCenterList(centers: List): String { + return centers.let { Json.encodeToString(it) } } @TypeConverter - fun jsonToCurrency(json: String?): Currency? { - return json?.let { Json.decodeFromString(it) } + fun toCenterList(json: String): List { + return json.let { Json.decodeFromString(it) } } } diff --git a/core/database/src/main/java/com/mifos/room/utils/typeconverters/LoanTypeConverters.kt b/core/database/src/main/java/com/mifos/room/utils/typeconverters/LoanTypeConverters.kt new file mode 100644 index 0000000000..8f7fa09679 --- /dev/null +++ b/core/database/src/main/java/com/mifos/room/utils/typeconverters/LoanTypeConverters.kt @@ -0,0 +1,228 @@ +/* + * 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.mifos.core.objects.account.loan.AmortizationType +import com.mifos.core.objects.account.loan.InterestCalculationPeriodType +import com.mifos.core.objects.account.loan.InterestRateFrequencyType +import com.mifos.core.objects.account.loan.InterestType +import com.mifos.core.objects.account.loan.Period +import com.mifos.core.objects.account.loan.RepaymentFrequencyType +import com.mifos.core.objects.account.loan.RepaymentSchedule +import com.mifos.core.objects.account.loan.TermPeriodFrequencyType +import com.mifos.core.objects.account.loan.Transaction +import com.mifos.core.objects.template.loan.Currency +import com.mifos.core.objects.template.loan.Type +import com.mifos.room.entities.PaymentTypeOption +import com.mifos.room.entities.accounts.loans.ActualDisbursementDate +import com.mifos.room.entities.accounts.loans.LoanType +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 kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json + +/** + * Created by Pronay Sarker on 24/01/2025 (3:07 PM) + */ +// TODO remove unused converters +class LoanTypeConverters { + + @TypeConverter + fun fromStatus(status: Status?): String? { + return status?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toStatus(json: String?): Status? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromLoanType(type: LoanType?): String? { + return type?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toLoanType(json: String?): LoanType? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromCurrency(currency: Currency?): String? { + return currency?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toCurrency(json: String?): Currency? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromTermPeriodFrequencyType(type: TermPeriodFrequencyType?): String? { + return type?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toTermPeriodFrequencyType(json: String?): TermPeriodFrequencyType? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromRepaymentFrequencyType(type: RepaymentFrequencyType?): String? { + return type?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toRepaymentFrequencyType(json: String?): RepaymentFrequencyType? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromInterestRateFrequencyType(type: InterestRateFrequencyType?): String? { + return type?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun fromSummary(summary: Summary?): String? { + return summary?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toSummary(json: String?): Summary? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun toInterestRateFrequencyType(json: String?): InterestRateFrequencyType? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromAmortizationType(type: AmortizationType?): String? { + return type?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toAmortizationType(json: String?): AmortizationType? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromInterestType(type: InterestType?): String? { + return type?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toInterestType(json: String?): InterestType? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromInterestCalculationPeriodType(type: InterestCalculationPeriodType?): String? { + return type?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toInterestCalculationPeriodType(json: String?): InterestCalculationPeriodType? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromTimeline(timeline: Timeline?): String? { + return timeline?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toTimeline(json: String?): Timeline? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromRepaymentSchedule(schedule: RepaymentSchedule?): String? { + return schedule?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toRepaymentSchedule(json: String?): RepaymentSchedule? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromTransactionList(transactions: List?): String? { + return transactions?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toTransactionList(json: String?): List? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromType(type: Type?): String? { + return type?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toType(json: String?): Type? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromListInt(date: List?): String? { + return date?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toListInt(json: String?): List? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromActualDisbursementDate(date: ActualDisbursementDate?): String? { + return date?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toActualDisbursementDate(json: String?): ActualDisbursementDate? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromMutableListInt(date: MutableList?): String? { + return date?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toMutableListInt(json: String?): MutableList? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromMutableListPaymentTypeOptions(type: MutableList?): String? { + return type?.let { Json.encodeToString(it) } + } + + @TypeConverter + fun toMutableListPaymentTypeOptions(json: String?): MutableList? { + return json?.let { Json.decodeFromString(it) } + } + + @TypeConverter + fun fromPeriodList(json: String?): List? { + return json?.let { Json.decodeFromString>(it) } + } + + @TypeConverter + fun toPeriodList(periodList: List?): String? { + return periodList?.let { Json.encodeToString(it) } + } +} diff --git a/core/domain/src/main/java/com/mifos/core/domain/useCases/PaymentTypeOptionUseCase.kt b/core/domain/src/main/java/com/mifos/core/domain/useCases/PaymentTypeOptionUseCase.kt deleted file mode 100644 index f649be6cad..0000000000 --- a/core/domain/src/main/java/com/mifos/core/domain/useCases/PaymentTypeOptionUseCase.kt +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2024 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.core.domain.useCases - -import com.mifos.core.common.utils.Resource -import com.mifos.core.data.repository.SyncSavingsAccountTransactionRepository -import com.mifos.room.entities.PaymentTypeOption -import kotlinx.coroutines.channels.awaitClose -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.callbackFlow -import rx.Subscriber -import rx.android.schedulers.AndroidSchedulers -import rx.schedulers.Schedulers -import javax.inject.Inject - -/** - * Created by Pronay Sarker on 04/08/2024 (10:20 PM) - */ -class PaymentTypeOptionUseCase @Inject constructor(private val repository: SyncSavingsAccountTransactionRepository) { - - suspend operator fun invoke(): Flow>> = callbackFlow { - try { - trySend(Resource.Loading()) - repository.paymentTypeOption() - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber>() { - override fun onCompleted() { - } - - override fun onError(e: Throwable) { - trySend(Resource.Error(e.message.toString())) - } - - override fun onNext(paymentOptions: List) { - trySend(Resource.Success(paymentOptions)) - } - }) - - awaitClose { channel.close() } - } catch (exception: Exception) { - send(Resource.Error(exception.message.toString())) - } - } -} diff --git a/core/domain/src/main/java/com/mifos/core/domain/useCases/UpdateLoanRepaymentTransactionSavingsUseCase.kt b/core/domain/src/main/java/com/mifos/core/domain/useCases/UpdateLoanRepaymentTransactionSavingsUseCase.kt new file mode 100644 index 0000000000..10e8206984 --- /dev/null +++ b/core/domain/src/main/java/com/mifos/core/domain/useCases/UpdateLoanRepaymentTransactionSavingsUseCase.kt @@ -0,0 +1,58 @@ +/* + * Copyright 2024 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.core.domain.useCases + +import com.mifos.core.common.utils.Resource +import com.mifos.core.data.repository.SyncSavingsAccountTransactionRepository +import com.mifos.core.entity.accounts.savings.SavingsAccountTransactionRequest +import kotlinx.coroutines.channels.awaitClose +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.callbackFlow +import rx.Subscriber +import rx.android.schedulers.AndroidSchedulers +import rx.schedulers.Schedulers +import javax.inject.Inject + +/** + * Created by Pronay Sarker on 04/08/2024 (4:55 PM) + */ +class UpdateLoanRepaymentTransactionSavingsUseCase @Inject constructor( + private val repository: SyncSavingsAccountTransactionRepository, +) { + + suspend operator fun invoke(request: SavingsAccountTransactionRequest?): Flow> = + callbackFlow { + try { + trySend(Resource.Loading()) + + repository.updateLoanRepaymentTransaction(request!!) + .observeOn(AndroidSchedulers.mainThread()) + .subscribeOn(Schedulers.io()) + .subscribe( + object : Subscriber() { + override fun onCompleted() { + } + + override fun onError(e: Throwable) { + trySend(Resource.Error(e.message.toString())) + } + + override fun onNext(savingsAccountTransactionRequest: SavingsAccountTransactionRequest?) { + trySend(Resource.Success(savingsAccountTransactionRequest)) + } + }, + ) + + awaitClose { channel.close() } + } catch (exception: Exception) { + send(Resource.Error(exception.message.toString())) + } + } +} diff --git a/core/domain/src/main/java/com/mifos/core/domain/useCases/UpdateLoanRepaymentTransactionUseCase.kt b/core/domain/src/main/java/com/mifos/core/domain/useCases/UpdateLoanRepaymentTransactionUseCase.kt deleted file mode 100644 index 5f2bf42915..0000000000 --- a/core/domain/src/main/java/com/mifos/core/domain/useCases/UpdateLoanRepaymentTransactionUseCase.kt +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2024 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.core.domain.useCases - -import com.mifos.core.common.utils.Resource -import com.mifos.core.data.repository.SyncSavingsAccountTransactionRepository -import com.mifos.core.entity.accounts.savings.SavingsAccountTransactionRequest -import kotlinx.coroutines.channels.awaitClose -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.callbackFlow -import rx.Subscriber -import rx.android.schedulers.AndroidSchedulers -import rx.schedulers.Schedulers -import javax.inject.Inject - -/** - * Created by Pronay Sarker on 04/08/2024 (4:55 PM) - */ -class UpdateLoanRepaymentTransactionUseCase @Inject constructor(private val repository: SyncSavingsAccountTransactionRepository) { - - suspend operator fun invoke(request: SavingsAccountTransactionRequest?): Flow> = callbackFlow { - try { - trySend(Resource.Loading()) - - repository.updateLoanRepaymentTransaction(request!!) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() { - } - - override fun onError(e: Throwable) { - trySend(Resource.Error(e.message.toString())) - } - - override fun onNext(savingsAccountTransactionRequest: SavingsAccountTransactionRequest?) { - trySend(Resource.Success(savingsAccountTransactionRequest)) - } - }) - - awaitClose { channel.close() } - } catch (exception: Exception) { - send(Resource.Error(exception.message.toString())) - } - } -} diff --git a/core/model/build.gradle.kts b/core/model/build.gradle.kts index ccb7fd6519..2b5c3398d7 100644 --- a/core/model/build.gradle.kts +++ b/core/model/build.gradle.kts @@ -21,4 +21,5 @@ android{ } dependencies { implementation(libs.converter.gson) + implementation(libs.kotlinx.serialization.json) } diff --git a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/AmortizationType.kt b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/AmortizationType.kt index be4994b2da..d035bdf20c 100644 --- a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/AmortizationType.kt +++ b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/AmortizationType.kt @@ -11,8 +11,10 @@ package com.mifos.core.objects.account.loan import android.os.Parcelable import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable @Parcelize +@Serializable class AmortizationType( var id: Int? = null, diff --git a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Currency.kt b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Currency.kt index 169f99c7fe..cec74f7350 100644 --- a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Currency.kt +++ b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Currency.kt @@ -11,11 +11,13 @@ package com.mifos.core.objects.account.loan import android.os.Parcelable import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable /** * Created by mayankjindal on 22/10/16. */ @Parcelize +@Serializable data class Currency( var code: String? = null, diff --git a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/InterestCalculationPeriodType.kt b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/InterestCalculationPeriodType.kt index ef94c693aa..ee9aa3de4c 100644 --- a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/InterestCalculationPeriodType.kt +++ b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/InterestCalculationPeriodType.kt @@ -11,8 +11,10 @@ package com.mifos.core.objects.account.loan import android.os.Parcelable import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable @Parcelize +@Serializable data class InterestCalculationPeriodType( var id: Int? = null, diff --git a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/InterestRateFrequencyType.kt b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/InterestRateFrequencyType.kt index bc8dc0059b..ae8090c58f 100644 --- a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/InterestRateFrequencyType.kt +++ b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/InterestRateFrequencyType.kt @@ -11,8 +11,10 @@ package com.mifos.core.objects.account.loan import android.os.Parcelable import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable @Parcelize +@Serializable data class InterestRateFrequencyType( var id: Int? = null, diff --git a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/InterestType.kt b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/InterestType.kt index bbbcc0f8c6..10d8da359b 100644 --- a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/InterestType.kt +++ b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/InterestType.kt @@ -11,8 +11,10 @@ package com.mifos.core.objects.account.loan import android.os.Parcelable import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable @Parcelize +@Serializable class InterestType( var id: Int? = null, diff --git a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/PaymentDetailData.kt b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/PaymentDetailData.kt index 1eed09c29d..e308cccd59 100644 --- a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/PaymentDetailData.kt +++ b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/PaymentDetailData.kt @@ -11,8 +11,10 @@ package com.mifos.core.objects.account.loan import android.os.Parcelable import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable @Parcelize +@Serializable data class PaymentDetailData( var id: Int? = null, diff --git a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/PaymentType.kt b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/PaymentType.kt index 1cb3dc3c87..4e6ecffa82 100644 --- a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/PaymentType.kt +++ b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/PaymentType.kt @@ -11,8 +11,10 @@ package com.mifos.core.objects.account.loan import android.os.Parcelable import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable @Parcelize +@Serializable data class PaymentType( var id: Int? = null, diff --git a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Period.kt b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Period.kt index 656b510ea4..a071be9a0f 100644 --- a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Period.kt +++ b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Period.kt @@ -11,11 +11,13 @@ package com.mifos.core.objects.account.loan import android.os.Parcelable import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable /** * Created by ishankhanna on 19/06/14. */ @Parcelize +@Serializable data class Period( var complete: Boolean? = null, diff --git a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/RepaymentFrequencyType.kt b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/RepaymentFrequencyType.kt index 37b54a3f33..0050a20867 100644 --- a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/RepaymentFrequencyType.kt +++ b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/RepaymentFrequencyType.kt @@ -11,8 +11,10 @@ package com.mifos.core.objects.account.loan import android.os.Parcelable import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable @Parcelize +@Serializable data class RepaymentFrequencyType( var id: Int? = null, diff --git a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/RepaymentSchedule.kt b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/RepaymentSchedule.kt index 0ad18a506d..6a5cbf8ef9 100644 --- a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/RepaymentSchedule.kt +++ b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/RepaymentSchedule.kt @@ -10,12 +10,15 @@ package com.mifos.core.objects.account.loan import android.os.Parcelable +import com.mifos.core.objects.template.loan.Currency import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable /** * Created by ishankhanna on 19/06/14. */ @Parcelize +@Serializable data class RepaymentSchedule( var currency: Currency? = null, diff --git a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/TermPeriodFrequencyType.kt b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/TermPeriodFrequencyType.kt index be8e56a260..a0642aceeb 100644 --- a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/TermPeriodFrequencyType.kt +++ b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/TermPeriodFrequencyType.kt @@ -11,9 +11,12 @@ package com.mifos.core.objects.account.loan import android.os.Parcelable import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable @Parcelize +@Serializable data class TermPeriodFrequencyType( + var id: Int? = null, var code: String? = null, diff --git a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Transaction.kt b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Transaction.kt index dade75c407..647ee11425 100644 --- a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Transaction.kt +++ b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Transaction.kt @@ -11,8 +11,10 @@ package com.mifos.core.objects.account.loan import android.os.Parcelable import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable @Parcelize +@Serializable data class Transaction( var id: Int? = null, diff --git a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Type.kt b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Type.kt index e4bef794bc..8d296fd3c1 100644 --- a/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Type.kt +++ b/core/model/src/main/kotlin/com/mifos/core/objects/account/loan/Type.kt @@ -11,8 +11,10 @@ package com.mifos.core.objects.account.loan import android.os.Parcelable import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable @Parcelize +@Serializable data class Type( var id: Int? = null, diff --git a/core/model/src/main/kotlin/com/mifos/core/objects/template/loan/Currency.kt b/core/model/src/main/kotlin/com/mifos/core/objects/template/loan/Currency.kt index 41b482fcc2..b92cfd8e58 100644 --- a/core/model/src/main/kotlin/com/mifos/core/objects/template/loan/Currency.kt +++ b/core/model/src/main/kotlin/com/mifos/core/objects/template/loan/Currency.kt @@ -11,11 +11,13 @@ package com.mifos.core.objects.template.loan import android.os.Parcelable import kotlinx.parcelize.Parcelize +import kotlinx.serialization.Serializable /** * Created by Rajan Maurya on 16/07/16. */ @Parcelize +@Serializable data class Currency( var code: String? = null, diff --git a/core/network/src/main/java/com/mifos/core/network/DataManager.kt b/core/network/src/main/java/com/mifos/core/network/DataManager.kt index 129f59bb61..b7898ff849 100644 --- a/core/network/src/main/java/com/mifos/core/network/DataManager.kt +++ b/core/network/src/main/java/com/mifos/core/network/DataManager.kt @@ -9,7 +9,6 @@ */ package com.mifos.core.network -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.entity.accounts.loan.Loans import com.mifos.core.entity.client.Charges import com.mifos.core.entity.group.Center @@ -30,6 +29,7 @@ import com.mifos.core.objects.template.client.ChargeTemplate import com.mifos.core.objects.template.loan.GroupLoanTemplate import com.mifos.core.payloads.ChargesPayload import com.mifos.core.payloads.GroupLoanPayload +import com.mifos.room.entities.accounts.loans.LoanWithAssociations import com.mifos.room.entities.group.CenterWithAssociations import com.mifos.room.entities.group.GroupWithAssociations import okhttp3.ResponseBody diff --git a/core/network/src/main/java/com/mifos/core/network/datamanager/DataManagerLoan.kt b/core/network/src/main/java/com/mifos/core/network/datamanager/DataManagerLoan.kt index fccc94727f..77cfceb05f 100644 --- a/core/network/src/main/java/com/mifos/core/network/datamanager/DataManagerLoan.kt +++ b/core/network/src/main/java/com/mifos/core/network/datamanager/DataManagerLoan.kt @@ -10,19 +10,22 @@ package com.mifos.core.network.datamanager import com.mifos.core.databasehelper.DatabaseHelperLoan -import com.mifos.core.entity.accounts.loan.LoanRepaymentRequest -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.entity.accounts.loan.Loans -import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate import com.mifos.core.network.BaseApiManager import com.mifos.core.network.GenericResponse import com.mifos.core.network.model.LoansPayload import com.mifos.core.objects.account.loan.LoanDisbursement -import com.mifos.core.objects.account.loan.LoanRepaymentResponse import com.mifos.core.objects.organisations.LoanProducts import com.mifos.room.entities.PaymentTypeOption +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest +import com.mifos.room.entities.accounts.loans.LoanRepaymentResponse +import com.mifos.room.entities.accounts.loans.LoanWithAssociations +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate import com.mifos.room.entities.templates.loans.LoanTemplate import com.mifos.room.entities.templates.loans.LoanTransactionTemplate +import com.mifos.room.helper.LoanDaoHelper +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.flow import rx.Observable import javax.inject.Inject import javax.inject.Singleton @@ -34,6 +37,7 @@ import javax.inject.Singleton class DataManagerLoan @Inject constructor( val mBaseApiManager: BaseApiManager, val mDatabaseHelperLoan: DatabaseHelperLoan, + val loanDaoHelper: LoanDaoHelper, private val prefManager: com.mifos.core.datastore.PrefManager, ) { /** @@ -50,14 +54,14 @@ class DataManagerLoan @Inject constructor( * @param loanId Loan Id of the Loan * @return LoanWithAssociation */ - fun getLoanById(loanId: Int): Observable { + fun getLoanById(loanId: Int): Flow { return when (prefManager.userStatus) { - false -> mBaseApiManager.loanApi.getLoanByIdWithAllAssociations(loanId) + false -> flow { emit(mBaseApiManager.loanApi.getLoanByIdWithAllAssociations(loanId)) } true -> /** - * Return LoanWithAssociation from DatabaseHelperLoan. + * offline Mode, Return LoanWithAssociation from LoanDaoHelper. */ - mDatabaseHelperLoan.getLoanById(loanId) + loanDaoHelper.getLoanById(loanId) } } @@ -70,14 +74,12 @@ class DataManagerLoan @Inject constructor( * @param loanId Loan Id * @return LoanWithAssociations */ - fun syncLoanById(loanId: Int): Observable { - return mBaseApiManager.loanApi - .getLoanByIdWithAllAssociations(loanId) - .concatMap { loanWithAssociations -> - mDatabaseHelperLoan.saveLoanById( - loanWithAssociations, - ) - } + fun syncLoanById(loanId: Int): Flow { + return flow { + val loanWithAssociations = mBaseApiManager.loanApi.getLoanByIdWithAllAssociations(loanId) + loanDaoHelper.saveLoanById(loanWithAssociations) + emit(loanWithAssociations) + } } val allLoans: Observable> @@ -110,14 +112,15 @@ class DataManagerLoan @Inject constructor( * @param loanId Loan Id of the LoanRepaymentTemplate * @return LoanRepaymentTemplate */ - fun getLoanRepayTemplate(loanId: Int): Observable { - return when (prefManager.userStatus) { - false -> mBaseApiManager.loanApi.getLoanRepaymentTemplate(loanId) - true -> + fun getLoanRepayTemplate(loanId: Int): Flow { + return flow { + when (prefManager.userStatus) { + false -> mBaseApiManager.loanApi.getLoanRepaymentTemplate(loanId) /** * Return LoanRepaymentTemplate from DatabaseHelperLoan. */ - mDatabaseHelperLoan.getLoanRepayTemplate(loanId) + true -> loanDaoHelper.getLoanRepayTemplate(loanId) + } } } @@ -134,14 +137,13 @@ class DataManagerLoan @Inject constructor( * @param loanId Loan Id * @return LoanRepaymentTemplate */ - fun syncLoanRepaymentTemplate(loanId: Int): Observable { - return mBaseApiManager.loanApi.getLoanRepaymentTemplate(loanId) - .concatMap { loanRepaymentTemplate -> - mDatabaseHelperLoan.saveLoanRepaymentTemplate( - loanId, - loanRepaymentTemplate, - ) + + fun syncLoanRepaymentTemplate(loanId: Int): Flow { + return flow { + mBaseApiManager.loanApi.getLoanRepaymentTemplate(loanId).also { + loanDaoHelper.saveLoanRepaymentTemplate(loanId, it) } + } } /** @@ -158,19 +160,18 @@ class DataManagerLoan @Inject constructor( * @param request Request Body of POST Request * @return LoanRepaymentResponse */ - fun submitPayment( + suspend fun submitPayment( loanId: Int, request: LoanRepaymentRequest, - ): Observable { + ): LoanRepaymentResponse { return when (prefManager.userStatus) { false -> mBaseApiManager.loanApi.submitPayment(loanId, request) - .concatMap { loanRepaymentResponse -> Observable.just(loanRepaymentResponse) } true -> /** * Return LoanRepaymentResponse from DatabaseHelperLoan. */ - mDatabaseHelperLoan.saveLoanRepaymentTransaction(loanId, request) + loanDaoHelper.saveLoanRepaymentTransaction(loanId, request) } } @@ -179,9 +180,10 @@ class DataManagerLoan @Inject constructor( * These LoanRepayment are those LoanRepayment that are saved during the Offline LoanRepayment. * * @return List + * */ - val databaseLoanRepayments: Observable> - get() = mDatabaseHelperLoan.readAllLoanRepaymentTransaction() + val databaseLoanRepayments: Flow> + get() = loanDaoHelper.readAllLoanRepaymentTransaction() /** * This method request a Observable to DatabaseHelperLoan and DatabaseHelper check in @@ -199,8 +201,10 @@ class DataManagerLoan @Inject constructor( * @param loanId * @return LoanRepayment with this Loan Id reference. */ - fun getDatabaseLoanRepaymentByLoanId(loanId: Int): Observable { - return mDatabaseHelperLoan.getDatabaseLoanRepaymentByLoanId(loanId) + fun getDatabaseLoanRepaymentByLoanId(loanId: Int): Flow { + return flow { + emit(loanDaoHelper.getDatabaseLoanRepaymentByLoanId(loanId)) + } } /** @@ -209,8 +213,8 @@ class DataManagerLoan @Inject constructor( * * @return List */ - val paymentTypeOption: Observable> - get() = mDatabaseHelperLoan.paymentTypeOption + val paymentTypeOption: Flow> + get() = loanDaoHelper.getAllPaymentTypeOption /** * This method sending request DatabaseHelper and Deleting the LoanRepayment with loanId @@ -219,8 +223,8 @@ class DataManagerLoan @Inject constructor( * @param loanId Loan Id of the Loan * @return List */ - fun deleteAndUpdateLoanRepayments(loanId: Int): Observable> { - return mDatabaseHelperLoan.deleteAndUpdateLoanRepayments(loanId) + fun deleteAndUpdateLoanRepayments(loanId: Int): Flow> { + return loanDaoHelper.deleteAndUpdateLoanRepayments(loanId) } /** @@ -231,8 +235,11 @@ class DataManagerLoan @Inject constructor( */ fun updateLoanRepaymentTransaction( loanRepaymentRequest: LoanRepaymentRequest, - ): Observable { - return mDatabaseHelperLoan.updateLoanRepaymentTransaction(loanRepaymentRequest) + ): Flow { + return flow { + loanDaoHelper.updateLoanRepaymentTransaction(loanRepaymentRequest) + emit(loanRepaymentRequest) + } } /** diff --git a/core/network/src/main/java/com/mifos/core/network/services/LoanService.kt b/core/network/src/main/java/com/mifos/core/network/services/LoanService.kt index 0e89a775db..2771af3989 100644 --- a/core/network/src/main/java/com/mifos/core/network/services/LoanService.kt +++ b/core/network/src/main/java/com/mifos/core/network/services/LoanService.kt @@ -9,21 +9,21 @@ */ package com.mifos.core.network.services -import com.mifos.core.entity.accounts.loan.LoanRepaymentRequest -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.entity.accounts.loan.Loans import com.mifos.core.entity.client.Charges -import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate import com.mifos.core.network.GenericResponse import com.mifos.core.network.model.LoansPayload import com.mifos.core.objects.account.loan.LoanApproval import com.mifos.core.objects.account.loan.LoanDisbursement -import com.mifos.core.objects.account.loan.LoanRepaymentResponse import com.mifos.core.objects.clients.Page import com.mifos.core.objects.organisations.LoanProducts import com.mifos.core.objects.template.loan.GroupLoanTemplate import com.mifos.core.payloads.GroupLoanPayload import com.mifos.room.basemodel.APIEndPoint +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest +import com.mifos.room.entities.accounts.loans.LoanRepaymentResponse +import com.mifos.room.entities.accounts.loans.LoanWithAssociations +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate import com.mifos.room.entities.templates.loans.LoanTemplate import com.mifos.room.entities.templates.loans.LoanTransactionTemplate import retrofit2.http.Body @@ -38,10 +38,10 @@ import rx.Observable */ interface LoanService { @GET(APIEndPoint.LOANS + "/{loanId}?associations=all&exclude=guarantors,futureSchedule") - fun getLoanByIdWithAllAssociations(@Path("loanId") loanId: Int): Observable + suspend fun getLoanByIdWithAllAssociations(@Path("loanId") loanId: Int): LoanWithAssociations @GET(APIEndPoint.LOANS + "/{loanId}/transactions/template?command=repayment") - fun getLoanRepaymentTemplate(@Path("loanId") loanId: Int): Observable + suspend fun getLoanRepaymentTemplate(@Path("loanId") loanId: Int): LoanRepaymentTemplate // Mandatory Fields // 1. String approvedOnDate @@ -60,10 +60,10 @@ interface LoanService { ): Observable @POST(APIEndPoint.LOANS + "/{loanId}/transactions?command=repayment") - fun submitPayment( + suspend fun submitPayment( @Path("loanId") loanId: Int, @Body loanRepaymentRequest: LoanRepaymentRequest?, - ): Observable + ): LoanRepaymentResponse @GET(APIEndPoint.LOANS + "/{loanId}?associations=repaymentSchedule") fun getLoanRepaymentSchedule(@Path("loanId") loanId: Int): Observable diff --git a/feature/center/src/main/java/com/mifos/feature/center/syncCentersDialog/SyncCentersDialogViewModel.kt b/feature/center/src/main/java/com/mifos/feature/center/syncCentersDialog/SyncCentersDialogViewModel.kt index 8b094b90db..16249a1583 100644 --- a/feature/center/src/main/java/com/mifos/feature/center/syncCentersDialog/SyncCentersDialogViewModel.kt +++ b/feature/center/src/main/java/com/mifos/feature/center/syncCentersDialog/SyncCentersDialogViewModel.kt @@ -30,8 +30,12 @@ import com.mifos.room.entities.zipmodels.LoanAndLoanRepayment import com.mifos.room.entities.zipmodels.SavingsAccountAndTransactionTemplate import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import retrofit2.HttpException @@ -155,33 +159,35 @@ class SyncCentersDialogViewModel @Inject constructor( repository.syncCenterAccounts(centerId) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + onAccountSyncFailed(e) + } - override fun onNext(centerAccounts: CenterAccounts) { - mLoanAccountList = getActiveLoanAccounts( - centerAccounts - .loanAccounts, - ) - mSavingsAccountList = getActiveSavingsAccounts( - centerAccounts - .savingsAccounts, - ) - mMemberLoanAccountsList = getActiveLoanAccounts( - centerAccounts - .memberLoanAccounts, - ) - // Updating UI - maxSingleSyncCenterProgressBar = ( - mLoanAccountList.size + - mSavingsAccountList.size + mMemberLoanAccountsList.size + override fun onNext(centerAccounts: CenterAccounts) { + mLoanAccountList = getActiveLoanAccounts( + centerAccounts + .loanAccounts, ) - checkAccountsSyncStatusAndSyncAccounts() - } - }) + mSavingsAccountList = getActiveSavingsAccounts( + centerAccounts + .savingsAccounts, + ) + mMemberLoanAccountsList = getActiveLoanAccounts( + centerAccounts + .memberLoanAccounts, + ) + // Updating UI + maxSingleSyncCenterProgressBar = ( + mLoanAccountList.size + + mSavingsAccountList.size + mMemberLoanAccountsList.size + ) + checkAccountsSyncStatusAndSyncAccounts() + } + }, + ) } /** @@ -278,14 +284,12 @@ class SyncCentersDialogViewModel @Inject constructor( * @param loanId Loan Id */ private fun syncLoanAndLoanRepayment(loanId: Int) { - getLoanAndLoanRepayment(loanId) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) + viewModelScope.launch { + getLoanAndLoanRepayment(loanId) + .catch { + onAccountSyncFailed(it) } - - override fun onNext(loanAndLoanRepayment: LoanAndLoanRepayment) { + .collect { mLoanAndRepaymentSyncIndex += 1 _syncCenterData.update { it.copy(singleSyncCount = mLoanAndRepaymentSyncIndex) } if (mLoanAndRepaymentSyncIndex != mLoanAccountList.size) { @@ -295,37 +299,8 @@ class SyncCentersDialogViewModel @Inject constructor( checkAccountsSyncStatusAndSyncAccounts() } } - }) - } - -// /** -// * This Method Syncing the Member's Loan and their LoanRepayment. This is the -// * Observable.combineLatest In Which two request is going to server Loans and LoanRepayment -// * and This request will not complete till that both request completed successfully with -// * response (200 OK). If one will fail then response will come in onError. and If both -// * request is 200 response then response will come in onNext. -// * -// * @param loanId Loan Id -// */ -// private fun syncMemberLoanAndMemberLoanRepayment(loanId: Int) { -// getLoanAndLoanRepayment(loanId) -// .subscribe(object : Subscriber() { -// override fun onCompleted() {} -// override fun onError(e: Throwable) { -// onAccountSyncFailed(e) -// } -// -// override fun onNext(loanAndLoanRepayment: LoanAndLoanRepayment) { -// mMemberLoanSyncIndex += 1 -// _syncCenterData.update { it.copy(singleSyncCount = mLoanAndRepaymentSyncIndex + mSavingsAndTransactionSyncIndex + mMemberLoanSyncIndex) } -// if (mMemberLoanSyncIndex != mMemberLoanAccountsList.size) { -// checkNetworkConnectionAndSyncMemberLoanAndMemberLoanRepayment() -// } else { -// mCenterList[mCenterSyncIndex].id?.let { loadCenterAssociateGroups(it) } -// } -// } -// }) -// } + } + } /** * This Method Fetching the SavingsAccount and SavingsAccountTransactionTemplate and Syncing @@ -336,23 +311,25 @@ class SyncCentersDialogViewModel @Inject constructor( */ private fun syncSavingsAccountAndTemplate(savingsAccountType: String, savingsAccountId: Int) { getSavingsAccountAndTemplate(savingsAccountType, savingsAccountId) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + onAccountSyncFailed(e) + } - override fun onNext(savingsAccountAndTransactionTemplate: SavingsAccountAndTransactionTemplate) { - mSavingsAndTransactionSyncIndex += 1 - _syncCenterData.update { it.copy(singleSyncCount = mLoanAndRepaymentSyncIndex + mSavingsAndTransactionSyncIndex) } - if (mSavingsAndTransactionSyncIndex != mSavingsAccountList.size) { - checkNetworkConnectionAndSyncSavingsAccountAndTransactionTemplate() - } else { - setSavingAccountSyncStatusTrue() - checkAccountsSyncStatusAndSyncAccounts() + override fun onNext(savingsAccountAndTransactionTemplate: SavingsAccountAndTransactionTemplate) { + mSavingsAndTransactionSyncIndex += 1 + _syncCenterData.update { it.copy(singleSyncCount = mLoanAndRepaymentSyncIndex + mSavingsAndTransactionSyncIndex) } + if (mSavingsAndTransactionSyncIndex != mSavingsAccountList.size) { + checkNetworkConnectionAndSyncSavingsAccountAndTransactionTemplate() + } else { + setSavingAccountSyncStatusTrue() + checkAccountsSyncStatusAndSyncAccounts() + } } - } - }) + }, + ) } /** @@ -380,8 +357,8 @@ class SyncCentersDialogViewModel @Inject constructor( * @param loanId Loan Id * @return LoanAndLoanRepayment */ - private fun getLoanAndLoanRepayment(loanId: Int): Observable { - return Observable.combineLatest( + private suspend fun getLoanAndLoanRepayment(loanId: Int): Flow { + return combine( repository.syncLoanById(loanId), repository.syncLoanRepaymentTemplate(loanId), ) { loanWithAssociations, loanRepaymentTemplate -> @@ -390,8 +367,6 @@ class SyncCentersDialogViewModel @Inject constructor( loanRepaymentTemplate, ) } - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) } /** @@ -436,24 +411,26 @@ class SyncCentersDialogViewModel @Inject constructor( repository.getCenterWithAssociations(centerId) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + onAccountSyncFailed(e) + } - override fun onNext(centerWithAssociations: CenterWithAssociations) { - mGroups = centerWithAssociations.groupMembers - mGroupSyncIndex = 0 - resetIndexes() - if (mGroups.isNotEmpty()) { - _syncCenterData.update { it.copy(totalGroupsSyncCount = mGroups.size) } - mGroups[mGroupSyncIndex].id?.let { syncGroupAccounts(it) } - } else { - syncCenter(mCenterList[mCenterSyncIndex]) + override fun onNext(centerWithAssociations: CenterWithAssociations) { + mGroups = centerWithAssociations.groupMembers + mGroupSyncIndex = 0 + resetIndexes() + if (mGroups.isNotEmpty()) { + _syncCenterData.update { it.copy(totalGroupsSyncCount = mGroups.size) } + mGroups[mGroupSyncIndex].id?.let { syncGroupAccounts(it) } + } else { + syncCenter(mCenterList[mCenterSyncIndex]) + } } - } - }) + }, + ) } /** @@ -466,24 +443,26 @@ class SyncCentersDialogViewModel @Inject constructor( repository.getGroupWithAssociations(groupId) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + onAccountSyncFailed(e) + } - override fun onNext(groupWithAssociations: GroupWithAssociations) { - mClients = getActiveClients(groupWithAssociations.clientMembers) - mClientSyncIndex = 0 - resetIndexes() - if (mClients.isNotEmpty()) { - _syncCenterData.update { it.copy(totalClientSyncCount = mClients.size) } - syncClientAccounts(mClients[mClientSyncIndex].id) - } else { - syncGroup(mGroups[mGroupSyncIndex]) + override fun onNext(groupWithAssociations: GroupWithAssociations) { + mClients = getActiveClients(groupWithAssociations.clientMembers) + mClientSyncIndex = 0 + resetIndexes() + if (mClients.isNotEmpty()) { + _syncCenterData.update { it.copy(totalClientSyncCount = mClients.size) } + syncClientAccounts(mClients[mClientSyncIndex].id) + } else { + syncGroup(mGroups[mGroupSyncIndex]) + } } - } - }) + }, + ) } /** @@ -503,24 +482,26 @@ class SyncCentersDialogViewModel @Inject constructor( repository.syncGroupAccounts(groupId) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + onAccountSyncFailed(e) + } - override fun onNext(groupAccounts: GroupAccounts) { - mLoanAccountList = getActiveLoanAccounts( - groupAccounts - .loanAccounts, - ) - mSavingsAccountList = getActiveSavingsAccounts( - groupAccounts - .savingsAccounts, - ) - checkAccountsSyncStatusAndSyncGroupAccounts() - } - }) + override fun onNext(groupAccounts: GroupAccounts) { + mLoanAccountList = getActiveLoanAccounts( + groupAccounts + .loanAccounts, + ) + mSavingsAccountList = getActiveSavingsAccounts( + groupAccounts + .savingsAccounts, + ) + checkAccountsSyncStatusAndSyncGroupAccounts() + } + }, + ) } /** @@ -619,23 +600,25 @@ class SyncCentersDialogViewModel @Inject constructor( repository.syncGroupInDatabase(group) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - _syncCentersDialogUiState.value = - SyncCentersDialogUiState.Error(message = e.message.toString()) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + _syncCentersDialogUiState.value = + SyncCentersDialogUiState.Error(message = e.message.toString()) + } - override fun onNext(group: Group) { - resetIndexes() - mGroupSyncIndex += 1 - if (mGroups.size == mGroupSyncIndex) { - syncCenter(mCenterList[mCenterSyncIndex]) - } else { - mGroups[mGroupSyncIndex].id?.let { syncGroupAccounts(it) } + override fun onNext(group: Group) { + resetIndexes() + mGroupSyncIndex += 1 + if (mGroups.size == mGroupSyncIndex) { + syncCenter(mCenterList[mCenterSyncIndex]) + } else { + mGroups[mGroupSyncIndex].id?.let { syncGroupAccounts(it) } + } } - } - }) + }, + ) } /** @@ -650,23 +633,25 @@ class SyncCentersDialogViewModel @Inject constructor( repository.syncClientInDatabase(client) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - _syncCentersDialogUiState.value = - SyncCentersDialogUiState.Error(message = e.message.toString()) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + _syncCentersDialogUiState.value = + SyncCentersDialogUiState.Error(message = e.message.toString()) + } - override fun onNext(client: Client) { - resetIndexes() - mClientSyncIndex += 1 - if (mClients.size == mClientSyncIndex) { - syncGroup(mGroups[mGroupSyncIndex]) - } else { - syncClientAccounts(mClients[mClientSyncIndex].id) + override fun onNext(client: Client) { + resetIndexes() + mClientSyncIndex += 1 + if (mClients.size == mClientSyncIndex) { + syncGroup(mGroups[mGroupSyncIndex]) + } else { + syncClientAccounts(mClients[mClientSyncIndex].id) + } } - } - }) + }, + ) } /** @@ -679,14 +664,12 @@ class SyncCentersDialogViewModel @Inject constructor( * @param loanId Loan Id */ private fun syncGroupLoanAndLoanRepayment(loanId: Int) { - getLoanAndLoanRepayment(loanId) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) + viewModelScope.launch { + getLoanAndLoanRepayment(loanId) + .catch { + onAccountSyncFailed(it) } - - override fun onNext(loanAndLoanRepayment: LoanAndLoanRepayment) { + .collect { mLoanAndRepaymentSyncIndex += 1 if (mLoanAndRepaymentSyncIndex != mLoanAccountList.size) { mLoanAccountList[mLoanAndRepaymentSyncIndex].id?.let { @@ -699,7 +682,7 @@ class SyncCentersDialogViewModel @Inject constructor( checkAccountsSyncStatusAndSyncGroupAccounts() } } - }) + } } /** @@ -712,14 +695,11 @@ class SyncCentersDialogViewModel @Inject constructor( * @param loanId Loan Id */ private fun syncClientLoanAndLoanRepayment(loanId: Int) { - getLoanAndLoanRepayment(loanId) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) - } - - override fun onNext(loanAndLoanRepayment: LoanAndLoanRepayment) { + viewModelScope.launch { + getLoanAndLoanRepayment(loanId) + .catch { + onAccountSyncFailed(it) + }.collect { mLoanAndRepaymentSyncIndex += 1 if (mLoanAndRepaymentSyncIndex != mLoanAccountList.size) { mLoanAccountList[mLoanAndRepaymentSyncIndex].id?.let { @@ -732,7 +712,7 @@ class SyncCentersDialogViewModel @Inject constructor( checkAccountsSyncStatusAndSyncClientAccounts() } } - }) + } } /** @@ -747,29 +727,31 @@ class SyncCentersDialogViewModel @Inject constructor( savingsAccountId: Int, ) { getSavingsAccountAndTemplate(savingsAccountType, savingsAccountId) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + onAccountSyncFailed(e) + } - override fun onNext(savingsAccountAndTransactionTemplate: SavingsAccountAndTransactionTemplate) { - mSavingsAndTransactionSyncIndex += 1 - if (mSavingsAndTransactionSyncIndex != mSavingsAccountList.size) { - mSavingsAccountList[mSavingsAndTransactionSyncIndex] - .depositType?.endpoint?.let { - mSavingsAccountList[mSavingsAndTransactionSyncIndex].id?.let { it1 -> - syncGroupSavingsAccountAndTemplate( - it, - it1, - ) + override fun onNext(savingsAccountAndTransactionTemplate: SavingsAccountAndTransactionTemplate) { + mSavingsAndTransactionSyncIndex += 1 + if (mSavingsAndTransactionSyncIndex != mSavingsAccountList.size) { + mSavingsAccountList[mSavingsAndTransactionSyncIndex] + .depositType?.endpoint?.let { + mSavingsAccountList[mSavingsAndTransactionSyncIndex].id?.let { it1 -> + syncGroupSavingsAccountAndTemplate( + it, + it1, + ) + } } - } - } else { - mGroups[mGroupSyncIndex].id?.let { loadGroupAssociateClients(it) } + } else { + mGroups[mGroupSyncIndex].id?.let { loadGroupAssociateClients(it) } + } } - } - }) + }, + ) } /** @@ -784,29 +766,31 @@ class SyncCentersDialogViewModel @Inject constructor( savingsAccountId: Int, ) { getSavingsAccountAndTemplate(savingsAccountType, savingsAccountId) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + onAccountSyncFailed(e) + } - override fun onNext(savingsAccountAndTransactionTemplate: SavingsAccountAndTransactionTemplate) { - mSavingsAndTransactionSyncIndex += 1 - if (mSavingsAndTransactionSyncIndex != mSavingsAccountList.size) { - mSavingsAccountList[mSavingsAndTransactionSyncIndex].id?.let { - mSavingsAccountList[mSavingsAndTransactionSyncIndex] - .depositType?.endpoint?.let { it1 -> - syncClientSavingsAccountAndTemplate( - it1, - it, - ) - } + override fun onNext(savingsAccountAndTransactionTemplate: SavingsAccountAndTransactionTemplate) { + mSavingsAndTransactionSyncIndex += 1 + if (mSavingsAndTransactionSyncIndex != mSavingsAccountList.size) { + mSavingsAccountList[mSavingsAndTransactionSyncIndex].id?.let { + mSavingsAccountList[mSavingsAndTransactionSyncIndex] + .depositType?.endpoint?.let { it1 -> + syncClientSavingsAccountAndTemplate( + it1, + it, + ) + } + } + } else { + syncClient(mClients[mClientSyncIndex]) } - } else { - syncClient(mClients[mClientSyncIndex]) } - } - }) + }, + ) } private fun updateTotalSyncProgressBarAndCount() { diff --git a/feature/client/src/main/java/com/mifos/feature/client/syncClientDialog/SyncClientsDialogViewModel.kt b/feature/client/src/main/java/com/mifos/feature/client/syncClientDialog/SyncClientsDialogViewModel.kt index 4ffa4be18f..91ac6b6a96 100644 --- a/feature/client/src/main/java/com/mifos/feature/client/syncClientDialog/SyncClientsDialogViewModel.kt +++ b/feature/client/src/main/java/com/mifos/feature/client/syncClientDialog/SyncClientsDialogViewModel.kt @@ -26,6 +26,8 @@ import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import retrofit2.HttpException @@ -187,34 +189,31 @@ class SyncClientsDialogViewModel @Inject constructor( * @param loanId Loan Id */ private fun syncLoanAndLoanRepayment(loanId: Int) { - Observable.combineLatest( - repository.syncLoanById(loanId), - repository.syncLoanRepaymentTemplate(loanId), - ) { loanWithAssociations, loanRepaymentTemplate -> - val loanAndLoanRepayment = LoanAndLoanRepayment() - loanAndLoanRepayment.loanWithAssociations = loanWithAssociations - loanAndLoanRepayment.loanRepaymentTemplate = loanRepaymentTemplate - loanAndLoanRepayment - } - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) - } - - override fun onNext(loanAndLoanRepayment: LoanAndLoanRepayment) { - mLoanAndRepaymentSyncIndex += 1 - _syncClientData.update { it.copy(singleSyncCount = mLoanAndRepaymentSyncIndex) } - if (mLoanAndRepaymentSyncIndex != mLoanAccountList.size) { - checkNetworkConnectionAndSyncLoanAndLoanRepayment() - } else { - setLoanAccountSyncStatusTrue() - checkAccountsSyncStatusAndSyncAccounts() + viewModelScope.launch { + try { + combine( + repository.syncLoanById(loanId), + repository.syncLoanRepaymentTemplate(loanId), + ) { loanWithAssociations, loanRepaymentTemplate -> + LoanAndLoanRepayment().apply { + this.loanWithAssociations = loanWithAssociations + this.loanRepaymentTemplate = loanRepaymentTemplate + } + }.flowOn(Dispatchers.IO) + .collect { loanAndLoanRepayment -> + mLoanAndRepaymentSyncIndex += 1 + _syncClientData.update { it.copy(singleSyncCount = mLoanAndRepaymentSyncIndex) } + if (mLoanAndRepaymentSyncIndex != mLoanAccountList.size) { + checkNetworkConnectionAndSyncLoanAndLoanRepayment() + } else { + setLoanAccountSyncStatusTrue() + checkAccountsSyncStatusAndSyncAccounts() + } } - } - }) + } catch (e: Throwable) { + onAccountSyncFailed(e) + } + } } /** @@ -246,22 +245,24 @@ class SyncClientsDialogViewModel @Inject constructor( } .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + onAccountSyncFailed(e) + } - override fun onNext(savingsAccountAndTransactionTemplate: SavingsAccountAndTransactionTemplate) { - mSavingsAndTransactionSyncIndex += 1 - _syncClientData.update { it.copy(singleSyncCount = mLoanAndRepaymentSyncIndex + mSavingsAndTransactionSyncIndex) } - if (mSavingsAndTransactionSyncIndex != mSavingsAccountList.size) { - checkNetworkConnectionAndSyncSavingsAccountAndTransactionTemplate() - } else { - syncClient(mClientList[mClientSyncIndex]) + override fun onNext(savingsAccountAndTransactionTemplate: SavingsAccountAndTransactionTemplate) { + mSavingsAndTransactionSyncIndex += 1 + _syncClientData.update { it.copy(singleSyncCount = mLoanAndRepaymentSyncIndex + mSavingsAndTransactionSyncIndex) } + if (mSavingsAndTransactionSyncIndex != mSavingsAccountList.size) { + checkNetworkConnectionAndSyncSavingsAccountAndTransactionTemplate() + } else { + syncClient(mClientList[mClientSyncIndex]) + } } - } - }) + }, + ) } /** @@ -276,20 +277,22 @@ class SyncClientsDialogViewModel @Inject constructor( repository.syncClientInDatabase(client) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - _syncClientsDialogUiState.value = - SyncClientsDialogUiState.Error(message = e.message.toString()) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + _syncClientsDialogUiState.value = + SyncClientsDialogUiState.Error(message = e.message.toString()) + } - override fun onNext(client: Client) { - val singleSyncClientMax = maxSingleSyncClientProgressBar - _syncClientData.update { it.copy(singleSyncCount = singleSyncClientMax) } - mClientSyncIndex += 1 - syncClient() - } - }) + override fun onNext(client: Client) { + val singleSyncClientMax = maxSingleSyncClientProgressBar + _syncClientData.update { it.copy(singleSyncCount = singleSyncClientMax) } + mClientSyncIndex += 1 + syncClient() + } + }, + ) } private fun updateTotalSyncProgressBarAndCount() { diff --git a/feature/groups/src/main/java/com/mifos/feature/groups/syncGroupDialog/SyncGroupsDialogViewModel.kt b/feature/groups/src/main/java/com/mifos/feature/groups/syncGroupDialog/SyncGroupsDialogViewModel.kt index 8ffe7ae626..7c7c6f624d 100644 --- a/feature/groups/src/main/java/com/mifos/feature/groups/syncGroupDialog/SyncGroupsDialogViewModel.kt +++ b/feature/groups/src/main/java/com/mifos/feature/groups/syncGroupDialog/SyncGroupsDialogViewModel.kt @@ -27,8 +27,12 @@ import com.mifos.room.entities.zipmodels.LoanAndLoanRepayment import com.mifos.room.entities.zipmodels.SavingsAccountAndTransactionTemplate import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import retrofit2.HttpException @@ -204,25 +208,28 @@ class SyncGroupsDialogViewModel @Inject constructor( repository.syncGroupAccounts(groupId) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + onAccountSyncFailed(e) + } - override fun onNext(groupAccounts: GroupAccounts) { - mLoanAccountList = getActiveLoanAccounts( - groupAccounts.loanAccounts, - ) - mSavingsAccountList = getActiveSavingsAccounts( - groupAccounts.savingsAccounts, - ) - - // Updating UI - maxSingleSyncGroupProgressBar = mLoanAccountList.size + mSavingsAccountList.size - checkAccountsSyncStatusAndSyncAccounts() - } - }) + override fun onNext(groupAccounts: GroupAccounts) { + mLoanAccountList = getActiveLoanAccounts( + groupAccounts.loanAccounts, + ) + mSavingsAccountList = getActiveSavingsAccounts( + groupAccounts.savingsAccounts, + ) + + // Updating UI + maxSingleSyncGroupProgressBar = + mLoanAccountList.size + mSavingsAccountList.size + checkAccountsSyncStatusAndSyncAccounts() + } + }, + ) } /** @@ -235,14 +242,12 @@ class SyncGroupsDialogViewModel @Inject constructor( * @param loanId Loan Id */ private fun syncLoanAndLoanRepayment(loanId: Int) { - getLoanAndLoanRepayment(loanId) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { + viewModelScope.launch { + getLoanAndLoanRepaymentNew(loanId) + .flowOn(Dispatchers.IO) + .catch { e -> onAccountSyncFailed(e) - } - - override fun onNext(loanAndLoanRepayment: LoanAndLoanRepayment) { + }.collect { mLoanAndRepaymentSyncIndex += 1 _syncGroupData.update { it.copy(singleSyncCount = mLoanAndRepaymentSyncIndex) } @@ -253,7 +258,7 @@ class SyncGroupsDialogViewModel @Inject constructor( checkAccountsSyncStatusAndSyncAccounts() } } - }) + } } /** @@ -265,22 +270,24 @@ class SyncGroupsDialogViewModel @Inject constructor( */ private fun syncSavingsAccountAndTemplate(savingsAccountType: String, savingsAccountId: Int) { getSavingsAccountAndTemplate(savingsAccountType, savingsAccountId) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + onAccountSyncFailed(e) + } - override fun onNext(savingsAccountAndTransactionTemplate: SavingsAccountAndTransactionTemplate) { - mSavingsAndTransactionSyncIndex += 1 - _syncGroupData.update { it.copy(singleSyncCount = mLoanAndRepaymentSyncIndex + mSavingsAndTransactionSyncIndex) } - if (mSavingsAndTransactionSyncIndex != mSavingsAccountList.size) { - checkNetworkConnectionAndSyncSavingsAccountAndTransactionTemplate() - } else { - mGroupList[mGroupSyncIndex].id?.let { loadGroupAssociateClients(it) } + override fun onNext(savingsAccountAndTransactionTemplate: SavingsAccountAndTransactionTemplate) { + mSavingsAndTransactionSyncIndex += 1 + _syncGroupData.update { it.copy(singleSyncCount = mLoanAndRepaymentSyncIndex + mSavingsAndTransactionSyncIndex) } + if (mSavingsAndTransactionSyncIndex != mSavingsAccountList.size) { + checkNetworkConnectionAndSyncSavingsAccountAndTransactionTemplate() + } else { + mGroupList[mGroupSyncIndex].id?.let { loadGroupAssociateClients(it) } + } } - } - }) + }, + ) } /** @@ -293,24 +300,26 @@ class SyncGroupsDialogViewModel @Inject constructor( repository.getGroupWithAssociations(groupId) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + onAccountSyncFailed(e) + } - override fun onNext(groupWithAssociations: GroupWithAssociations) { - mClients = groupWithAssociations.clientMembers - mClientSyncIndex = 0 - resetIndexes() - if (mClients.isNotEmpty()) { - _syncGroupData.update { it.copy(totalClientSyncCount = mClients.size) } - syncClientAccounts(mClients[mClientSyncIndex].id) - } else { - syncGroup(mGroupList[mGroupSyncIndex]) + override fun onNext(groupWithAssociations: GroupWithAssociations) { + mClients = groupWithAssociations.clientMembers + mClientSyncIndex = 0 + resetIndexes() + if (mClients.isNotEmpty()) { + _syncGroupData.update { it.copy(totalClientSyncCount = mClients.size) } + syncClientAccounts(mClients[mClientSyncIndex].id) + } else { + syncGroup(mGroupList[mGroupSyncIndex]) + } } - } - }) + }, + ) } /** @@ -325,24 +334,26 @@ class SyncGroupsDialogViewModel @Inject constructor( repository.syncClientInDatabase(client) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - _syncGroupsDialogUiState.value = - SyncGroupsDialogUiState.Error(message = e.message.toString()) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + _syncGroupsDialogUiState.value = + SyncGroupsDialogUiState.Error(message = e.message.toString()) + } - override fun onNext(client: Client) { - resetIndexes() - mClientSyncIndex += 1 - _syncGroupData.update { it.copy(clientSyncCount = mClientSyncIndex) } - if (mClients.size == mClientSyncIndex) { - syncGroup(mGroupList[mGroupSyncIndex]) - } else { - syncClientAccounts(mClients[mClientSyncIndex].id) + override fun onNext(client: Client) { + resetIndexes() + mClientSyncIndex += 1 + _syncGroupData.update { it.copy(clientSyncCount = mClientSyncIndex) } + if (mClients.size == mClientSyncIndex) { + syncGroup(mGroupList[mGroupSyncIndex]) + } else { + syncClientAccounts(mClients[mClientSyncIndex].id) + } } - } - }) + }, + ) } /** @@ -405,25 +416,43 @@ class SyncGroupsDialogViewModel @Inject constructor( * @param loanId Loan Id */ private fun syncClientLoanAndLoanRepayment(loanId: Int) { - getLoanAndLoanRepayment(loanId) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) - } - - override fun onNext(loanAndLoanRepayment: LoanAndLoanRepayment) { - mLoanAndRepaymentSyncIndex += 1 - if (mLoanAndRepaymentSyncIndex != mLoanAccountList.size) { - mLoanAccountList[mLoanAndRepaymentSyncIndex].id?.let { - syncClientLoanAndLoanRepayment(it) - } - } else { - setLoanAccountSyncStatusTrue() - checkAccountsSyncStatusAndSyncClientAccounts() + viewModelScope.launch { + try { + getLoanAndLoanRepaymentNew(loanId) + + mLoanAndRepaymentSyncIndex += 1 + if (mLoanAndRepaymentSyncIndex != mLoanAccountList.size) { + mLoanAccountList[mLoanAndRepaymentSyncIndex].id?.let { + syncClientLoanAndLoanRepayment(it) } + } else { + setLoanAccountSyncStatusTrue() + checkAccountsSyncStatusAndSyncClientAccounts() } - }) + } catch (e: Exception) { + onAccountSyncFailed(e) + } + } +// getLoanAndLoanRepayment(loanId) +// .subscribe( +// object : Subscriber() { +// override fun onCompleted() {} +// override fun onError(e: Throwable) { +// } +// +// override fun onNext(loanAndLoanRepayment: LoanAndLoanRepayment) { +// mLoanAndRepaymentSyncIndex += 1 +// if (mLoanAndRepaymentSyncIndex != mLoanAccountList.size) { +// mLoanAccountList[mLoanAndRepaymentSyncIndex].id?.let { +// syncClientLoanAndLoanRepayment(it) +// } +// } else { +// setLoanAccountSyncStatusTrue() +// checkAccountsSyncStatusAndSyncClientAccounts() +// } +// } +// }, +// ) } /** @@ -438,29 +467,31 @@ class SyncGroupsDialogViewModel @Inject constructor( savingsAccountId: Int, ) { getSavingsAccountAndTemplate(savingsAccountType, savingsAccountId) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - onAccountSyncFailed(e) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + onAccountSyncFailed(e) + } - override fun onNext(savingsAccountAndTransactionTemplate: SavingsAccountAndTransactionTemplate) { - mSavingsAndTransactionSyncIndex += 1 - if (mSavingsAndTransactionSyncIndex != mSavingsAccountList.size) { - mSavingsAccountList[mSavingsAndTransactionSyncIndex] - .depositType?.endpoint?.let { - mSavingsAccountList[mSavingsAndTransactionSyncIndex].id?.let { it1 -> - syncClientSavingsAccountAndTemplate( - it, - it1, - ) + override fun onNext(savingsAccountAndTransactionTemplate: SavingsAccountAndTransactionTemplate) { + mSavingsAndTransactionSyncIndex += 1 + if (mSavingsAndTransactionSyncIndex != mSavingsAccountList.size) { + mSavingsAccountList[mSavingsAndTransactionSyncIndex] + .depositType?.endpoint?.let { + mSavingsAccountList[mSavingsAndTransactionSyncIndex].id?.let { it1 -> + syncClientSavingsAccountAndTemplate( + it, + it1, + ) + } } - } - } else { - syncClient(mClients[mClientSyncIndex]) + } else { + syncClient(mClients[mClientSyncIndex]) + } } - } - }) + }, + ) } /** @@ -488,8 +519,8 @@ class SyncGroupsDialogViewModel @Inject constructor( * @param loanId Loan Id * @return LoanAndLoanRepayment */ - private fun getLoanAndLoanRepayment(loanId: Int): Observable { - return Observable.combineLatest( + private fun getLoanAndLoanRepaymentNew(loanId: Int): Flow { + return combine( repository.syncLoanById(loanId), repository.syncLoanRepaymentTemplate(loanId), ) { loanWithAssociations, loanRepaymentTemplate -> @@ -497,9 +528,7 @@ class SyncGroupsDialogViewModel @Inject constructor( loanWithAssociations, loanRepaymentTemplate, ) - } - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) + }.flowOn(Dispatchers.IO) } /** diff --git a/feature/loan/src/main/java/com/mifos/feature/loan/loanAccountSummary/LoanAccountSummaryScreen.kt b/feature/loan/src/main/java/com/mifos/feature/loan/loanAccountSummary/LoanAccountSummaryScreen.kt index edff01b1d8..dd220c1c8d 100644 --- a/feature/loan/src/main/java/com/mifos/feature/loan/loanAccountSummary/LoanAccountSummaryScreen.kt +++ b/feature/loan/src/main/java/com/mifos/feature/loan/loanAccountSummary/LoanAccountSummaryScreen.kt @@ -67,10 +67,10 @@ import com.mifos.core.designsystem.theme.BluePrimary import com.mifos.core.designsystem.theme.BluePrimaryDark import com.mifos.core.designsystem.theme.DarkGray import com.mifos.core.designsystem.theme.White -import com.mifos.core.entity.accounts.loan.LoanWithAssociations -import com.mifos.core.entity.accounts.loan.Status -import com.mifos.core.entity.accounts.loan.Summary import com.mifos.feature.loan.R +import com.mifos.room.entities.accounts.loans.LoanWithAssociations +import com.mifos.room.entities.accounts.loans.Status +import com.mifos.room.entities.accounts.loans.Summary /** * Created by Pronay Sarker on 01/07/2024 (5:50 AM) @@ -99,7 +99,12 @@ internal fun LoanAccountSummaryScreen( uiState = uiState, navigateBack = navigateBack, onRetry = { viewModel.loadLoanById(loanAccountNumber) }, - onMoreInfoClicked = { onMoreInfoClicked.invoke(Constants.DATA_TABLE_NAME_LOANS, loanAccountNumber) }, + onMoreInfoClicked = { + onMoreInfoClicked.invoke( + Constants.DATA_TABLE_NAME_LOANS, + loanAccountNumber, + ) + }, onTransactionsClicked = { onTransactionsClicked.invoke(loanAccountNumber) }, onRepaymentScheduleClicked = { onRepaymentScheduleClicked.invoke(loanAccountNumber) }, onDocumentsClicked = { onDocumentsClicked(loanAccountNumber) }, @@ -563,7 +568,8 @@ private fun getInflateLoanSummaryValue(status: Status): Boolean { } } -private class LoanAccountSummaryPreviewProvider : PreviewParameterProvider { +private class LoanAccountSummaryPreviewProvider : + PreviewParameterProvider { private val demoSummary = Summary( loanId = 12345, principalDisbursed = 10000.0, diff --git a/feature/loan/src/main/java/com/mifos/feature/loan/loanAccountSummary/LoanAccountSummaryUiState.kt b/feature/loan/src/main/java/com/mifos/feature/loan/loanAccountSummary/LoanAccountSummaryUiState.kt index 0ac262dddd..af0c00daaf 100644 --- a/feature/loan/src/main/java/com/mifos/feature/loan/loanAccountSummary/LoanAccountSummaryUiState.kt +++ b/feature/loan/src/main/java/com/mifos/feature/loan/loanAccountSummary/LoanAccountSummaryUiState.kt @@ -9,7 +9,7 @@ */ package com.mifos.feature.loan.loanAccountSummary -import com.mifos.core.entity.accounts.loan.LoanWithAssociations +import com.mifos.room.entities.accounts.loans.LoanWithAssociations /** * Created by Aditya Gupta on 08/08/23. diff --git a/feature/loan/src/main/java/com/mifos/feature/loan/loanAccountSummary/LoanAccountSummaryViewModel.kt b/feature/loan/src/main/java/com/mifos/feature/loan/loanAccountSummary/LoanAccountSummaryViewModel.kt index e28bcf9812..32eec06e43 100644 --- a/feature/loan/src/main/java/com/mifos/feature/loan/loanAccountSummary/LoanAccountSummaryViewModel.kt +++ b/feature/loan/src/main/java/com/mifos/feature/loan/loanAccountSummary/LoanAccountSummaryViewModel.kt @@ -9,17 +9,18 @@ */ package com.mifos.feature.loan.loanAccountSummary +import android.util.Log import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.mifos.core.common.utils.Constants import com.mifos.core.data.repository.LoanAccountSummaryRepository -import com.mifos.core.entity.accounts.loan.LoanWithAssociations +import com.mifos.room.entities.accounts.loans.LoanWithAssociations import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow -import rx.Subscriber -import rx.android.schedulers.AndroidSchedulers -import rx.schedulers.Schedulers +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.launch import javax.inject.Inject /** @@ -27,11 +28,12 @@ import javax.inject.Inject */ @HiltViewModel class LoanAccountSummaryViewModel @Inject constructor( - private val repository: LoanAccountSummaryRepository, savedStateHandle: SavedStateHandle, + private val repository: LoanAccountSummaryRepository, ) : ViewModel() { - val loanAccountNumber = savedStateHandle.getStateFlow(key = Constants.LOAN_ACCOUNT_NUMBER, initialValue = 0) + val loanAccountNumber = + savedStateHandle.getStateFlow(key = Constants.LOAN_ACCOUNT_NUMBER, initialValue = 0) private val _loanAccountSummaryUiState = MutableStateFlow(LoanAccountSummaryUiState.ShowProgressbar) @@ -40,24 +42,20 @@ class LoanAccountSummaryViewModel @Inject constructor( get() = _loanAccountSummaryUiState fun loadLoanById(loanAccountNumber: Int) { - _loanAccountSummaryUiState.value = LoanAccountSummaryUiState.ShowProgressbar - repository.getLoanById(loanAccountNumber) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { + viewModelScope.launch { + _loanAccountSummaryUiState.value = LoanAccountSummaryUiState.ShowProgressbar + + repository.getLoanById(loanAccountNumber) + .catch { + Log.d("ErrorDebug", it.message.toString()) _loanAccountSummaryUiState.value = LoanAccountSummaryUiState.ShowFetchingError("Loan Account not found.") - } - - override fun onNext(loanWithAssociations: LoanWithAssociations?) { - _loanAccountSummaryUiState.value = loanWithAssociations?.let { + }.collect { loanWithAssociations -> + _loanAccountSummaryUiState.value = LoanAccountSummaryUiState.ShowLoanById( - it, + loanWithAssociations ?: LoanWithAssociations(), ) - }!! } - }) + } } } diff --git a/feature/loan/src/main/java/com/mifos/feature/loan/loanApproval/LoanAccountApprovalScreen.kt b/feature/loan/src/main/java/com/mifos/feature/loan/loanApproval/LoanAccountApprovalScreen.kt index 58f82b4b92..40747a644b 100644 --- a/feature/loan/src/main/java/com/mifos/feature/loan/loanApproval/LoanAccountApprovalScreen.kt +++ b/feature/loan/src/main/java/com/mifos/feature/loan/loanApproval/LoanAccountApprovalScreen.kt @@ -59,10 +59,10 @@ import com.mifos.core.designsystem.component.MifosScaffold import com.mifos.core.designsystem.icon.MifosIcons import com.mifos.core.designsystem.theme.BluePrimary import com.mifos.core.designsystem.theme.BluePrimaryDark -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.network.GenericResponse import com.mifos.core.objects.account.loan.LoanApproval import com.mifos.feature.loan.R +import com.mifos.room.entities.accounts.loans.LoanWithAssociations import java.text.SimpleDateFormat import java.util.Locale diff --git a/feature/loan/src/main/java/com/mifos/feature/loan/loanRepayment/LoanRepaymentScreen.kt b/feature/loan/src/main/java/com/mifos/feature/loan/loanRepayment/LoanRepaymentScreen.kt index 44ecf7f695..2700e3978c 100644 --- a/feature/loan/src/main/java/com/mifos/feature/loan/loanRepayment/LoanRepaymentScreen.kt +++ b/feature/loan/src/main/java/com/mifos/feature/loan/loanRepayment/LoanRepaymentScreen.kt @@ -69,11 +69,11 @@ import com.mifos.core.designsystem.theme.Black import com.mifos.core.designsystem.theme.BluePrimary import com.mifos.core.designsystem.theme.BluePrimaryDark import com.mifos.core.designsystem.theme.DarkGray -import com.mifos.core.entity.accounts.loan.LoanRepaymentRequest -import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate -import com.mifos.core.objects.account.loan.LoanRepaymentResponse import com.mifos.feature.loan.R import com.mifos.room.entities.PaymentTypeOption +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest +import com.mifos.room.entities.accounts.loans.LoanRepaymentResponse +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate import java.text.SimpleDateFormat import java.util.Locale @@ -479,20 +479,19 @@ private fun ShowLoanRepaymentConfirmationDialog( onClick = { onDismiss() if (Network.isOnline(context)) { - val request = LoanRepaymentRequest() - - request.accountNumber = loanAccountNumber - request.paymentTypeId = paymentTypeId - request.dateFormat = "dd MM yyyy" - request.locale = "en" - request.transactionAmount = total - request.transactionDate = SimpleDateFormat( - "dd MMMM yyyy", - Locale.getDefault(), - ).format( - repaymentDate, + val request = LoanRepaymentRequest( + accountNumber = loanAccountNumber, + paymentTypeId = paymentTypeId, + dateFormat = "dd MM yyyy", + locale = "en", + transactionAmount = total, + transactionDate = SimpleDateFormat( + "dd MMMM yyyy", + Locale.getDefault(), + ).format( + repaymentDate, + ), ) - submitPayment.invoke(request) } else { Toast.makeText( diff --git a/feature/loan/src/main/java/com/mifos/feature/loan/loanRepayment/LoanRepaymentUiState.kt b/feature/loan/src/main/java/com/mifos/feature/loan/loanRepayment/LoanRepaymentUiState.kt index ff53b92514..6c6a060e73 100644 --- a/feature/loan/src/main/java/com/mifos/feature/loan/loanRepayment/LoanRepaymentUiState.kt +++ b/feature/loan/src/main/java/com/mifos/feature/loan/loanRepayment/LoanRepaymentUiState.kt @@ -9,8 +9,8 @@ */ package com.mifos.feature.loan.loanRepayment -import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate -import com.mifos.core.objects.account.loan.LoanRepaymentResponse +import com.mifos.room.entities.accounts.loans.LoanRepaymentResponse +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate /** * Created by Aditya Gupta on 10/08/23. diff --git a/feature/loan/src/main/java/com/mifos/feature/loan/loanRepayment/LoanRepaymentViewModel.kt b/feature/loan/src/main/java/com/mifos/feature/loan/loanRepayment/LoanRepaymentViewModel.kt index 28a9235658..1b18322a2d 100644 --- a/feature/loan/src/main/java/com/mifos/feature/loan/loanRepayment/LoanRepaymentViewModel.kt +++ b/feature/loan/src/main/java/com/mifos/feature/loan/loanRepayment/LoanRepaymentViewModel.kt @@ -12,20 +12,21 @@ package com.mifos.feature.loan.loanRepayment import android.util.Log import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.google.gson.Gson import com.mifos.core.common.utils.Constants import com.mifos.core.data.repository.LoanRepaymentRepository -import com.mifos.core.entity.accounts.loan.LoanRepaymentRequest import com.mifos.core.entity.accounts.loan.LoanWithAssociations -import com.mifos.core.entity.templates.loans.LoanRepaymentTemplate -import com.mifos.core.objects.account.loan.LoanRepaymentResponse import com.mifos.feature.loan.R +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest +import com.mifos.room.entities.templates.loans.LoanRepaymentTemplate import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow -import rx.Subscriber -import rx.android.schedulers.AndroidSchedulers -import rx.schedulers.Schedulers +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.flowOn +import kotlinx.coroutines.launch import javax.inject.Inject /** @@ -33,12 +34,14 @@ import javax.inject.Inject */ @HiltViewModel class LoanRepaymentViewModel @Inject constructor( - private val repository: LoanRepaymentRepository, savedStateHandle: SavedStateHandle, + private val repository: LoanRepaymentRepository, ) : ViewModel() { - val arg = savedStateHandle.getStateFlow(key = Constants.LOAN_WITH_ASSOCIATIONS, initialValue = "") - val loanWithAssociations: LoanWithAssociations = Gson().fromJson(arg.value, LoanWithAssociations::class.java) + val arg = + savedStateHandle.getStateFlow(key = Constants.LOAN_WITH_ASSOCIATIONS, initialValue = "") + val loanWithAssociations: LoanWithAssociations = + Gson().fromJson(arg.value, LoanWithAssociations::class.java) private val _loanRepaymentUiState = MutableStateFlow(LoanRepaymentUiState.ShowProgressbar) @@ -51,66 +54,56 @@ class LoanRepaymentViewModel @Inject constructor( var amountInArrears = loanWithAssociations.summary.totalOverdue fun loanLoanRepaymentTemplate() { - _loanRepaymentUiState.value = LoanRepaymentUiState.ShowProgressbar - repository.getLoanRepayTemplate(loanId) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() { - } + viewModelScope.launch { + _loanRepaymentUiState.value = LoanRepaymentUiState.ShowProgressbar - override fun onError(e: Throwable) { - Log.d("loanRepaymentLog ", ", $loanId $e") + repository.getLoanRepayTemplate(loanId) + .catch { + Log.d("loanRepaymentLog ", ", $loanId ${it.message}") _loanRepaymentUiState.value = LoanRepaymentUiState.ShowError(R.string.feature_loan_failed_to_load_loan_repayment) } - - override fun onNext(loanRepaymentTemplate: LoanRepaymentTemplate?) { + .collect { _loanRepaymentUiState.value = LoanRepaymentUiState.ShowLoanRepayTemplate( - loanRepaymentTemplate ?: LoanRepaymentTemplate(), + it ?: LoanRepaymentTemplate(), ) } - }) + } } /** * app crashes on submit click */ fun submitPayment(request: LoanRepaymentRequest) { - _loanRepaymentUiState.value = LoanRepaymentUiState.ShowProgressbar - repository.submitPayment(loanId, request) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() { - } - - override fun onError(e: Throwable) { - _loanRepaymentUiState.value = - LoanRepaymentUiState.ShowError(R.string.feature_loan_payment_failed) - } + viewModelScope.launch { + _loanRepaymentUiState.value = LoanRepaymentUiState.ShowProgressbar - override fun onNext(loanRepaymentResponse: LoanRepaymentResponse?) { - _loanRepaymentUiState.value = - LoanRepaymentUiState.ShowPaymentSubmittedSuccessfully(loanRepaymentResponse) - } - }) + try { + val loanRepaymentResponse = repository.submitPayment(loanId, request) + _loanRepaymentUiState.value = + LoanRepaymentUiState.ShowPaymentSubmittedSuccessfully( + loanRepaymentResponse, + ) + } catch (e: Exception) { + _loanRepaymentUiState.value = + LoanRepaymentUiState.ShowError(R.string.feature_loan_payment_failed) + } + } } fun checkDatabaseLoanRepaymentByLoanId() { - _loanRepaymentUiState.value = LoanRepaymentUiState.ShowProgressbar - repository.getDatabaseLoanRepaymentByLoanId(loanId) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { + viewModelScope.launch { + _loanRepaymentUiState.value = LoanRepaymentUiState.ShowProgressbar + + repository.getDatabaseLoanRepaymentByLoanId(loanId) + .flowOn(Dispatchers.IO) + .catch { + Log.d("loanRepaymentLog ", ", $loanId ${it.message}") _loanRepaymentUiState.value = LoanRepaymentUiState.ShowError(R.string.feature_loan_failed_to_load_loan_repayment) } - - override fun onNext(loanRepaymentRequest: LoanRepaymentRequest?) { + .collect { loanRepaymentRequest -> if (loanRepaymentRequest != null) { _loanRepaymentUiState.value = LoanRepaymentUiState.ShowLoanRepaymentExistInDatabase @@ -119,6 +112,6 @@ class LoanRepaymentViewModel @Inject constructor( LoanRepaymentUiState.ShowLoanRepaymentDoesNotExistInDatabase } } - }) + } } } diff --git a/feature/loan/src/main/java/com/mifos/feature/loan/loanRepaymentSchedule/LoanRepaymentScheduleScreen.kt b/feature/loan/src/main/java/com/mifos/feature/loan/loanRepaymentSchedule/LoanRepaymentScheduleScreen.kt index ebbc46ff36..2f29451b49 100644 --- a/feature/loan/src/main/java/com/mifos/feature/loan/loanRepaymentSchedule/LoanRepaymentScheduleScreen.kt +++ b/feature/loan/src/main/java/com/mifos/feature/loan/loanRepaymentSchedule/LoanRepaymentScheduleScreen.kt @@ -45,10 +45,10 @@ import com.mifos.core.designsystem.component.MifosCircularProgress import com.mifos.core.designsystem.component.MifosScaffold import com.mifos.core.designsystem.component.MifosSweetError import com.mifos.core.designsystem.icon.MifosIcons -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.objects.account.loan.Period import com.mifos.core.objects.account.loan.RepaymentSchedule import com.mifos.feature.loan.R +import com.mifos.room.entities.accounts.loans.LoanWithAssociations /** * Created by Pronay Sarker on 03/07/2024 (9:18 AM) diff --git a/feature/loan/src/main/java/com/mifos/feature/loan/loanRepaymentSchedule/LoanRepaymentScheduleUiState.kt b/feature/loan/src/main/java/com/mifos/feature/loan/loanRepaymentSchedule/LoanRepaymentScheduleUiState.kt index 3a37298e11..c554be7c18 100644 --- a/feature/loan/src/main/java/com/mifos/feature/loan/loanRepaymentSchedule/LoanRepaymentScheduleUiState.kt +++ b/feature/loan/src/main/java/com/mifos/feature/loan/loanRepaymentSchedule/LoanRepaymentScheduleUiState.kt @@ -9,7 +9,7 @@ */ package com.mifos.feature.loan.loanRepaymentSchedule -import com.mifos.core.entity.accounts.loan.LoanWithAssociations +import com.mifos.room.entities.accounts.loans.LoanWithAssociations /** * Created by Aditya Gupta on 12/08/23. diff --git a/feature/loan/src/main/java/com/mifos/feature/loan/loanRepaymentSchedule/LoanRepaymentScheduleViewModel.kt b/feature/loan/src/main/java/com/mifos/feature/loan/loanRepaymentSchedule/LoanRepaymentScheduleViewModel.kt index ae3ae27c53..22e0e54796 100644 --- a/feature/loan/src/main/java/com/mifos/feature/loan/loanRepaymentSchedule/LoanRepaymentScheduleViewModel.kt +++ b/feature/loan/src/main/java/com/mifos/feature/loan/loanRepaymentSchedule/LoanRepaymentScheduleViewModel.kt @@ -13,7 +13,7 @@ import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import com.mifos.core.common.utils.Constants import com.mifos.core.data.repository.LoanRepaymentScheduleRepository -import com.mifos.core.entity.accounts.loan.LoanWithAssociations +import com.mifos.room.entities.accounts.loans.LoanWithAssociations import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow @@ -31,7 +31,8 @@ class LoanRepaymentScheduleViewModel @Inject constructor( savedStateHandle: SavedStateHandle, ) : ViewModel() { - val loanId = savedStateHandle.getStateFlow(key = Constants.LOAN_ACCOUNT_NUMBER, initialValue = 0) + val loanId = + savedStateHandle.getStateFlow(key = Constants.LOAN_ACCOUNT_NUMBER, initialValue = 0) private val _loanRepaymentScheduleUiState = MutableStateFlow(LoanRepaymentScheduleUiState.ShowProgressbar) @@ -42,19 +43,21 @@ class LoanRepaymentScheduleViewModel @Inject constructor( repository.getLoanRepaySchedule(loanId) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() { - } + .subscribe( + object : Subscriber() { + override fun onCompleted() { + } - override fun onError(e: Throwable) { - _loanRepaymentScheduleUiState.value = - LoanRepaymentScheduleUiState.ShowFetchingError(e.message.toString()) - } + override fun onError(e: Throwable) { + _loanRepaymentScheduleUiState.value = + LoanRepaymentScheduleUiState.ShowFetchingError(e.message.toString()) + } - override fun onNext(loanWithAssociations: LoanWithAssociations) { - _loanRepaymentScheduleUiState.value = - LoanRepaymentScheduleUiState.ShowLoanRepaySchedule(loanWithAssociations) - } - }) + override fun onNext(loanWithAssociations: LoanWithAssociations) { + _loanRepaymentScheduleUiState.value = + LoanRepaymentScheduleUiState.ShowLoanRepaySchedule(loanWithAssociations) + } + }, + ) } } diff --git a/feature/loan/src/main/java/com/mifos/feature/loan/loanTransaction/LoanTransactionsScreen.kt b/feature/loan/src/main/java/com/mifos/feature/loan/loanTransaction/LoanTransactionsScreen.kt index 3cb2461a39..7e9f68db2c 100644 --- a/feature/loan/src/main/java/com/mifos/feature/loan/loanTransaction/LoanTransactionsScreen.kt +++ b/feature/loan/src/main/java/com/mifos/feature/loan/loanTransaction/LoanTransactionsScreen.kt @@ -64,11 +64,11 @@ import com.mifos.core.designsystem.component.MifosSweetError import com.mifos.core.designsystem.icon.MifosIcons import com.mifos.core.designsystem.theme.Black import com.mifos.core.designsystem.theme.White -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.objects.account.loan.Transaction import com.mifos.core.objects.account.loan.Type import com.mifos.core.ui.components.MifosEmptyUi import com.mifos.feature.loan.R +import com.mifos.room.entities.accounts.loans.LoanWithAssociations /** * Created by Pronay Sarker on 04/07/2024 (11:31 AM) diff --git a/feature/loan/src/main/java/com/mifos/feature/loan/loanTransaction/LoanTransactionsUiState.kt b/feature/loan/src/main/java/com/mifos/feature/loan/loanTransaction/LoanTransactionsUiState.kt index 39dcd8956c..ff66de352f 100644 --- a/feature/loan/src/main/java/com/mifos/feature/loan/loanTransaction/LoanTransactionsUiState.kt +++ b/feature/loan/src/main/java/com/mifos/feature/loan/loanTransaction/LoanTransactionsUiState.kt @@ -9,7 +9,7 @@ */ package com.mifos.feature.loan.loanTransaction -import com.mifos.core.entity.accounts.loan.LoanWithAssociations +import com.mifos.room.entities.accounts.loans.LoanWithAssociations /** * Created by Aditya Gupta on 12/08/23. diff --git a/feature/loan/src/main/java/com/mifos/feature/loan/loanTransaction/LoanTransactionsViewModel.kt b/feature/loan/src/main/java/com/mifos/feature/loan/loanTransaction/LoanTransactionsViewModel.kt index 2295228af8..f5c2a895db 100644 --- a/feature/loan/src/main/java/com/mifos/feature/loan/loanTransaction/LoanTransactionsViewModel.kt +++ b/feature/loan/src/main/java/com/mifos/feature/loan/loanTransaction/LoanTransactionsViewModel.kt @@ -13,7 +13,7 @@ import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import com.mifos.core.common.utils.Constants import com.mifos.core.data.repository.LoanTransactionsRepository -import com.mifos.core.entity.accounts.loan.LoanWithAssociations +import com.mifos.room.entities.accounts.loans.LoanWithAssociations import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow @@ -31,7 +31,8 @@ class LoanTransactionsViewModel @Inject constructor( savedStateHandle: SavedStateHandle, ) : ViewModel() { - val loanId = savedStateHandle.getStateFlow(key = Constants.LOAN_ACCOUNT_NUMBER, initialValue = 0) + val loanId = + savedStateHandle.getStateFlow(key = Constants.LOAN_ACCOUNT_NUMBER, initialValue = 0) private val _loanTransactionsUiState = MutableStateFlow(LoanTransactionsUiState.ShowProgressBar) @@ -42,17 +43,19 @@ class LoanTransactionsViewModel @Inject constructor( repository.getLoanTransactions(loanId) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - _loanTransactionsUiState.value = - LoanTransactionsUiState.ShowFetchingError(e.message.toString()) - } + .subscribe( + object : Subscriber() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + _loanTransactionsUiState.value = + LoanTransactionsUiState.ShowFetchingError(e.message.toString()) + } - override fun onNext(loanWithAssociations: LoanWithAssociations) { - _loanTransactionsUiState.value = - LoanTransactionsUiState.ShowLoanTransaction(loanWithAssociations) - } - }) + override fun onNext(loanWithAssociations: LoanWithAssociations) { + _loanTransactionsUiState.value = + LoanTransactionsUiState.ShowLoanTransaction(loanWithAssociations) + } + }, + ) } } diff --git a/feature/loan/src/main/java/com/mifos/feature/loan/navigation/LoanNavigation.kt b/feature/loan/src/main/java/com/mifos/feature/loan/navigation/LoanNavigation.kt index 71791c0b04..5efdb09544 100644 --- a/feature/loan/src/main/java/com/mifos/feature/loan/navigation/LoanNavigation.kt +++ b/feature/loan/src/main/java/com/mifos/feature/loan/navigation/LoanNavigation.kt @@ -16,7 +16,6 @@ import androidx.navigation.compose.composable import androidx.navigation.navArgument import androidx.navigation.navigation import com.mifos.core.common.utils.Constants -import com.mifos.core.entity.accounts.loan.LoanWithAssociations import com.mifos.core.entity.noncore.DataTable import com.mifos.core.network.model.LoansPayload import com.mifos.feature.loan.groupLoanAccount.GroupLoanAccountScreen @@ -28,6 +27,7 @@ import com.mifos.feature.loan.loanDisbursement.LoanAccountDisbursementScreen import com.mifos.feature.loan.loanRepayment.LoanRepaymentScreen import com.mifos.feature.loan.loanRepaymentSchedule.LoanRepaymentScheduleScreen import com.mifos.feature.loan.loanTransaction.LoanTransactionsScreen +import com.mifos.room.entities.accounts.loans.LoanWithAssociations /** * Created by Pronay Sarker on 16/08/2024 (2:24 AM) diff --git a/feature/loan/src/main/java/com/mifos/feature/loan/navigation/LoanScreens.kt b/feature/loan/src/main/java/com/mifos/feature/loan/navigation/LoanScreens.kt index 911b5de456..92f9fd4137 100644 --- a/feature/loan/src/main/java/com/mifos/feature/loan/navigation/LoanScreens.kt +++ b/feature/loan/src/main/java/com/mifos/feature/loan/navigation/LoanScreens.kt @@ -12,7 +12,7 @@ package com.mifos.feature.loan.navigation import com.google.gson.Gson import com.mifos.core.common.utils.Constants import com.mifos.core.entity.accounts.loan.LoanApprovalData -import com.mifos.core.entity.accounts.loan.LoanWithAssociations +import com.mifos.room.entities.accounts.loans.LoanWithAssociations /** * Created by Pronay Sarker on 16/08/2024 (2:25 AM) diff --git a/feature/offline/src/main/java/com/mifos/feature/offline/dashboard/OfflineDashboardViewModel.kt b/feature/offline/src/main/java/com/mifos/feature/offline/dashboard/OfflineDashboardViewModel.kt index 5dd1f97946..d8c2d41e9f 100644 --- a/feature/offline/src/main/java/com/mifos/feature/offline/dashboard/OfflineDashboardViewModel.kt +++ b/feature/offline/src/main/java/com/mifos/feature/offline/dashboard/OfflineDashboardViewModel.kt @@ -12,7 +12,6 @@ package com.mifos.feature.offline.dashboard import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.mifos.core.data.repository.OfflineDashboardRepository -import com.mifos.core.entity.accounts.loan.LoanRepaymentRequest import com.mifos.core.entity.accounts.savings.SavingsAccountTransactionRequest import com.mifos.core.entity.center.CenterPayload import com.mifos.core.entity.client.ClientPayload @@ -21,6 +20,10 @@ import com.mifos.feature.offline.R import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.observeOn +import kotlinx.coroutines.flow.subscribe +import kotlinx.coroutines.flow.subscribeOn import kotlinx.coroutines.launch import rx.Subscriber import rx.android.schedulers.AndroidSchedulers @@ -32,87 +35,94 @@ class OfflineDashboardViewModel @Inject constructor( private val repository: OfflineDashboardRepository, ) : ViewModel() { - private val _offlineDashboardUiState = MutableStateFlow(OfflineDashboardUiState.SyncUiState(initGetSyncData())) + private val _offlineDashboardUiState = + MutableStateFlow(OfflineDashboardUiState.SyncUiState(initGetSyncData())) val offlineDashboardUiState: StateFlow = _offlineDashboardUiState fun loadDatabaseClientPayload() { repository.allDatabaseClientPayload() .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber>() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - setError(Type.SYNC_CLIENTS, e.message.toString()) - } - - override fun onNext(clientPayloads: List) { - setCountOfSyncData(Type.SYNC_CLIENTS, clientPayloads.size) - } - }) + .subscribe( + object : Subscriber>() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + setError(Type.SYNC_CLIENTS, e.message.toString()) + } + + override fun onNext(clientPayloads: List) { + setCountOfSyncData(Type.SYNC_CLIENTS, clientPayloads.size) + } + }, + ) } fun loadDatabaseGroupPayload() { repository.allDatabaseGroupPayload() .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber>() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - setError(Type.SYNC_GROUPS, e.message.toString()) - } - - override fun onNext(groupPayloads: List) { - setCountOfSyncData(Type.SYNC_GROUPS, groupPayloads.size) - } - }) + .subscribe( + object : Subscriber>() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + setError(Type.SYNC_GROUPS, e.message.toString()) + } + + override fun onNext(groupPayloads: List) { + setCountOfSyncData(Type.SYNC_GROUPS, groupPayloads.size) + } + }, + ) } fun loadDatabaseCenterPayload() { repository.allDatabaseCenterPayload() .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber>() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - setError(Type.SYNC_CENTERS, e.message.toString()) - } - - override fun onNext(centerPayloads: List) { - setCountOfSyncData(Type.SYNC_CENTERS, centerPayloads.size) - } - }) + .subscribe( + object : Subscriber>() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + setError(Type.SYNC_CENTERS, e.message.toString()) + } + + override fun onNext(centerPayloads: List) { + setCountOfSyncData(Type.SYNC_CENTERS, centerPayloads.size) + } + }, + ) } fun loadDatabaseLoanRepaymentTransactions() { - repository.databaseLoanRepayments() - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber>() { - override fun onCompleted() {} - override fun onError(e: Throwable) { + viewModelScope.launch { + repository.databaseLoanRepayments() + .catch { e -> setError(Type.SYNC_LOAN_REPAYMENTS, e.message.toString()) - } - - override fun onNext(loanRepaymentRequests: List) { + }.collect { loanRepaymentRequests -> setCountOfSyncData(Type.SYNC_LOAN_REPAYMENTS, loanRepaymentRequests.size) } - }) + } } fun loadDatabaseSavingsAccountTransactions() { repository.allSavingsAccountTransactions() .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber>() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - setError(Type.SYNC_SAVINGS_ACCOUNT_TRANSACTION, e.message.toString()) - } - - override fun onNext(transactionRequests: List) { - setCountOfSyncData(Type.SYNC_SAVINGS_ACCOUNT_TRANSACTION, transactionRequests.size) - } - }) + .subscribe( + object : Subscriber>() { + override fun onCompleted() {} + override fun onError(e: Throwable) { + setError(Type.SYNC_SAVINGS_ACCOUNT_TRANSACTION, e.message.toString()) + } + + override fun onNext(transactionRequests: List) { + setCountOfSyncData( + Type.SYNC_SAVINGS_ACCOUNT_TRANSACTION, + transactionRequests.size, + ) + } + }, + ) } private fun setCountOfSyncData(type: Type, count: Int) { @@ -143,11 +153,31 @@ class OfflineDashboardViewModel @Inject constructor( private fun initGetSyncData(): List { return listOf( - SyncStateData(count = 0, name = R.string.feature_offline_sync_clients, type = Type.SYNC_CLIENTS), - SyncStateData(count = 0, name = R.string.feature_offline_sync_groups, type = Type.SYNC_GROUPS), - SyncStateData(count = 0, name = R.string.feature_offline_sync_centers, type = Type.SYNC_CENTERS), - SyncStateData(count = 0, name = R.string.feature_offline_sync_loanRepayments, type = Type.SYNC_LOAN_REPAYMENTS), - SyncStateData(count = 0, name = R.string.feature_offline_sync_savingsAccountTransactions, type = Type.SYNC_SAVINGS_ACCOUNT_TRANSACTION), + SyncStateData( + count = 0, + name = R.string.feature_offline_sync_clients, + type = Type.SYNC_CLIENTS, + ), + SyncStateData( + count = 0, + name = R.string.feature_offline_sync_groups, + type = Type.SYNC_GROUPS, + ), + SyncStateData( + count = 0, + name = R.string.feature_offline_sync_centers, + type = Type.SYNC_CENTERS, + ), + SyncStateData( + count = 0, + name = R.string.feature_offline_sync_loanRepayments, + type = Type.SYNC_LOAN_REPAYMENTS, + ), + SyncStateData( + count = 0, + name = R.string.feature_offline_sync_savingsAccountTransactions, + type = Type.SYNC_SAVINGS_ACCOUNT_TRANSACTION, + ), ) } } diff --git a/feature/offline/src/main/java/com/mifos/feature/offline/syncLoanRepaymentTransaction/SyncLoanRepaymentTransactionScreen.kt b/feature/offline/src/main/java/com/mifos/feature/offline/syncLoanRepaymentTransaction/SyncLoanRepaymentTransactionScreen.kt index bd4e46e1a8..6f378b3a4f 100644 --- a/feature/offline/src/main/java/com/mifos/feature/offline/syncLoanRepaymentTransaction/SyncLoanRepaymentTransactionScreen.kt +++ b/feature/offline/src/main/java/com/mifos/feature/offline/syncLoanRepaymentTransaction/SyncLoanRepaymentTransactionScreen.kt @@ -59,10 +59,10 @@ import com.mifos.core.common.utils.Network import com.mifos.core.designsystem.component.MifosCircularProgress import com.mifos.core.designsystem.component.MifosScaffold import com.mifos.core.designsystem.icon.MifosIcons -import com.mifos.core.entity.accounts.loan.LoanRepaymentRequest import com.mifos.feature.offline.R import com.mifos.feature.offline.syncSavingsAccountTransaction.getPaymentTypeName import com.mifos.room.entities.PaymentTypeOption +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest @Composable internal fun SyncLoanRepaymentTransactionScreenRoute( diff --git a/feature/offline/src/main/java/com/mifos/feature/offline/syncLoanRepaymentTransaction/SyncLoanRepaymentTransactionUiState.kt b/feature/offline/src/main/java/com/mifos/feature/offline/syncLoanRepaymentTransaction/SyncLoanRepaymentTransactionUiState.kt index bffc7ab14f..666c1dcead 100644 --- a/feature/offline/src/main/java/com/mifos/feature/offline/syncLoanRepaymentTransaction/SyncLoanRepaymentTransactionUiState.kt +++ b/feature/offline/src/main/java/com/mifos/feature/offline/syncLoanRepaymentTransaction/SyncLoanRepaymentTransactionUiState.kt @@ -9,8 +9,8 @@ */ package com.mifos.feature.offline.syncLoanRepaymentTransaction -import com.mifos.core.entity.accounts.loan.LoanRepaymentRequest import com.mifos.room.entities.PaymentTypeOption +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest /** * Created by Aditya Gupta on 16/08/23. diff --git a/feature/offline/src/main/java/com/mifos/feature/offline/syncLoanRepaymentTransaction/SyncLoanRepaymentTransactionViewModel.kt b/feature/offline/src/main/java/com/mifos/feature/offline/syncLoanRepaymentTransaction/SyncLoanRepaymentTransactionViewModel.kt index 1fb9177bdb..dc5bb85734 100644 --- a/feature/offline/src/main/java/com/mifos/feature/offline/syncLoanRepaymentTransaction/SyncLoanRepaymentTransactionViewModel.kt +++ b/feature/offline/src/main/java/com/mifos/feature/offline/syncLoanRepaymentTransaction/SyncLoanRepaymentTransactionViewModel.kt @@ -11,21 +11,22 @@ package com.mifos.feature.offline.syncLoanRepaymentTransaction import android.util.Log import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.mifos.core.common.utils.FileUtils.LOG_TAG import com.mifos.core.data.repository.SyncLoanRepaymentTransactionRepository import com.mifos.core.datastore.PrefManager -import com.mifos.core.entity.accounts.loan.LoanRepaymentRequest import com.mifos.core.entity.center.CenterPayload_Table.errorMessage -import com.mifos.core.objects.account.loan.LoanRepaymentResponse import com.mifos.feature.offline.R import com.mifos.room.entities.PaymentTypeOption +import com.mifos.room.entities.accounts.loans.LoanRepaymentRequest import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow -import rx.Subscriber -import rx.android.schedulers.AndroidSchedulers -import rx.schedulers.Schedulers +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.flowOn +import kotlinx.coroutines.launch import javax.inject.Inject /** @@ -63,44 +64,37 @@ class SyncLoanRepaymentTransactionViewModel @Inject constructor( } fun loadDatabaseLoanRepaymentTransactions() { - _syncLoanRepaymentTransactionUiState.value = - SyncLoanRepaymentTransactionUiState.ShowProgressbar - repository.databaseLoanRepayments() - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber>() { - override fun onCompleted() {} - override fun onError(e: Throwable) { + viewModelScope.launch { + _syncLoanRepaymentTransactionUiState.value = + SyncLoanRepaymentTransactionUiState.ShowProgressbar + + repository.databaseLoanRepayments() + .catch { _syncLoanRepaymentTransactionUiState.value = SyncLoanRepaymentTransactionUiState.ShowError(R.string.feature_offline_failed_to_load_loanrepayment) } - - override fun onNext(loanRepaymentRequests: List) { + .collect { loanRepaymentRequests -> mLoanRepaymentRequests = loanRepaymentRequests.toMutableList() updateUiState() } - }) + } } fun loanPaymentTypeOption() { - _syncLoanRepaymentTransactionUiState.value = - SyncLoanRepaymentTransactionUiState.ShowProgressbar - repository - .paymentTypeOption() - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber>() { - override fun onCompleted() {} - override fun onError(e: Throwable) { + viewModelScope.launch { + _syncLoanRepaymentTransactionUiState.value = + SyncLoanRepaymentTransactionUiState.ShowProgressbar + + repository.paymentTypeOption() + .catch { _syncLoanRepaymentTransactionUiState.value = SyncLoanRepaymentTransactionUiState.ShowError(R.string.feature_offline_failed_to_load_paymentoptions) } - - override fun onNext(paymentTypeOptions: List) { + .collect { paymentTypeOptions -> mPaymentTypeOptions = paymentTypeOptions updateUiState() } - }) + } } private fun updateUiState() { @@ -119,81 +113,70 @@ class SyncLoanRepaymentTransactionViewModel @Inject constructor( } private fun syncLoanRepayment(loanId: Int, loanRepaymentRequest: LoanRepaymentRequest?) { - _syncLoanRepaymentTransactionUiState.value = - SyncLoanRepaymentTransactionUiState.ShowProgressbar - repository.submitPayment(loanId, loanRepaymentRequest!!) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - val eLoanRepaymentRequest = mLoanRepaymentRequests[mClientSyncIndex] - eLoanRepaymentRequest.errorMessage = errorMessage.toString() - updateLoanRepayment(eLoanRepaymentRequest) - } + viewModelScope.launch { + _syncLoanRepaymentTransactionUiState.value = + SyncLoanRepaymentTransactionUiState.ShowProgressbar - override fun onNext(loanRepaymentResponse: LoanRepaymentResponse) { - mLoanRepaymentRequests[mClientSyncIndex].loanId?.let { - deleteAndUpdateLoanRepayments( - it, - ) - } + try { + repository.submitPayment(loanId, loanRepaymentRequest!!) + mLoanRepaymentRequests[mClientSyncIndex].loanId?.let { + deleteAndUpdateLoanRepayments( + it, + ) } - }) + } catch (e: Exception) { + val eLoanRepaymentRequest = mLoanRepaymentRequests[mClientSyncIndex].copy( + errorMessage = errorMessage.toString(), + ) + updateLoanRepayment(eLoanRepaymentRequest) + } + } } fun deleteAndUpdateLoanRepayments(loanId: Int) { - _syncLoanRepaymentTransactionUiState.value = - SyncLoanRepaymentTransactionUiState.ShowProgressbar - repository.deleteAndUpdateLoanRepayments(loanId) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber>() { - override fun onCompleted() {} - override fun onError(e: Throwable) { + viewModelScope.launch(Dispatchers.IO) { + _syncLoanRepaymentTransactionUiState.value = + SyncLoanRepaymentTransactionUiState.ShowProgressbar + + repository.deleteAndUpdateLoanRepayments(loanId).catch { + _syncLoanRepaymentTransactionUiState.value = + SyncLoanRepaymentTransactionUiState.ShowError(R.string.feature_offline_failed_to_update_list) + }.collect { loanRepaymentRequests -> + mClientSyncIndex = 0 + mLoanRepaymentRequests = + loanRepaymentRequests as MutableList + if (mLoanRepaymentRequests.isNotEmpty()) { + syncGroupPayload() + } else { _syncLoanRepaymentTransactionUiState.value = - SyncLoanRepaymentTransactionUiState.ShowError(R.string.feature_offline_failed_to_update_list) - } - - override fun onNext(loanRepaymentRequests: List) { - mClientSyncIndex = 0 - mLoanRepaymentRequests = - loanRepaymentRequests as MutableList - if (mLoanRepaymentRequests.isNotEmpty()) { - syncGroupPayload() - } else { - _syncLoanRepaymentTransactionUiState.value = - SyncLoanRepaymentTransactionUiState.ShowEmptyLoanRepayments( - R.string.feature_offline_no_loanrepayment_to_sync.toString(), - ) - } - updateUiState() + SyncLoanRepaymentTransactionUiState.ShowEmptyLoanRepayments( + R.string.feature_offline_no_loanrepayment_to_sync.toString(), + ) } - }) + updateUiState() + } + } } fun updateLoanRepayment(loanRepaymentRequest: LoanRepaymentRequest?) { - _syncLoanRepaymentTransactionUiState.value = + viewModelScope.launch { SyncLoanRepaymentTransactionUiState.ShowProgressbar - repository.updateLoanRepaymentTransaction(loanRepaymentRequest!!) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe(object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { + + repository.updateLoanRepaymentTransaction(loanRepaymentRequest!!) + .flowOn(Dispatchers.IO) + .catch { _syncLoanRepaymentTransactionUiState.value = SyncLoanRepaymentTransactionUiState.ShowError(R.string.feature_offline_failed_to_load_loanrepayment) } - - override fun onNext(loanRepaymentRequest: LoanRepaymentRequest) { - mLoanRepaymentRequests[mClientSyncIndex] = loanRepaymentRequest + .collect { + mLoanRepaymentRequests[mClientSyncIndex] = it ?: LoanRepaymentRequest() mClientSyncIndex += 1 if (mLoanRepaymentRequests.size != mClientSyncIndex) { syncGroupPayload() } updateUiState() } - }) + } } fun syncGroupPayload() { diff --git a/feature/offline/src/main/java/com/mifos/feature/offline/syncSavingsAccountTransaction/SyncSavingsAccountTransactionViewModel.kt b/feature/offline/src/main/java/com/mifos/feature/offline/syncSavingsAccountTransaction/SyncSavingsAccountTransactionViewModel.kt index cc397e669b..3200611696 100644 --- a/feature/offline/src/main/java/com/mifos/feature/offline/syncSavingsAccountTransaction/SyncSavingsAccountTransactionViewModel.kt +++ b/feature/offline/src/main/java/com/mifos/feature/offline/syncSavingsAccountTransaction/SyncSavingsAccountTransactionViewModel.kt @@ -12,12 +12,12 @@ package com.mifos.feature.offline.syncSavingsAccountTransaction import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.mifos.core.common.utils.Resource +import com.mifos.core.data.repository.SyncSavingsAccountTransactionRepository import com.mifos.core.datastore.PrefManager import com.mifos.core.domain.useCases.AllSavingsAccountTransactionsUseCase import com.mifos.core.domain.useCases.DeleteAndUpdateTransactionsUseCase -import com.mifos.core.domain.useCases.PaymentTypeOptionUseCase import com.mifos.core.domain.useCases.ProcessTransactionUseCase -import com.mifos.core.domain.useCases.UpdateLoanRepaymentTransactionUseCase +import com.mifos.core.domain.useCases.UpdateLoanRepaymentTransactionSavingsUseCase import com.mifos.core.entity.accounts.savings.SavingsAccountTransactionRequest import com.mifos.feature.offline.R import com.mifos.room.entities.PaymentTypeOption @@ -26,6 +26,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.catch import kotlinx.coroutines.launch import rx.Observable import javax.inject.Inject @@ -35,11 +36,11 @@ import javax.inject.Inject */ @HiltViewModel class SyncSavingsAccountTransactionViewModel @Inject constructor( - private val paymentTypeOptionUseCase: PaymentTypeOptionUseCase, private val processTransactionUseCase: ProcessTransactionUseCase, private val allSavingsAccountTransactionsUseCase: AllSavingsAccountTransactionsUseCase, - private val updateLoanRepaymentTransactionUseCase: UpdateLoanRepaymentTransactionUseCase, + private val updateLoanRepaymentTransactionSavingsUseCase: UpdateLoanRepaymentTransactionSavingsUseCase, private val deleteAndUpdateTransactionsUseCase: DeleteAndUpdateTransactionsUseCase, + private val repository: SyncSavingsAccountTransactionRepository, private val prefManager: PrefManager, ) : ViewModel() { @@ -212,21 +213,15 @@ class SyncSavingsAccountTransactionViewModel @Inject constructor( * and update the UI. */ fun loadPaymentTypeOption() = viewModelScope.launch(Dispatchers.IO) { - paymentTypeOptionUseCase().collect { result -> - when (result) { - is Resource.Error -> - _syncSavingsAccountTransactionUiState.value = - SyncSavingsAccountTransactionUiState.ShowError(R.string.feature_offline_failed_to_load_paymentoptions) + _syncSavingsAccountTransactionUiState.value = + SyncSavingsAccountTransactionUiState.Loading - is Resource.Loading -> - _syncSavingsAccountTransactionUiState.value = - SyncSavingsAccountTransactionUiState.Loading - - is Resource.Success -> { - mPaymentTypeOptions = result.data ?: emptyList() - updateUiState() - } - } + repository.paymentTypeOption().catch { + _syncSavingsAccountTransactionUiState.value = + SyncSavingsAccountTransactionUiState.ShowError(R.string.feature_offline_failed_to_load_paymentoptions) + }.collect { list -> + mPaymentTypeOptions = list + updateUiState() } } @@ -312,7 +307,7 @@ class SyncSavingsAccountTransactionViewModel @Inject constructor( */ private fun updateSavingsAccountTransaction(request: SavingsAccountTransactionRequest?) = viewModelScope.launch(Dispatchers.IO) { - updateLoanRepaymentTransactionUseCase(request).collect { result -> + updateLoanRepaymentTransactionSavingsUseCase(request).collect { result -> when (result) { is Resource.Error -> _syncSavingsAccountTransactionUiState.value =