Add confirm exist setting (closes #2615)

This commit is contained in:
arkon 2020-04-18 20:05:31 -04:00
parent 8a243ffb57
commit 321a4b24b9
5 changed files with 39 additions and 0 deletions

View File

@ -11,6 +11,8 @@ object PreferenceKeys {
const val themeDark = "pref_theme_dark_key" const val themeDark = "pref_theme_dark_key"
const val confirmExit = "pref_confirm_exit"
const val rotation = "pref_rotation_type_key" const val rotation = "pref_rotation_type_key"
const val enableTransitions = "pref_enable_transitions_key" const val enableTransitions = "pref_enable_transitions_key"

View File

@ -54,6 +54,8 @@ class PreferencesHelper(val context: Context) {
fun startScreen() = prefs.getInt(Keys.startScreen, 1) fun startScreen() = prefs.getInt(Keys.startScreen, 1)
fun confirmExit() = prefs.getBoolean(Keys.confirmExit, false)
fun useBiometricLock() = flowPrefs.getBoolean(Keys.useBiometricLock, false) fun useBiometricLock() = flowPrefs.getBoolean(Keys.useBiometricLock, false)
fun lockAppAfter() = flowPrefs.getInt(Keys.lockAppAfter, 0) fun lockAppAfter() = flowPrefs.getInt(Keys.lockAppAfter, 0)

View File

@ -4,6 +4,7 @@ import android.app.SearchManager
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Toast
import com.bluelinelabs.conductor.Conductor import com.bluelinelabs.conductor.Conductor
import com.bluelinelabs.conductor.Controller import com.bluelinelabs.conductor.Controller
import com.bluelinelabs.conductor.ControllerChangeHandler 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.SourceController
import eu.kanade.tachiyomi.ui.source.global_search.GlobalSearchController import eu.kanade.tachiyomi.ui.source.global_search.GlobalSearchController
import eu.kanade.tachiyomi.util.lang.launchInUI import eu.kanade.tachiyomi.util.lang.launchInUI
import eu.kanade.tachiyomi.util.lang.launchUI
import java.util.Date import java.util.Date
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
@ -53,6 +56,7 @@ class MainActivity : BaseActivity<MainActivityBinding>() {
lateinit var tabAnimator: ViewHeightAnimator lateinit var tabAnimator: ViewHeightAnimator
private lateinit var bottomNavAnimator: ViewHeightAnimator private lateinit var bottomNavAnimator: ViewHeightAnimator
private var isConfirmingExit: Boolean = false
private var isHandlingShortcut: Boolean = false private var isHandlingShortcut: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -259,12 +263,35 @@ class MainActivity : BaseActivity<MainActivityBinding>() {
override fun onBackPressed() { override fun onBackPressed() {
val backstackSize = router.backstackSize val backstackSize = router.backstackSize
if (backstackSize == 1 && router.getControllerWithTag("$startScreenId") == null) { if (backstackSize == 1 && router.getControllerWithTag("$startScreenId") == null) {
// Return to start screen
setSelectedNavItem(startScreenId) setSelectedNavItem(startScreenId)
} else if (shouldHandleExitConfirmation()) {
// Exit confirmation (resets after 2 seconds)
launchUI { resetExitConfirmation() }
} else if (backstackSize == 1 || !router.handleBack()) { } else if (backstackSize == 1 || !router.handleBack()) {
// Regular back
super.onBackPressed() 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) { fun setSelectedNavItem(itemId: Int) {
if (!isFinishing) { if (!isFinishing) {
binding.bottomNav.selectedItemId = itemId binding.bottomNav.selectedItemId = itemId

View File

@ -16,6 +16,7 @@ import eu.kanade.tachiyomi.util.preference.onChange
import eu.kanade.tachiyomi.util.preference.onClick import eu.kanade.tachiyomi.util.preference.onClick
import eu.kanade.tachiyomi.util.preference.preference import eu.kanade.tachiyomi.util.preference.preference
import eu.kanade.tachiyomi.util.preference.preferenceCategory 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.preference.titleRes
import eu.kanade.tachiyomi.util.system.LocaleHelper import eu.kanade.tachiyomi.util.system.LocaleHelper
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
@ -36,6 +37,11 @@ class SettingsGeneralController : SettingsController() {
defaultValue = "1" defaultValue = "1"
summary = "%s" summary = "%s"
} }
switchPreference {
key = Keys.confirmExit
titleRes = R.string.pref_confirm_exit
defaultValue = false
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
preference { preference {
titleRes = R.string.pref_manage_notifications titleRes = R.string.pref_manage_notifications

View File

@ -26,6 +26,7 @@
<string name="label_help">Help</string> <string name="label_help">Help</string>
<string name="unlock_app">Unlock Tachiyomi</string> <string name="unlock_app">Unlock Tachiyomi</string>
<string name="confirm_exit">Press back again to exit</string>
<!-- Actions --> <!-- Actions -->
<string name="action_settings">Settings</string> <string name="action_settings">Settings</string>
@ -143,6 +144,7 @@
<string name="pref_language">Language</string> <string name="pref_language">Language</string>
<string name="system_default">System default</string> <string name="system_default">System default</string>
<string name="pref_date_format">Date format</string> <string name="pref_date_format">Date format</string>
<string name="pref_confirm_exit">Confirm exit</string>
<string name="pref_manage_notifications">Manage notifications</string> <string name="pref_manage_notifications">Manage notifications</string>
<string name="pref_category_security">Security</string> <string name="pref_category_security">Security</string>