Skip to content

Commit 3bdfee1

Browse files
fix : Loan Module clean up (Api , Viewmodel ) (#2411)
Co-authored-by: Nagarjuna <nagarjunabanda4@gmail.com>
1 parent 0579a26 commit 3bdfee1

18 files changed

Lines changed: 178 additions & 251 deletions

File tree

core/database/src/commonMain/kotlin/com/mifos/room/entities/client/ChargesEntity.kt

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import com.mifos.room.utils.UNDEFINED
2121
import com.mifos.room.utils.UNSPECIFIED
2222
import com.mifos.room.utils.VALUE_UNSPECIFIED
2323
import kotlinx.serialization.Serializable
24-
import kotlinx.serialization.json.Json
2524

2625
/**
2726
* Created by nellyk on 2/15/2016.
@@ -88,7 +87,7 @@ data class ChargesEntity(
8887
@ColumnInfo(index = true, name = INHERIT_FIELD_NAME, typeAffinity = UNDEFINED, collate = UNSPECIFIED, defaultValue = VALUE_UNSPECIFIED)
8988
val chargeDueDate: ClientDateEntity? = null,
9089

91-
val dueDate: String? = null,
90+
val dueDate: List<Int>? = null,
9291

9392
@ColumnInfo(index = true, name = INHERIT_FIELD_NAME, typeAffinity = UNDEFINED, collate = UNSPECIFIED, defaultValue = VALUE_UNSPECIFIED)
9493
val chargeCalculationType: ChargeCalculationTypeEntity? = null,
@@ -116,20 +115,9 @@ data class ChargesEntity(
116115
) : Parcelable {
117116

118117
val formattedDueDate: String
119-
get() {
120-
val pattern = "%s-%s-%s"
121-
122-
val dueDateList = try {
123-
dueDate?.let { Json.decodeFromString<List<Int>>(it) }
124-
} catch (e: kotlinx.serialization.SerializationException) {
125-
emptyList()
126-
}
127-
128-
if (dueDateList != null) {
129-
if (dueDateList.size > 2) {
130-
return "${dueDateList[0]}-${dueDateList[1]}-${dueDateList[2]}"
131-
}
132-
}
133-
return "No Due Date"
118+
get() = if (dueDate?.size == 3) {
119+
"${dueDate[0]}-${dueDate[1]}-${dueDate[2]}"
120+
} else {
121+
"No Due Date"
134122
}
135123
}

core/database/src/commonMain/kotlin/com/mifos/room/helper/ChargeDaoHelper.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ class ChargeDaoHelper(
4141
clientId: Int,
4242
) {
4343
val updatedCharges = chargesPage.pageItems.map { charges ->
44-
val dateParts = charges.dueDate.orEmpty().split("-").mapNotNull { it.toIntOrNull() }
44+
val dateParts = charges.dueDate.orEmpty()
4545

4646
val clientDate = if (dateParts.size == 3) {
47-
charges.id?.toLong()?.let { chargeId ->
47+
charges.id.toLong().let { chargeId ->
4848
ClientDateEntity(
4949
0,
5050
chargeId,
@@ -75,7 +75,7 @@ class ChargeDaoHelper(
7575
.map { chargesList ->
7676
Page<ChargesEntity>().apply {
7777
pageItems = chargesList.map { charge ->
78-
charge.copy(dueDate = charge.chargeDueDate?.run { "$year-$month-$day" })
78+
charge.copy(dueDate = charge.dueDate)
7979
}
8080
}
8181
}

core/model/src/commonMain/kotlin/com/mifos/core/model/objects/account/loan/RepaymentSchedule.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ data class RepaymentSchedule(
5252
var totalWrittenOff: Double? = null,
5353
) : Parcelable {
5454

55-
fun getlistOfActualPeriods(): List<Period> {
56-
return periods!!.subList(1, periods!!.size)
55+
fun getListOfActualPeriods(): List<Period> {
56+
val list = periods ?: return emptyList()
57+
return if (list.size > 1) list.subList(1, list.size).toList() else emptyList()
5758
}
5859

5960
companion object {

core/model/src/commonMain/kotlin/com/mifos/core/model/objects/payloads/ChargesPayload.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
package com.mifos.core.model.objects.payloads
1111

12+
import kotlinx.serialization.Serializable
13+
1214
/**
1315
* Created by nellyk on 2/15/2016.
1416
*/
@@ -17,12 +19,13 @@ package com.mifos.core.model.objects.payloads
1719
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
1820
*/
1921

20-
class ChargesPayload {
21-
var chargeId: Int? = null
22-
var clientId: Int? = null
23-
var loanId: Int? = null
24-
var amount: String? = null
25-
var locale: String? = null
26-
var dueDate: String? = null
27-
var dateFormat: String? = null
28-
}
22+
@Serializable
23+
data class ChargesPayload(
24+
var chargeId: Int? = null,
25+
var clientId: Int? = null,
26+
var loanId: Int? = null,
27+
var amount: String? = null,
28+
var locale: String? = null,
29+
var dueDate: String? = null,
30+
var dateFormat: String? = null,
31+
)

core/network/src/commonMain/kotlin/com/mifos/core/network/datamanager/DataManagerLoan.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,12 @@ class DataManagerLoan(
116116
@OptIn(ExperimentalCoroutinesApi::class)
117117
fun getLoanRepayTemplate(loanId: Int): Flow<LoanRepaymentTemplateEntity?> {
118118
return prefManager.userInfo.flatMapLatest { userData ->
119-
flow {
120-
when (userData.userStatus) {
121-
false -> mBaseApiManager.loanApi.getLoanRepaymentTemplate(loanId)
122-
/**
123-
* Return LoanRepaymentTemplate from DatabaseHelperLoan.
124-
*/
125-
true -> loanDaoHelper.getLoanRepayTemplate(loanId)
119+
if (userData.userStatus) {
120+
loanDaoHelper.getLoanRepayTemplate(loanId)
121+
} else {
122+
flow {
123+
val result = mBaseApiManager.loanApi.getLoanRepaymentTemplate(loanId)
124+
emit(result)
126125
}
127126
}
128127
}

feature/loan/src/commonMain/composeResources/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,7 @@
130130
<string name="feature_loan_id">Id</string>
131131
<string name="feature_loan_office">Office</string>
132132
<string name="feature_loan_break_down">Break Down</string>
133+
134+
<string name="feature_loan_unknown_error_occured">An Unknown Error Occured</string>
135+
<string name="feature_loan_payment_success_message">Payment Successful, Transaction ID = </string>
133136
</resources>

feature/loan/src/commonMain/kotlin/com/mifos/feature/loan/groupLoanAccount/GroupLoanAccountViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class GroupLoanAccountViewModel(
6666
_groupLoanAccountUiState.value =
6767
GroupLoanAccountUiState.Error(Res.string.feature_loan_failed_to_load_template)
6868

69-
is DataState.Loading -> Unit
69+
is DataState.Loading -> GroupLoanAccountUiState.Loading
7070

7171
is DataState.Success ->
7272
_groupLoanAccountUiState.value =

feature/loan/src/commonMain/kotlin/com/mifos/feature/loan/loanAccount/LoanAccountScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ fun LoanAccountScreen(
190190
snackbarHostState.showSnackbar(
191191
message = message,
192192
)
193+
onBackPressed()
193194
}
194-
onBackPressed()
195195
}
196196
}
197197
}

feature/loan/src/commonMain/kotlin/com/mifos/feature/loan/loanAccountSummary/LoanAccountSummaryViewModel.kt

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
*/
1010
package com.mifos.feature.loan.loanAccountSummary
1111

12+
import androidclient.feature.loan.generated.resources.Res
13+
import androidclient.feature.loan.generated.resources.feature_loan_unknown_error_occured
1214
import androidx.lifecycle.SavedStateHandle
1315
import androidx.lifecycle.ViewModel
1416
import androidx.lifecycle.viewModelScope
1517
import com.mifos.core.common.utils.Constants
18+
import com.mifos.core.common.utils.DataState
1619
import com.mifos.core.data.repository.LoanAccountSummaryRepository
1720
import com.mifos.room.entities.accounts.loans.LoanWithAssociationsEntity
1821
import kotlinx.coroutines.flow.MutableStateFlow
1922
import kotlinx.coroutines.flow.StateFlow
20-
import kotlinx.coroutines.flow.catch
2123
import kotlinx.coroutines.launch
24+
import org.jetbrains.compose.resources.getString
2225

2326
class LoanAccountSummaryViewModel(
2427
savedStateHandle: SavedStateHandle,
@@ -36,18 +39,25 @@ class LoanAccountSummaryViewModel(
3639

3740
fun loadLoanById(loanAccountNumber: Int) {
3841
viewModelScope.launch {
39-
_loanAccountSummaryUiState.value = LoanAccountSummaryUiState.ShowProgressbar
40-
41-
repository.getLoanById(loanAccountNumber)
42-
.catch {
43-
_loanAccountSummaryUiState.value =
44-
LoanAccountSummaryUiState.ShowFetchingError("Loan Account not found.")
45-
}.collect { loanWithAssociations ->
46-
_loanAccountSummaryUiState.value =
47-
LoanAccountSummaryUiState.ShowLoanById(
48-
loanWithAssociations.data ?: LoanWithAssociationsEntity(),
42+
repository.getLoanById(loanAccountNumber).collect { dataState ->
43+
when (dataState) {
44+
is DataState.Loading -> {
45+
_loanAccountSummaryUiState.value = LoanAccountSummaryUiState.ShowProgressbar
46+
}
47+
48+
is DataState.Success -> {
49+
_loanAccountSummaryUiState.value = LoanAccountSummaryUiState.ShowLoanById(
50+
dataState.data ?: LoanWithAssociationsEntity(),
51+
)
52+
}
53+
54+
is DataState.Error -> {
55+
_loanAccountSummaryUiState.value = LoanAccountSummaryUiState.ShowFetchingError(
56+
getString(Res.string.feature_loan_unknown_error_occured),
4957
)
58+
}
5059
}
60+
}
5161
}
5262
}
5363
}

feature/loan/src/commonMain/kotlin/com/mifos/feature/loan/loanApproval/LoanAccountApprovalViewModel.kt

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,19 @@
99
*/
1010
package com.mifos.feature.loan.loanApproval
1111

12+
import androidclient.feature.loan.generated.resources.Res
13+
import androidclient.feature.loan.generated.resources.feature_loan_unknown_error_occured
1214
import androidx.lifecycle.SavedStateHandle
1315
import androidx.lifecycle.ViewModel
1416
import androidx.lifecycle.viewModelScope
17+
import com.mifos.core.common.utils.DataState
1518
import com.mifos.core.data.repository.LoanAccountApprovalRepository
1619
import com.mifos.room.entities.accounts.loans.LoanApprovalData
17-
import io.ktor.client.plugins.ClientRequestException
18-
import io.ktor.client.plugins.ServerResponseException
1920
import kotlinx.coroutines.flow.MutableStateFlow
2021
import kotlinx.coroutines.flow.StateFlow
21-
import kotlinx.coroutines.flow.catch
2222
import kotlinx.coroutines.launch
23-
import kotlinx.io.IOException
24-
import kotlinx.serialization.SerializationException
2523
import kotlinx.serialization.json.Json
24+
import org.jetbrains.compose.resources.getString
2625

2726
class LoanAccountApprovalViewModel(
2827
private val repository: LoanAccountApprovalRepository,
@@ -41,40 +40,27 @@ class LoanAccountApprovalViewModel(
4140

4241
fun approveLoan(loanApproval: com.mifos.core.model.objects.account.loan.LoanApproval?) {
4342
viewModelScope.launch {
44-
repository.approveLoan(loanId, loanApproval)
45-
.catch {
46-
when (it) {
47-
is ClientRequestException, is ServerResponseException -> {
48-
_loanAccountApprovalUiState.value =
49-
LoanAccountApprovalUiState.ShowLoanApproveFailed(
50-
it.message ?: "Server error occurred",
51-
)
52-
}
53-
is IOException -> {
54-
_loanAccountApprovalUiState.value =
55-
LoanAccountApprovalUiState.ShowLoanApproveFailed(
56-
it.message ?: "Network error occurred",
57-
)
58-
}
59-
is SerializationException -> {
60-
_loanAccountApprovalUiState.value =
61-
LoanAccountApprovalUiState.ShowLoanApproveFailed(
62-
it.message ?: "Data parsing error",
63-
)
64-
}
65-
else -> {
66-
_loanAccountApprovalUiState.value =
67-
LoanAccountApprovalUiState.ShowLoanApproveFailed(
68-
it.message ?: "Unknown error",
69-
)
70-
}
43+
repository.approveLoan(loanId, loanApproval).collect { dataState ->
44+
when (dataState) {
45+
is DataState.Loading -> {
46+
_loanAccountApprovalUiState.value =
47+
LoanAccountApprovalUiState.ShowProgressbar
48+
}
49+
50+
is DataState.Success -> {
51+
val response = dataState.data
52+
_loanAccountApprovalUiState.value =
53+
LoanAccountApprovalUiState.ShowLoanApproveSuccessfully(response)
54+
}
55+
56+
is DataState.Error -> {
57+
_loanAccountApprovalUiState.value =
58+
LoanAccountApprovalUiState.ShowLoanApproveFailed(
59+
getString(Res.string.feature_loan_unknown_error_occured),
60+
)
7161
}
7262
}
73-
.collect {
74-
_loanAccountApprovalUiState.value = it.data?.let { genericResponse ->
75-
LoanAccountApprovalUiState.ShowLoanApproveSuccessfully(genericResponse)
76-
} ?: LoanAccountApprovalUiState.ShowLoanApproveFailed("Something went wrong")
77-
}
63+
}
7864
}
7965
}
8066
}

0 commit comments

Comments
 (0)