Skip to content

Commit

Permalink
feat: add number input dialog for slider preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
timschneeb committed Oct 22, 2022
1 parent 671dbef commit 919e602
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ class FileLibraryDialogFragment : ListPreferenceDialogFragmentCompat() {
layoutInflater,
R.string.filelibrary_context_duplicate,
R.string.filelibrary_new_file_name,
selectedFile.nameWithoutExtension
selectedFile.nameWithoutExtension,
false,
null
) {
if (it != null) {
val newFile =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ class GraphicEqualizerFragment : Fragment() {
layoutInflater,
R.string.geq_edit_as_string,
R.string.geq_edit_hint,
adapter.nodes.serialize()
adapter.nodes.serialize(),
false,
null
) {
it?.let {
adapter.nodes.deserialize(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.widget.SeekBar
import android.widget.TextView
import android.widget.Toast
import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder
import androidx.preference.SeekBarPreference
import com.google.android.material.slider.Slider
import me.timschneeberger.rootlessjamesdsp.R
import me.timschneeberger.rootlessjamesdsp.utils.ContextExtensions.showInputAlert
import timber.log.Timber
import java.util.*
import kotlin.math.abs
import kotlin.math.min

Expand Down Expand Up @@ -175,6 +179,32 @@ class MaterialSeekbarPreference : Preference {
mSeekBar!!.value = mSeekBarValue
updateLabelValue(mSeekBarValue)
mSeekBar!!.isEnabled = isEnabled

this.setOnPreferenceClickListener {
context.showInputAlert(
LayoutInflater.from(context),
context.getString(R.string.slider_dialog_title),
title?.toString(),
"%.${mPrecision}f".format(Locale.ROOT, getValue()),
true,
mUnit
) {
it ?: return@showInputAlert
try {
setValue(it.toFloat())
}
catch (ex: Exception) {
Timber.e("Failed to parse number input")
Timber.d(ex)
Toast.makeText(
context,
context.getString(R.string.slider_dialog_format_error),
Toast.LENGTH_SHORT
).show()
}
}
true
}
}

override fun onSetInitialValue(_defaultValue: Any?) {
Expand Down Expand Up @@ -391,7 +421,7 @@ class MaterialSeekbarPreference : Preference {

if(valueLabelOverride == null)
{
mSeekBarValueTextView!!.text = "%.${mPrecision}f${mUnit}".format(value)
mSeekBarValueTextView!!.text = "%.${mPrecision}f${mUnit}".format(Locale.ROOT, value)
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.os.PowerManager
import android.os.Process
import android.provider.Settings
import android.text.Editable
import android.text.InputType
import android.util.Base64
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -172,14 +173,34 @@ object ContextExtensions {
.show()
}

fun Context.showInputAlert(layoutInflater: LayoutInflater, @StringRes title: Int, @StringRes hint: Int, value: String, callback: ((String?) -> Unit)) {
showInputAlert(layoutInflater, getString(title), getString(hint), value, callback)
fun Context.showInputAlert(
layoutInflater: LayoutInflater,
@StringRes title: Int,
@StringRes hint: Int,
value: String,
isNumberInput: Boolean,
suffix: String?,
callback: ((String?) -> Unit)
) {
showInputAlert(layoutInflater, getString(title), getString(hint), value, isNumberInput, suffix, callback)
}

fun Context.showInputAlert(layoutInflater: LayoutInflater, title: String, hint: String, value: String, callback: ((String?) -> Unit)) {
fun Context.showInputAlert(
layoutInflater: LayoutInflater,
title: String?,
hint: String?,
value: String,
isNumberInput: Boolean,
suffix: String?,
callback: ((String?) -> Unit)
) {
val content = DialogTextinputBinding.inflate(layoutInflater)
content.textInputLayout.hint = hint
content.text1.text = Editable.Factory.getInstance().newEditable(value)
if(isNumberInput)
content.text1.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL

content.textInputLayout.suffixText = suffix

AlertDialog.Builder(this)
.setTitle(title)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/dialog_textinput.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
android:paddingEnd="?attr/dialogPreferredPadding">

<com.google.android.material.textfield.TextInputLayout
style="?attr/textInputFilledStyle"
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,9 @@
<!-- Number box view -->
<string name="number_box_decrement">Decrement</string>
<string name="number_box_increment">Increment</string>

<!-- Slider widget -->
<string name="slider_dialog_title">Edit value</string>
<string name="slider_dialog_format_error">Invalid number format. Changes not applied.</string>

</resources>

0 comments on commit 919e602

Please sign in to comment.