Skip to content

Commit

Permalink
handled states
Browse files Browse the repository at this point in the history
  • Loading branch information
devrahul-2508 committed Mar 26, 2024
1 parent 06c6de1 commit 5f5688a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.mifos.mobilewallet.mifospay.bank.presenter
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.util.Log
import androidx.lifecycle.ViewModel
import com.mifos.mobilewallet.model.domain.BankAccountDetails
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -31,10 +30,13 @@ class LinkBankAccountViewModel @Inject constructor() : ViewModel() {
private val _filteredBanks = MutableStateFlow<List<Bank>>(emptyList())
var filteredBanks = _filteredBanks.asStateFlow()

private val _bankState = MutableStateFlow<BankUiState>(BankUiState.Loading)
val bankState = _bankState.asStateFlow()


fun getAllBanks(context: Context) {

Log.d("Bam", "Get Bank Function called");
_bankState.value = BankUiState.Loading

val popularBanks = ArrayList<Bank>()
popularBanks.add(Bank("RBL Bank", R.drawable.logo_rbl, 0))
Expand All @@ -58,6 +60,11 @@ class LinkBankAccountViewModel @Inject constructor() : ViewModel() {
}
_allBanks.value = banksList
_filteredBanks.value = banksList
_bankState.value = BankUiState.LoadedBankState(
popularBanks = _popularBanks.value,
allBanks = _filteredBanks.value
)

}

fun filterBanks(query: String) {
Expand All @@ -68,8 +75,11 @@ class LinkBankAccountViewModel @Inject constructor() : ViewModel() {
_filteredBanks.value = _allBanks.value
} else {
_filteredBanks.value = filteredList

}
_bankState.value = BankUiState.LoadedBankState(
popularBanks = _popularBanks.value,
allBanks = _filteredBanks.value
)
}

fun fetchBankAccountDetails(bankName: String?) {
Expand All @@ -96,4 +106,13 @@ class LinkBankAccountViewModel @Inject constructor() : ViewModel() {
companion object {
private val mRandom = Random()
}
}

sealed interface BankUiState {
data class LoadedBankState(
val popularBanks: List<Bank>,
val allBanks: List<Bank>
) : BankUiState

data object Loading : BankUiState
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.mifos.mobilewallet.mifospay.bank.ui

import android.app.Activity
import android.content.Context
import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
Expand Down Expand Up @@ -53,7 +52,9 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import org.mifos.mobilewallet.mifospay.R
import org.mifos.mobilewallet.mifospay.bank.presenter.BankUiState
import org.mifos.mobilewallet.mifospay.bank.presenter.LinkBankAccountViewModel
import org.mifos.mobilewallet.mifospay.designsystem.component.MfOverlayLoadingWheel
import org.mifos.mobilewallet.mifospay.designsystem.component.MifosOutlinedTextField
import org.mifos.mobilewallet.mifospay.designsystem.component.MifosTopBar
import org.mifos.mobilewallet.mifospay.designsystem.theme.greyBackgroundColor
Expand All @@ -67,7 +68,7 @@ fun LinkBankAccountScreen(
) {

var showSimBottomSheet by rememberSaveable { mutableStateOf(false) }
var bankSelected=""
var bankSelected = ""


Scaffold(topBar = {
Expand All @@ -84,18 +85,19 @@ fun LinkBankAccountScreen(

}

LinkBankAccountScreenContent(it, viewModel) {
val uiState by viewModel.bankState.collectAsState()


LinkBankAccountScreenContent(it, uiState, viewModel) {
bankSelected = it.name
showSimBottomSheet = true
}

if (showSimBottomSheet){
ChooseSimDialogSheet {selectedSim->
Log.d("BAM",selectedSim.toString())
if (selectedSim!=0){
if (showSimBottomSheet) {
ChooseSimDialogSheet { selectedSim ->
if (selectedSim != 0) {
viewModel.fetchBankAccountDetails(bankSelected)
}

showSimBottomSheet = false
}
}
Expand All @@ -105,17 +107,45 @@ fun LinkBankAccountScreen(
@Composable
fun LinkBankAccountScreenContent(
padding: PaddingValues,
uiState: BankUiState,
viewModel: LinkBankAccountViewModel,
onSelectBank: (Bank) -> Unit
) {


when (uiState) {
BankUiState.Loading -> {
MfOverlayLoadingWheel(
contentDesc = stringResource(R.string.loading),
)
}

is BankUiState.LoadedBankState -> {
LoadedContent(
popularBanks = uiState.popularBanks,
filteredBanks = uiState.allBanks,
viewModel,
onSelectBank
)
}
}


}

@Composable
fun LoadedContent(
popularBanks: List<Bank>,
filteredBanks: List<Bank>,
viewModel: LinkBankAccountViewModel,
onSelectBank: (Bank) -> Unit


) {

val search = remember {
mutableStateOf("")
}

val popularBanks = viewModel.popularBanks.collectAsState().value
val filteredBanks = viewModel.filteredBanks.collectAsState().value
Box(
modifier = Modifier
.fillMaxSize()
Expand Down

0 comments on commit 5f5688a

Please sign in to comment.