Skip to content

Commit 9f206dc

Browse files
david-allisonlukstbit
authored andcommitted
chore: convert AxisSelector to ViewBinding
Issue 11116
1 parent cecd967 commit 9f206dc

2 files changed

Lines changed: 17 additions & 27 deletions

File tree

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

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ package com.ichi2.ui
1919
import android.content.Context
2020
import android.util.AttributeSet
2121
import android.view.LayoutInflater
22-
import android.widget.Button
2322
import android.widget.LinearLayout
24-
import android.widget.TextView
2523
import com.ichi2.anki.R
24+
import com.ichi2.anki.databinding.AxisDisplayBinding
2625
import com.ichi2.anki.reviewer.Axis
2726
import com.ichi2.anki.reviewer.Binding
2827
import timber.log.Timber
@@ -31,12 +30,13 @@ import timber.log.Timber
3130
* Displays live values of an [Axis] (joystick/trigger), and allows selection of a binding if an
3231
* [extremity][AxisValueDisplay.isExtremity] has been received
3332
*
34-
* The [name] of the Axis (AXIS_X)
33+
* The [axisName][AxisDisplayBinding.axisName] of the Axis (AXIS_X)
3534
*
3635
* The [value] of the Axis [-1, 1]
3736
* - If a value hits an extremity, the display changes color. See [AxisValueDisplay]
3837
*
39-
* Two buttons: [minButton] and [maxButton]
38+
* Two buttons: [selectMinExtremity][AxisDisplayBinding.selectMinExtremity] and
39+
* [selectMaxExtremity][AxisDisplayBinding.selectMaxExtremity]
4040
* - If an [extremity][AxisValueDisplay.isExtremity] is reached, these are activated
4141
* - Calls [onExtremitySelectedListener] if tapped
4242
*
@@ -52,34 +52,24 @@ class AxisSelector : LinearLayout {
5252
defStyle,
5353
)
5454

55-
private val name: TextView
56-
private val minButton: Button
57-
private val axisDisplay: AxisValueDisplay
58-
private val maxButton: Button
55+
private val binding = AxisDisplayBinding.inflate(LayoutInflater.from(context), this, true)
5956

6057
private var onExtremitySelectedListener: ((Binding.AxisButtonBinding) -> Unit)? = null
6158

6259
init {
63-
val inflater = LayoutInflater.from(context)
64-
inflater.inflate(R.layout.axis_display, this, true)
65-
name = findViewById(R.id.axis_name)
66-
minButton = findViewById(R.id.select_min_extremity)
67-
axisDisplay = findViewById(R.id.axis_value)
68-
maxButton = findViewById(R.id.select_max_extremity)
69-
7060
// Disabling buttons ensures that a user cannot map a negative value on a unidirectional
7161
// axis, such as the triggers on my 8BitDo
7262
// It's hard to know if an axis is single, or multidimensional until we've received input
73-
axisDisplay.setExtremityListener { valueAtExtremity ->
63+
binding.axisDisplay.setExtremityListener { valueAtExtremity ->
7464
when (valueAtExtremity) {
7565
-1f -> enableMinButton()
7666
1f -> enableMaxButton()
7767
}
7868
}
7969

8070
// call onExtremitySelectedListener
81-
minButton.setOnClickListener { selectMinimumValue() }
82-
maxButton.setOnClickListener { selectMaximumValue() }
71+
binding.selectMinExtremity.setOnClickListener { selectMinimumValue() }
72+
binding.selectMaxExtremity.setOnClickListener { selectMaximumValue() }
8373
}
8474

8575
private fun selectMaximumValue() {
@@ -93,15 +83,15 @@ class AxisSelector : LinearLayout {
9383
}
9484

9585
private fun enableMaxButton() {
96-
if (maxButton.isEnabled) return
86+
if (binding.selectMaxExtremity.isEnabled) return
9787
Timber.i("%s: max button enabled", axis)
98-
maxButton.isEnabled = true
88+
binding.selectMaxExtremity.isEnabled = true
9989
}
10090

10191
private fun enableMinButton() {
102-
if (minButton.isEnabled) return
92+
if (binding.selectMinExtremity.isEnabled) return
10393
Timber.i("%s: min button enabled", axis)
104-
minButton.isEnabled = true
94+
binding.selectMinExtremity.isEnabled = true
10595
}
10696

10797
/**
@@ -117,12 +107,12 @@ class AxisSelector : LinearLayout {
117107

118108
/** The name of the axis */
119109
var text: String
120-
get() = name.text.toString()
110+
get() = binding.axisName.text.toString()
121111
private set(value) {
122-
name.text = value
112+
binding.axisName.text = value
123113
}
124114

125-
var value: Float by axisDisplay::value
115+
var value: Float by binding.axisDisplay::value
126116

127117
fun setOnExtremitySelectedListener(listener: ((Binding.AxisButtonBinding) -> Unit)?) {
128118
onExtremitySelectedListener = listener

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252

5353
<com.ichi2.ui.AxisValueDisplay
54-
android:id="@+id/axis_value"
54+
android:id="@+id/axis_display"
5555
android:layout_width="0dp"
5656
android:layout_height="wrap_content"
5757
app:layout_constraintBottom_toBottomOf="@+id/select_min_extremity"
@@ -75,7 +75,7 @@
7575
android:enabled="false"
7676

7777
app:layout_constraintTop_toBottomOf="@id/axis_name"
78-
app:layout_constraintStart_toEndOf="@id/axis_value"
78+
app:layout_constraintStart_toEndOf="@id/axis_display"
7979
app:layout_constraintEnd_toEndOf="parent"
8080

8181
app:shapeAppearance="?attr/shapeAppearanceCornerLarge" />

0 commit comments

Comments
 (0)