Skip to content

Commit

Permalink
fix #1586: Migrate LinkBankAccountActivity to Compose
Browse files Browse the repository at this point in the history
  • Loading branch information
devrahul-2508 committed Mar 26, 2024
1 parent 2881c38 commit de9978f
Show file tree
Hide file tree
Showing 7 changed files with 713 additions and 305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ val red = Color(0xFFCD0000)
val mifosText = Color(0xFF212121)
val chipSelectedColor = Color(0xFF00BFFF)
val creditTextColor = Color(0xFF009688)
val debitTextColor = Color(0xFFF10606)
val debitTextColor = Color(0xFFF10606)
val greyBackgroundColor = Color(0xFFF7F7F7)
val primaryDarkBlue = Color(0xFF303F9F)
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package org.mifos.mobilewallet.mifospay.bank.presenter

import android.content.Context
import android.content.Intent
import android.os.Handler
import androidx.lifecycle.ViewModel
import com.mifos.mobilewallet.model.domain.BankAccountDetails
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import org.json.JSONObject
import org.mifos.mobilewallet.mifospay.R
import org.mifos.mobilewallet.mifospay.common.Constants
import org.mifos.mobilewallet.mifospay.domain.model.Bank
import org.mifos.mobilewallet.mifospay.utils.FileUtils
import java.util.Random
import javax.inject.Inject


@HiltViewModel
class LinkBankAccountViewModel @Inject constructor() : ViewModel() {


private val _popularBanks = MutableStateFlow<List<Bank>>(emptyList())
var popularBanks = _popularBanks.asStateFlow()

private val _allBanks = MutableStateFlow<List<Bank>>(emptyList())
var allBanks = _allBanks.asStateFlow()

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) {

_bankState.value = BankUiState.Loading

val popularBanks = ArrayList<Bank>()
popularBanks.add(Bank("RBL Bank", R.drawable.logo_rbl, 0))
popularBanks.add(Bank("SBI Bank", R.drawable.logo_sbi, 0))
popularBanks.add(Bank("PNB Bank", R.drawable.logo_pnb, 0))
popularBanks.add(Bank("HDFC Bank", R.drawable.logo_hdfc, 0))
popularBanks.add(Bank("ICICI Bank", R.drawable.logo_icici, 0))
popularBanks.add(Bank("AXIS Bank", R.drawable.logo_axis, 0))

_popularBanks.value = popularBanks

val jsonObject: JSONObject
jsonObject = FileUtils.readJson(context, "banks.json")!!
val banksList = ArrayList<Bank>()
for (i in 0 until jsonObject.getJSONArray("banks").length()) {
banksList.add(
Bank(
(jsonObject.getJSONArray("banks")[i] as String), R.drawable.ic_bank, 1
)
)
}
_allBanks.value = banksList
_filteredBanks.value = banksList
_bankState.value = BankUiState.LoadedBankState(
popularBanks = _popularBanks.value,
allBanks = _filteredBanks.value
)

}

fun filterBanks(query: String) {
val filteredList = _allBanks.value.filter {
it.name.contains(query, ignoreCase = true)
}
if (query.isBlank()) {
_filteredBanks.value = _allBanks.value
} else {
_filteredBanks.value = filteredList
}
_bankState.value = BankUiState.LoadedBankState(
popularBanks = _popularBanks.value,
allBanks = _filteredBanks.value
)
}

fun fetchBankAccountDetails(bankName: String?) {
// TODO:: UPI API implement
addBankAccount(
BankAccountDetails(
bankName, "Ankur Sharma", "New Delhi",
mRandom.nextInt().toString() + " ", "Savings"
)


)
}

fun addBankAccount(bankAccountDetails: BankAccountDetails?) {
val handler = Handler()
handler.postDelayed({
val intent = Intent()
intent.putExtra(Constants.NEW_BANK_ACCOUNT, bankAccountDetails)

}, 1500)
}

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 @@ -80,7 +80,6 @@ class ChooseSimDialog : BottomSheetDialogFragment() {
if (activity is LinkBankAccountActivity
&& (selectedSim == 1 || selectedSim == 2)
) {
(activity as LinkBankAccountActivity?)!!.linkBankAccount(selectedSim)
} else {
Toast.makeText(
context,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package org.mifos.mobilewallet.mifospay.bank.ui

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.Button
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.mifos.mobilewallet.mifospay.R
import org.mifos.mobilewallet.mifospay.designsystem.component.MifosBottomSheet
import org.mifos.mobilewallet.mifospay.designsystem.theme.primaryDarkBlue

@Composable
fun ChooseSimDialogSheet(
onSimSelected: (Int) -> Unit
) {
MifosBottomSheet(content = {
ChooseSimDialogSheetContent(onSimSelected)
}, onDismiss = {
onSimSelected.invoke(0)
})
}

@Composable
fun ChooseSimDialogSheetContent(onSimSelected: (Int) -> Unit) {
var selectedSim by remember { mutableStateOf(0) }

Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxSize()
.padding(8.dp)
) {
Text(
text = stringResource(id = R.string.verify_mobile_number),
fontSize = 25.sp,
color = primaryDarkBlue
)

Spacer(modifier = Modifier.height(15.dp))

Text(
text = stringResource(id = R.string.confirm_mobile_number_message),
textAlign = TextAlign.Center,
color = primaryDarkBlue
)

Spacer(modifier = Modifier.height(15.dp))

Text(
text = stringResource(id = R.string.bank_account_mobile_verification_conditions),

)

Spacer(modifier = Modifier.height(20.dp))

Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically
) {
SimCard(
simNumber = 1,
isSelected = selectedSim == 1,
onSimSelected = { selectedSim = 1 })

Spacer(modifier = Modifier.width(24.dp))
Text(text = "or")
Spacer(modifier = Modifier.width(24.dp))

SimCard(
simNumber = 2,
isSelected = selectedSim == 2,
onSimSelected = { selectedSim = 2 })
}

Spacer(modifier = Modifier.height(24.dp))

Text(
text = "*Regular charges will apply", color = Color.Black
)

Spacer(modifier = Modifier.height(18.dp))

Button(
onClick = { onSimSelected(selectedSim) },
modifier = Modifier
.align(Alignment.CenterHorizontally)
.width(250.dp)
.clip(CircleShape)
) {
Text(text = "Confirm")
}
}
}

@Composable
fun SimCard(
simNumber: Int, isSelected: Boolean, onSimSelected: () -> Unit
) {
val drawableId = if (isSelected) {
R.drawable.sim_card_selected
} else {
R.drawable.sim_card_unselected
}
val drawable: Painter = painterResource(id = drawableId)


Image(painter = drawable,
contentDescription = "SIM Card $simNumber",
contentScale = ContentScale.Fit,
modifier = Modifier
.size(50.dp)
.clickable { onSimSelected() })
}


@Preview
@Composable
fun SimSelectionPreview() {
Surface(color = Color.White) {
ChooseSimDialogSheet(onSimSelected = {})
}
}
Loading

0 comments on commit de9978f

Please sign in to comment.