Split url and domain validations #1043
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package org.koitharu.kotatsu.settings.utils.validation
|
||||
|
||||
import android.webkit.URLUtil
|
||||
import okhttp3.HttpUrl
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.util.EditTextValidator
|
||||
@@ -21,20 +20,15 @@ class DomainValidator : EditTextValidator() {
|
||||
|
||||
companion object {
|
||||
|
||||
fun isValidDomain(value: String): Boolean {
|
||||
if ( ! URLUtil.isValidUrl(value) ) {
|
||||
return runCatching {
|
||||
require(value.isNotEmpty())
|
||||
val parts = value.split(':')
|
||||
require(parts.size <= 2)
|
||||
val urlBuilder = HttpUrl.Builder()
|
||||
urlBuilder.host(parts.first())
|
||||
if (parts.size == 2) {
|
||||
urlBuilder.port(parts[1].toInt())
|
||||
}
|
||||
}.isSuccess
|
||||
fun isValidDomain(value: String): Boolean = runCatching {
|
||||
require(value.isNotEmpty())
|
||||
val parts = value.split(':')
|
||||
require(parts.size <= 2)
|
||||
val urlBuilder = HttpUrl.Builder()
|
||||
urlBuilder.host(parts.first())
|
||||
if (parts.size == 2) {
|
||||
urlBuilder.port(parts[1].toInt())
|
||||
}
|
||||
return true
|
||||
}
|
||||
}.isSuccess
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.koitharu.kotatsu.settings.utils.validation
|
||||
|
||||
import android.webkit.URLUtil
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.util.EditTextValidator
|
||||
|
||||
class UrlValidator : EditTextValidator() {
|
||||
|
||||
override fun validate(text: String): ValidationResult {
|
||||
val trimmed = text.trim()
|
||||
if (trimmed.isEmpty()) {
|
||||
return ValidationResult.Success
|
||||
}
|
||||
return if (!isValidUrl(trimmed)) {
|
||||
ValidationResult.Failed(context.getString(R.string.invalid_server_address_message))
|
||||
} else {
|
||||
ValidationResult.Success
|
||||
}
|
||||
}
|
||||
|
||||
private fun isValidUrl(str: String): Boolean {
|
||||
return URLUtil.isValidUrl(str) || DomainValidator.isValidDomain(str)
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ import org.koitharu.kotatsu.core.ui.AlertDialogFragment
|
||||
import org.koitharu.kotatsu.core.util.ext.ifNullOrEmpty
|
||||
import org.koitharu.kotatsu.core.util.ext.withArgs
|
||||
import org.koitharu.kotatsu.databinding.PreferenceDialogAutocompletetextviewBinding
|
||||
import org.koitharu.kotatsu.settings.utils.validation.DomainValidator
|
||||
import org.koitharu.kotatsu.settings.utils.validation.UrlValidator
|
||||
import org.koitharu.kotatsu.sync.data.SyncSettings
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -58,7 +58,7 @@ class SyncHostDialogFragment : AlertDialogFragment<PreferenceDialogAutocompletet
|
||||
binding.dropdown.setOnClickListener {
|
||||
editText.showDropDown()
|
||||
}
|
||||
DomainValidator().attachToEditText(editText)
|
||||
UrlValidator().attachToEditText(editText)
|
||||
}
|
||||
|
||||
override fun onClick(dialog: DialogInterface, which: Int) {
|
||||
|
||||
@@ -278,7 +278,8 @@
|
||||
<string name="exclude_nsfw_from_history_summary">Manga marked as NSFW will never be added to the history and your progress will not be saved</string>
|
||||
<string name="clear_cookies_summary">Can help in case of some issues. All authorizations will be invalidated</string>
|
||||
<string name="show_all">Show all</string>
|
||||
<string name="invalid_domain_message">Invalid server address</string>
|
||||
<string name="invalid_domain_message">Invalid domain</string>
|
||||
<string name="invalid_server_address_message">Invalid server address</string>
|
||||
<string name="select_range">Select range</string>
|
||||
<string name="clear_all_history">Clear all history</string>
|
||||
<string name="last_2_hours">Last 2 hours</string>
|
||||
|
||||
Reference in New Issue
Block a user