Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.mifos.room.utils.UNDEFINED
import com.mifos.room.utils.UNSPECIFIED
import com.mifos.room.utils.VALUE_UNSPECIFIED
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json

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

val dueDate: String? = null,
val dueDate: List<Int>? = null,

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

val formattedDueDate: String
get() {
val pattern = "%s-%s-%s"

val dueDateList = try {
dueDate?.let { Json.decodeFromString<List<Int>>(it) }
} catch (e: kotlinx.serialization.SerializationException) {
emptyList()
}

if (dueDateList != null) {
if (dueDateList.size > 2) {
return "${dueDateList[0]}-${dueDateList[1]}-${dueDateList[2]}"
}
}
return "No Due Date"
get() = if (dueDate?.size == 3) {
"${dueDate[0]}-${dueDate[1]}-${dueDate[2]}"
} else {
"No Due Date"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ class ChargeDaoHelper(
clientId: Int,
) {
val updatedCharges = chargesPage.pageItems.map { charges ->
val dateParts = charges.dueDate.orEmpty().split("-").mapNotNull { it.toIntOrNull() }
val dateParts = charges.dueDate.orEmpty()

val clientDate = if (dateParts.size == 3) {
charges.id?.toLong()?.let { chargeId ->
charges.id.toLong().let { chargeId ->
ClientDateEntity(
0,
chargeId,
Expand Down Expand Up @@ -75,7 +75,7 @@ class ChargeDaoHelper(
.map { chargesList ->
Page<ChargesEntity>().apply {
pageItems = chargesList.map { charge ->
charge.copy(dueDate = charge.chargeDueDate?.run { "$year-$month-$day" })
charge.copy(dueDate = charge.dueDate)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ data class RepaymentSchedule(
var totalWrittenOff: Double? = null,
) : Parcelable {

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

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
package com.mifos.core.model.objects.payloads

import kotlinx.serialization.Serializable

/**
* Created by nellyk on 2/15/2016.
*/
Expand All @@ -17,12 +19,13 @@ package com.mifos.core.model.objects.payloads
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/

class ChargesPayload {
var chargeId: Int? = null
var clientId: Int? = null
var loanId: Int? = null
var amount: String? = null
var locale: String? = null
var dueDate: String? = null
var dateFormat: String? = null
}
@Serializable
data class ChargesPayload(
var chargeId: Int? = null,
var clientId: Int? = null,
var loanId: Int? = null,
var amount: String? = null,
var locale: String? = null,
var dueDate: String? = null,
var dateFormat: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,12 @@ class DataManagerLoan(
@OptIn(ExperimentalCoroutinesApi::class)
fun getLoanRepayTemplate(loanId: Int): Flow<LoanRepaymentTemplateEntity?> {
return prefManager.userInfo.flatMapLatest { userData ->
flow {
when (userData.userStatus) {
false -> mBaseApiManager.loanApi.getLoanRepaymentTemplate(loanId)
/**
* Return LoanRepaymentTemplate from DatabaseHelperLoan.
*/
true -> loanDaoHelper.getLoanRepayTemplate(loanId)
if (userData.userStatus) {
loanDaoHelper.getLoanRepayTemplate(loanId)
} else {
flow {
val result = mBaseApiManager.loanApi.getLoanRepaymentTemplate(loanId)
emit(result)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,7 @@
<string name="feature_loan_id">Id</string>
<string name="feature_loan_office">Office</string>
<string name="feature_loan_break_down">Break Down</string>

<string name="feature_loan_unknown_error_occured">An Unknown Error Occured</string>
<string name="feature_loan_payment_success_message">Payment Successful, Transaction ID = </string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class GroupLoanAccountViewModel(
_groupLoanAccountUiState.value =
GroupLoanAccountUiState.Error(Res.string.feature_loan_failed_to_load_template)

is DataState.Loading -> Unit
is DataState.Loading -> GroupLoanAccountUiState.Loading

is DataState.Success ->
_groupLoanAccountUiState.value =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ fun LoanAccountScreen(
snackbarHostState.showSnackbar(
message = message,
)
onBackPressed()
}
onBackPressed()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
*/
package com.mifos.feature.loan.loanAccountSummary

import androidclient.feature.loan.generated.resources.Res
import androidclient.feature.loan.generated.resources.feature_loan_unknown_error_occured
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mifos.core.common.utils.Constants
import com.mifos.core.common.utils.DataState
import com.mifos.core.data.repository.LoanAccountSummaryRepository
import com.mifos.room.entities.accounts.loans.LoanWithAssociationsEntity
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
import org.jetbrains.compose.resources.getString

class LoanAccountSummaryViewModel(
savedStateHandle: SavedStateHandle,
Expand All @@ -36,18 +39,25 @@ class LoanAccountSummaryViewModel(

fun loadLoanById(loanAccountNumber: Int) {
viewModelScope.launch {
_loanAccountSummaryUiState.value = LoanAccountSummaryUiState.ShowProgressbar

repository.getLoanById(loanAccountNumber)
.catch {
_loanAccountSummaryUiState.value =
LoanAccountSummaryUiState.ShowFetchingError("Loan Account not found.")
}.collect { loanWithAssociations ->
_loanAccountSummaryUiState.value =
LoanAccountSummaryUiState.ShowLoanById(
loanWithAssociations.data ?: LoanWithAssociationsEntity(),
repository.getLoanById(loanAccountNumber).collect { dataState ->
when (dataState) {
is DataState.Loading -> {
_loanAccountSummaryUiState.value = LoanAccountSummaryUiState.ShowProgressbar
}

is DataState.Success -> {
_loanAccountSummaryUiState.value = LoanAccountSummaryUiState.ShowLoanById(
dataState.data ?: LoanWithAssociationsEntity(),
)
}

is DataState.Error -> {
_loanAccountSummaryUiState.value = LoanAccountSummaryUiState.ShowFetchingError(
getString(Res.string.feature_loan_unknown_error_occured),
)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@
*/
package com.mifos.feature.loan.loanApproval

import androidclient.feature.loan.generated.resources.Res
import androidclient.feature.loan.generated.resources.feature_loan_unknown_error_occured
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mifos.core.common.utils.DataState
import com.mifos.core.data.repository.LoanAccountApprovalRepository
import com.mifos.room.entities.accounts.loans.LoanApprovalData
import io.ktor.client.plugins.ClientRequestException
import io.ktor.client.plugins.ServerResponseException
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
import kotlinx.io.IOException
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import org.jetbrains.compose.resources.getString

class LoanAccountApprovalViewModel(
private val repository: LoanAccountApprovalRepository,
Expand All @@ -41,40 +40,27 @@ class LoanAccountApprovalViewModel(

fun approveLoan(loanApproval: com.mifos.core.model.objects.account.loan.LoanApproval?) {
viewModelScope.launch {
repository.approveLoan(loanId, loanApproval)
.catch {
when (it) {
is ClientRequestException, is ServerResponseException -> {
_loanAccountApprovalUiState.value =
LoanAccountApprovalUiState.ShowLoanApproveFailed(
it.message ?: "Server error occurred",
)
}
is IOException -> {
_loanAccountApprovalUiState.value =
LoanAccountApprovalUiState.ShowLoanApproveFailed(
it.message ?: "Network error occurred",
)
}
is SerializationException -> {
_loanAccountApprovalUiState.value =
LoanAccountApprovalUiState.ShowLoanApproveFailed(
it.message ?: "Data parsing error",
)
}
else -> {
_loanAccountApprovalUiState.value =
LoanAccountApprovalUiState.ShowLoanApproveFailed(
it.message ?: "Unknown error",
)
}
repository.approveLoan(loanId, loanApproval).collect { dataState ->
when (dataState) {
is DataState.Loading -> {
_loanAccountApprovalUiState.value =
LoanAccountApprovalUiState.ShowProgressbar
}

is DataState.Success -> {
val response = dataState.data
_loanAccountApprovalUiState.value =
LoanAccountApprovalUiState.ShowLoanApproveSuccessfully(response)
}

is DataState.Error -> {
_loanAccountApprovalUiState.value =
LoanAccountApprovalUiState.ShowLoanApproveFailed(
getString(Res.string.feature_loan_unknown_error_occured),
)
}
}
.collect {
_loanAccountApprovalUiState.value = it.data?.let { genericResponse ->
LoanAccountApprovalUiState.ShowLoanApproveSuccessfully(genericResponse)
} ?: LoanAccountApprovalUiState.ShowLoanApproveFailed("Something went wrong")
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ internal fun LoanChargeDialogScreen(
val payload = ChargesPayload().apply {
this.amount = amount
this.locale = locale
this.dateFormat = "dd MMMM yyyy"
this.dateFormat = "dd-MM-yyyy"
this.chargeId = chargeId
this.dueDate = DateHelper.getDateAsStringFromLong(
dueDate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ class LoanChargeDialogViewModel(
LoanChargeDialogUiState.Loading

is DataState.Success -> {
// result.data?.let {
// _loanChargeDialogUiState.value =
// LoanChargeDialogUiState.AllChargesV3(it)
// }
mapResourceBodyToChargeList(result.data)
}
}
Expand Down Expand Up @@ -105,32 +101,5 @@ class LoanChargeDialogViewModel(
_loanChargeDialogUiState.value =
LoanChargeDialogUiState.Error(Res.string.feature_loan_charge_failed_to_load_charge)
}

// val charges: MutableList<ChargesEntity> = ArrayList()
// var reader: BufferedReader? = null
// val sb = StringBuilder()
// try {
// reader = BufferedReader(InputStreamReader(result.rawContent as InputStream?))
// var line: String?
// while (reader.readLine().also { line = it } != null) {
// sb.append(line)
// }
// val obj = JSONObject(sb.toString())
// if (obj.has("chargeOptions")) {
// val chargesTypes = obj.getJSONArray("chargeOptions")
// for (i in 0 until chargesTypes.length()) {
// val chargesObject = chargesTypes.getJSONObject(i)
// val charge = ChargesEntity(
// id = chargesObject.optInt("id"),
// name = chargesObject.optString("name"),
// )
// charges.add(charge)
// }
// }
// _loanChargeDialogUiState.value = LoanChargeDialogUiState.AllChargesV3(charges)
// } catch (e: Exception) {
// _loanChargeDialogUiState.value =
// LoanChargeDialogUiState.Error(R.string.feature_loan_charge_failed_to_load_charge)
// }
}
}
Loading