From 84fcd77f02c0f47f31ac901a7ea8f40f3019b491 Mon Sep 17 00:00:00 2001 From: OneWay Date: Tue, 6 Oct 2026 11:08:37 +0800 Subject: [PATCH] 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. --- .../components/DictionaryUrlInputContent.kt | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/app/src/main/java/com/example/research/ui/settings/components/DictionaryUrlInputContent.kt b/app/src/main/java/com/example/research/ui/settings/components/DictionaryUrlInputContent.kt index 44774c9..cad9781 100644 --- a/app/src/main/java/com/example/research/ui/settings/components/DictionaryUrlInputContent.kt +++ b/app/src/main/java/com/example/research/ui/settings/components/DictionaryUrlInputContent.kt @@ -66,6 +66,15 @@ private fun validateDictionaryUrl(context: UrlValidationContext): String? { } } +private val urlSplitRegex = Regex("[\\s,;]+") + +private fun splitPastedUrls(text: String): List? = + 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),