Skip to content

Commit

Permalink
refactor: extract Binding equality to original classes
Browse files Browse the repository at this point in the history
so it's possible to compare bindings without a MappableBinding
  • Loading branch information
BrayanDSO committed Dec 26, 2024
1 parent 2a909fe commit 8dd9330
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 40 deletions.
24 changes: 23 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/reviewer/Binding.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
}

/**
Expand All @@ -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(" ")
Expand Down Expand Up @@ -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(
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)

Expand Down
41 changes: 2 additions & 39 deletions AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MappableBinding.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down

0 comments on commit 8dd9330

Please sign in to comment.