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..bb9dbf8efa46 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( @@ -75,6 +76,7 @@ sealed interface Binding { val modifierKeys: ModifierKeys } + @Suppress("EqualsOrHashCode") data class KeyCode( val keycode: Int, override val modifierKeys: ModifierKeys = ModifierKeys.none(), @@ -101,8 +103,12 @@ sealed interface Binding { append(modifierKeys.toString()) append(keycode) } + + // don't include the modifierKeys + override fun hashCode(): Int = Objects.hash(keycode) } + @Suppress("EqualsOrHashCode") data class UnicodeCharacter( val unicodeCharacter: Char, override val modifierKeys: ModifierKeys = AppDefinedModifierKeys.allowShift(), @@ -121,6 +127,9 @@ sealed interface Binding { append(modifierKeys.toString()) append(unicodeCharacter) } + + // don't include the modifierKeys + override fun hashCode(): Int = Objects.hash(unicodeCharacter) } data object UnknownBinding : Binding { @@ -168,7 +177,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 +188,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)