Require authentication to toggle showing NSFW sources

This commit is contained in:
arkon 2021-10-09 17:10:04 -04:00
parent ea34ba53b9
commit f343131802
3 changed files with 53 additions and 24 deletions

View File

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.ui.setting
import androidx.fragment.app.FragmentActivity
import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.extension.ExtensionUpdateJob
@ -7,6 +8,7 @@ import eu.kanade.tachiyomi.util.preference.defaultValue
import eu.kanade.tachiyomi.util.preference.infoPreference
import eu.kanade.tachiyomi.util.preference.onChange
import eu.kanade.tachiyomi.util.preference.preferenceCategory
import eu.kanade.tachiyomi.util.preference.requireAuthentication
import eu.kanade.tachiyomi.util.preference.summaryRes
import eu.kanade.tachiyomi.util.preference.switchPreference
import eu.kanade.tachiyomi.util.preference.titleRes
@ -51,6 +53,12 @@ class SettingsBrowseController : SettingsController() {
titleRes = R.string.pref_show_nsfw_source
summaryRes = R.string.requires_app_restart
defaultValue = true
requireAuthentication(
activity as? FragmentActivity,
activity!!.getString(R.string.pref_category_nsfw_content),
activity!!.getString(R.string.confirm_lock_change),
)
}
infoPreference(R.string.parental_controls_info)

View File

@ -8,6 +8,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.asImmediateFlow
import eu.kanade.tachiyomi.util.preference.defaultValue
import eu.kanade.tachiyomi.util.preference.intListPreference
import eu.kanade.tachiyomi.util.preference.requireAuthentication
import eu.kanade.tachiyomi.util.preference.summaryRes
import eu.kanade.tachiyomi.util.preference.switchPreference
import eu.kanade.tachiyomi.util.preference.titleRes
@ -28,32 +29,14 @@ class SettingsSecurityController : SettingsController() {
key = Keys.useAuthenticator
titleRes = R.string.lock_with_biometrics
defaultValue = false
onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
(activity as? FragmentActivity)?.startAuthentication(
requireAuthentication(
activity as? FragmentActivity,
activity!!.getString(R.string.lock_with_biometrics),
activity!!.getString(R.string.confirm_lock_change),
callback = object : AuthenticatorUtil.AuthenticationCallback() {
override fun onAuthenticationSucceeded(
activity: FragmentActivity?,
result: BiometricPrompt.AuthenticationResult
) {
super.onAuthenticationSucceeded(activity, result)
isChecked = newValue as Boolean
)
}
override fun onAuthenticationError(
activity: FragmentActivity?,
errorCode: Int,
errString: CharSequence
) {
super.onAuthenticationError(activity, errorCode, errString)
activity?.toast(errString.toString())
}
}
)
false
}
}
intListPreference {
key = Keys.lockAppAfter
titleRes = R.string.lock_when_idle
@ -107,6 +90,7 @@ class SettingsSecurityController : SettingsController() {
summaryRes = R.string.secure_screen_summary
defaultValue = false
}
switchPreference {
key = Keys.hideNotificationContent
titleRes = R.string.hide_notification_content

View File

@ -2,6 +2,8 @@ package eu.kanade.tachiyomi.util.preference
import androidx.annotation.StringRes
import androidx.appcompat.content.res.AppCompatResources
import androidx.biometric.BiometricPrompt
import androidx.fragment.app.FragmentActivity
import androidx.preference.CheckBoxPreference
import androidx.preference.DialogPreference
import androidx.preference.EditTextPreference
@ -14,7 +16,11 @@ import androidx.preference.PreferenceManager
import androidx.preference.PreferenceScreen
import androidx.preference.SwitchPreferenceCompat
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.system.AuthenticatorUtil
import eu.kanade.tachiyomi.util.system.AuthenticatorUtil.isAuthenticationSupported
import eu.kanade.tachiyomi.util.system.AuthenticatorUtil.startAuthentication
import eu.kanade.tachiyomi.util.system.getResourceColor
import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.widget.preference.AdaptiveTitlePreferenceCategory
import eu.kanade.tachiyomi.widget.preference.IntListPreference
import eu.kanade.tachiyomi.widget.preference.SwitchPreferenceCategory
@ -125,6 +131,37 @@ inline fun Preference.onChange(crossinline block: (Any?) -> Boolean) {
setOnPreferenceChangeListener { _, newValue -> block(newValue) }
}
inline fun SwitchPreferenceCompat.requireAuthentication(activity: FragmentActivity?, title: String, subtitle: String?) {
onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
if (context.isAuthenticationSupported()) {
activity?.startAuthentication(
title,
subtitle,
callback = object : AuthenticatorUtil.AuthenticationCallback() {
override fun onAuthenticationSucceeded(
activity: FragmentActivity?,
result: BiometricPrompt.AuthenticationResult
) {
super.onAuthenticationSucceeded(activity, result)
isChecked = newValue as Boolean
}
override fun onAuthenticationError(
activity: FragmentActivity?,
errorCode: Int,
errString: CharSequence
) {
super.onAuthenticationError(activity, errorCode, errString)
activity?.toast(errString.toString())
}
}
)
}
false
}
}
var Preference.defaultValue: Any?
get() = null // set only
set(value) {