-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored the fragments of SetupUpiPinActivity to compose screen
- Loading branch information
1 parent
47de8b7
commit 0250bd1
Showing
11 changed files
with
651 additions
and
192 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
204 changes: 204 additions & 0 deletions
204
mifospay/src/main/java/org/mifos/mobilewallet/mifospay/bank/composeScreen/DebitCardScreen.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,204 @@ | ||
package org.mifos.mobilewallet.mifospay.bank.fragment | ||
|
||
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.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.foundation.text.BasicTextField | ||
import androidx.compose.foundation.text.KeyboardActions | ||
import androidx.compose.foundation.text.KeyboardOptions | ||
import androidx.compose.material.OutlinedTextField | ||
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.ExperimentalComposeUiApi | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.focus.FocusDirection | ||
import androidx.compose.ui.focus.FocusRequester | ||
import androidx.compose.ui.focus.focusProperties | ||
import androidx.compose.ui.focus.focusRequester | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.platform.LocalFocusManager | ||
import androidx.compose.ui.text.TextRange | ||
import androidx.compose.ui.text.input.ImeAction | ||
import androidx.compose.ui.text.input.KeyboardType | ||
import androidx.compose.ui.text.input.TextFieldValue | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import org.mifos.mobilewallet.mifospay.bank.presenter.DebitCardUiState | ||
import org.mifos.mobilewallet.mifospay.bank.presenter.DebitCardViewModel | ||
import org.mifos.mobilewallet.mifospay.bank.ui.SetupUpiPinActivity | ||
import org.mifos.mobilewallet.mifospay.common.Constants | ||
import org.mifos.mobilewallet.mifospay.designsystem.component.MfLoadingWheel | ||
import org.mifos.mobilewallet.mifospay.theme.MifosTheme | ||
import org.mifos.mobilewallet.mifospay.utils.Toaster.showToast | ||
|
||
@OptIn(ExperimentalComposeUiApi::class) | ||
@Composable | ||
fun DebitCardScreen( | ||
viewModel: DebitCardViewModel = hiltViewModel(), | ||
modifier: Modifier = Modifier, | ||
onDebitCardVerified: (String) -> Unit, | ||
onDebitCardVerificationFailed:(String) -> Unit | ||
) { | ||
val focusManager = LocalFocusManager.current | ||
|
||
val context = LocalContext.current | ||
|
||
var cardNumber by remember { mutableStateOf("") } | ||
var month by remember { mutableStateOf("") } | ||
var year by remember { mutableStateOf("") } | ||
|
||
val debitCardUiState by viewModel.debitCardUiState.collectAsStateWithLifecycle() | ||
|
||
Column( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(12.dp) | ||
) { | ||
val (a, b, c) = FocusRequester.createRefs() | ||
|
||
OutlinedTextField( | ||
value = cardNumber, | ||
onValueChange = { cardNumber = it }, | ||
label = { Text(text = "Debit Card Number") }, | ||
keyboardOptions = KeyboardOptions.Default.copy( | ||
keyboardType = KeyboardType.Number, | ||
imeAction = ImeAction.Done | ||
), | ||
keyboardActions = KeyboardActions( | ||
onDone = { | ||
focusManager.moveFocus(FocusDirection.Next) | ||
} | ||
), | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.focusRequester(a) | ||
.focusProperties { | ||
next = b | ||
} | ||
) | ||
|
||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(10.dp), | ||
verticalAlignment = Alignment.Bottom | ||
) { | ||
BasicTextField( | ||
modifier = Modifier | ||
.focusRequester(b) | ||
.focusProperties { | ||
next = c | ||
}, | ||
value = TextFieldValue(month, selection = TextRange(month.length)), | ||
onValueChange = { | ||
month = it.text | ||
if (month.length == 2) { | ||
focusManager.moveFocus(FocusDirection.Next) | ||
} | ||
}, | ||
keyboardActions = KeyboardActions(onDone = | ||
{ | ||
focusManager.moveFocus(FocusDirection.Next) | ||
|
||
}), | ||
keyboardOptions = KeyboardOptions.Default.copy( | ||
keyboardType = KeyboardType.Number, | ||
imeAction = ImeAction.Done | ||
), | ||
decorationBox = { | ||
Row(horizontalArrangement = Arrangement.Center) { | ||
repeat(2) { index -> | ||
CharView( | ||
index = index, | ||
text = month | ||
) | ||
Spacer(modifier = Modifier.width(8.dp)) | ||
} | ||
} | ||
} | ||
) | ||
Text( | ||
text = "/", | ||
fontSize = 30.sp, | ||
modifier = Modifier.padding(horizontal = 10.dp) | ||
) | ||
|
||
BasicTextField( | ||
modifier = Modifier | ||
.focusRequester(c) | ||
.focusProperties { | ||
next = a | ||
}, | ||
value = TextFieldValue(year, selection = TextRange(year.length)), | ||
onValueChange = { | ||
year = it.text | ||
}, | ||
keyboardOptions = KeyboardOptions.Default.copy( | ||
keyboardType = KeyboardType.Number, | ||
imeAction = ImeAction.Done | ||
), | ||
keyboardActions = KeyboardActions(onDone = | ||
{ | ||
viewModel.verifyDebitCard(cardNumber, month, year) | ||
}), | ||
decorationBox = { | ||
Row(horizontalArrangement = Arrangement.Center) { | ||
repeat(4) { index -> | ||
CharView( | ||
index = index, | ||
text = year | ||
) | ||
Spacer(modifier = Modifier.width(8.dp)) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
} | ||
when (debitCardUiState) { | ||
is DebitCardUiState.Initials -> { | ||
// do nothing | ||
} | ||
|
||
is DebitCardUiState.Verifying -> { | ||
MfLoadingWheel( | ||
contentDesc = Constants.PLEASE_WAIT, | ||
backgroundColor = Color.White | ||
) | ||
} | ||
|
||
is DebitCardUiState.Verified -> { | ||
onDebitCardVerified((debitCardUiState as DebitCardUiState.Verified).otp) | ||
} | ||
|
||
is DebitCardUiState.VerificationFailed -> { | ||
onDebitCardVerificationFailed((debitCardUiState as DebitCardUiState.VerificationFailed).errorMessage) | ||
} | ||
|
||
else -> {} | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
fun DebitCardScreenPreview() { | ||
MifosTheme { | ||
DebitCardScreen( | ||
onDebitCardVerified = {}, | ||
onDebitCardVerificationFailed = {} | ||
) | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
mifospay/src/main/java/org/mifos/mobilewallet/mifospay/bank/composeScreen/OtpScreen.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,55 @@ | ||
package org.mifos.mobilewallet.mifospay.bank.fragment | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.res.stringResource | ||
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.theme.MifosTheme | ||
|
||
|
||
@Composable | ||
fun OtpScreen( | ||
realOtp: String, | ||
onOtpTextCorrectlyEntered: () -> Unit | ||
) | ||
{ | ||
Column( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(20.dp), | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally | ||
) { | ||
Text( | ||
text = stringResource(id =R.string.enter_otp), | ||
color = Color(0xFF1E90FF), | ||
fontSize = 18.sp | ||
) | ||
OtpTextField( | ||
modifier = Modifier.padding(top = 20.dp), | ||
realOtp = realOtp, | ||
onOtpTextCorrectlyEntered = onOtpTextCorrectlyEntered | ||
) | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
fun OtpScreenPreview() { | ||
MifosTheme { | ||
OtpScreen( | ||
realOtp = "1234", | ||
onOtpTextCorrectlyEntered = {} | ||
) | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
mifospay/src/main/java/org/mifos/mobilewallet/mifospay/bank/composeScreen/OtpTextField.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,120 @@ | ||
package org.mifos.mobilewallet.mifospay.bank.fragment | ||
|
||
import androidx.compose.foundation.border | ||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.foundation.text.BasicTextField | ||
import androidx.compose.foundation.text.KeyboardActions | ||
import androidx.compose.foundation.text.KeyboardOptions | ||
import androidx.compose.material.MaterialTheme | ||
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.ExperimentalComposeUiApi | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController | ||
import androidx.compose.ui.text.TextRange | ||
import androidx.compose.ui.text.input.ImeAction | ||
import androidx.compose.ui.text.input.KeyboardType | ||
import androidx.compose.ui.text.input.TextFieldValue | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.unit.dp | ||
|
||
@OptIn(ExperimentalComposeUiApi::class) | ||
@Composable | ||
fun OtpTextField( | ||
modifier: Modifier = Modifier, | ||
realOtp: String = "", | ||
otpCount: Int = 4, | ||
onOtpTextCorrectlyEntered : () -> Unit | ||
) { | ||
var otpText by remember { mutableStateOf("") } | ||
var isError by remember { mutableStateOf(false) } | ||
Column( | ||
modifier = modifier | ||
.fillMaxWidth() | ||
.padding(16.dp), | ||
horizontalAlignment = Alignment.CenterHorizontally | ||
) { | ||
|
||
BasicTextField( | ||
modifier = modifier, | ||
value = TextFieldValue(otpText, selection = TextRange(otpText.length)), | ||
onValueChange = { | ||
otpText = it.text | ||
isError = false | ||
}, | ||
keyboardActions = KeyboardActions(onDone = | ||
{ | ||
if(otpText != realOtp) { | ||
isError = true | ||
} else { | ||
onOtpTextCorrectlyEntered.invoke() | ||
} | ||
println("OTP: $otpText and $isError") | ||
}), | ||
keyboardOptions = KeyboardOptions.Default.copy( | ||
keyboardType = KeyboardType.Number, | ||
imeAction = ImeAction.Done | ||
), | ||
decorationBox = { | ||
Row(horizontalArrangement = Arrangement.Center) { | ||
repeat(otpCount) { index -> | ||
CharView( | ||
index = index, | ||
text = otpText | ||
) | ||
Spacer(modifier = Modifier.width(8.dp)) | ||
} | ||
} | ||
} | ||
) | ||
if (isError) { | ||
// display erro message in text | ||
Text( | ||
text = "Invalid OTP", | ||
style = MaterialTheme.typography.body1, | ||
color = Color.Red, | ||
textAlign = TextAlign.Center, | ||
modifier = Modifier.padding(top = 8.dp) | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun CharView( | ||
index: Int, | ||
text: String | ||
) { | ||
val isFocused = text.length == index | ||
val char = when { | ||
index == text.length -> "_" | ||
index > text.length -> "_" | ||
else -> text[index].toString() | ||
} | ||
Text( | ||
modifier = Modifier | ||
.width(40.dp) | ||
.border( | ||
1.dp, when { | ||
isFocused -> Color.DarkGray | ||
else -> Color.LightGray | ||
}, RoundedCornerShape(8.dp) | ||
) | ||
.wrapContentHeight(align = Alignment.CenterVertically), | ||
text = char, | ||
style = MaterialTheme.typography.h4, | ||
color = if (isFocused) { | ||
Color.LightGray | ||
} else { | ||
Color.DarkGray | ||
}, | ||
textAlign = TextAlign.Center | ||
) | ||
} |
Oops, something went wrong.