Skip to content

Commit

Permalink
Use view binding instead of findViewById where possible
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Gunnerson <chillermillerlong@hotmail.com>
  • Loading branch information
chenxiaolong committed May 29, 2022
1 parent a463b45 commit 42ab5f4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 46 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ android {
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
viewBinding = true
}
}

dependencies {
Expand Down
1 change: 1 addition & 0 deletions app/magisk/updates/release/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

Non-user-facing changes:
* Improve output format parameter abstraction (PR: #49, @chenxiaolong)
* Use view binding instead of findViewById where possible (PR: #50, @chenxiaolong)

### Version 1.5

Expand Down
77 changes: 36 additions & 41 deletions app/src/main/java/com/chiller3/bcr/FormatBottomSheetFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.ViewCompat
import androidx.core.view.isVisible
import com.chiller3.bcr.databinding.FormatBottomSheetBinding
import com.chiller3.bcr.databinding.FormatBottomSheetButtonBinding
import com.chiller3.bcr.format.*
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.button.MaterialButton
import com.google.android.material.button.MaterialButtonToggleGroup
import com.google.android.material.slider.LabelFormatter
import com.google.android.material.slider.Slider

class FormatBottomSheetFragment : BottomSheetDialogFragment(),
MaterialButtonToggleGroup.OnButtonCheckedListener, LabelFormatter, Slider.OnChangeListener,
View.OnClickListener {
private lateinit var formatParamGroup: LinearLayout
private lateinit var formatParamTitle: TextView
private lateinit var formatParamSlider: Slider
private lateinit var formatReset: MaterialButton
private lateinit var formatNameGroup: MaterialButtonToggleGroup
private var _binding: FormatBottomSheetBinding? = null
private val binding
get() = _binding!!

private val buttonIdToFormat = HashMap<Int, Format>()
private val formatToButtonId = HashMap<Format, Int>()
private lateinit var formatParamInfo: FormatParamInfo
Expand All @@ -31,42 +29,39 @@ class FormatBottomSheetFragment : BottomSheetDialogFragment(),
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val bottomSheet = inflater.inflate(R.layout.format_bottom_sheet, container, false)

formatParamGroup = bottomSheet.findViewById(R.id.format_param_group)

formatParamTitle = bottomSheet.findViewById(R.id.format_param_title)
): View {
_binding = FormatBottomSheetBinding.inflate(inflater, container, false)

formatParamSlider = bottomSheet.findViewById(R.id.format_param_slider)
formatParamSlider.setLabelFormatter(this)
formatParamSlider.addOnChangeListener(this)
binding.paramSlider.setLabelFormatter(this)
binding.paramSlider.addOnChangeListener(this)

formatReset = bottomSheet.findViewById(R.id.format_reset)
formatReset.setOnClickListener(this)

formatNameGroup = bottomSheet.findViewById(R.id.format_name_group)!!
binding.reset.setOnClickListener(this)

for (format in Formats.all) {
if (!format.supported) {
continue
}

val button = layoutInflater.inflate(
R.layout.format_bottom_sheet_button, formatNameGroup, false) as MaterialButton
val buttonBinding = FormatBottomSheetButtonBinding.inflate(
inflater, binding.nameGroup, false)
val id = ViewCompat.generateViewId()
button.id = id
button.text = format.name
formatNameGroup.addView(button)
buttonBinding.root.id = id
buttonBinding.root.text = format.name
binding.nameGroup.addView(buttonBinding.root)
buttonIdToFormat[id] = format
formatToButtonId[format] = id
}

formatNameGroup.addOnButtonCheckedListener(this)
binding.nameGroup.addOnButtonCheckedListener(this)

refreshFormat()

return bottomSheet
return binding.root
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

/**
Expand All @@ -76,7 +71,7 @@ class FormatBottomSheetFragment : BottomSheetDialogFragment(),
*/
private fun refreshFormat() {
val (format, _) = Formats.fromPreferences(requireContext())
formatNameGroup.check(formatToButtonId[format]!!)
binding.nameGroup.check(formatToButtonId[format]!!)
}

/**
Expand All @@ -88,28 +83,28 @@ class FormatBottomSheetFragment : BottomSheetDialogFragment(),

when (val info = format.paramInfo) {
is RangedParamInfo -> {
formatParamGroup.isVisible = true
binding.paramGroup.isVisible = true

formatParamTitle.setText(when (info.type) {
binding.paramTitle.setText(when (info.type) {
RangedParamType.CompressionLevel -> R.string.bottom_sheet_compression_level
RangedParamType.Bitrate -> R.string.bottom_sheet_bitrate
})

formatParamSlider.valueFrom = info.range.first.toFloat()
formatParamSlider.valueTo = info.range.last.toFloat()
formatParamSlider.stepSize = info.stepSize.toFloat()
formatParamSlider.value = (param ?: info.default).toFloat()
binding.paramSlider.valueFrom = info.range.first.toFloat()
binding.paramSlider.valueTo = info.range.last.toFloat()
binding.paramSlider.stepSize = info.stepSize.toFloat()
binding.paramSlider.value = (param ?: info.default).toFloat()
}
NoParamInfo -> {
formatParamGroup.isVisible = false
binding.paramGroup.isVisible = false

// Needed due to a bug in the material3 library where the slider label does not disappear
// when the slider visibility is set to View.GONE
// https://github.com/material-components/material-components-android/issues/2726
val ensureLabelsRemoved = formatParamSlider.javaClass.superclass
val ensureLabelsRemoved = binding.paramSlider.javaClass.superclass
.getDeclaredMethod("ensureLabelsRemoved")
ensureLabelsRemoved.isAccessible = true
ensureLabelsRemoved.invoke(formatParamSlider)
ensureLabelsRemoved.invoke(binding.paramSlider)
}
}
}
Expand All @@ -130,16 +125,16 @@ class FormatBottomSheetFragment : BottomSheetDialogFragment(),

override fun onValueChange(slider: Slider, value: Float, fromUser: Boolean) {
when (slider) {
formatParamSlider -> {
val format = buttonIdToFormat[formatNameGroup.checkedButtonId]!!
binding.paramSlider -> {
val format = buttonIdToFormat[binding.nameGroup.checkedButtonId]!!
Preferences.setFormatParam(requireContext(), format.name, value.toUInt())
}
}
}

override fun onClick(v: View?) {
when (v) {
formatReset -> {
binding.reset -> {
Preferences.resetAllFormats(requireContext())
refreshFormat()
// Need to explicitly refresh the parameter when the default format is already chosen
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/res/layout/format_bottom_sheet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,35 @@
android:textAppearance="?attr/textAppearanceHeadline6" />

<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/format_name_group"
android:id="@+id/name_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:selectionRequired="true"
app:singleSelection="true" />

<LinearLayout
android:id="@+id/format_param_group"
android:id="@+id/param_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:id="@+id/format_param_title"
android:id="@+id/param_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/bottom_sheet_section_separation"
android:layout_marginBottom="@dimen/bottom_sheet_title_margin_bottom"
android:textAppearance="?attr/textAppearanceHeadline6" />

<com.google.android.material.slider.Slider
android:id="@+id/format_param_slider"
android:id="@+id/param_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelBehavior="visible" />
</LinearLayout>

<com.google.android.material.button.MaterialButton
android:id="@+id/format_reset"
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/bottom_sheet_section_separation"
Expand Down

0 comments on commit 42ab5f4

Please sign in to comment.