Skip to content

Commit

Permalink
trnasformed setupi pin activity to compose
Browse files Browse the repository at this point in the history
  • Loading branch information
NiranjanNlc committed Apr 22, 2024
1 parent 47de8b7 commit f195a90
Show file tree
Hide file tree
Showing 21 changed files with 1,187 additions and 493 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.mifos.mobilewallet.mifospay.ui

import androidx.compose.foundation.layout.Arrangement
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.layout.wrapContentHeight
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
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.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.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun ExpiryDateInput(
date: String,
onDateChange: (String) -> Unit,
onDone: () -> Unit,
) {
val (a, b, c) = FocusRequester.createRefs()
Row(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp), verticalAlignment = Alignment.Bottom
) {
BasicTextField(modifier = Modifier
.focusRequester(b)
.focusProperties {
next = c
},
value = TextFieldValue(date, selection = TextRange(date.length)),
onValueChange = {
if (it.text.length == 3 && it.text[2] != '/') {
val newText = it.text.substring(0, 2) + '/' + it.text.substring(2)
onDateChange(newText)
return@BasicTextField
}
if (it.text.length > date.length) {
// If the user is typing at the third position, ensure it remains '/'
if (it.text.length >= 3 && it.text[2] != '/') {
val newText = it.text.substring(0, 2) + '/' + it.text.substring(3)
onDateChange(newText)
return@BasicTextField
}
}
onDateChange(it.text)
},
keyboardActions = KeyboardActions(onDone = {
onDone()
}),
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Number, imeAction = ImeAction.Done
),
decorationBox = {
Row(horizontalArrangement = Arrangement.Center) {
repeat(7) { index ->
FormattedDateView(
index = index, text = date
)
Spacer(modifier = Modifier.width(8.dp))
}
}
})
}
}

@Composable
fun FormattedDateView(
index: Int, text: String
) {
val isFocused = text.length == index

val char = when {
index == 2 -> "/"
index == text.length -> "_"
index > text.length -> "_"
else -> text[index].toString()
}
androidx.compose.material3.Text(
modifier = Modifier
.width(40.dp)
.wrapContentHeight(align = Alignment.CenterVertically),
text = char,
style = MaterialTheme.typography.headlineSmall,
color = if (isFocused) {
Color.DarkGray
} else {
Color.LightGray
},
textAlign = TextAlign.Center
)
}

@Preview
@Composable
fun ExpiryDateInputPreview() {
ExpiryDateInput(date = "", onDateChange = {}, onDone = {})
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.mifos.mobilewallet.mifospay.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.mifos.mobilewallet.mifospay.designsystem.theme.MifosTheme


@Composable
fun VerifyStepHeader(text: String, isVerified: Boolean) {
Row(
modifier = Modifier
.fillMaxWidth(),
) {
Text(
text = text,
color = Color(0xFF212121),
fontSize = 17.sp,
overflow = TextOverflow.Ellipsis,
modifier = Modifier
.padding(16.dp)
.weight(1f),
textAlign = TextAlign.Start,
style = TextStyle(fontWeight = FontWeight.Bold)
)
IconButton(
onClick = { },
) {
if (isVerified)
Icon(
imageVector = Icons.Default.Check,
contentDescription = null,
tint = if (isVerified) Color.Black else Color.Gray,
modifier = Modifier.size(24.dp)
)
}
}
}
@Preview
@Composable
fun VerifyStepHeaderVerifiedPreview() {
MifosTheme {
VerifyStepHeader(text = "Debit card Details ", isVerified = true)
}
}

@Preview
@Composable
fun VerifyStepHeaderUnverifiedPreview() {
VerifyStepHeader(text = "Enter OTP ", isVerified = false)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package org.mifos.mobilewallet.mifospay.ui

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.material3.MaterialTheme
import androidx.compose.material3.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.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.tooling.preview.Preview
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
if (otpText.length == otpCount) {
if (otpText != realOtp) {
isError = true
} else {
onOtpTextCorrectlyEntered.invoke()
}
}
},
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.bodyMedium,
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)
.wrapContentHeight(align = Alignment.CenterVertically),
text = char,
style = MaterialTheme.typography.headlineSmall,
color = if (isFocused) {
Color.DarkGray
} else {
Color.LightGray
},
textAlign = TextAlign.Center
)
}

@Preview
@Composable
fun prviewOtpTextField() {
OtpTextField(
realOtp = "1234",
otpCount = 4,
onOtpTextCorrectlyEntered = {}
)
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ androidxTracing = "1.3.0-alpha02"
coil = "2.6.0"
androidxMetrics = "1.0.0-beta01"
androidxBrowser = "1.8.0"
activity = "1.8.0"

[libraries]
androidx-activity-ktx = { module = "androidx.activity:activity-ktx", version.ref = "activityVersion" }
Expand Down Expand Up @@ -152,6 +153,7 @@ firebase-performance-gradlePlugin = { group = "com.google.firebase", name = "per
kotlin-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" }
ksp-gradlePlugin = { group = "com.google.devtools.ksp", name = "com.google.devtools.ksp.gradle.plugin", version.ref = "ksp" }
room-gradlePlugin = { group = "androidx.room", name = "room-gradle-plugin", version.ref = "room" }
androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }

[plugins]
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }
Expand Down
3 changes: 3 additions & 0 deletions mifospay/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins {
alias(libs.plugins.mifospay.android.application.firebase)
id("com.google.android.gms.oss-licenses-plugin")
alias(libs.plugins.roborazzi)
alias(libs.plugins.kotlin.android)
}

apply(from = "../config/quality/quality.gradle")
Expand Down Expand Up @@ -89,6 +90,8 @@ dependencies {
implementation(libs.androidx.tracing.ktx)
implementation(libs.kotlinx.coroutines.guava)
implementation(libs.coil.kt)
implementation(libs.material)
implementation(libs.androidx.activity)

ksp(libs.hilt.compiler)

Expand Down
4 changes: 2 additions & 2 deletions mifospay/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
android:theme="@style/SplashScreenTheme"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
Expand Down
Loading

0 comments on commit f195a90

Please sign in to comment.