From 321a4b24b988bc7be5356dc66cf40836fbbffaa2 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 18 Apr 2020 20:05:31 -0400 Subject: [PATCH] Add confirm exist setting (closes #2615) --- .../data/preference/PreferenceKeys.kt | 2 ++ .../data/preference/PreferencesHelper.kt | 2 ++ .../kanade/tachiyomi/ui/main/MainActivity.kt | 27 +++++++++++++++++++ .../ui/setting/SettingsGeneralController.kt | 6 +++++ app/src/main/res/values/strings.xml | 2 ++ 5 files changed, 39 insertions(+) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index 26abc0b0c..5b267c259 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -11,6 +11,8 @@ object PreferenceKeys { const val themeDark = "pref_theme_dark_key" + const val confirmExit = "pref_confirm_exit" + const val rotation = "pref_rotation_type_key" const val enableTransitions = "pref_enable_transitions_key" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index fa7514daf..81852b3fe 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -54,6 +54,8 @@ class PreferencesHelper(val context: Context) { fun startScreen() = prefs.getInt(Keys.startScreen, 1) + fun confirmExit() = prefs.getBoolean(Keys.confirmExit, false) + fun useBiometricLock() = flowPrefs.getBoolean(Keys.useBiometricLock, false) fun lockAppAfter() = flowPrefs.getInt(Keys.lockAppAfter, 0) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index 7c7c786db..b8fa65858 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -4,6 +4,7 @@ import android.app.SearchManager import android.content.Intent import android.os.Bundle import android.view.ViewGroup +import android.widget.Toast import com.bluelinelabs.conductor.Conductor import com.bluelinelabs.conductor.Controller import com.bluelinelabs.conductor.ControllerChangeHandler @@ -30,10 +31,12 @@ import eu.kanade.tachiyomi.ui.recent.updates.UpdatesController import eu.kanade.tachiyomi.ui.source.SourceController import eu.kanade.tachiyomi.ui.source.global_search.GlobalSearchController import eu.kanade.tachiyomi.util.lang.launchInUI +import eu.kanade.tachiyomi.util.lang.launchUI import java.util.Date import java.util.concurrent.TimeUnit import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.delay import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import timber.log.Timber @@ -53,6 +56,7 @@ class MainActivity : BaseActivity() { lateinit var tabAnimator: ViewHeightAnimator private lateinit var bottomNavAnimator: ViewHeightAnimator + private var isConfirmingExit: Boolean = false private var isHandlingShortcut: Boolean = false override fun onCreate(savedInstanceState: Bundle?) { @@ -259,12 +263,35 @@ class MainActivity : BaseActivity() { override fun onBackPressed() { val backstackSize = router.backstackSize if (backstackSize == 1 && router.getControllerWithTag("$startScreenId") == null) { + // Return to start screen setSelectedNavItem(startScreenId) + } else if (shouldHandleExitConfirmation()) { + // Exit confirmation (resets after 2 seconds) + launchUI { resetExitConfirmation() } } else if (backstackSize == 1 || !router.handleBack()) { + // Regular back super.onBackPressed() } } + private suspend fun resetExitConfirmation() { + isConfirmingExit = true + val toast = Toast.makeText(this, R.string.confirm_exit, Toast.LENGTH_LONG) + toast.show() + + delay(2000) + + toast.cancel() + isConfirmingExit = false + } + + private fun shouldHandleExitConfirmation(): Boolean { + return router.backstackSize == 1 && + router.getControllerWithTag("$startScreenId") != null && + preferences.confirmExit() && + !isConfirmingExit + } + fun setSelectedNavItem(itemId: Int) { if (!isFinishing) { binding.bottomNav.selectedItemId = itemId diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt index c491d3392..b5875a982 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt @@ -16,6 +16,7 @@ import eu.kanade.tachiyomi.util.preference.onChange import eu.kanade.tachiyomi.util.preference.onClick import eu.kanade.tachiyomi.util.preference.preference import eu.kanade.tachiyomi.util.preference.preferenceCategory +import eu.kanade.tachiyomi.util.preference.switchPreference import eu.kanade.tachiyomi.util.preference.titleRes import eu.kanade.tachiyomi.util.system.LocaleHelper import kotlinx.coroutines.flow.onEach @@ -36,6 +37,11 @@ class SettingsGeneralController : SettingsController() { defaultValue = "1" summary = "%s" } + switchPreference { + key = Keys.confirmExit + titleRes = R.string.pref_confirm_exit + defaultValue = false + } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { preference { titleRes = R.string.pref_manage_notifications diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 661c44ca0..925c7fe18 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -26,6 +26,7 @@ Help Unlock Tachiyomi + Press back again to exit Settings @@ -143,6 +144,7 @@ Language System default Date format + Confirm exit Manage notifications Security