From f3431318021eeee60c37db7c1a48b30068694b51 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 9 Oct 2021 17:10:04 -0400 Subject: [PATCH] Require authentication to toggle showing NSFW sources --- .../ui/setting/SettingsBrowseController.kt | 8 ++++ .../ui/setting/SettingsSecurityController.kt | 32 ++++------------ .../util/preference/PreferenceDSL.kt | 37 +++++++++++++++++++ 3 files changed, 53 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBrowseController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBrowseController.kt index f2956a114..08d82cb00 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBrowseController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBrowseController.kt @@ -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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSecurityController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSecurityController.kt index a99931de8..195ab4d21 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSecurityController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSecurityController.kt @@ -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( - 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 - } + requireAuthentication( + activity as? FragmentActivity, + activity!!.getString(R.string.lock_with_biometrics), + activity!!.getString(R.string.confirm_lock_change), + ) } + 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 diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/preference/PreferenceDSL.kt b/app/src/main/java/eu/kanade/tachiyomi/util/preference/PreferenceDSL.kt index 857548f1e..81dbcf3c1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/preference/PreferenceDSL.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/preference/PreferenceDSL.kt @@ -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) {