|
| 1 | +package to.bitkit.ui.screens.wallets.send |
| 2 | + |
| 3 | +import androidx.compose.animation.AnimatedVisibility |
| 4 | +import androidx.compose.foundation.background |
| 5 | +import androidx.compose.foundation.layout.Column |
| 6 | +import androidx.compose.foundation.layout.Spacer |
| 7 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 8 | +import androidx.compose.foundation.layout.height |
| 9 | +import androidx.compose.foundation.layout.navigationBarsPadding |
| 10 | +import androidx.compose.foundation.layout.padding |
| 11 | +import androidx.compose.runtime.Composable |
| 12 | +import androidx.compose.runtime.LaunchedEffect |
| 13 | +import androidx.compose.runtime.getValue |
| 14 | +import androidx.compose.runtime.mutableStateOf |
| 15 | +import androidx.compose.runtime.remember |
| 16 | +import androidx.compose.runtime.setValue |
| 17 | +import androidx.compose.ui.Alignment |
| 18 | +import androidx.compose.ui.Modifier |
| 19 | +import androidx.compose.ui.res.stringResource |
| 20 | +import androidx.compose.ui.text.style.TextAlign |
| 21 | +import androidx.compose.ui.tooling.preview.Preview |
| 22 | +import androidx.compose.ui.unit.dp |
| 23 | +import androidx.lifecycle.compose.collectAsStateWithLifecycle |
| 24 | +import to.bitkit.R |
| 25 | +import to.bitkit.env.Env |
| 26 | +import to.bitkit.ui.appViewModel |
| 27 | +import to.bitkit.ui.components.BodyM |
| 28 | +import to.bitkit.ui.components.BodyS |
| 29 | +import to.bitkit.ui.components.KEY_DELETE |
| 30 | +import to.bitkit.ui.components.PinDots |
| 31 | +import to.bitkit.ui.components.PinNumberPad |
| 32 | +import to.bitkit.ui.scaffold.SheetTopBar |
| 33 | +import to.bitkit.ui.shared.util.clickableAlpha |
| 34 | +import to.bitkit.ui.shared.util.gradientBackground |
| 35 | +import to.bitkit.ui.theme.AppThemeSurface |
| 36 | +import to.bitkit.ui.theme.Colors |
| 37 | + |
| 38 | +const val PIN_CHECK_RESULT_KEY = "PIN_CHECK_RESULT_KEY" |
| 39 | + |
| 40 | +@Composable |
| 41 | +fun PinCheckScreen( |
| 42 | + onBack: () -> Unit, |
| 43 | + onSuccess: () -> Unit, |
| 44 | +) { |
| 45 | + val app = appViewModel ?: return |
| 46 | + val attemptsRemaining by app.pinAttemptsRemaining.collectAsStateWithLifecycle() |
| 47 | + var pin by remember { mutableStateOf("") } |
| 48 | + |
| 49 | + LaunchedEffect(pin) { |
| 50 | + if (pin.length == Env.PIN_LENGTH) { |
| 51 | + if (app.validatePin(pin)) { |
| 52 | + onSuccess() |
| 53 | + } |
| 54 | + pin = "" |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + PinCheckContent( |
| 59 | + pin = pin, |
| 60 | + attemptsRemaining = attemptsRemaining, |
| 61 | + onKeyPress = { key -> |
| 62 | + if (key == KEY_DELETE) { |
| 63 | + if (pin.isNotEmpty()) { |
| 64 | + pin = pin.dropLast(1) |
| 65 | + } |
| 66 | + } else if (pin.length < Env.PIN_LENGTH) { |
| 67 | + pin += key |
| 68 | + } |
| 69 | + }, |
| 70 | + onBack = onBack, |
| 71 | + onClickForgotPin = { app.setShowForgotPin(true) }, |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +@Composable |
| 76 | +private fun PinCheckContent( |
| 77 | + pin: String, |
| 78 | + attemptsRemaining: Int, |
| 79 | + onKeyPress: (String) -> Unit, |
| 80 | + onBack: () -> Unit, |
| 81 | + onClickForgotPin: () -> Unit, |
| 82 | +) { |
| 83 | + val isLastAttempt = attemptsRemaining == 1 |
| 84 | + |
| 85 | + Column( |
| 86 | + modifier = Modifier |
| 87 | + .fillMaxWidth() |
| 88 | + .gradientBackground() |
| 89 | + .navigationBarsPadding() |
| 90 | + ) { |
| 91 | + SheetTopBar( |
| 92 | + titleText = stringResource(R.string.security__pin_send_title), |
| 93 | + onBack = onBack, |
| 94 | + ) |
| 95 | + Spacer(Modifier.height(32.dp)) |
| 96 | + |
| 97 | + Column( |
| 98 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 99 | + ) { |
| 100 | + BodyM( |
| 101 | + text = stringResource(R.string.security__pin_send), |
| 102 | + color = Colors.White64, |
| 103 | + modifier = Modifier.padding(horizontal = 32.dp) |
| 104 | + ) |
| 105 | + |
| 106 | + Spacer(modifier = Modifier.height(32.dp)) |
| 107 | + |
| 108 | + AnimatedVisibility(visible = attemptsRemaining < Env.PIN_ATTEMPTS) { |
| 109 | + if (isLastAttempt) { |
| 110 | + BodyS( |
| 111 | + text = stringResource(R.string.security__pin_last_attempt), |
| 112 | + color = Colors.Brand, |
| 113 | + textAlign = TextAlign.Center, |
| 114 | + modifier = Modifier.padding(horizontal = 32.dp) |
| 115 | + ) |
| 116 | + } else { |
| 117 | + BodyS( |
| 118 | + text = stringResource(R.string.security__pin_attempts) |
| 119 | + .replace("{attemptsRemaining}", "$attemptsRemaining"), |
| 120 | + color = Colors.Brand, |
| 121 | + textAlign = TextAlign.Center, |
| 122 | + modifier = Modifier |
| 123 | + .padding(horizontal = 32.dp) |
| 124 | + .clickableAlpha { onClickForgotPin() } |
| 125 | + ) |
| 126 | + } |
| 127 | + Spacer(modifier = Modifier.height(16.dp)) |
| 128 | + } |
| 129 | + |
| 130 | + PinDots( |
| 131 | + pin = pin, |
| 132 | + modifier = Modifier.padding(vertical = 16.dp), |
| 133 | + ) |
| 134 | + |
| 135 | + Spacer(modifier = Modifier.weight(1f)) |
| 136 | + |
| 137 | + PinNumberPad( |
| 138 | + onPress = onKeyPress, |
| 139 | + modifier = Modifier |
| 140 | + .height(350.dp) |
| 141 | + .background(Colors.Black) |
| 142 | + ) |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +@Preview |
| 148 | +@Composable |
| 149 | +private fun Preview() { |
| 150 | + AppThemeSurface { |
| 151 | + PinCheckContent( |
| 152 | + pin = "123", |
| 153 | + attemptsRemaining = 8, |
| 154 | + onKeyPress = {}, |
| 155 | + onBack = {}, |
| 156 | + onClickForgotPin = {}, |
| 157 | + ) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +@Preview |
| 162 | +@Composable |
| 163 | +private fun PreviewAttempts() { |
| 164 | + AppThemeSurface { |
| 165 | + PinCheckContent( |
| 166 | + pin = "123", |
| 167 | + attemptsRemaining = 3, |
| 168 | + onKeyPress = {}, |
| 169 | + onBack = {}, |
| 170 | + onClickForgotPin = {}, |
| 171 | + ) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +@Preview |
| 176 | +@Composable |
| 177 | +private fun PreviewAttemptsLast() { |
| 178 | + AppThemeSurface { |
| 179 | + PinCheckContent( |
| 180 | + pin = "123", |
| 181 | + attemptsRemaining = 1, |
| 182 | + onKeyPress = {}, |
| 183 | + onBack = {}, |
| 184 | + onClickForgotPin = {}, |
| 185 | + ) |
| 186 | + } |
| 187 | +} |
0 commit comments