Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: FlashardViewer - remove 'controlBlocked' #15032

Merged
merged 5 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@
package com.ichi2.anki.testutil

import com.ichi2.anki.reviewer.ReviewerUi
import com.ichi2.anki.reviewer.ReviewerUi.ControlBlock

class MockReviewerUi : ReviewerUi {
override var isDisplayingAnswer = false
private set

override val controlBlocked: ControlBlock?
get() = null

override val isControlBlocked: Boolean = false

companion object {
fun displayingAnswer(): ReviewerUi {
val mockReviewerUi = MockReviewerUi()
Expand Down
224 changes: 94 additions & 130 deletions AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ import com.ichi2.anki.reviewer.*
import com.ichi2.anki.reviewer.AutomaticAnswer.AutomaticallyAnswered
import com.ichi2.anki.reviewer.FullScreenMode.Companion.DEFAULT
import com.ichi2.anki.reviewer.FullScreenMode.Companion.fromPreference
import com.ichi2.anki.reviewer.ReviewerUi.ControlBlock
import com.ichi2.anki.servicelayer.LanguageHintService.applyLanguageHint
import com.ichi2.anki.servicelayer.NoteService.isMarked
import com.ichi2.anki.services.migrationServiceWhileStartedOrNull
Expand Down Expand Up @@ -253,9 +252,6 @@ abstract class AbstractFlashcardViewer :
/** Lock to allow thread-safe regeneration of mCard */
private val mCardLock: ReadWriteLock = ReentrantReadWriteLock()

/** whether controls are currently blocked, and how long we expect them to be */
open var controlBlocked = ControlBlock.SLOW

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

Expand Down Expand Up @@ -1668,7 +1664,6 @@ abstract class AbstractFlashcardViewer :
}

protected open fun unblockControls() {
controlBlocked = ControlBlock.UNBLOCKED
mCardFrame!!.isEnabled = true
flipCardLayout?.isEnabled = true
easeButton1?.unblockBasedOnEase(mCurrentEase)
Expand All @@ -1683,30 +1678,6 @@ abstract class AbstractFlashcardViewer :
invalidateOptionsMenu()
}

/**
* @param quick Whether we expect the control to come back quickly
*/
@VisibleForTesting
protected open fun blockControls(quick: Boolean) {
controlBlocked = if (quick) {
ControlBlock.QUICK
} else {
ControlBlock.SLOW
}
mCardFrame!!.isEnabled = false
flipCardLayout!!.isEnabled = false
mTouchLayer!!.visibility = View.INVISIBLE
mInAnswer = true
easeButton1!!.blockBasedOnEase(mCurrentEase)
easeButton2!!.blockBasedOnEase(mCurrentEase)
easeButton3!!.blockBasedOnEase(mCurrentEase)
easeButton4!!.blockBasedOnEase(mCurrentEase)
if (typeAnswer!!.validForEditText()) {
answerField!!.isEnabled = false
}
invalidateOptionsMenu()
}

fun buryCard(): Boolean {
launchCatchingTask {
withProgress {
Expand Down Expand Up @@ -1765,126 +1736,122 @@ abstract class AbstractFlashcardViewer :
}

override fun executeCommand(which: ViewerCommand, fromGesture: Gesture?): Boolean {
return if (isControlBlocked && which !== ViewerCommand.EXIT) {
false
} else {
when (which) {
ViewerCommand.SHOW_ANSWER -> {
if (displayAnswer) {
return false
}
displayCardAnswer()
true
return when (which) {
ViewerCommand.SHOW_ANSWER -> {
if (displayAnswer) {
return false
}
displayCardAnswer()
true
}

ViewerCommand.FLIP_OR_ANSWER_EASE1 -> {
flipOrAnswerCard(EASE_1)
true
}
ViewerCommand.FLIP_OR_ANSWER_EASE1 -> {
flipOrAnswerCard(EASE_1)
true
}

ViewerCommand.FLIP_OR_ANSWER_EASE2 -> {
flipOrAnswerCard(EASE_2)
true
}
ViewerCommand.FLIP_OR_ANSWER_EASE2 -> {
flipOrAnswerCard(EASE_2)
true
}

ViewerCommand.FLIP_OR_ANSWER_EASE3 -> {
flipOrAnswerCard(EASE_3)
true
}
ViewerCommand.FLIP_OR_ANSWER_EASE3 -> {
flipOrAnswerCard(EASE_3)
true
}

ViewerCommand.FLIP_OR_ANSWER_EASE4 -> {
flipOrAnswerCard(EASE_4)
true
}
ViewerCommand.FLIP_OR_ANSWER_EASE4 -> {
flipOrAnswerCard(EASE_4)
true
}

ViewerCommand.EXIT -> {
closeReviewer(RESULT_DEFAULT)
true
}
ViewerCommand.EXIT -> {
closeReviewer(RESULT_DEFAULT)
true
}

ViewerCommand.UNDO -> {
undo()
true
}
ViewerCommand.UNDO -> {
undo()
true
}

ViewerCommand.EDIT -> {
editCard(fromGesture)
true
}
ViewerCommand.EDIT -> {
editCard(fromGesture)
true
}

ViewerCommand.TAG -> {
showTagsDialog()
true
}
ViewerCommand.TAG -> {
showTagsDialog()
true
}

ViewerCommand.BURY_CARD -> buryCard()
ViewerCommand.BURY_NOTE -> buryNote()
ViewerCommand.SUSPEND_CARD -> suspendCard()
ViewerCommand.SUSPEND_NOTE -> suspendNote()
ViewerCommand.DELETE -> {
showDeleteNoteDialog()
true
}
ViewerCommand.BURY_CARD -> buryCard()
ViewerCommand.BURY_NOTE -> buryNote()
ViewerCommand.SUSPEND_CARD -> suspendCard()
ViewerCommand.SUSPEND_NOTE -> suspendNote()
ViewerCommand.DELETE -> {
showDeleteNoteDialog()
true
}

ViewerCommand.PLAY_MEDIA -> {
playSounds(true)
true
}
ViewerCommand.PLAY_MEDIA -> {
playSounds(true)
true
}

ViewerCommand.PAGE_UP -> {
onPageUp()
true
}
ViewerCommand.PAGE_UP -> {
onPageUp()
true
}

ViewerCommand.PAGE_DOWN -> {
onPageDown()
true
}
ViewerCommand.PAGE_DOWN -> {
onPageDown()
true
}

ViewerCommand.ABORT_AND_SYNC -> {
abortAndSync()
true
}
ViewerCommand.ABORT_AND_SYNC -> {
abortAndSync()
true
}

ViewerCommand.RECORD_VOICE -> {
recordVoice()
true
}
ViewerCommand.RECORD_VOICE -> {
recordVoice()
true
}

ViewerCommand.REPLAY_VOICE -> {
replayVoice()
true
}
ViewerCommand.REPLAY_VOICE -> {
replayVoice()
true
}

ViewerCommand.TOGGLE_WHITEBOARD -> {
toggleWhiteboard()
true
}
ViewerCommand.TOGGLE_WHITEBOARD -> {
toggleWhiteboard()
true
}

ViewerCommand.CLEAR_WHITEBOARD -> {
clearWhiteboard()
true
}
ViewerCommand.CLEAR_WHITEBOARD -> {
clearWhiteboard()
true
}

ViewerCommand.CHANGE_WHITEBOARD_PEN_COLOR -> {
changeWhiteboardPenColor()
true
}
ViewerCommand.CHANGE_WHITEBOARD_PEN_COLOR -> {
changeWhiteboardPenColor()
true
}

ViewerCommand.SHOW_HINT -> {
loadUrlInViewer("javascript: showHint();")
true
}
ViewerCommand.SHOW_HINT -> {
loadUrlInViewer("javascript: showHint();")
true
}

ViewerCommand.SHOW_ALL_HINTS -> {
loadUrlInViewer("javascript: showAllHints();")
true
}
ViewerCommand.SHOW_ALL_HINTS -> {
loadUrlInViewer("javascript: showAllHints();")
true
}

else -> {
Timber.w("Unknown command requested: %s", which)
false
}
else -> {
Timber.w("Unknown command requested: %s", which)
false
}
}
}
Expand Down Expand Up @@ -2698,9 +2665,6 @@ abstract class AbstractFlashcardViewer :
val isDisplayingAnswer
get() = displayAnswer

open val isControlBlocked: Boolean
get() = controlBlocked !== ControlBlock.UNBLOCKED

internal fun showTagsDialog() {
val tags = ArrayList(getColUnsafe.tags.all())
val selTags = ArrayList(currentCard!!.note().tags)
Expand Down
22 changes: 6 additions & 16 deletions AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ open class Reviewer :
// Preferences from the collection
private var mShowRemainingCardCount = false
private var stopTimerOnAnswer = false
private val mActionButtons = ActionButtons(this)
private val mActionButtons = ActionButtons()
private lateinit var mToolbar: Toolbar

@VisibleForTesting
Expand Down Expand Up @@ -549,13 +549,6 @@ open class Reviewer :
super.unblockControls()
}

public override fun blockControls(quick: Boolean) {
if (prefWhiteboard && whiteboard != null) {
whiteboard!!.isEnabled = false
}
super.blockControls(quick)
}

override fun closeReviewer(result: Int) {
// Stop the mic recording if still pending
audioView?.notifyStopRecord()
Expand Down Expand Up @@ -695,7 +688,7 @@ open class Reviewer :
menuInflater.inflate(R.menu.reviewer, menu)
displayIcons(menu)
mActionButtons.setCustomButtonsStatus(menu)
var alpha = if (super.controlBlocked !== ReviewerUi.ControlBlock.SLOW) Themes.ALPHA_ICON_ENABLED_LIGHT else Themes.ALPHA_ICON_DISABLED_LIGHT
var alpha = Themes.ALPHA_ICON_ENABLED_LIGHT
val markCardIcon = menu.findItem(R.id.action_mark_card)
if (currentCard != null && isMarked(currentCard!!.note())) {
markCardIcon.setTitle(R.string.menu_unmark_note).setIcon(R.drawable.ic_star_white)
Expand Down Expand Up @@ -732,7 +725,7 @@ open class Reviewer :
undoIconId = R.drawable.ic_undo_white
undoEnabled = colIsOpenUnsafe() && getColUnsafe.undoAvailable()
}
val alphaUndo = if (undoEnabled && super.controlBlocked !== ReviewerUi.ControlBlock.SLOW) Themes.ALPHA_ICON_ENABLED_LIGHT else Themes.ALPHA_ICON_DISABLED_LIGHT
val alphaUndo = Themes.ALPHA_ICON_ENABLED_LIGHT
val undoIcon = menu.findItem(R.id.action_undo)
undoIcon.setIcon(undoIconId)
undoIcon.setEnabled(undoEnabled).iconAlpha = alphaUndo
Expand Down Expand Up @@ -846,7 +839,7 @@ open class Reviewer :
buryIcon.setIcon(R.drawable.ic_flip_to_back_white)
buryIcon.setTitle(R.string.menu_bury_card)
}
alpha = if (super.controlBlocked !== ReviewerUi.ControlBlock.SLOW) Themes.ALPHA_ICON_ENABLED_LIGHT else Themes.ALPHA_ICON_DISABLED_LIGHT
alpha = Themes.ALPHA_ICON_ENABLED_LIGHT
buryIcon.iconAlpha = alpha
suspendIcon.iconAlpha = alpha
MenuItemCompat.setActionProvider(menu.findItem(R.id.action_schedule), ScheduleProvider(this))
Expand Down Expand Up @@ -1162,9 +1155,6 @@ open class Reviewer :
}

override fun executeCommand(which: ViewerCommand, fromGesture: Gesture?): Boolean {
if (isControlBlocked && which !== ViewerCommand.EXIT) {
return false
}
when (which) {
ViewerCommand.TOGGLE_FLAG_RED -> {
toggleFlag(Flag.RED)
Expand Down Expand Up @@ -1425,7 +1415,7 @@ open class Reviewer :
*/
@KotlinCleanup("mCurrentCard handling")
private fun suspendNoteAvailable(): Boolean {
return if (currentCard == null || isControlBlocked) {
return if (currentCard == null) {
false
} else {
getColUnsafe.db.queryScalar(
Expand All @@ -1439,7 +1429,7 @@ open class Reviewer :

@KotlinCleanup("mCurrentCard handling")
private fun buryNoteAvailable(): Boolean {
return if (currentCard == null || isControlBlocked) {
return if (currentCard == null) {
false
} else {
getColUnsafe.db.queryScalar(
Expand Down
Loading
Loading