-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathEnterUrlComposable.kt
361 lines (343 loc) · 12.5 KB
/
EnterUrlComposable.kt
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/***************************************************************************************************
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
**************************************************************************************************/
package at.bitfire.icsdroid.ui.views
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.PressInteraction
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.FolderOpen
import androidx.compose.material.icons.filled.Link
import androidx.compose.material.icons.rounded.Warning
import androidx.compose.material3.Icon
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em
import at.bitfire.icsdroid.R
import at.bitfire.icsdroid.ui.ResourceInfo
import at.bitfire.icsdroid.ui.partials.AlertDialog
import kotlinx.coroutines.launch
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun EnterUrlComposable(
requiresAuth: Boolean,
onRequiresAuthChange: (Boolean) -> Unit,
username: String?,
onUsernameChange: (String) -> Unit,
password: String?,
onPasswordChange: (String) -> Unit,
isInsecure: Boolean,
url: String?,
fileName: String?,
onUrlChange: (String?) -> Unit,
urlError: String?,
supportsAuthentication: Boolean,
isVerifyingUrl: Boolean,
validationResult: ResourceInfo?,
onValidationResultDismiss: () -> Unit,
onPickFileRequested: () -> Unit,
onSubmit: () -> Unit
) {
val context = LocalContext.current
validationResult?.exception?.let { exception ->
val errorMessage = exception.localizedMessage ?: exception.message ?: exception.toString()
AlertDialog(
errorMessage, exception, onValidationResultDismiss
)
}
val snackbarHostState = remember { SnackbarHostState() }
LaunchedEffect(isVerifyingUrl) {
if (isVerifyingUrl) {
snackbarHostState.showSnackbar(
context.getString(R.string.add_calendar_validating),
duration = SnackbarDuration.Indefinite
)
} else {
snackbarHostState.currentSnackbarData?.dismiss()
}
}
Scaffold(
snackbarHost = { SnackbarHost(snackbarHostState) }
) { paddingValues ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
.verticalScroll(rememberScrollState())
) {
AnimatedVisibility(isVerifyingUrl) {
LinearProgressIndicator(Modifier.fillMaxWidth())
}
val scope = rememberCoroutineScope()
val state = rememberPagerState(pageCount = { 2 })
TabRow(
state.currentPage,
modifier = Modifier.padding(horizontal = 16.dp)
) {
Tab(state.currentPage == 0, onClick = {
onUrlChange(null)
scope.launch { state.scrollToPage(0) }},
modifier = Modifier.padding(vertical = 8.dp)
) {
val color = if (state.currentPage == 0) MaterialTheme.colorScheme.primary else Color.Gray
Spacer(modifier = Modifier.height(4.dp))
Icon(
Icons.Default.Link,
stringResource(R.string.add_calendar_subscribe_url),
tint = color
)
Text(
stringResource(R.string.add_calendar_subscribe_url).uppercase(),
modifier = Modifier.padding(8.dp, 0.dp, 8.dp, 4.dp),
color = color,
fontSize = 3.em,
fontWeight = FontWeight.Bold
)
}
Tab(state.currentPage == 1, onClick = {
onUrlChange(null)
scope.launch { state.scrollToPage(1) }},
modifier = Modifier.padding(vertical = 8.dp)
) {
val color = if (state.currentPage == 1) MaterialTheme.colorScheme.primary else Color.Gray
Spacer(modifier = Modifier.height(4.dp))
Icon(
Icons.Default.FolderOpen,
stringResource(R.string.add_calendar_subscribe_url),
tint = color
)
Text(
stringResource(R.string.add_calendar_subscribe_file).uppercase(),
modifier = Modifier.padding(8.dp, 0.dp, 8.dp, 4.dp),
color = color,
fontSize = 3.em,
fontWeight = FontWeight.Bold
)
}
}
// Instead of adding vertical padding to column, use spacer so that if content is
// scrolled, it is not spaced
Spacer(modifier = Modifier.height(16.dp))
HorizontalPager(
state,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp)
.weight(1f),
verticalAlignment = Alignment.Top
) { index ->
Column(
modifier = Modifier
.padding(8.dp)
.fillMaxWidth()
.verticalScroll(rememberScrollState())) {
when (index) {
0 -> SubscribeToUrl(
url,
onUrlChange,
onSubmit,
urlError,
isVerifyingUrl,
isInsecure,
supportsAuthentication,
requiresAuth,
username,
password,
onRequiresAuthChange,
onUsernameChange,
onPasswordChange
)
1 -> SubscribeToFile(
fileName,
onUrlChange,
onSubmit,
urlError,
isVerifyingUrl,
onPickFileRequested
)
}
}
}
Spacer(modifier = Modifier.height(16.dp))
}
}
}
@Composable
private fun ColumnScope.SubscribeToUrl(
url: String?,
onUrlChange: (String) -> Unit,
onSubmit: () -> Unit,
error: String?,
verifying: Boolean,
isInsecure: Boolean,
supportsAuthentication: Boolean,
requiresAuth: Boolean,
username: String?,
password: String?,
onRequiresAuthChange: (Boolean) -> Unit,
onUsernameChange: (String) -> Unit,
onPasswordChange: (String) -> Unit
) {
ResourceInput(
url,
onUrlChange,
verifying,
onSubmit,
error,
labelText = stringResource(R.string.add_calendar_pick_url_label)
)
AnimatedVisibility(
visible = isInsecure,
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
) {
Row(Modifier.fillMaxWidth()) {
Icon(imageVector = Icons.Rounded.Warning, contentDescription = null)
Text(
text = stringResource(R.string.add_calendar_authentication_without_https_warning),
style = MaterialTheme.typography.bodyLarge,
modifier = Modifier.fillMaxWidth()
)
}
}
AnimatedVisibility(visible = supportsAuthentication) {
LoginCredentialsComposable(
requiresAuth,
username,
password,
onRequiresAuthChange,
onUsernameChange,
onPasswordChange
)
}
}
@Composable
private fun ColumnScope.SubscribeToFile(
filename: String?,
onChange: (String) -> Unit,
onSubmit: () -> Unit,
error: String?,
verifying: Boolean,
onPickFileRequested: () -> Unit
) {
ResourceInput(
filename,
onChange,
verifying,
onSubmit,
error,
stringResource(R.string.add_calendar_pick_file),
readOnly = true,
onPickFileRequested
)
}
@Composable
private fun ColumnScope.ResourceInput(
value: String?,
onChange: (String) -> Unit,
enabled: Boolean,
onSubmit: () -> Unit,
error: String?,
labelText: String,
readOnly: Boolean = false,
onClick: () -> Unit = {}
) {
TextField(
value = value ?: "",
onValueChange = onChange,
modifier = Modifier
.fillMaxWidth()
.padding(end = 16.dp),
enabled = !enabled,
readOnly = readOnly,
keyboardOptions = KeyboardOptions(
capitalization = KeyboardCapitalization.None,
keyboardType = KeyboardType.Uri,
imeAction = ImeAction.Go
),
keyboardActions = KeyboardActions { onSubmit() },
maxLines = 8,
placeholder = { Text(labelText) },
isError = error != null,
interactionSource = remember { MutableInteractionSource() }.also { interactionSource ->
LaunchedEffect(interactionSource) {
interactionSource.interactions.collect {
if (it is PressInteraction.Release)
onClick()
}
}
}
)
AnimatedVisibility(visible = error != null) {
Text(
text = error ?: "",
color = MaterialTheme.colorScheme.error,
modifier = Modifier.fillMaxWidth(),
style = MaterialTheme.typography.bodySmall
)
}
}
@Preview
@Composable
fun EnterUrlComposable_Preview() {
EnterUrlComposable(
requiresAuth = true,
onRequiresAuthChange = {},
username = "previewUser",
onUsernameChange = {},
password = "previewUserPassword",
onPasswordChange = {},
isInsecure = true,
url = "http://previewUrl.com/looong/looong/looong/looong/looong/looong/calendarfile.ics" +
"\n\n a\n b\n c\n\n" +
"http://previewUrl.com/looong/looong/looong/looong/looong/looong/calendarfile.ics",
fileName = "file name",
onUrlChange = {},
urlError = "",
supportsAuthentication = true,
isVerifyingUrl = true,
validationResult = null,
onValidationResultDismiss = {},
onPickFileRequested = {},
onSubmit = {}
)
}