feat(settings): split pasted dictionary URLs into separate fields
Pasting several URLs separated by spaces, newlines or commas into one field now distributes them across separate fields when every token is a valid source template. Previously the joined text passed validation as a single URL and failed at download time.
This commit is contained in:
1 parent
3d83d04cea
commit
84fcd77f02
1 file changed
+20
+20
@@ -66,6 +66,15 @@ private fun validateDictionaryUrl(context: UrlValidationContext): String? {
|
||||
}
|
||||
}
|
||||
|
||||
private val urlSplitRegex = Regex("[\\s,;]+")
|
||||
|
||||
private fun splitPastedUrls(text: String): List<String>? =
|
||||
text.split(urlSplitRegex).filter { it.isNotEmpty() }.takeIf { tokens ->
|
||||
tokens.size > 1 && tokens.all {
|
||||
DictionarySource.validateTemplate(it) is DictionarySource.ValidationResult.Valid
|
||||
}
|
||||
}
|
||||
|
||||
private val urlInputTransformation = InputTransformation {
|
||||
val text = toString()
|
||||
val filtered = text
|
||||
@@ -92,6 +101,17 @@ fun DictionaryUrlInputContent(
|
||||
val scrollState = rememberScrollState()
|
||||
val focusManager = LocalFocusManager.current
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
snapshotFlow { urls.map { it.text.toString() } }.collect { texts ->
|
||||
val (index, tokens) = texts.withIndex().firstNotNullOfOrNull { (i, text) ->
|
||||
splitPastedUrls(text)?.let { i to it }
|
||||
} ?: return@collect
|
||||
urls[index].edit { replace(0, length, tokens.first()) }
|
||||
urls.addAll(index + 1, tokens.subList(1, tokens.size).map { TextFieldState(it) })
|
||||
validationErrors.clear()
|
||||
}
|
||||
}
|
||||
|
||||
val errorMessages = ErrorMessages(
|
||||
empty = stringResource(R.string.dictionary_source_url_empty),
|
||||
duplicateInForm = stringResource(R.string.dictionary_source_duplicate_in_form),
|
||||
|
||||
Reference in new issue
Block a user