Skip to content

Commit

Permalink
show warning inside control dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
RobozinhoD committed Mar 1, 2024
1 parent fc3f3bf commit 3095584
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.ichi2.anki.dialogs

import android.content.Context
import android.widget.TextView
import androidx.core.view.isVisible
import com.ichi2.anki.cardviewer.GestureListener
import com.ichi2.ui.GesturePicker

Expand All @@ -32,3 +34,17 @@ object GestureSelectionDialogUtils {
setGestureChangedListener(listener)
}
}

interface WarningDisplay {
val warningTextView: TextView

fun setWarning(text: CharSequence) {
warningTextView.isVisible = true
warningTextView.text = text
}

fun clearWarning() {
warningTextView.isVisible = false
warningTextView.text = ""
}
}
25 changes: 14 additions & 11 deletions AnkiDroid/src/main/java/com/ichi2/preferences/ControlPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.ichi2.anki.dialogs.CardSideSelectionDialog
import com.ichi2.anki.dialogs.GestureSelectionDialogUtils
import com.ichi2.anki.dialogs.GestureSelectionDialogUtils.onGestureChanged
import com.ichi2.anki.dialogs.KeySelectionDialogUtils
import com.ichi2.anki.dialogs.WarningDisplay
import com.ichi2.anki.preferences.sharedPrefs
import com.ichi2.anki.reviewer.CardSide
import com.ichi2.anki.reviewer.MappableBinding
Expand Down Expand Up @@ -127,12 +128,7 @@ class ControlPreference : ListPreference {
customView(view = gesturePicker)

gesturePicker.onGestureChanged { gesture ->
showToastIfBindingIsUsed(
fromGesture(
gesture,
screenBuilder
)
)
warnIfBindingIsUsed(fromGesture(gesture, screenBuilder), gesturePicker)
}
}
}
Expand All @@ -145,12 +141,11 @@ class ControlPreference : ListPreference {

// When the user presses a key
keyPicker.setBindingChangedListener { binding ->
showToastIfBindingIsUsed(
MappableBinding(
binding,
screenBuilder(CardSide.BOTH)
)
val mappableBinding = MappableBinding(
binding,
screenBuilder(CardSide.BOTH)
)
warnIfBindingIsUsed(mappableBinding, keyPicker)
}

positiveButton(R.string.dialog_ok) {
Expand Down Expand Up @@ -216,6 +211,14 @@ class ControlPreference : ListPreference {
return getCommandWithBindingExceptThis(binding) != null
}

private fun warnIfBindingIsUsed(binding: MappableBinding, warningDisplay: WarningDisplay) {
getCommandWithBindingExceptThis(binding)?.let {
val name = context.getString(it.resourceId)
val warning = context.getString(R.string.bindings_already_bound, name)
warningDisplay.setWarning(warning)
} ?: warningDisplay.clearWarning()
}

/** Displays a warning to the user if the provided binding couldn't be used */
private fun showToastIfBindingIsUsed(binding: MappableBinding) {
val bindingCommand = getCommandWithBindingExceptThis(binding)
Expand Down
9 changes: 7 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/ui/GesturePicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.ichi2.anki.R
import com.ichi2.anki.cardviewer.Gesture
import com.ichi2.anki.cardviewer.GestureListener
import com.ichi2.anki.dialogs.WarningDisplay
import com.ichi2.utils.UiUtil.setSelectedValue
import timber.log.Timber

Expand All @@ -38,10 +40,12 @@ import timber.log.Timber
*/
// This class exists as elements resized when adding in the spinner to GestureDisplay.kt
class GesturePicker(ctx: Context, attributeSet: AttributeSet? = null, defStyleAttr: Int = 0) :
ConstraintLayout(ctx, attributeSet, defStyleAttr) {
ConstraintLayout(ctx, attributeSet, defStyleAttr),
WarningDisplay {

private val gestureSpinner: Spinner
private val gestureDisplay: GestureDisplay
override val warningTextView: TextView

private var onGestureListener: GestureListener? = null

Expand All @@ -50,6 +54,7 @@ class GesturePicker(ctx: Context, attributeSet: AttributeSet? = null, defStyleAt
inflater.inflate(R.layout.gesture_picker, this)
gestureDisplay = findViewById(R.id.gestureDisplay)
gestureSpinner = findViewById(R.id.spinner_gesture)
warningTextView = findViewById(R.id.warning)
gestureDisplay.setGestureChangedListener(this::onGesture)
gestureSpinner.adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, allGestures())
gestureSpinner.onItemSelectedListener = InnerSpinner()
Expand Down Expand Up @@ -79,7 +84,7 @@ class GesturePicker(ctx: Context, attributeSet: AttributeSet? = null, defStyleAt
onGestureListener = listener
}

fun allGestures(): List<GestureWrapper> {
private fun allGestures(): List<GestureWrapper> {
return (listOf(null) + availableGestures()).map(this::GestureWrapper).toList()
}

Expand Down
5 changes: 4 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/ui/KeyPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.ichi2.anki.R
import com.ichi2.anki.dialogs.WarningDisplay
import com.ichi2.anki.reviewer.Binding
import timber.log.Timber

Expand All @@ -31,9 +32,11 @@ typealias KeyCode = Int
* Square dialog which allows a user to select a [Binding] for a key press
* This does not yet support bluetooth headsets.
*/
class KeyPicker(val rootLayout: View) {
class KeyPicker(val rootLayout: View) : WarningDisplay {
private val textView: TextView = rootLayout.findViewById(R.id.key_picker_selected_key)

override val warningTextView: TextView = rootLayout.findViewById(R.id.warning)

private val context: Context get() = rootLayout.context

private var text: String
Expand Down
22 changes: 20 additions & 2 deletions AnkiDroid/src/main/res/layout/dialog_key_picker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<com.ichi2.ui.FixedTextView
android:id="@+id/key_picker_selected_key"
Expand All @@ -30,9 +31,26 @@
android:text="@string/key_picker_default_press_key"
android:focusable="true"
android:focusableInTouchMode="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@id/warning"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/warning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?attr/colorError"
tools:text="Already used in X"
android:layout_margin="6dp"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/key_picker_selected_key"
android:drawableStart="@drawable/ic_warning"
android:drawableTint="?attr/colorError"
android:drawablePadding="4dp"
tools:visibility="visible"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
20 changes: 19 additions & 1 deletion AnkiDroid/src/main/res/layout/gesture_picker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@
android:spinnerMode="dropdown"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
app:layout_constraintBottom_toTopOf="@id/warning"/>

<TextView
android:id="@+id/warning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?attr/colorError"
tools:text="Already used in X"
android:layout_margin="6dp"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/spinner_gesture"
android:drawableStart="@drawable/ic_warning"
android:drawableTint="?attr/colorError"
android:drawablePadding="4dp"
tools:visibility="visible"
/>


</androidx.constraintlayout.widget.ConstraintLayout>
6 changes: 5 additions & 1 deletion AnkiDroid/src/test/java/com/ichi2/ui/KeyPickerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import com.ichi2.anki.RobolectricTest
import com.ichi2.anki.dialogs.KeySelectionDialogUtils
import com.ichi2.testutils.KeyEventUtils
import com.ichi2.themes.Theme
import org.hamcrest.CoreMatchers.*
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test
Expand All @@ -28,7 +29,10 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class KeyPickerTest : RobolectricTest() {

private var keyPicker: KeyPicker = KeyPicker.inflate(targetContext)
private var keyPicker: KeyPicker = run {
targetContext.setTheme(Theme.LIGHT.resId)
KeyPicker.inflate(targetContext)
}

@Test
fun test_normal_binding() {
Expand Down

0 comments on commit 3095584

Please sign in to comment.