Skip to content

Commit

Permalink
make notifications serviceDelay logarithmic
Browse files Browse the repository at this point in the history
  • Loading branch information
benibela committed Feb 25, 2024
1 parent fc82bea commit 38e220a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ class Options : VideLibriBaseActivity() {
onChanged(saveOptionsAndroidOnly)
} as PreferenceSeekBar
seekBarToToggle!!.apply {
//showSeekBarValue = true
dynamicSummary = getString(R.string.lay_options_label_autocheckdelay_summary)
safeMax = 120
safeMax = 1200
unsafeWarning = getString(R.string.lay_options_label_autocheckdelay_too_large)
isEnabled = globalOptionsAndroid.notifications.enabled
showDynamicSummary()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class PreferenceSeekBar @JvmOverloads constructor(context: Context, attrs: Attri
var safeMax: Int
var unsafeWarning: String?

var valueDisplayMapper: ((Int)->Int) = { x -> x }

init {

val a = context.obtainStyledAttributes(attrs, R.styleable.PreferenceSeekBar, defStyleAttr, defStyleRes)
Expand Down Expand Up @@ -56,7 +58,7 @@ class PreferenceSeekBar @JvmOverloads constructor(context: Context, attrs: Attri
}

fun showDynamicSummary() {
val s = dynamicSummary?.let { String.format(it, value) } ?: ""
val s = dynamicSummary?.let { String.format(it, valueDisplayMapper( value)) } ?: ""
//todo: plural https://stackoverflow.com/a/25648349
summary = if (editable) s
else s + " " + getString(R.string.lay_options_seekbar_tap_to_change)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import android.content.Context
import androidx.annotation.ArrayRes
import androidx.annotation.StringRes
import androidx.preference.*
import kotlin.math.log2
import kotlin.math.pow
import kotlin.math.round
import kotlin.reflect.KMutableProperty0

class PreferenceScreenBuilder(val context: Context, val screen: PreferenceScreen, init: PreferenceScreenBuilder.() -> Unit) {
Expand Down Expand Up @@ -147,6 +150,38 @@ class SeekBarBuilder(ctx: Context, customPreference: SeekBarPreference?):
super.property(p)
preference.value = p.get()
}
//scale the seekbar exponential
//this is kind of a hack, since the seekbar and preference is still linear, the value is scaled before being written to persistent storage
//so all the preference options (max, safeMax, showSeekBarValue) show the wrong value
fun logarithmicProperty(p: KMutableProperty0<Int>){
/*
a good looking mapping:
180 -> 30
360 -> 60
540 -> 120
720 -> 240
900 -> 480
corresponds to
x -> 15*2**( x/180 )
reverse:
y -> log2(y/15) *180
but use 5, because that is also the minimum since exp(0) = 1
*/
val propertyTransformer = object {
val actualProperty = p
var fakeProperty: Int
get() = getterTransform( p.get() )
set(value) {
p.set( setterTransform( value ) );
// Log.i("videlibriLOG", "$value -> ${setterTransform( value )} -> ${p.get()}")
}
fun setterTransform(x: Int) = round( 5 * (2.0.pow(x/180.0)) ).toInt()
fun getterTransform(y: Int) = round( log2 (y / 5.0) * 180 ).toInt()
}
property(propertyTransformer::fakeProperty)
(preference as? PreferenceSeekBar)?.valueDisplayMapper = propertyTransformer::setterTransform
}
}

/*
Expand Down

0 comments on commit 38e220a

Please sign in to comment.