Skip to content

Commit

Permalink
refactor: extract Binding equality to original classes
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Dec 26, 2024
1 parent 2a909fe commit e7b7ae8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 40 deletions.
15 changes: 14 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 Down Expand Up @@ -75,6 +76,7 @@ sealed interface Binding {
val modifierKeys: ModifierKeys
}

@Suppress("EqualsOrHashCode")
data class KeyCode(
val keycode: Int,
override val modifierKeys: ModifierKeys = ModifierKeys.none(),
Expand All @@ -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(),
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)

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 e7b7ae8

Please sign in to comment.