-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAuthCheckView.kt
More file actions
290 lines (274 loc) · 9.32 KB
/
AuthCheckView.kt
File metadata and controls
290 lines (274 loc) · 9.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package to.bitkit.ui.components
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.testTag
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.lifecycle.compose.collectAsStateWithLifecycle
import to.bitkit.R
import to.bitkit.env.Env
import to.bitkit.ui.scaffold.AppTopBar
import to.bitkit.ui.shared.modifiers.clickableAlpha
import to.bitkit.ui.shared.util.blockPointerInputPassthrough
import to.bitkit.ui.theme.AppThemeSurface
import to.bitkit.ui.theme.Colors
import to.bitkit.ui.utils.rememberBiometricAuthSupported
import to.bitkit.viewmodels.AppViewModel
import to.bitkit.viewmodels.SettingsViewModel
@Composable
fun AuthCheckView(
showLogoOnPin: Boolean = false,
appViewModel: AppViewModel,
settingsViewModel: SettingsViewModel,
isBiometrySupported: Boolean = rememberBiometricAuthSupported(),
requireBiometrics: Boolean = false,
requirePin: Boolean = false,
onSuccess: (() -> Unit)? = null,
onBack: (() -> Unit)? = null,
) {
val isBiometricsEnabled by settingsViewModel.isBiometricEnabled.collectAsStateWithLifecycle()
val attemptsRemaining by appViewModel.pinAttemptsRemaining.collectAsStateWithLifecycle()
AuthCheckViewContent(
isBiometricsEnabled = isBiometricsEnabled,
isBiometrySupported = isBiometrySupported,
showLogoOnPin = showLogoOnPin,
attemptsRemaining = attemptsRemaining,
requireBiometrics = requireBiometrics,
requirePin = requirePin,
validatePin = appViewModel::validatePin,
onSuccess = onSuccess,
onBack = onBack,
onClickForgotPin = { appViewModel.setShowForgotPin(true) },
)
}
@Suppress("ComplexCondition")
@Composable
private fun AuthCheckViewContent(
isBiometricsEnabled: Boolean,
isBiometrySupported: Boolean,
showLogoOnPin: Boolean,
attemptsRemaining: Int,
requireBiometrics: Boolean = false,
requirePin: Boolean = false,
validatePin: (String) -> Boolean,
onSuccess: (() -> Unit)? = null,
onBack: (() -> Unit)?,
onClickForgotPin: () -> Unit,
) {
var showBio by rememberSaveable { mutableStateOf(isBiometricsEnabled) }
LaunchedEffect(isBiometricsEnabled) {
showBio = isBiometricsEnabled
}
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.fillMaxSize()
.background(Colors.Black)
.blockPointerInputPassthrough()
.navigationBarsPadding()
) {
if ((showBio && isBiometrySupported && !requirePin) || requireBiometrics) {
BiometricsView(
onSuccess = { onSuccess?.invoke() },
onFailure = { showBio = false },
)
} else {
PinPad(
showLogo = showLogoOnPin,
validatePin = validatePin,
attemptsRemaining = attemptsRemaining,
allowBiometrics = isBiometricsEnabled && isBiometrySupported && !requirePin,
onShowBiometrics = { showBio = true },
onSuccess = onSuccess,
onBack = onBack,
onClickForgotPin = onClickForgotPin,
)
}
}
}
@Composable
private fun PinPad(
showLogo: Boolean = false,
validatePin: (String) -> Boolean,
attemptsRemaining: Int,
allowBiometrics: Boolean,
onShowBiometrics: () -> Unit,
onSuccess: (() -> Unit)?,
onBack: (() -> Unit)? = null,
onClickForgotPin: () -> Unit = {},
) {
var pin by remember { mutableStateOf("") }
val isLastAttempt = attemptsRemaining == 1
LaunchedEffect(pin) {
if (pin.length == Env.PIN_LENGTH) {
if (validatePin(pin)) {
onSuccess?.invoke()
}
pin = ""
}
}
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.testTag("PinPad")
) {
AppTopBar(titleText = null, onBackClick = onBack)
Box(
contentAlignment = Alignment.BottomCenter,
modifier = Modifier.weight(1f)
) {
if (showLogo) {
Image(
painter = painterResource(R.drawable.bitkit_logo),
contentDescription = null,
contentScale = ContentScale.Fit,
modifier = Modifier.size(280.dp)
)
}
}
Subtitle(text = stringResource(R.string.security__pin_enter), modifier = Modifier.padding(bottom = 32.dp))
if (attemptsRemaining < Env.PIN_ATTEMPTS) {
if (isLastAttempt) {
BodyS(
text = stringResource(R.string.security__pin_last_attempt),
color = Colors.Brand,
textAlign = TextAlign.Center,
modifier = Modifier
.padding(horizontal = 16.dp)
.testTag("LastAttempt")
)
} else {
BodyS(
text = stringResource(R.string.security__pin_attempts)
.replace("{attemptsRemaining}", "$attemptsRemaining"),
color = Colors.Brand,
textAlign = TextAlign.Center,
modifier = Modifier
.clickableAlpha { onClickForgotPin() }
.testTag("AttemptsRemaining")
)
}
VerticalSpacer(16.dp)
}
if (allowBiometrics) {
val biometricsName = stringResource(R.string.security__bio)
PrimaryButton(
text = stringResource(R.string.security__pin_use_biometrics)
.replace("{biometricsName}", biometricsName),
onClick = onShowBiometrics,
fullWidth = false,
size = ButtonSize.Small,
icon = {
Icon(
painter = painterResource(R.drawable.ic_fingerprint),
contentDescription = null,
tint = Colors.Brand,
modifier = Modifier.size(16.dp)
)
},
)
VerticalSpacer(16.dp)
}
PinDots(
pin = pin,
modifier = Modifier.padding(vertical = 16.dp),
)
NumberPad(
onPress = { key ->
if (key == KEY_DELETE) {
if (pin.isNotEmpty()) {
pin = pin.dropLast(1)
}
} else if (pin.length < Env.PIN_LENGTH) {
pin += key
}
},
type = NumberPadType.SIMPLE,
modifier = Modifier.height(310.dp),
)
}
}
@Preview(showBackground = true)
@Composable
private fun PreviewBio() {
AppThemeSurface {
AuthCheckViewContent(
onSuccess = {},
onBack = {},
isBiometricsEnabled = true,
isBiometrySupported = true,
showLogoOnPin = true,
validatePin = { true },
attemptsRemaining = 8,
onClickForgotPin = {},
)
}
}
@Preview(showBackground = true)
@Composable
private fun PreviewPin() {
AppThemeSurface {
AuthCheckViewContent(
onSuccess = {},
onBack = {},
isBiometricsEnabled = false,
isBiometrySupported = true,
showLogoOnPin = true,
validatePin = { true },
attemptsRemaining = 8,
onClickForgotPin = {},
)
}
}
@Preview(showBackground = true)
@Composable
private fun PreviewPinAttempts() {
AppThemeSurface {
AuthCheckViewContent(
onSuccess = {},
onBack = null,
isBiometricsEnabled = false,
isBiometrySupported = true,
showLogoOnPin = false,
validatePin = { true },
attemptsRemaining = 6,
onClickForgotPin = {},
)
}
}
@Preview(showBackground = true)
@Composable
private fun PreviewPinAttemptLast() {
AppThemeSurface {
AuthCheckViewContent(
onSuccess = {},
onBack = {},
isBiometricsEnabled = false,
isBiometrySupported = true,
showLogoOnPin = true,
validatePin = { true },
attemptsRemaining = 1,
onClickForgotPin = {},
)
}
}