-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor loan charges fragment to compose (#2141)
* refactor: refactor loan charges fragment to compose * refactor: refactor loan charges fragment to compose
- Loading branch information
1 parent
6074317
commit 66d304a
Showing
17 changed files
with
383 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
core/data/src/main/java/com/mifos/core/data/repository/LoanChargeRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.mifos.core.data.repository | ||
|
||
import com.mifos.core.objects.client.Charges | ||
|
||
/** | ||
* Created by Aditya Gupta on 10/08/23. | ||
*/ | ||
interface LoanChargeRepository { | ||
|
||
suspend fun getListOfLoanCharges(loanId: Int): List<Charges> | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
core/domain/src/main/java/com/mifos/core/domain/use_cases/GetListOfLoanChargesUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.mifos.core.domain.use_cases | ||
|
||
import com.mifos.core.common.utils.Resource | ||
import com.mifos.core.data.repository.LoanChargeRepository | ||
import com.mifos.core.objects.client.Charges | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import javax.inject.Inject | ||
|
||
class GetListOfLoanChargesUseCase @Inject constructor(private val repository: LoanChargeRepository) { | ||
|
||
suspend operator fun invoke(loanId: Int): Flow<Resource<List<Charges>>> = flow { | ||
try { | ||
emit(Resource.Loading()) | ||
val response = repository.getListOfLoanCharges(loanId) | ||
emit(Resource.Success(response)) | ||
} catch (exception: Exception) { | ||
emit(Resource.Error(exception.message.toString())) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
247 changes: 247 additions & 0 deletions
247
feature/loan/src/main/java/com/mifos/feature/loan/loan_charge/LoanChargeScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
@file:OptIn(ExperimentalMaterialApi::class) | ||
|
||
package com.mifos.feature.loan.loan_charge | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material.ExperimentalMaterialApi | ||
import androidx.compose.material.pullrefresh.PullRefreshIndicator | ||
import androidx.compose.material.pullrefresh.pullRefresh | ||
import androidx.compose.material.pullrefresh.rememberPullRefreshState | ||
import androidx.compose.material3.Card | ||
import androidx.compose.material3.CardDefaults | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.material3.SnackbarHostState | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.text.font.FontStyle | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.tooling.preview.PreviewParameter | ||
import androidx.compose.ui.tooling.preview.PreviewParameterProvider | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
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.designsystem.theme.Black | ||
import com.mifos.core.designsystem.theme.BlueSecondary | ||
import com.mifos.core.designsystem.theme.DarkGray | ||
import com.mifos.core.objects.client.Charges | ||
import com.mifos.core.ui.components.MifosEmptyUi | ||
import com.mifos.feature.loan.R | ||
|
||
@Composable | ||
fun LoanChargeScreen(loanAccountNumber: Int, onBackPressed: () -> Unit) { | ||
|
||
val viewModel: LoanChargeViewModel = hiltViewModel() | ||
val state by viewModel.loanChargeUiState.collectAsStateWithLifecycle() | ||
val refreshState by viewModel.isRefreshing.collectAsStateWithLifecycle() | ||
|
||
LaunchedEffect(Unit) { | ||
viewModel.loadLoanChargesList(loanAccountNumber) | ||
} | ||
|
||
LoanChargeScreen( | ||
state = state, | ||
onBackPressed = onBackPressed, | ||
onRetry = { | ||
viewModel.loadLoanChargesList(loanAccountNumber) | ||
}, | ||
refreshState = refreshState, | ||
onRefresh = { | ||
viewModel.refreshLoanChargeList(loanAccountNumber) | ||
} | ||
) | ||
|
||
} | ||
|
||
@Composable | ||
fun LoanChargeScreen( | ||
state: LoanChargeUiState, | ||
onBackPressed: () -> Unit, | ||
onRetry: () -> Unit, | ||
refreshState: Boolean, | ||
onRefresh: () -> Unit, | ||
) { | ||
val snackbarHostState by remember { mutableStateOf(SnackbarHostState()) } | ||
val pullRefreshState = rememberPullRefreshState( | ||
refreshing = refreshState, | ||
onRefresh = onRefresh | ||
) | ||
|
||
MifosScaffold( | ||
icon = MifosIcons.arrowBack, | ||
title = stringResource(id = R.string.feature_loan_loan_charges), | ||
onBackPressed = onBackPressed, | ||
actions = { | ||
IconButton(onClick = { | ||
// TODO Implement Loan Charges Add Dialog | ||
}) { | ||
Icon(imageVector = MifosIcons.Add, contentDescription = null) | ||
} | ||
}, | ||
snackbarHostState = snackbarHostState | ||
) { paddingValues -> | ||
Column(modifier = Modifier.padding(paddingValues)) { | ||
Box(modifier = Modifier.pullRefresh(pullRefreshState)) { | ||
when (state) { | ||
is LoanChargeUiState.Error -> MifosSweetError(message = stringResource(id = state.message)) { | ||
onRetry() | ||
} | ||
|
||
is LoanChargeUiState.Loading -> MifosCircularProgress() | ||
|
||
is LoanChargeUiState.LoanChargesList -> { | ||
if (state.loanCharges.isEmpty()) { | ||
MifosEmptyUi( | ||
text = stringResource(id = R.string.feature_loan_no_loan_charges), | ||
icon = MifosIcons.fileTask | ||
) | ||
} else { | ||
LoanChargesContent(loanCharges = state.loanCharges) | ||
} | ||
} | ||
} | ||
|
||
PullRefreshIndicator( | ||
refreshing = refreshState, | ||
state = pullRefreshState, | ||
modifier = Modifier.align(Alignment.TopCenter) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun LoanChargesContent( | ||
loanCharges: List<Charges> | ||
) { | ||
LazyColumn { | ||
items(loanCharges) { charges -> | ||
LoanChargeItem( | ||
charges = charges | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun LoanChargeItem( | ||
charges: Charges | ||
) { | ||
Card( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(8.dp), | ||
shape = RoundedCornerShape(0.dp), | ||
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp), | ||
colors = CardDefaults.cardColors( | ||
containerColor = BlueSecondary | ||
) | ||
) { | ||
Spacer(modifier = Modifier.height(8.dp)) | ||
MifosLoanChargeDetailsText( | ||
stringResource(id = R.string.feature_loan_client_id), | ||
charges.chargeId.toString() | ||
) | ||
MifosLoanChargeDetailsText( | ||
stringResource(id = R.string.feature_loan_charge_name), | ||
charges.name ?: "" | ||
) | ||
MifosLoanChargeDetailsText( | ||
stringResource(id = R.string.feature_loan_charge_amount), | ||
charges.amount.toString() | ||
) | ||
MifosLoanChargeDetailsText( | ||
stringResource(id = R.string.feature_loan_charge_due_date), | ||
charges.formattedDueDate | ||
) | ||
Spacer(modifier = Modifier.height(8.dp)) | ||
} | ||
} | ||
|
||
@Composable | ||
fun MifosLoanChargeDetailsText(field: String, value: String) { | ||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth(), | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
Text( | ||
modifier = Modifier | ||
.weight(1f) | ||
.padding(start = 16.dp), | ||
text = field, | ||
style = TextStyle( | ||
fontSize = 16.sp, | ||
fontWeight = FontWeight.Normal, | ||
fontStyle = FontStyle.Normal | ||
), | ||
color = Black, | ||
textAlign = TextAlign.Start | ||
) | ||
Text( | ||
modifier = Modifier.weight(1f), | ||
text = value, | ||
style = TextStyle( | ||
fontSize = 16.sp, | ||
fontWeight = FontWeight.Normal, | ||
fontStyle = FontStyle.Normal | ||
), | ||
color = DarkGray, | ||
textAlign = TextAlign.Start | ||
) | ||
} | ||
} | ||
|
||
class LoanChargeUiStateProvider : PreviewParameterProvider<LoanChargeUiState> { | ||
|
||
override val values: Sequence<LoanChargeUiState> | ||
get() = sequenceOf( | ||
LoanChargeUiState.Loading, | ||
LoanChargeUiState.Error(R.string.feature_loan_failed_to_load_loan_charges), | ||
LoanChargeUiState.LoanChargesList(sampleLoanChargeList) | ||
) | ||
} | ||
|
||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
private fun LoanChargeScreenPreview( | ||
@PreviewParameter(LoanChargeUiStateProvider::class) state: LoanChargeUiState | ||
) { | ||
LoanChargeScreen( | ||
state = state, | ||
onBackPressed = {}, | ||
onRetry = {}, | ||
refreshState = false, | ||
onRefresh = {} | ||
) | ||
} | ||
|
||
val sampleLoanChargeList = List(10) { | ||
Charges(name = "name $it", chargeId = it, amount = it.toDouble()) | ||
} |
15 changes: 15 additions & 0 deletions
15
feature/loan/src/main/java/com/mifos/feature/loan/loan_charge/LoanChargeUiState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.mifos.feature.loan.loan_charge | ||
|
||
import com.mifos.core.objects.client.Charges | ||
|
||
/** | ||
* Created by Aditya Gupta on 10/08/23. | ||
*/ | ||
sealed class LoanChargeUiState { | ||
|
||
data object Loading : LoanChargeUiState() | ||
|
||
data class Error(val message: Int) : LoanChargeUiState() | ||
|
||
data class LoanChargesList(val loanCharges: List<Charges>) : LoanChargeUiState() | ||
} |
Oops, something went wrong.