Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement swipe refresh in payments screen #1594

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 2 additions & 0 deletions mifospay/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ dependencies {
implementation("de.hdodenhof:circleimageview:3.1.0")
implementation("com.github.yalantis:ucrop:2.2.2")

implementation("com.google.accompanist:accompanist-swiperefresh:0.27.0")

kspTest(libs.hilt.compiler)

testImplementation(libs.junit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class AccountViewModel @Inject constructor() : ViewModel() {
val accountsUiState: StateFlow<AccountsUiState> = _accountUiState

private val mRandom = Random()
val isRefreshing = MutableStateFlow(false)

init {
fetchLinkedAccount()
Expand Down Expand Up @@ -82,6 +83,11 @@ class AccountViewModel @Inject constructor() : ViewModel() {
)
return bankAccountDetailsList
}
fun refreshAccountList(){
isRefreshing.value = true
fetchLinkedAccount()
isRefreshing.value = false
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -26,6 +29,8 @@ 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.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import com.mifos.mobilewallet.model.domain.BankAccountDetails
import org.mifos.mobilewallet.mifospay.R
import org.mifos.mobilewallet.mifospay.bank.presenter.AccountViewModel
Expand All @@ -42,11 +47,18 @@ fun AccountsScreen(
) {
val accountsUiState by viewModel.accountsUiState.collectAsStateWithLifecycle()
val sampleList = viewModel.bankAccountDetailsList
AccountScreen(
accountsUiState = accountsUiState,
onAddAccount = onAddAccount,
sampleList = sampleList,
)
val isRefreshing by viewModel.isRefreshing.collectAsStateWithLifecycle()
val swipeRefreshState = rememberSwipeRefreshState(isRefreshing = isRefreshing)
SwipeRefresh(
state = swipeRefreshState,
onRefresh = viewModel::refreshAccountList
){
AccountScreen(
accountsUiState = accountsUiState,
onAddAccount = onAddAccount,
sampleList = sampleList,
)
}
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class HistoryViewModel @Inject constructor(
private val _historyUiState = MutableStateFlow<HistoryUiState>(HistoryUiState.Loading)
val historyUiState: StateFlow<HistoryUiState> = _historyUiState

val isRefreshing = MutableStateFlow(false)

fun fetchTransactions() {
_historyUiState.value = HistoryUiState.Loading
mUseCaseHandler.execute(mFetchAccountUseCase,
Expand Down Expand Up @@ -59,6 +61,11 @@ class HistoryViewModel @Inject constructor(
init {
fetchTransactions()
}
fun refreshTransactionHistory(){
isRefreshing.value = true
fetchTransactions()
isRefreshing.value = false
}
}

sealed class HistoryUiState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package org.mifos.mobilewallet.mifospay.history.ui

import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Info
import androidx.compose.material3.Button
Expand All @@ -19,7 +22,9 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
Expand All @@ -28,6 +33,8 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import com.mifos.mobilewallet.model.domain.Currency
import com.mifos.mobilewallet.model.domain.Transaction
import com.mifos.mobilewallet.model.domain.TransactionType
Expand All @@ -45,9 +52,18 @@ fun HistoryScreen(
viewModel: HistoryViewModel = hiltViewModel()
) {
val historyUiState by viewModel.historyUiState.collectAsStateWithLifecycle()
HistoryScreen(
historyUiState = historyUiState
)

val isRefreshing by viewModel.isRefreshing.collectAsStateWithLifecycle()
val swipeRefreshState = rememberSwipeRefreshState(isRefreshing = isRefreshing)

SwipeRefresh(
state = swipeRefreshState,
onRefresh = viewModel::refreshTransactionHistory
){
HistoryScreen(
historyUiState = historyUiState
)
}
}

@Composable
Expand All @@ -57,75 +73,83 @@ fun HistoryScreen(
var selectedChip by remember { mutableStateOf(TransactionType.OTHER) }
var filteredTransactions by remember { mutableStateOf(emptyList<Transaction>()) }

when (historyUiState) {
HistoryUiState.Empty -> {
EmptyContentScreen(
modifier = Modifier,
title = stringResource(id = R.string.error_oops),
subTitle = stringResource(id = R.string.empty_no_transaction_history_title),
iconTint = Color.Black,
iconImageVector = Icons.Rounded.Info
)
}
Box(modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState()),
contentAlignment = Alignment.Center
){
when (historyUiState) {
HistoryUiState.Empty -> {
EmptyContentScreen(
modifier = Modifier,
title = stringResource(id = R.string.error_oops),
subTitle = stringResource(id = R.string.empty_no_transaction_history_title),
iconTint = Color.Black,
iconImageVector = Icons.Rounded.Info
)
}

is HistoryUiState.Error -> {
EmptyContentScreen(
modifier = Modifier,
title = stringResource(id = R.string.error_oops),
subTitle = stringResource(id = R.string.unexpected_error_subtitle),
iconTint = Color.Black,
iconImageVector = Icons.Rounded.Info
)
}
is HistoryUiState.Error -> {
EmptyContentScreen(
modifier = Modifier,
title = stringResource(id = R.string.error_oops),
subTitle = stringResource(id = R.string.unexpected_error_subtitle),
iconTint = Color.Black,
iconImageVector = Icons.Rounded.Info
)
}

is HistoryUiState.HistoryList -> {
LaunchedEffect(selectedChip) {
filteredTransactions = when (selectedChip) {
TransactionType.OTHER -> historyUiState.list!!
else -> historyUiState.list!!.filter { it.transactionType == selectedChip }
is HistoryUiState.HistoryList -> {
LaunchedEffect(selectedChip) {
filteredTransactions = when (selectedChip) {
TransactionType.OTHER -> historyUiState.list!!
else -> historyUiState.list!!.filter { it.transactionType == selectedChip }
}
}
}
Column(modifier = Modifier.fillMaxSize()) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(10.dp),
horizontalArrangement = Arrangement.SpaceBetween
Column(
modifier = Modifier.fillMaxSize()
) {
Chip(
selected = selectedChip == TransactionType.OTHER,
onClick = { selectedChip = TransactionType.OTHER },
label = stringResource(R.string.all)
)
Chip(
selected = selectedChip == TransactionType.CREDIT,
onClick = { selectedChip = TransactionType.CREDIT },
label = stringResource(R.string.credits)
)
Chip(
selected = selectedChip == TransactionType.DEBIT,
onClick = { selectedChip = TransactionType.DEBIT },
label = stringResource(R.string.debits)
)
}
LazyColumn(modifier = Modifier.fillMaxSize()) {
items(filteredTransactions) {
HistoryItem(
date = it.date.toString(),
amount = it.amount.toString(),
transactionType = it.transactionType
Row(
modifier = Modifier
.fillMaxWidth()
.padding(10.dp),
horizontalArrangement = Arrangement.SpaceBetween
) {
Chip(
selected = selectedChip == TransactionType.OTHER,
onClick = { selectedChip = TransactionType.OTHER },
label = stringResource(R.string.all)
)
Chip(
selected = selectedChip == TransactionType.CREDIT,
onClick = { selectedChip = TransactionType.CREDIT },
label = stringResource(R.string.credits)
)
Chip(
selected = selectedChip == TransactionType.DEBIT,
onClick = { selectedChip = TransactionType.DEBIT },
label = stringResource(R.string.debits)
)
}
LazyColumn(modifier = Modifier.fillMaxSize()) {
items(filteredTransactions) {
HistoryItem(
date = it.date.toString(),
amount = it.amount.toString(),
transactionType = it.transactionType
)
}
}
}
}

}
}

HistoryUiState.Loading -> {
MifosLoadingWheel(
modifier = Modifier.fillMaxWidth(),
contentDesc = stringResource(R.string.loading)
)
HistoryUiState.Loading -> {
MifosLoadingWheel(
modifier = Modifier.fillMaxWidth(),
contentDesc = stringResource(R.string.loading)
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class InvoicesViewModel @Inject constructor(
private val _invoiceUiState = MutableStateFlow<InvoiceUiState>(InvoiceUiState.Loading)
val invoiceUiState: StateFlow<InvoiceUiState> = _invoiceUiState

val isRefreshing = MutableStateFlow(false)

fun fetchInvoices() {
_invoiceUiState.value = InvoiceUiState.Loading
mUseCaseHandler.execute(fetchInvoicesUseCase,
Expand All @@ -50,6 +52,11 @@ class InvoicesViewModel @Inject constructor(
init {
fetchInvoices()
}
fun refreshInvoices(){
isRefreshing.value = true
fetchInvoices()
isRefreshing.value = false
}
}

sealed class InvoiceUiState {
Expand Down
Loading