Skip to content

Commit

Permalink
Use chips for sample rate instead of a second slider
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 31, 2022
1 parent bcaea20 commit b65727c
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 91 deletions.
91 changes: 50 additions & 41 deletions app/src/main/java/com/chiller3/bcr/FormatBottomSheetFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class FormatBottomSheetFragment : BottomSheetDialogFragment(),
private val formatToChipId = HashMap<Format, Int>()
private lateinit var formatParamInfo: FormatParamInfo

private val chipIdToSampleRate = HashMap<Int, UInt?>()
private val sampleRateToChipId = HashMap<UInt?, Int>()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -37,31 +40,25 @@ class FormatBottomSheetFragment : BottomSheetDialogFragment(),
}
binding.paramSlider.addOnChangeListener(this)

binding.sampleRateSlider.setLabelFormatter {
SampleRates.format(sampleRateFromIndex(it.toInt()))
}
binding.sampleRateSlider.addOnChangeListener(this)

binding.reset.setOnClickListener(this)

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

val chipBinding = FormatBottomSheetChipBinding.inflate(
inflater, binding.nameGroup, false)
val id = View.generateViewId()
chipBinding.root.id = id
chipBinding.root.text = format.name
chipBinding.root.layoutDirection = View.LAYOUT_DIRECTION_LOCALE
binding.nameGroup.addView(chipBinding.root)
chipIdToFormat[id] = format
formatToChipId[format] = id
addFormatChip(inflater, format)
}

binding.nameGroup.setOnCheckedStateChangeListener(this)

addSampleRateChip(inflater, null)
for (sampleRate in SampleRates.all) {
addSampleRateChip(inflater, sampleRate)
}

binding.sampleRateGroup.setOnCheckedStateChangeListener(this)

refreshFormat()
refreshSampleRate()

Expand All @@ -73,6 +70,30 @@ class FormatBottomSheetFragment : BottomSheetDialogFragment(),
_binding = null
}

private fun addFormatChip(inflater: LayoutInflater, format: Format) {
val chipBinding = FormatBottomSheetChipBinding.inflate(
inflater, binding.nameGroup, false)
val id = View.generateViewId()
chipBinding.root.id = id
chipBinding.root.text = format.name
chipBinding.root.layoutDirection = View.LAYOUT_DIRECTION_LOCALE
binding.nameGroup.addView(chipBinding.root)
chipIdToFormat[id] = format
formatToChipId[format] = id
}

private fun addSampleRateChip(inflater: LayoutInflater, sampleRate: UInt?) {
val chipBinding = FormatBottomSheetChipBinding.inflate(
inflater, binding.sampleRateGroup, false)
val id = View.generateViewId()
chipBinding.root.id = id
chipBinding.root.text = SampleRates.format(requireContext(), sampleRate)
chipBinding.root.layoutDirection = View.LAYOUT_DIRECTION_LOCALE
binding.sampleRateGroup.addView(chipBinding.root)
chipIdToSampleRate[id] = sampleRate
sampleRateToChipId[sampleRate] = id
}

/**
* Update UI based on currently selected format in the preferences.
*
Expand All @@ -83,6 +104,11 @@ class FormatBottomSheetFragment : BottomSheetDialogFragment(),
binding.nameGroup.check(formatToChipId[format]!!)
}

private fun refreshSampleRate() {
val sampleRate = SampleRates.fromPreferences(requireContext())
binding.sampleRateGroup.check(sampleRateToChipId[sampleRate]!!)
}

/**
* Update parameter title and slider to match format parameter specifications.
*/
Expand Down Expand Up @@ -118,36 +144,26 @@ class FormatBottomSheetFragment : BottomSheetDialogFragment(),
}
}

private fun refreshSampleRate() {
val sampleRate = SampleRates.fromPreferences(requireContext())

// Index == SampleRates.all.size is used to represent the native sample rate option
binding.sampleRateSlider.valueFrom = 0f
binding.sampleRateSlider.valueTo = SampleRates.all.size.toFloat()
binding.sampleRateSlider.stepSize = 1f
override fun onCheckedChanged(group: ChipGroup, checkedIds: MutableList<Int>) {
val context = requireContext()

if (sampleRate == null) {
binding.sampleRateSlider.value = SampleRates.all.size.toFloat()
} else {
binding.sampleRateSlider.value = SampleRates.all.indexOf(sampleRate).toFloat()
when (group) {
binding.nameGroup -> {
Preferences.setFormatName(context, chipIdToFormat[checkedIds.first()]!!.name)
refreshParam()
}
binding.sampleRateGroup -> {
Preferences.setSampleRate(context, chipIdToSampleRate[checkedIds.first()])
}
}
}

override fun onCheckedChanged(group: ChipGroup, checkedIds: MutableList<Int>) {
Preferences.setFormatName(requireContext(), chipIdToFormat[checkedIds.first()]!!.name)
refreshParam()
}

override fun onValueChange(slider: Slider, value: Float, fromUser: Boolean) {
when (slider) {
binding.paramSlider -> {
val format = chipIdToFormat[binding.nameGroup.checkedChipId]!!
Preferences.setFormatParam(requireContext(), format.name, value.toUInt())
}
binding.sampleRateSlider -> {
val sampleRate = sampleRateFromIndex(value.toInt())
Preferences.setSampleRate(requireContext(), sampleRate)
}
}
}

Expand All @@ -167,12 +183,5 @@ class FormatBottomSheetFragment : BottomSheetDialogFragment(),

companion object {
val TAG: String = FormatBottomSheetFragment::class.java.simpleName

private fun sampleRateFromIndex(index: Int) =
if (index == SampleRates.all.size) {
null
} else {
SampleRates.all[index]
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/chiller3/bcr/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class SettingsActivity : AppCompatActivity() {
is RangedParamInfo -> "${info.format(formatParam)}, "
NoParamInfo -> ""
}
val sampleRate = SampleRates.format(SampleRates.fromPreferences(context))
val sampleRate = SampleRates.format(context, SampleRates.fromPreferences(context))

prefOutputFormat.summary = "${summary}\n\n${format.name} (${prefix}${sampleRate})"
}
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/chiller3/bcr/format/SampleRates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package com.chiller3.bcr.format

import android.content.Context
import com.chiller3.bcr.Preferences
import com.chiller3.bcr.R

object SampleRates {
/**
Expand Down Expand Up @@ -37,9 +38,9 @@ object SampleRates {
return null
}

fun format(sampleRate: UInt?): String =
fun format(context: Context, sampleRate: UInt?): String =
if (sampleRate == null) {
"native"
context.getString(R.string.bottom_sheet_sample_rate_native)
} else {
"$sampleRate Hz"
}
Expand Down
100 changes: 53 additions & 47 deletions app/src/main/res/layout/format_bottom_sheet.xml
Original file line number Diff line number Diff line change
@@ -1,66 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:padding="@dimen/bottom_sheet_overall_padding">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/bottom_sheet_title_margin_bottom"
android:text="@string/bottom_sheet_output_format"
android:textAppearance="?attr/textAppearanceHeadline6" />

<com.chiller3.bcr.ChipGroupCentered
android:id="@+id/name_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:selectionRequired="true"
app:singleSelection="true" />
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/param_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
android:gravity="center_horizontal"
android:padding="@dimen/bottom_sheet_overall_padding">

<TextView
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:text="@string/bottom_sheet_output_format"
android:textAppearance="?attr/textAppearanceHeadline6" />

<com.google.android.material.slider.Slider
android:id="@+id/param_slider"
<com.chiller3.bcr.ChipGroupCentered
android:id="@+id/name_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelBehavior="visible" />
</LinearLayout>
app:selectionRequired="true"
app:singleSelection="true" />

<TextView
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:text="@string/bottom_sheet_sample_rate"
android:textAppearance="?attr/textAppearanceHeadline6" />
<LinearLayout
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/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/sample_rate_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelBehavior="visible" />
<com.google.android.material.slider.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/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/bottom_sheet_section_separation"
android:text="@string/bottom_sheet_reset"
style="?attr/materialButtonOutlinedStyle" />
</LinearLayout>
<TextView
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:text="@string/bottom_sheet_sample_rate"
android:textAppearance="?attr/textAppearanceHeadline6" />

<com.chiller3.bcr.ChipGroupCentered
android:id="@+id/sample_rate_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:selectionRequired="true"
app:singleSelection="true" />

<com.google.android.material.button.MaterialButton
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/bottom_sheet_section_separation"
android:text="@string/bottom_sheet_reset"
style="?attr/materialButtonOutlinedStyle" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<string name="bottom_sheet_compression_level">Compression level</string>
<string name="bottom_sheet_bitrate">Bitrate</string>
<string name="bottom_sheet_sample_rate">Sample rate</string>
<string name="bottom_sheet_sample_rate_native">Native sample rate</string>
<string name="bottom_sheet_reset">Reset to defaults</string>

<!-- Notifications -->
Expand Down

0 comments on commit b65727c

Please sign in to comment.