Skip to content

Commit

Permalink
improvement(reviewer): use back callbacks
Browse files Browse the repository at this point in the history
Issue 14558
  • Loading branch information
david-allison committed Jan 16, 2025
1 parent 3511953 commit db54e4a
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import android.webkit.WebViewClient
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.contract.ActivityResultContracts
Expand All @@ -82,6 +83,7 @@ import com.ichi2.anim.ActivityTransitionAnimation
import com.ichi2.anki.AbstractFlashcardViewer.Signal.Companion.toSignal
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.anki.android.back.exitViaDoubleTapBackCallback
import com.ichi2.anki.cardviewer.AndroidCardRenderContext
import com.ichi2.anki.cardviewer.AndroidCardRenderContext.Companion.createInstance
import com.ichi2.anki.cardviewer.CardMediaPlayer
Expand Down Expand Up @@ -134,7 +136,6 @@ import com.ichi2.libanki.Card
import com.ichi2.libanki.CardId
import com.ichi2.libanki.ChangeManager
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Consts
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.Decks
import com.ichi2.libanki.Sound.getAvTag
Expand All @@ -146,7 +147,6 @@ import com.ichi2.themes.Themes
import com.ichi2.themes.Themes.getResFromAttr
import com.ichi2.ui.FixedEditText
import com.ichi2.utils.ClipboardUtil.getText
import com.ichi2.utils.HandlerUtils.executeFunctionWithDelay
import com.ichi2.utils.HandlerUtils.newHandler
import com.ichi2.utils.HashUtil.hashSetInit
import com.ichi2.utils.Stopwatch
Expand Down Expand Up @@ -291,9 +291,6 @@ abstract class AbstractFlashcardViewer :
/** Lock to allow thread-safe regeneration of mCard */
private val cardLock: ReadWriteLock = ReentrantReadWriteLock()

/** Preference: Whether the user wants press back twice to return to the main screen" */
private var exitViaDoubleTapBack = false

@VisibleForTesting
val onRenderProcessGoneDelegate = OnRenderProcessGoneDelegate(this)
protected val tts = TTS()
Expand Down Expand Up @@ -340,6 +337,14 @@ abstract class AbstractFlashcardViewer :
},
)

private val defaultOnBackCallback =
object : OnBackPressedCallback(enabled = true) {
override fun handleOnBackPressed() {
// TODO: This should be improved now we're using callbacks
closeReviewer(RESULT_DEFAULT)
}
}

protected inner class FlashCardViewerResultCallback(
private val callback: (result: ActivityResult, reloadRequired: Boolean) -> Unit = { _, _ -> },
) : ActivityResultCallback<ActivityResult> {
Expand Down Expand Up @@ -564,6 +569,12 @@ abstract class AbstractFlashcardViewer :
TtsVoicesFieldFilter.ensureApplied()
}

override fun setupBackPressedCallbacks() {
onBackPressedDispatcher.addCallback(this, defaultOnBackCallback)
onBackPressedDispatcher.addCallback(this, exitViaDoubleTapBackCallback())
super.setupBackPressedCallbacks()
}

protected open fun getContentViewAttr(fullscreenMode: FullScreenMode): Int = R.layout.reviewer

@get:VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
Expand Down Expand Up @@ -655,25 +666,6 @@ abstract class AbstractFlashcardViewer :
}
}

@Deprecated("Deprecated in Java")
override fun onBackPressed() {
if (isDrawerOpen) {
@Suppress("DEPRECATION")
super.onBackPressed()
} else {
Timber.i("Back key pressed")
if (!exitViaDoubleTapBack || backButtonPressedToReturn) {
closeReviewer(RESULT_DEFAULT)
} else {
showSnackbar(R.string.back_pressed_once_reviewer, Snackbar.LENGTH_SHORT)
}
backButtonPressedToReturn = true
executeFunctionWithDelay(Consts.SHORT_TOAST_DURATION) {
backButtonPressedToReturn = false
}
}
}

override fun onKeyDown(
keyCode: Int,
event: KeyEvent,
Expand Down Expand Up @@ -1233,7 +1225,6 @@ abstract class AbstractFlashcardViewer :
largeAnswerButtons = preferences.getBoolean("showLargeAnswerButtons", false)
doubleTapTimeInterval =
preferences.getInt(DOUBLE_TAP_TIME_INTERVAL, DEFAULT_DOUBLE_TAP_TIME_INTERVAL)
exitViaDoubleTapBack = preferences.getBoolean("exitViaDoubleTapBack", false)
gesturesEnabled = preferences.getBoolean(GestureProcessor.PREF_KEY, false)
if (gesturesEnabled) {
gestureProcessor.init(preferences)
Expand Down

0 comments on commit db54e4a

Please sign in to comment.