Skip to content

Commit 8a4c286

Browse files
david-allisonlukstbit
authored andcommitted
chore: convert GestureDisplay to ViewBinding
Issue 11116
1 parent 9f206dc commit 8a4c286

2 files changed

Lines changed: 20 additions & 32 deletions

File tree

AnkiDroid/src/main/java/com/ichi2/ui/GestureDisplay.kt

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import android.view.MotionEvent
2525
import android.view.View
2626
import android.widget.ImageView
2727
import androidx.constraintlayout.widget.ConstraintLayout
28-
import com.ichi2.anki.R
2928
import com.ichi2.anki.cardviewer.Gesture
3029
import com.ichi2.anki.cardviewer.Gesture.SWIPE_DOWN
3130
import com.ichi2.anki.cardviewer.Gesture.SWIPE_LEFT
@@ -42,6 +41,7 @@ import com.ichi2.anki.cardviewer.Gesture.TAP_TOP_LEFT
4241
import com.ichi2.anki.cardviewer.Gesture.TAP_TOP_RIGHT
4342
import com.ichi2.anki.cardviewer.GestureListener
4443
import com.ichi2.anki.cardviewer.TapGestureMode
44+
import com.ichi2.anki.databinding.GestureDisplayBinding
4545
import com.ichi2.anki.settings.Prefs
4646
import timber.log.Timber
4747

@@ -59,6 +59,8 @@ class GestureDisplay
5959
attributeSet: AttributeSet? = null,
6060
defStyleAttr: Int = 0,
6161
) : ConstraintLayout(context, attributeSet, defStyleAttr) {
62+
private val binding = GestureDisplayBinding.inflate(LayoutInflater.from(context), this)
63+
6264
/** Converts a touch event into a call to [setGesture] */
6365
private val detector: GestureDetector
6466

@@ -71,19 +73,13 @@ class GestureDisplay
7173
/** The last recorded gesture (null if no gestures provided, or if explicitly set) */
7274
private var gesture: Gesture? = null
7375

74-
private var swipeView: ImageView
75-
7676
init {
77-
val inflater = LayoutInflater.from(context)
78-
inflater.inflate(R.layout.gesture_display, this)
79-
8077
val listener = OnGestureListener.createInstance(this, this::setGesture)
8178
detector = GestureDetector(context, listener)
8279
tapGestureMode = listener.getTapGestureMode()
8380
setTapGestureMode(tapGestureMode)
84-
swipeView = findViewById(R.id.swipe_select)
8581
// if we don't call mutate, state is persisted outside the dialog when we call .setImageLevel
86-
swipeView.drawable?.mutate()
82+
binding.swipeView.drawable?.mutate()
8783
}
8884

8985
/** Lists all selectable gestures from this view (excludes null) */
@@ -138,7 +134,7 @@ class GestureDisplay
138134
SWIPE_RIGHT -> 4
139135
else -> 0
140136
}
141-
swipeView.setImageLevel(level)
137+
binding.swipeView.setImageLevel(level)
142138
}
143139

144140
/**
@@ -150,31 +146,25 @@ class GestureDisplay
150146
) {
151147
// revert the old change, and implement the new change
152148
// does nothing if neither are taps
153-
tapGestureToView(oldGesture)?.isSelected = false
154-
tapGestureToView(gesture)?.isSelected = true
149+
binding.tapGestureToView(oldGesture)?.isSelected = false
150+
binding.tapGestureToView(gesture)?.isSelected = true
155151
}
156152

157153
/**
158154
* Maps from a [Gesture] to an [ImageView].
159155
* @return The associated [ImageView], or null if input is null, or isn't a tap gesture
160156
*/
161-
private fun tapGestureToView(gesture: Gesture?): ImageView? {
162-
val viewId = tapGestureToViewId(gesture) ?: return null
163-
return findViewById(viewId)
164-
}
165-
166-
/** Maps from a tap gesture to its view Id, or null if the gesture isn't a tap */
167-
private fun tapGestureToViewId(gesture: Gesture?): Int? =
157+
private fun GestureDisplayBinding.tapGestureToView(gesture: Gesture?): ImageView? =
168158
when (gesture) {
169-
TAP_TOP_LEFT -> R.id.top_left
170-
TAP_TOP -> R.id.top_center
171-
TAP_TOP_RIGHT -> R.id.top_right
172-
TAP_LEFT -> R.id.left
173-
TAP_CENTER -> R.id.center
174-
TAP_RIGHT -> R.id.right
175-
TAP_BOTTOM_LEFT -> R.id.bottom_left
176-
TAP_BOTTOM -> R.id.bottom_center
177-
TAP_BOTTOM_RIGHT -> R.id.bottom_right
159+
TAP_TOP_LEFT -> topLeft
160+
TAP_TOP -> topCenter
161+
TAP_TOP_RIGHT -> topRight
162+
TAP_LEFT -> left
163+
TAP_CENTER -> center
164+
TAP_RIGHT -> right
165+
TAP_BOTTOM_LEFT -> bottomLeft
166+
TAP_BOTTOM -> bottomCenter
167+
TAP_BOTTOM_RIGHT -> bottomRight
178168
else -> null
179169
}
180170

@@ -188,10 +178,8 @@ class GestureDisplay
188178
TapGestureMode.NINE_POINT -> View.VISIBLE
189179
}
190180

191-
NINE_POINT_TAP_GESTURES.forEach {
192-
val id = tapGestureToViewId(it) ?: return
193-
val view = findViewById<ImageView>(id)
194-
view.visibility = ninePointVisibility
181+
NINE_POINT_TAP_GESTURES.forEach { gesture ->
182+
binding.tapGestureToView(gesture)?.visibility = ninePointVisibility
195183
}
196184
}
197185

AnkiDroid/src/main/res/layout/gesture_display.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
app:layout_constraintTop_toTopOf="parent" />
109109

110110
<ImageView
111-
android:id="@+id/swipe_select"
111+
android:id="@+id/swipe_view"
112112
android:layout_width="0dp"
113113
android:layout_height="0dp"
114114
app:layout_constraintBottom_toBottomOf="parent"

0 commit comments

Comments
 (0)