-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement OTP input field on WalletActivationScreen
- Loading branch information
1 parent
7cff367
commit 55fed05
Showing
3 changed files
with
96 additions
and
17 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
app/src/main/kotlin/net/primal/android/core/compose/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,86 @@ | ||
package net.primal.android.core.compose | ||
|
||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.height | ||
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.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
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 | ||
import net.primal.android.theme.AppTheme | ||
|
||
@Composable | ||
fun OtpTextField( | ||
modifier: Modifier = Modifier, | ||
otpText: String, | ||
onOtpTextChange: (String) -> Unit, | ||
otpCount: Int = 6, | ||
onCodeConfirmed: ((String) -> Unit)? = null, | ||
) { | ||
BasicTextField( | ||
modifier = modifier, | ||
value = TextFieldValue( | ||
text = otpText, | ||
selection = TextRange(otpText.length), | ||
), | ||
onValueChange = { | ||
if (it.text.length <= otpCount) { | ||
onOtpTextChange(it.text) | ||
} | ||
}, | ||
keyboardOptions = KeyboardOptions( | ||
keyboardType = KeyboardType.NumberPassword, | ||
imeAction = if (otpText.length == otpCount) ImeAction.Go else ImeAction.Done, | ||
), | ||
keyboardActions = KeyboardActions( | ||
onGo = { | ||
if (otpText.length == otpCount) { | ||
onCodeConfirmed?.invoke(otpText) | ||
} | ||
}, | ||
), | ||
decorationBox = { | ||
Row(horizontalArrangement = Arrangement.Center) { | ||
repeat(otpCount) { | ||
CharText(index = it, text = otpText) | ||
Spacer(modifier = Modifier.width(8.dp)) | ||
} | ||
} | ||
}, | ||
) | ||
} | ||
|
||
@Composable | ||
private fun CharText(index: Int, text: String) { | ||
val char = when { | ||
index == text.length -> "" | ||
index > text.length -> "" | ||
else -> text[index].toString() | ||
} | ||
Text( | ||
modifier = Modifier | ||
.width(44.dp) | ||
.height(56.dp) | ||
.background( | ||
color = AppTheme.extraColorScheme.surfaceVariantAlt1, | ||
shape = AppTheme.shapes.large, | ||
) | ||
.padding(horizontal = 2.dp) | ||
.padding(top = 8.dp), | ||
text = char, | ||
style = AppTheme.typography.headlineLarge, | ||
textAlign = TextAlign.Center, | ||
) | ||
} |
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
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