Skip to content

Commit

Permalink
removida fromPreference
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Dec 25, 2024
1 parent 83e4db9 commit 3165197
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.ichi2.anki.cardviewer
import android.content.SharedPreferences
import com.ichi2.anki.reviewer.Binding
import com.ichi2.anki.reviewer.GestureMapper
import com.ichi2.anki.reviewer.MappableBinding

class GestureProcessor(
private val processor: ViewerCommand.CommandProcessor?,
Expand Down Expand Up @@ -58,7 +57,7 @@ class GestureProcessor(

val associatedCommands = HashMap<Gesture, ViewerCommand>()
for (command in ViewerCommand.entries) {
for (mappableBinding in MappableBinding.fromPreference(preferences, command)) {
for (mappableBinding in command.getBindings(preferences)) {
if (mappableBinding.binding is Binding.GestureInput) {
associatedCommands[mappableBinding.binding.gesture] = command
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.ichi2.anki.reviewer.Binding.ModifierKeys.Companion.ctrl
import com.ichi2.anki.reviewer.Binding.ModifierKeys.Companion.shift
import com.ichi2.anki.reviewer.CardSide
import com.ichi2.anki.reviewer.MappableBinding
import com.ichi2.anki.reviewer.MappableBinding.Companion.fromPreference
import com.ichi2.anki.reviewer.MappableBinding.Companion.toPreferenceString
import com.ichi2.anki.reviewer.ReviewerBinding

Expand Down Expand Up @@ -137,7 +136,7 @@ enum class ViewerCommand(
binding: MappableBinding,
performAdd: (MutableList<MappableBinding>, MappableBinding) -> Boolean,
) {
val bindings: MutableList<MappableBinding> = fromPreference(preferences, this)
val bindings: MutableList<MappableBinding> = this.getBindings(preferences).toMutableList()
performAdd(bindings, binding)
val newValue: String = bindings.toPreferenceString()
preferences.edit { putString(preferenceKey, newValue) }
Expand Down
23 changes: 7 additions & 16 deletions AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MappableBinding.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,11 @@ sealed class MappableBinding(
}
}

@CheckResult
fun fromPreference(
prefs: SharedPreferences,
command: ViewerCommand,
): MutableList<MappableBinding> {
val value = prefs.getString(command.preferenceKey, null) ?: return command.defaultValue.toMutableList()
return fromPreferenceString(value)
}

@CheckResult
fun allMappings(prefs: SharedPreferences): MutableList<Pair<ViewerCommand, MutableList<MappableBinding>>> =
ViewerCommand.entries
.map {
Pair(it, fromPreference(prefs, it))
Pair(it, it.getBindings(prefs).toMutableList<MappableBinding>())
}.toMutableList()
}
}
Expand Down Expand Up @@ -210,7 +201,10 @@ class ReviewerBinding(

fun fromString(string: String): ReviewerBinding? {
if (string.isEmpty()) return null
val bindingString = string.substring(0, string.length - 1)
val bindingString =
StringBuilder(string)
.substring(0, string.length - 1)
.removePrefix(PREFIX)
val binding = Binding.fromString(bindingString)
val side =
when (string.last()) {
Expand All @@ -223,11 +217,8 @@ class ReviewerBinding(

fun fromPreferenceString(prefString: String?): List<ReviewerBinding> {
if (prefString.isNullOrEmpty()) return emptyList()
val strings = getPreferenceBindingStrings(prefString) // TODO
return strings.mapNotNull {
if (it.isEmpty()) return@mapNotNull null
fromString(it.substring(1))
}
val strings = getPreferenceBindingStrings(prefString)
return strings.mapNotNull { fromString(it) }
}

@CheckResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.ichi2.anki.AbstractFlashcardViewer
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.cardviewer.ViewerCommand
import com.ichi2.anki.preferences.sharedPrefs
import com.ichi2.anki.reviewer.MappableBinding.Companion.fromPreference
import com.ichi2.compat.CompatHelper
import timber.log.Timber

Expand Down Expand Up @@ -117,7 +116,7 @@ class MotionEventHandler(
val prefs = context.sharedPrefs()
val mappings =
ViewerCommand.entries.map {
Pair(it, fromPreference(prefs, it))
Pair(it, it.getBindings(prefs))
}
return sequence {
for ((command, bindings) in mappings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import com.ichi2.anki.cardviewer.ViewerCommand
import com.ichi2.anki.preferences.sharedPrefs
import com.ichi2.anki.reviewer.Binding.Companion.possibleKeyBindings
import com.ichi2.anki.reviewer.CardSide.Companion.fromAnswer
import com.ichi2.anki.reviewer.MappableBinding.Companion.fromPreference

/** Accepts peripheral input, mapping via various keybinding strategies,
* and converting them to commands for the Reviewer. */
Expand All @@ -50,9 +49,7 @@ class PeripheralKeymap(
command: ViewerCommand,
preferences: SharedPreferences,
) {
val bindings =
fromPreference(preferences, command)
.filterIsInstance<ReviewerBinding>()
val bindings = command.getBindings(preferences)
for (b in bindings) {
if (!b.isKey) {
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import com.ichi2.anki.preferences.sharedPrefs
import com.ichi2.anki.reviewer.Binding
import com.ichi2.anki.reviewer.FullScreenMode
import com.ichi2.anki.reviewer.FullScreenMode.Companion.setPreference
import com.ichi2.anki.reviewer.MappableBinding
import com.ichi2.anki.reviewer.MappableBinding.Companion.toPreferenceString
import com.ichi2.anki.reviewer.ReviewerBinding
import com.ichi2.libanki.Consts
Expand Down Expand Up @@ -297,7 +296,7 @@ class ReviewerNoParamTest : RobolectricTest() {
private fun disableGestures(vararg gestures: Gesture) {
val prefs = targetContext.sharedPrefs()
for (command in ViewerCommand.entries) {
for (mappableBinding in MappableBinding.fromPreference(prefs, command)) {
for (mappableBinding in command.getBindings(prefs)) {
val gestureBinding = mappableBinding.binding as? Binding.GestureInput? ?: continue
if (gestureBinding.gesture in gestures) {
val bindings: MutableList<ReviewerBinding> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class UpgradeGesturesToControlsTest(

assertThat(prefs.contains(testData.affectedPreferenceKey), equalTo(true))
assertThat(prefs.contains(testData.unaffectedPreferenceKey), equalTo(false))
assertThat("example command should have no defaults", MappableBinding.fromPreference(prefs, command), empty())
assertThat("example command should have no defaults", command.getBindings(prefs), empty())

upgradeAllGestures()

Expand All @@ -83,7 +83,7 @@ class UpgradeGesturesToControlsTest(
assertThat("legacy preference removed", prefs.contains(testData.affectedPreferenceKey), equalTo(false))
assertThat("new preference added", prefs.contains(command.preferenceKey), equalTo(true))

val fromPreference = MappableBinding.fromPreference(prefs, command)
val fromPreference = command.getBindings(prefs)
assertThat(fromPreference, hasSize(1))
val binding = fromPreference.first()

Expand All @@ -102,7 +102,7 @@ class UpgradeGesturesToControlsTest(
assertThat(prefs.contains(testData.affectedPreferenceKey), equalTo(true))
assertThat(prefs.contains(testData.unaffectedPreferenceKey), equalTo(false))
assertThat("new preference does not exist", prefs.contains(command.preferenceKey), equalTo(false))
val previousCommands = MappableBinding.fromPreference(prefs, command)
val previousCommands = command.getBindings(prefs)
assertThat("example command should have defaults", previousCommands, not(empty()))

upgradeAllGestures()
Expand All @@ -115,7 +115,7 @@ class UpgradeGesturesToControlsTest(
assertThat("legacy preference removed", prefs.contains(testData.affectedPreferenceKey), equalTo(false))
assertThat("new preference exists", prefs.contains(command.preferenceKey), equalTo(true))

val currentCommands = MappableBinding.fromPreference(prefs, command)
val currentCommands = command.getBindings(prefs)
assertThat("a binding was added to '${command.preferenceKey}'", currentCommands, hasSize(previousCommands.size + 1))

// ensure that the order was not changed - the last element is not included in the zip
Expand All @@ -142,7 +142,7 @@ class UpgradeGesturesToControlsTest(
assertThat(prefs.contains(testData.affectedPreferenceKey), equalTo(true))
assertThat(prefs.contains(testData.unaffectedPreferenceKey), equalTo(false))
assertThat("new preference exists", prefs.contains(command.preferenceKey), equalTo(true))
val previousCommands = MappableBinding.fromPreference(prefs, command)
val previousCommands = command.getBindings(prefs)
assertThat("example command should have defaults", previousCommands, hasSize(2))
assertThat(previousCommands.first(), equalTo(testData.binding))

Expand Down

0 comments on commit 3165197

Please sign in to comment.