1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/*
* Copyright 2018 Allan Wang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ca.allanwang.kau.kpref.activity.items
import android.widget.SeekBar
import android.widget.TextView
import ca.allanwang.kau.kpref.activity.GlobalOptions
import ca.allanwang.kau.kpref.activity.KClick
import ca.allanwang.kau.kpref.activity.KPrefItemActions
import ca.allanwang.kau.kpref.activity.R
import ca.allanwang.kau.utils.tint
/**
* Created by Allan Wang on 2017-06-07.
*
* Checkbox preference
* When clicked, will toggle the preference and the apply the result to the checkbox
*/
open class KPrefSeekbar(val builder: KPrefSeekbarContract) : KPrefItemBase<Int>(builder) {
protected inline val min get() = builder.min
protected inline val max get() = builder.max
protected inline val increment get() = builder.increments
override fun KClick<Int>.defaultOnClick() = Unit
override fun bindView(holder: ViewHolder, payloads: List<Any>) {
super.bindView(holder, payloads)
val text = holder.bindInnerView<TextView>(R.layout.kau_pref_seekbar_text)
withTextColor(text::setTextColor)
val tvc = builder.textViewConfigs
text.tvc()
val seekbar = holder.bindLowerView<SeekBar>(R.layout.kau_pref_seekbar) {
it.max = (max - min) / increment
it.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(sb: SeekBar, progress: Int, fromUser: Boolean) {
text.text = builder.toText(progress.fromProgress)
}
override fun onStartTrackingTouch(sb: SeekBar) {}
override fun onStopTrackingTouch(sb: SeekBar) {
pref = sb.progress.fromProgress
}
})
}
withAccentColor(seekbar::tint)
text.text =
builder.toText(seekbar.progress.fromProgress) // set initial text in case no change occurs
seekbar.progress = pref.toProgress
seekbar.isEnabled = builder.enabler()
}
/**
* Extension of the base contract
*/
interface KPrefSeekbarContract : BaseContract<Int> {
var min: Int
var max: Int
var increments: Int
/**
* Once a seekbar is let go, calculates what text to show in the text view
*/
var toText: (Int) -> String
var textViewConfigs: TextView.() -> Unit
}
/**
* Default implementation of [KPrefSeekbarContract]
*/
class KPrefSeekbarBuilder(
globalOptions: GlobalOptions,
titleId: Int,
getter: () -> Int,
setter: KPrefItemActions.(value: Int) -> Unit
) : KPrefSeekbarContract,
BaseContract<Int> by BaseBuilder(globalOptions, titleId, getter, setter) {
override var min: Int = 0
override var max: Int = 100
override var increments: Int = 1
set(value) {
if (value <= 0)
throw IllegalArgumentException("Seekbar must increment by at least 1")
field = value
}
override var toText: (Int) -> String = Int::toString
override var textViewConfigs: TextView.() -> Unit = {}
}
/**
* Helper to convert true value to progress value
* Progress values start at 0 and increment by 1
*/
protected inline val Int.toProgress: Int
get() = (this - min) / increment
/**
* Inverse of [Int.toProgress] to find the true value from the seekbar
*/
protected inline val Int.fromProgress: Int
get() = this * increment + min
override val type: Int
get() = R.id.kau_item_pref_seekbar
}
|