Validate header value in settings
parent
3778a9e1d4
commit
bf0d34e9cf
@ -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
|
||||
}
|
||||
Loading…
Reference in New Issue