-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The configuration is implemented as a bottom sheet that appears when clicking the new output format preferences. Changes are applied immediately and per-codec configuration values are preserved when switching codecs. Issue: #21 Signed-off-by: Andrew Gunnerson <chillermillerlong@hotmail.com>
- Loading branch information
1 parent
cc3bc70
commit 4243381
Showing
14 changed files
with
237 additions
and
29 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
app/src/main/java/com/chiller3/bcr/CodecBottomSheetFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package com.chiller3.bcr | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.core.view.ViewCompat | ||
import com.chiller3.bcr.codec.Codec | ||
import com.chiller3.bcr.codec.CodecParamType | ||
import com.chiller3.bcr.codec.Codecs | ||
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 CodecBottomSheetFragment : BottomSheetDialogFragment(), | ||
MaterialButtonToggleGroup.OnButtonCheckedListener, LabelFormatter, Slider.OnChangeListener, | ||
View.OnClickListener { | ||
private lateinit var codecParamTitle: TextView | ||
private lateinit var codecParam: Slider | ||
private lateinit var codecReset: MaterialButton | ||
private lateinit var codecNameGroup: MaterialButtonToggleGroup | ||
private val buttonIdToCodec = HashMap<Int, Codec>() | ||
private val codecToButtonId = HashMap<Codec, Int>() | ||
private lateinit var codecParamType: CodecParamType | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
val bottomSheet = inflater.inflate(R.layout.codec_bottom_sheet, container, false) | ||
|
||
codecParamTitle = bottomSheet.findViewById(R.id.codec_param_title) | ||
|
||
codecParam = bottomSheet.findViewById(R.id.codec_param) | ||
codecParam.setLabelFormatter(this) | ||
codecParam.addOnChangeListener(this) | ||
|
||
codecReset = bottomSheet.findViewById(R.id.codec_reset) | ||
codecReset.setOnClickListener(this) | ||
|
||
codecNameGroup = bottomSheet.findViewById(R.id.codec_name_group)!! | ||
|
||
for (codec in Codecs.all) { | ||
if (!codec.supported) { | ||
continue | ||
} | ||
|
||
val button = layoutInflater.inflate( | ||
R.layout.codec_bottom_sheet_button, codecNameGroup, false) as MaterialButton | ||
val id = ViewCompat.generateViewId() | ||
button.id = id | ||
button.text = codec.name | ||
codecNameGroup.addView(button) | ||
buttonIdToCodec[id] = codec | ||
codecToButtonId[codec] = id | ||
} | ||
|
||
codecNameGroup.addOnButtonCheckedListener(this) | ||
|
||
refreshCodec() | ||
|
||
return bottomSheet | ||
} | ||
|
||
/** | ||
* Update UI based on currently selected codec in the preferences. | ||
* | ||
* Calls [refreshParam] via [onButtonChecked]. | ||
*/ | ||
private fun refreshCodec() { | ||
val (codec, _) = Codecs.fromPreferences(requireContext()) | ||
codecNameGroup.check(codecToButtonId[codec]!!) | ||
} | ||
|
||
/** | ||
* Update parameter title and slider to match codec parameter specifications. | ||
*/ | ||
private fun refreshParam() { | ||
val (codec, param) = Codecs.fromPreferences(requireContext()) | ||
codecParamType = codec.paramType | ||
|
||
val titleResId = when (codec.paramType) { | ||
CodecParamType.CompressionLevel -> R.string.bottom_sheet_compression_level | ||
CodecParamType.Bitrate -> R.string.bottom_sheet_bitrate | ||
} | ||
|
||
codecParamTitle.setText(titleResId) | ||
|
||
codecParam.valueFrom = codec.paramRange.first.toFloat() | ||
codecParam.valueTo = codec.paramRange.last.toFloat() | ||
codecParam.stepSize = codec.paramStepSize.toFloat() | ||
|
||
codecParam.value = (param ?: codec.paramDefault).toFloat() | ||
} | ||
|
||
override fun onButtonChecked( | ||
group: MaterialButtonToggleGroup?, | ||
checkedId: Int, | ||
isChecked: Boolean | ||
) { | ||
if (isChecked) { | ||
Preferences.setCodecName(requireContext(), buttonIdToCodec[checkedId]!!.name) | ||
refreshParam() | ||
} | ||
} | ||
|
||
override fun getFormattedValue(value: Float): String = | ||
codecParamType.format(value.toUInt()) | ||
|
||
override fun onValueChange(slider: Slider, value: Float, fromUser: Boolean) { | ||
when (slider) { | ||
codecParam -> { | ||
val codec = buttonIdToCodec[codecNameGroup.checkedButtonId]!! | ||
Preferences.setCodecParam(requireContext(), codec.name, value.toUInt()) | ||
} | ||
} | ||
} | ||
|
||
override fun onClick(v: View?) { | ||
when (v) { | ||
codecReset -> { | ||
Preferences.resetAllCodecs(requireContext()) | ||
refreshCodec() | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
val TAG = CodecBottomSheetFragment::class.java.simpleName | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout 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.google.android.material.button.MaterialButtonToggleGroup | ||
android:id="@+id/codec_name_group" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
app:selectionRequired="true" | ||
app:singleSelection="true" /> | ||
|
||
<TextView | ||
android:id="@+id/codec_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/codec_param" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
app:labelBehavior="visible" /> | ||
|
||
<com.google.android.material.button.MaterialButton | ||
android:id="@+id/codec_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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<com.google.android.material.button.MaterialButton xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
style="?attr/materialButtonOutlinedStyle" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<dimen name="bottom_sheet_overall_padding">30dp</dimen> | ||
<dimen name="bottom_sheet_title_margin_bottom">16dp</dimen> | ||
<dimen name="bottom_sheet_section_separation">30dp</dimen> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.