diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/GestureProcessor.kt b/AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/GestureProcessor.kt index 264901f63144..03a272a0b86d 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/GestureProcessor.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/GestureProcessor.kt @@ -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?, @@ -58,7 +57,7 @@ class GestureProcessor( val associatedCommands = HashMap() 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 } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/ViewerCommand.kt b/AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/ViewerCommand.kt index 5dc336b71dac..095deb9284b4 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/ViewerCommand.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/ViewerCommand.kt @@ -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 @@ -137,7 +136,7 @@ enum class ViewerCommand( binding: MappableBinding, performAdd: (MutableList, MappableBinding) -> Boolean, ) { - val bindings: MutableList = fromPreference(preferences, this) + val bindings: MutableList = this.getBindings(preferences).toMutableList() performAdd(bindings, binding) val newValue: String = bindings.toPreferenceString() preferences.edit { putString(preferenceKey, newValue) } 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 dfe0ae1bff82..863e9c4a114b 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MappableBinding.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MappableBinding.kt @@ -148,20 +148,11 @@ sealed class MappableBinding( } } - @CheckResult - fun fromPreference( - prefs: SharedPreferences, - command: ViewerCommand, - ): MutableList { - val value = prefs.getString(command.preferenceKey, null) ?: return command.defaultValue.toMutableList() - return fromPreferenceString(value) - } - @CheckResult fun allMappings(prefs: SharedPreferences): MutableList>> = ViewerCommand.entries .map { - Pair(it, fromPreference(prefs, it)) + Pair(it, it.getBindings(prefs).toMutableList()) }.toMutableList() } } @@ -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()) { @@ -223,11 +217,8 @@ class ReviewerBinding( fun fromPreferenceString(prefString: String?): List { 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 diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MotionEventHandler.kt b/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MotionEventHandler.kt index 6d31e7ac5439..ba0af455e4bd 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MotionEventHandler.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/MotionEventHandler.kt @@ -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 @@ -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) { diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/PeripheralKeymap.kt b/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/PeripheralKeymap.kt index 5330b06bedf0..f3564a98bc2b 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/PeripheralKeymap.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/PeripheralKeymap.kt @@ -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. */ @@ -50,9 +49,7 @@ class PeripheralKeymap( command: ViewerCommand, preferences: SharedPreferences, ) { - val bindings = - fromPreference(preferences, command) - .filterIsInstance() + val bindings = command.getBindings(preferences) for (b in bindings) { if (!b.isKey) { continue diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerNoParamTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerNoParamTest.kt index 9b1e963bafd3..bf5076c6a981 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerNoParamTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerNoParamTest.kt @@ -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 @@ -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 = diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/servicemodel/UpgradeGesturesToControlsTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/servicemodel/UpgradeGesturesToControlsTest.kt index c0143e793589..5270c5f85dbc 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/servicemodel/UpgradeGesturesToControlsTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/servicemodel/UpgradeGesturesToControlsTest.kt @@ -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() @@ -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() @@ -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() @@ -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 @@ -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))