Skip to content

Commit cecd967

Browse files
david-allisonlukstbit
authored andcommitted
chore: convert AxisPicker to ViewBinding
Issue 11116
1 parent 367917a commit cecd967

2 files changed

Lines changed: 18 additions & 28 deletions

File tree

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

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ import android.content.Context
2020
import android.view.LayoutInflater
2121
import android.view.MotionEvent
2222
import android.view.View
23-
import android.widget.LinearLayout
24-
import android.widget.TextView
25-
import androidx.constraintlayout.widget.ConstraintLayout
26-
import com.ichi2.anki.R
23+
import com.ichi2.anki.databinding.DialogAxisPickerBinding
2724
import com.ichi2.anki.reviewer.Axis
2825
import com.ichi2.anki.reviewer.Binding
2926
import timber.log.Timber
@@ -37,20 +34,9 @@ import timber.log.Timber
3734
* @see AxisSelector
3835
*/
3936
class AxisPicker(
40-
val rootLayout: ConstraintLayout,
37+
private val binding: DialogAxisPickerBinding,
4138
) {
42-
// DDisplays a message asking a user to provide input to the screen
43-
// We use a TextView to listen due to issues with handling AXIS_BRAKE and AXIS_GAS
44-
// When listening to 'rootLayout', these axes are ONLY detected after another joystick is moved
45-
private val textView: TextView = rootLayout.findViewById(R.id.axis_picker_selected_axis)
46-
47-
/** Contains [availableAxes], initially invisible */
48-
private val availableAxesContainer: View = rootLayout.findViewById(R.id.axis_picker_scrollview)
49-
50-
/** Where [AxisSelector] are added dynamically if motion is detected*/
51-
private val availableAxes: LinearLayout = rootLayout.findViewById(R.id.axis_picker_available_axes)
52-
53-
private val context: Context get() = rootLayout.context
39+
val rootLayout = binding.root
5440

5541
/** Maps from an [Axis] to the [AxisSelector] displaying + allowing selection of it */
5642
private val axisMap = mutableMapOf<Axis, AxisSelector>()
@@ -62,8 +48,10 @@ class AxisPicker(
6248
}
6349

6450
init {
65-
textView.requestFocus()
66-
textView.setOnGenericMotionListener { _, event -> handleMotionEvent(event) }
51+
// We use a TextView to listen due to issues with handling AXIS_BRAKE and AXIS_GAS
52+
// When listening to 'rootLayout', these axes are ONLY detected after another joystick is moved
53+
binding.selectedAxisTextView.requestFocus()
54+
binding.selectedAxisTextView.setOnGenericMotionListener { _, event -> handleMotionEvent(event) }
6755
}
6856

6957
@Suppress("SameReturnValue")
@@ -91,28 +79,28 @@ class AxisPicker(
9179

9280
// when adding the first control, we want to make the
9381
// available axes visible, so the user can see their current values
94-
availableAxesContainer.visibility = View.VISIBLE
82+
binding.availableAxesContainer.visibility = View.VISIBLE
9583
// we also want to hide the TextView, but we can't make it invisible as it's
9684
// providing our input events. Blanking the text has the same effect
97-
textView.text = ""
85+
binding.selectedAxisTextView.text = ""
9886

9987
// setup & return the control
100-
return AxisSelector(context).also { view ->
88+
return AxisSelector(binding.root.context).also { view ->
10189
view.axis = axis
10290
view.setOnExtremitySelectedListener { binding ->
10391
Timber.d("selected binding %s", binding)
10492
onBindingChangedListener?.invoke(binding)
10593
}
10694

10795
axisMap[axis] = view
108-
availableAxes.addView(view)
96+
binding.availableAxes.addView(view)
10997
}
11098
}
11199

112100
companion object {
113101
fun inflate(context: Context): AxisPicker {
114-
val layout = LayoutInflater.from(context).inflate(R.layout.dialog_axis_picker, null)
115-
return AxisPicker(layout as ConstraintLayout)
102+
val binding = DialogAxisPickerBinding.inflate(LayoutInflater.from(context))
103+
return AxisPicker(binding)
116104
}
117105
}
118106
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
<com.ichi2.ui.FixedTextView
24-
android:id="@+id/axis_picker_selected_axis"
24+
android:id="@+id/selected_axis_text_view"
2525
app:layout_constraintDimensionRatio="1:1"
2626
android:layout_width="0dp"
2727
android:layout_height="0dp"
@@ -37,14 +37,16 @@
3737
app:layout_constraintTop_toTopOf="parent" />
3838

3939

40+
<!-- Contains `available_axes`, initially invisible -->
4041
<ScrollView
41-
android:id="@+id/axis_picker_scrollview"
42+
android:id="@+id/available_axes_container"
4243
android:layout_width="match_parent"
4344
android:layout_height="match_parent"
4445
android:visibility="gone"
4546
>
47+
<!-- AxisSelector elements are added here dynamically if motion is detected -->
4648
<LinearLayout
47-
android:id="@+id/axis_picker_available_axes"
49+
android:id="@+id/available_axes"
4850
android:layout_width="match_parent"
4951
android:orientation="vertical"
5052
android:layout_height="0dp"/>

0 commit comments

Comments
 (0)