@@ -25,7 +25,6 @@ import android.view.MotionEvent
2525import android.view.View
2626import android.widget.ImageView
2727import androidx.constraintlayout.widget.ConstraintLayout
28- import com.ichi2.anki.R
2928import com.ichi2.anki.cardviewer.Gesture
3029import com.ichi2.anki.cardviewer.Gesture.SWIPE_DOWN
3130import com.ichi2.anki.cardviewer.Gesture.SWIPE_LEFT
@@ -42,6 +41,7 @@ import com.ichi2.anki.cardviewer.Gesture.TAP_TOP_LEFT
4241import com.ichi2.anki.cardviewer.Gesture.TAP_TOP_RIGHT
4342import com.ichi2.anki.cardviewer.GestureListener
4443import com.ichi2.anki.cardviewer.TapGestureMode
44+ import com.ichi2.anki.databinding.GestureDisplayBinding
4545import com.ichi2.anki.settings.Prefs
4646import 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
0 commit comments