diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/Binding.kt b/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/Binding.kt index 8a9e923cdb99..2228add1036d 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/Binding.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/Binding.kt @@ -23,6 +23,7 @@ import com.ichi2.anki.utils.ext.ifNotZero import com.ichi2.utils.StringUtil import com.ichi2.utils.lastIndexOfOrNull import timber.log.Timber +import java.util.Objects sealed interface Binding { data class GestureInput( @@ -35,6 +36,10 @@ sealed interface Binding { append(GESTURE_PREFIX) append(gesture) } + + override fun equals(other: Any?): Boolean = other is GestureInput && gesture == other.gesture + + override fun hashCode(): Int = Objects.hash(gesture) } /** @@ -59,6 +64,10 @@ sealed interface Binding { append(threshold) } + override fun equals(other: Any?): Boolean = other is AxisButtonBinding && axis == other.axis && threshold == other.threshold + + override fun hashCode(): Int = Objects.hash(axis.motionEventValue, threshold.toInt()) + companion object { fun from(preferenceString: String): AxisButtonBinding { val s = preferenceString.split(" ") @@ -101,6 +110,10 @@ sealed interface Binding { append(modifierKeys.toString()) append(keycode) } + + override fun equals(other: Any?): Boolean = other is KeyCode && keycode == other.keycode && modifierKeys == other.modifierKeys + + override fun hashCode(): Int = Objects.hash(keycode) } data class UnicodeCharacter( @@ -121,6 +134,11 @@ sealed interface Binding { append(modifierKeys.toString()) append(unicodeCharacter) } + + override fun equals(other: Any?): Boolean = + other is UnicodeCharacter && unicodeCharacter == other.unicodeCharacter && modifierKeys == other.modifierKeys + + override fun hashCode(): Int = Objects.hash(unicodeCharacter) } data object UnknownBinding : Binding { @@ -168,7 +186,7 @@ sealed interface Binding { if (shift) append("Shift+") } - fun semiStructuralEquals(keys: ModifierKeys): Boolean { + private fun semiStructuralEquals(keys: ModifierKeys): Boolean { if (this.alt != keys.alt || this.ctrl != keys.ctrl) { return false } @@ -179,6 +197,10 @@ sealed interface Binding { ) } + override fun equals(other: Any?): Boolean = other is ModifierKeys && semiStructuralEquals(other) + + override fun hashCode(): Int = Objects.hash(ctrl, alt, shift, shiftMatches(true), shiftMatches(false)) + companion object { fun none(): ModifierKeys = ModifierKeys(shift = false, ctrl = false, alt = false) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MappableBinding.kt b/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MappableBinding.kt index 51fec49cbfa0..c2bd88e0c796 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MappableBinding.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MappableBinding.kt @@ -22,11 +22,8 @@ import androidx.annotation.CheckResult import com.ichi2.anki.R import com.ichi2.anki.cardviewer.Gesture import com.ichi2.anki.cardviewer.ViewerCommand -import com.ichi2.anki.reviewer.Binding.AxisButtonBinding import com.ichi2.anki.reviewer.Binding.GestureInput import com.ichi2.anki.reviewer.Binding.KeyBinding -import com.ichi2.anki.reviewer.Binding.KeyCode -import com.ichi2.anki.reviewer.Binding.UnicodeCharacter import com.ichi2.utils.hash import timber.log.Timber import java.util.Objects @@ -47,48 +44,14 @@ class MappableBinding( if (other == null) return false val otherBinding = (other as MappableBinding).binding - val bindingEquals = - when { - binding is KeyCode && otherBinding is KeyCode -> binding.keycode == otherBinding.keycode && modifierEquals(otherBinding) - binding is UnicodeCharacter && otherBinding is UnicodeCharacter -> { - binding.unicodeCharacter == otherBinding.unicodeCharacter && - modifierEquals(otherBinding) - } - binding is GestureInput && otherBinding is GestureInput -> binding.gesture == otherBinding.gesture - binding is AxisButtonBinding && otherBinding is AxisButtonBinding -> { - binding.axis == otherBinding.axis && binding.threshold == otherBinding.threshold - } - else -> false - } - if (!bindingEquals) { + if (binding != otherBinding) { return false } return screen.screenEquals(other.screen) } - override fun hashCode(): Int { - // don't include the modifierKeys or mSide - val bindingHash = - when (binding) { - is KeyCode -> binding.keycode - is UnicodeCharacter -> binding.unicodeCharacter - is GestureInput -> binding.gesture - is AxisButtonBinding -> hash(binding.axis.motionEventValue, binding.threshold.toInt()) - else -> 0 - } - return Objects.hash(bindingHash, screen.prefix) - } - - private fun modifierEquals(otherBinding: KeyBinding): Boolean { - // equals allowing subclasses - val keys = otherBinding.modifierKeys - val thisKeys = (this.binding as KeyBinding).modifierKeys - if (thisKeys === keys) return true - return thisKeys.semiStructuralEquals(keys) - - // allow subclasses to work - a subclass which overrides shiftMatches will return true on one of the tests - } + override fun hashCode(): Int = Objects.hash(binding, screen.prefix) fun toDisplayString(context: Context): String = screen.toDisplayString(context, binding)