mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-30 22:07:57 +01:00 
			
		
		
		
	Migrate some RxSharedPreferences to FlowSharedPreferences
This commit is contained in:
		| @@ -12,7 +12,6 @@ import androidx.lifecycle.ProcessLifecycleOwner | ||||
| import androidx.multidex.MultiDex | ||||
| import eu.kanade.tachiyomi.data.notification.Notifications | ||||
| import eu.kanade.tachiyomi.data.preference.PreferencesHelper | ||||
| import eu.kanade.tachiyomi.data.preference.getOrDefault | ||||
| import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate | ||||
| import eu.kanade.tachiyomi.util.system.LocaleHelper | ||||
| import org.acra.ACRA | ||||
| @@ -72,7 +71,7 @@ open class App : Application(), LifecycleObserver { | ||||
|     @Suppress("unused") | ||||
|     fun onAppBackgrounded() { | ||||
|         val preferences: PreferencesHelper by injectLazy() | ||||
|         if (preferences.lockAppAfter().getOrDefault() >= 0) { | ||||
|         if (preferences.lockAppAfter().get() >= 0) { | ||||
|             SecureActivityDelegate.locked = true | ||||
|         } | ||||
|     } | ||||
|   | ||||
| @@ -7,6 +7,7 @@ import android.os.Environment | ||||
| import androidx.preference.PreferenceManager | ||||
| import com.f2prateek.rx.preferences.Preference | ||||
| import com.f2prateek.rx.preferences.RxSharedPreferences | ||||
| import com.tfcporciuncula.flow.FlowSharedPreferences | ||||
| import eu.kanade.tachiyomi.R | ||||
| import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys | ||||
| import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values | ||||
| @@ -15,6 +16,7 @@ import java.io.File | ||||
| import java.text.DateFormat | ||||
| import java.text.SimpleDateFormat | ||||
| import java.util.Locale | ||||
| import kotlinx.coroutines.ExperimentalCoroutinesApi | ||||
|  | ||||
| fun <T> Preference<T>.getOrDefault(): T = get() ?: defaultValue()!! | ||||
|  | ||||
| @@ -36,10 +38,12 @@ private class DateFormatConverter : Preference.Adapter<DateFormat> { | ||||
|     } | ||||
| } | ||||
|  | ||||
| @OptIn(ExperimentalCoroutinesApi::class) | ||||
| class PreferencesHelper(val context: Context) { | ||||
|  | ||||
|     private val prefs = PreferenceManager.getDefaultSharedPreferences(context) | ||||
|     private val rxPrefs = RxSharedPreferences.create(prefs) | ||||
|     private val flowPrefs = FlowSharedPreferences(prefs) | ||||
|  | ||||
|     private val defaultDownloadsDir = Uri.fromFile( | ||||
|             File(Environment.getExternalStorageDirectory().absolutePath + File.separator + | ||||
| @@ -51,13 +55,13 @@ class PreferencesHelper(val context: Context) { | ||||
|  | ||||
|     fun startScreen() = prefs.getInt(Keys.startScreen, 1) | ||||
|  | ||||
|     fun useBiometricLock() = rxPrefs.getBoolean(Keys.useBiometricLock, false) | ||||
|     fun useBiometricLock() = flowPrefs.getBoolean(Keys.useBiometricLock, false) | ||||
|  | ||||
|     fun lockAppAfter() = rxPrefs.getInteger(Keys.lockAppAfter, 0) | ||||
|     fun lockAppAfter() = flowPrefs.getInt(Keys.lockAppAfter, 0) | ||||
|  | ||||
|     fun lastAppUnlock() = rxPrefs.getLong(Keys.lastAppUnlock, 0) | ||||
|     fun lastAppUnlock() = flowPrefs.getLong(Keys.lastAppUnlock, 0) | ||||
|  | ||||
|     fun secureScreen() = rxPrefs.getBoolean(Keys.secureScreen, false) | ||||
|     fun secureScreen() = flowPrefs.getBoolean(Keys.secureScreen, false) | ||||
|  | ||||
|     fun hideNotificationContent() = prefs.getBoolean(Keys.hideNotificationContent, false) | ||||
|  | ||||
|   | ||||
| @@ -77,10 +77,4 @@ abstract class BaseActivity : AppCompatActivity() { | ||||
|  | ||||
|         secureActivityDelegate.onResume() | ||||
|     } | ||||
|  | ||||
|     override fun onDestroy() { | ||||
|         secureActivityDelegate.onDestroy() | ||||
|  | ||||
|         super.onDestroy() | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -27,10 +27,4 @@ abstract class BaseRxActivity<P : BasePresenter<*>> : NucleusAppCompatActivity<P | ||||
|  | ||||
|         secureActivityDelegate.onResume() | ||||
|     } | ||||
|  | ||||
|     override fun onDestroy() { | ||||
|         secureActivityDelegate.onDestroy() | ||||
|  | ||||
|         super.onDestroy() | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -5,30 +5,33 @@ import android.view.WindowManager | ||||
| import androidx.biometric.BiometricManager | ||||
| import androidx.fragment.app.FragmentActivity | ||||
| import eu.kanade.tachiyomi.data.preference.PreferencesHelper | ||||
| import eu.kanade.tachiyomi.data.preference.getOrDefault | ||||
| import java.util.Date | ||||
| import rx.Subscription | ||||
| import kotlinx.coroutines.CoroutineScope | ||||
| import kotlinx.coroutines.Dispatchers | ||||
| import kotlinx.coroutines.flow.launchIn | ||||
| import kotlinx.coroutines.flow.onEach | ||||
| import uy.kohesive.injekt.injectLazy | ||||
|  | ||||
| class SecureActivityDelegate(private val activity: FragmentActivity) { | ||||
|  | ||||
|     private val preferences by injectLazy<PreferencesHelper>() | ||||
|  | ||||
|     private var secureScreenSubscription: Subscription? = null | ||||
|     private val uiScope = CoroutineScope(Dispatchers.Main) | ||||
|  | ||||
|     fun onCreate() { | ||||
|         secureScreenSubscription = preferences.secureScreen().asObservable() | ||||
|                 .subscribe { | ||||
|                     if (it) { | ||||
|                         activity.window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE) | ||||
|                     } else { | ||||
|                         activity.window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) | ||||
|                     } | ||||
|         preferences.secureScreen().asFlow() | ||||
|             .onEach { | ||||
|                 if (it) { | ||||
|                     activity.window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE) | ||||
|                 } else { | ||||
|                     activity.window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) | ||||
|                 } | ||||
|             } | ||||
|             .launchIn(uiScope) | ||||
|     } | ||||
|  | ||||
|     fun onResume() { | ||||
|         val lockApp = preferences.useBiometricLock().getOrDefault() | ||||
|         val lockApp = preferences.useBiometricLock().get() | ||||
|         if (lockApp && BiometricManager.from(activity).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) { | ||||
|             if (isAppLocked()) { | ||||
|                 val intent = Intent(activity, BiometricUnlockActivity::class.java) | ||||
| @@ -40,14 +43,10 @@ class SecureActivityDelegate(private val activity: FragmentActivity) { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fun onDestroy() { | ||||
|         secureScreenSubscription?.unsubscribe() | ||||
|     } | ||||
|  | ||||
|     private fun isAppLocked(): Boolean { | ||||
|         return locked && | ||||
|                 (preferences.lockAppAfter().getOrDefault() <= 0 || | ||||
|                         Date().time >= preferences.lastAppUnlock().getOrDefault() + 60 * 1000 * preferences.lockAppAfter().getOrDefault()) | ||||
|                 (preferences.lockAppAfter().get() <= 0 || | ||||
|                         Date().time >= preferences.lastAppUnlock().get() + 60 * 1000 * preferences.lockAppAfter().get()) | ||||
|     } | ||||
|  | ||||
|     companion object { | ||||
|   | ||||
| @@ -9,9 +9,15 @@ import eu.kanade.tachiyomi.util.preference.intListPreference | ||||
| import eu.kanade.tachiyomi.util.preference.summaryRes | ||||
| import eu.kanade.tachiyomi.util.preference.switchPreference | ||||
| import eu.kanade.tachiyomi.util.preference.titleRes | ||||
| import kotlinx.coroutines.CoroutineScope | ||||
| import kotlinx.coroutines.Dispatchers | ||||
| import kotlinx.coroutines.flow.launchIn | ||||
| import kotlinx.coroutines.flow.onEach | ||||
|  | ||||
| class SettingsSecurityController : SettingsController() { | ||||
|  | ||||
|     private val uiScope = CoroutineScope(Dispatchers.Main) | ||||
|  | ||||
|     override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) { | ||||
|         titleRes = R.string.pref_category_security | ||||
|  | ||||
| @@ -36,8 +42,10 @@ class SettingsSecurityController : SettingsController() { | ||||
|                 defaultValue = "0" | ||||
|                 summary = "%s" | ||||
|  | ||||
|                 preferences.useBiometricLock().asObservable() | ||||
|                         .subscribeUntilDestroy { isVisible = it } | ||||
|                 isVisible = preferences.useBiometricLock().get() | ||||
|                 preferences.useBiometricLock().asFlow() | ||||
|                     .onEach { isVisible = it } | ||||
|                     .launchIn(uiScope) | ||||
|             } | ||||
|         } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user