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
package com.vauth.foxyvpn.ui.screens
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.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.vauth.foxyvpn.data.FxaAuthRepository
import com.vauth.foxyvpn.data.model.BatchImportItemResult
import com.vauth.foxyvpn.ui.l10n.s
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
/**
* Guided one-by-one batch import. Entries are processed strictly in order:
* entries that sign in cleanly are added immediately; when FxA asks for an
* email verification code the import PAUSES on that entry (the session is
* kept pending in the repository), the user types the code, the entry is
* added, and the loop automatically continues with the next line. Each
* paused entry can also be skipped. Cancelling stops the loop and keeps
* whatever was imported so far.
*/
@Composable
fun GuidedBatchImport(
authRepository: FxaAuthRepository,
onImported: () -> Unit = {},
) {
val scope = rememberCoroutineScope()
var input by remember { mutableStateOf("") }
var items by remember { mutableStateOf<List<FxaAuthRepository.BatchParsedItem>>(emptyList()) }
var index by remember { mutableStateOf(0) }
var results by remember { mutableStateOf<List<BatchImportItemResult>>(emptyList()) }
var running by remember { mutableStateOf(false) }
var processing by remember { mutableStateOf(false) }
var awaitingEmail by remember { mutableStateOf<String?>(null) }
var code by remember { mutableStateOf("") }
var codeError by remember { mutableStateOf<String?>(null) }
var formError by remember { mutableStateOf<String?>(null) }
var verifying by remember { mutableStateOf(false) }
var notice by remember { mutableStateOf<String?>(null) }
var job by remember { mutableStateOf<Job?>(null) }
fun launchAdvance() {
job = scope.launch {
processing = true
try {
while (index < items.size) {
val item = items[index]
when (val step = authRepository.importBatchItem(item)) {
is FxaAuthRepository.GuidedImportStep.Done -> {
results = results + step.result
index += 1
}
is FxaAuthRepository.GuidedImportStep.NeedsCode -> {
awaitingEmail = step.item.email
code = ""
codeError = null
notice = null
processing = false
return@launch
}
}
}
running = false
processing = false
if (results.any { it.success }) onImported()
} catch (cancelled: CancellationException) {
processing = false
throw cancelled
}
}
}
fun stopImport() {
job?.cancel()
if (awaitingEmail != null) authRepository.abandonPendingLogin()
awaitingEmail = null
processing = false
running = false
}
Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
Text(
"Paste one entry per line (email:password) or a JSON array of " +
"{email, password} / {email, accessToken, refreshToken} objects. " +
"Accounts that ask for an email code pause here so you can enter it.",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
OutlinedTextField(
value = input,
onValueChange = { input = it },
modifier = Modifier
.fillMaxWidth()
.height(140.dp),
placeholder = { Text("name@example.com:password") },
enabled = !running,
)
formError?.let {
Text(it, color = MaterialTheme.colorScheme.error, style = MaterialTheme.typography.bodySmall)
}
if (running && awaitingEmail == null && index < items.size) {
Row(verticalAlignment = Alignment.CenterVertically) {
if (processing) {
CircularProgressIndicator(modifier = Modifier.size(16.dp), strokeWidth = 2.dp)
Spacer(Modifier.width(8.dp))
}
Text(
"${index + 1}/${items.size} · ${items[index].email}",
style = MaterialTheme.typography.labelMedium,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
}
}
awaitingEmail?.let { pausedEmail ->
Text(s("Enter the confirmation code sent to your email"), style = MaterialTheme.typography.bodyMedium)
Text(
pausedEmail,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
OutlinedTextField(
value = code,
onValueChange = { code = it.replace(Regex("[\\s]+"), "") },
label = { Text(s("Confirmation code")) },
singleLine = true,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.NumberPassword),
modifier = Modifier.fillMaxWidth(),
)
codeError?.let {
Text(it, color = MaterialTheme.colorScheme.error, style = MaterialTheme.typography.bodySmall)
}
notice?.let {
Text(it, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.primary)
}
Row(
Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
Button(
onClick = {
verifying = true
codeError = null
scope.launch {
authRepository.submitPendingAccountCode(code)
.onSuccess { account ->
results = results + BatchImportItemResult(
identifier = pausedEmail,
success = true,
account = account,
)
awaitingEmail = null
index += 1
verifying = false
launchAdvance()
}
.onFailure {
codeError = it.message ?: "Verification failed"
verifying = false
}
}
},
enabled = !verifying && code.isNotBlank(),
modifier = Modifier.weight(1f),
) {
if (verifying) CircularProgressIndicator(modifier = Modifier.size(16.dp), strokeWidth = 2.dp)
else Text(s("Verify"))
}
OutlinedButton(
onClick = {
scope.launch {
authRepository.resendPendingCode()
.onSuccess { notice = "A new code was sent." }
.onFailure { codeError = it.message ?: "Could not resend the code" }
}
},
enabled = !verifying,
) {
Text(s("Resend code"))
}
TextButton(
onClick = {
authRepository.abandonPendingLogin()
results = results + BatchImportItemResult(
identifier = pausedEmail,
success = false,
error = "Skipped",
)
awaitingEmail = null
index += 1
launchAdvance()
},
enabled = !verifying,
) {
Text(s("Skip"))
}
}
}
if (results.isNotEmpty()) {
val okCount = results.count { it.success }
Text(
"Imported $okCount of ${results.size}",
style = MaterialTheme.typography.titleSmall,
)
results.forEach { r ->
Row(verticalAlignment = Alignment.CenterVertically) {
Text(
r.identifier,
style = MaterialTheme.typography.bodySmall,
modifier = Modifier.weight(1f),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
Text(
if (r.success) "✓" else "✗ ${r.error.orEmpty()}",
style = MaterialTheme.typography.labelSmall,
color = if (r.success) MaterialTheme.colorScheme.primary
else MaterialTheme.colorScheme.error,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f),
)
}
}
}
if (running) {
OutlinedButton(
onClick = { stopImport() },
modifier = Modifier.fillMaxWidth(),
) {
Text(s("Cancel"))
}
} else {
Button(
onClick = {
val parsed = authRepository.parseBatchInput(input)
if (parsed.isEmpty()) {
formError = "Nothing to import — check the pasted lines."
} else {
formError = null
codeError = null
items = parsed
index = 0
results = emptyList()
awaitingEmail = null
running = true
launchAdvance()
}
},
enabled = input.isNotBlank(),
modifier = Modifier.fillMaxWidth(),
) {
Text(s("Import"))
}
}
AccountHelpRow()
}
}