Validate header value in settings
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package org.koitharu.kotatsu.settings
|
||||
|
||||
import okhttp3.Headers
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.network.CommonHeaders
|
||||
import org.koitharu.kotatsu.utils.EditTextValidator
|
||||
|
||||
class HeaderValidator : EditTextValidator() {
|
||||
|
||||
private val headers = Headers.Builder()
|
||||
|
||||
override fun validate(text: String): ValidationResult {
|
||||
val trimmed = text.trim()
|
||||
if (trimmed.isEmpty()) {
|
||||
return ValidationResult.Success
|
||||
}
|
||||
return if (!validateImpl(trimmed)) {
|
||||
ValidationResult.Failed(context.getString(R.string.invalid_value_message))
|
||||
} else {
|
||||
ValidationResult.Success
|
||||
}
|
||||
}
|
||||
|
||||
private fun validateImpl(value: String): Boolean = runCatching {
|
||||
headers[CommonHeaders.USER_AGENT] = value
|
||||
}.isSuccess
|
||||
}
|
||||
@@ -44,7 +44,7 @@ fun PreferenceFragmentCompat.addPreferencesFromRepository(repository: RemoteMang
|
||||
EditTextBindListener(
|
||||
inputType = EditorInfo.TYPE_CLASS_TEXT,
|
||||
hint = key.defaultValue,
|
||||
validator = null,
|
||||
validator = HeaderValidator(),
|
||||
),
|
||||
)
|
||||
setTitle(R.string.user_agent)
|
||||
|
||||
@@ -416,4 +416,5 @@
|
||||
<string name="suggestions_enable_prompt">Do you want to receive personalized manga suggestions?</string>
|
||||
<string name="translations">Translations</string>
|
||||
<string name="web_view_unavailable">WebView not available: check if WebView provider is installed</string>
|
||||
<string name="invalid_value_message">Invalid value</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user