From 0092e2f2d2ad01226004a059d0adb9ec0f89927d Mon Sep 17 00:00:00 2001 From: Brayan Oliveira <69634269+brayandso@users.noreply.github.com> Date: Sat, 23 Nov 2024 09:48:24 -0300 Subject: [PATCH] refactor: cleanups refactor: toSentenceCase 1. Replace Activity with Context (activities are also Context) 2. Add method that accepts `Resources` 3. Remove repeated code fix: check if HeaderFragment is attached parentFragmentManager may throw if not refactor: inline ViewerCommand::allDefaultBindings Only used in tests refactor: use Kotlin notation instead of java BiFunction refactor: inline removeBinding only used in a test refactor: inline keyMap IDE suggestion --- .../ichi2/anki/cardviewer/ViewerCommand.kt | 52 ++++++------------- .../ichi2/anki/preferences/HeaderFragment.kt | 1 + .../ichi2/anki/reviewer/PeripheralKeymap.kt | 6 +-- .../ui/internationalization/SentenceCase.kt | 13 ++--- .../com/ichi2/anki/ReviewerNoParamTest.kt | 8 ++- .../anki/reviewer/MappableBindingTest.kt | 9 +++- 6 files changed, 36 insertions(+), 53 deletions(-) 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 e69f7fa764e0..08f9c73ae4d8 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/ViewerCommand.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/ViewerCommand.kt @@ -29,9 +29,6 @@ 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.MappableBinding.Screen -import java.util.Arrays -import java.util.function.BiFunction -import java.util.stream.Collectors /** Abstraction: Discuss moving many of these to 'Reviewer' */ enum class ViewerCommand( @@ -91,13 +88,6 @@ enum class ViewerCommand( ; companion object { - val allDefaultBindings: List - get() = - Arrays - .stream(entries.toTypedArray()) - .flatMap { x: ViewerCommand -> x.defaultValue.stream() } - .collect(Collectors.toList()) - fun fromPreferenceKey(key: String) = entries.first { it.preferenceKey == key } } @@ -108,13 +98,12 @@ enum class ViewerCommand( preferences: SharedPreferences, binding: MappableBinding, ) { - val addAtStart = - BiFunction { collection: MutableList, element: MappableBinding -> - // reorder the elements, moving the added binding to the first position - collection.remove(element) - collection.add(0, element) - true - } + val addAtStart: (MutableList, MappableBinding) -> Boolean = { collection, element -> + // reorder the elements, moving the added binding to the first position + collection.remove(element) + collection.add(0, element) + true + } addBindingInternal(preferences, binding, addAtStart) } @@ -122,40 +111,29 @@ enum class ViewerCommand( preferences: SharedPreferences, binding: MappableBinding, ) { - val addAtEnd = - BiFunction { collection: MutableList, element: MappableBinding -> - // do not reorder the elements - if (collection.contains(element)) { - return@BiFunction false - } + val addAtEnd: (MutableList, MappableBinding) -> Boolean = { collection, element -> + // do not reorder the elements + if (collection.contains(element)) { + false + } else { collection.add(element) - return@BiFunction true + true } + } addBindingInternal(preferences, binding, addAtEnd) } private fun addBindingInternal( preferences: SharedPreferences, binding: MappableBinding, - performAdd: BiFunction, MappableBinding, Boolean>, + performAdd: (MutableList, MappableBinding) -> Boolean, ) { val bindings: MutableList = fromPreference(preferences, this) - performAdd.apply(bindings, binding) + performAdd(bindings, binding) val newValue: String = bindings.toPreferenceString() preferences.edit { putString(preferenceKey, newValue) } } - fun removeBinding( - prefs: SharedPreferences, - binding: MappableBinding, - ) { - val bindings: MutableList = MappableBinding.fromPreferenceString(preferenceKey) - bindings.remove(binding) - prefs.edit { - putString(preferenceKey, bindings.toPreferenceString()) - } - } - // If we use the serialised format, then this adds additional coupling to the properties. val defaultValue: List get() { diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/preferences/HeaderFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/preferences/HeaderFragment.kt index d68b4135f2a7..fdb8d34f8826 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/preferences/HeaderFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/preferences/HeaderFragment.kt @@ -73,6 +73,7 @@ class HeaderFragment : } parentFragmentManager.addOnBackStackChangedListener { + if (!isAdded) return@addOnBackStackChangedListener val fragment = parentFragmentManager.findFragmentById(R.id.settings_container) ?: return@addOnBackStackChangedListener 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 8bd1c916e33f..6b0081c26361 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/PeripheralKeymap.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/PeripheralKeymap.kt @@ -32,7 +32,7 @@ class PeripheralKeymap( reviewerUi: ReviewerUi, commandProcessor: ViewerCommand.CommandProcessor, ) { - private val keyMap: KeyMap + private val keyMap: KeyMap = KeyMap(commandProcessor, reviewerUi) { Screen.Reviewer(it) } private var hasSetup = false fun setup() { @@ -110,8 +110,4 @@ class PeripheralKeymap( operator fun get(key: MappableBinding): ViewerCommand? = bindingMap[key] } - - init { - keyMap = KeyMap(commandProcessor, reviewerUi) { Screen.Reviewer(it) } - } } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/ui/internationalization/SentenceCase.kt b/AnkiDroid/src/main/java/com/ichi2/anki/ui/internationalization/SentenceCase.kt index 468061548307..8b922f5b36d4 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/ui/internationalization/SentenceCase.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/ui/internationalization/SentenceCase.kt @@ -16,7 +16,7 @@ package com.ichi2.anki.ui.internationalization -import android.app.Activity +import android.content.Context import androidx.annotation.StringRes import androidx.fragment.app.Fragment @@ -30,10 +30,10 @@ import androidx.fragment.app.Fragment * ``` */ fun String.toSentenceCase( - activity: Activity, + context: Context, @StringRes resId: Int, ): String { - val resString = activity.getString(resId) + val resString = context.getString(resId) // lowercase both for the comparison: sentence case doesn't mean all words are lowercase if (this.lowercase() == resString.lowercase()) return resString return this @@ -42,9 +42,4 @@ fun String.toSentenceCase( fun String.toSentenceCase( fragment: Fragment, @StringRes resId: Int, -): String { - val resString = fragment.getString(resId) - // lowercase both for the comparison: sentence case doesn't mean all words are lowercase - if (this.lowercase() == resString.lowercase()) return resString - return this -} +): String = toSentenceCase(fragment.requireContext(), resId) diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerNoParamTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerNoParamTest.kt index 327be4b513d3..e5c1968a9182 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerNoParamTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerNoParamTest.kt @@ -32,6 +32,7 @@ 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.MappableBinding.Screen import com.ichi2.libanki.Consts import com.ichi2.libanki.DeckId @@ -299,7 +300,12 @@ class ReviewerNoParamTest : RobolectricTest() { for (mappableBinding in MappableBinding.fromPreference(prefs, command)) { val gestureBinding = mappableBinding.binding as? Binding.GestureInput? ?: continue if (gestureBinding.gesture in gestures) { - command.removeBinding(prefs, mappableBinding) + val bindings: MutableList = + MappableBinding.fromPreferenceString(command.preferenceKey) + bindings.remove(mappableBinding) + prefs.edit { + putString(command.preferenceKey, bindings.toPreferenceString()) + } } } } diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/reviewer/MappableBindingTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/reviewer/MappableBindingTest.kt index 1779de59fb82..9a1f0235919f 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/reviewer/MappableBindingTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/reviewer/MappableBindingTest.kt @@ -17,10 +17,13 @@ package com.ichi2.anki.reviewer import android.view.KeyEvent import com.ichi2.anki.cardviewer.ViewerCommand +import com.ichi2.anki.cardviewer.ViewerCommand.entries import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.hasItem import org.hamcrest.Matchers.not import org.junit.Test +import java.util.Arrays +import java.util.stream.Collectors class MappableBindingTest { @Test @@ -39,7 +42,11 @@ class MappableBindingTest { assertThat(allBindings, not(hasItem(keyCode(KeyEvent.KEYCODE_L)))) } - private fun getAllBindings() = ViewerCommand.allDefaultBindings + private fun getAllBindings() = + Arrays + .stream(entries.toTypedArray()) + .flatMap { x: ViewerCommand -> x.defaultValue.stream() } + .collect(Collectors.toList()) @Suppress("SameParameterValue") private fun keyCode(code: Int) = fromBinding(BindingTest.keyCode(code))