From 77684bb070dcbd011ffb3b5fb50a88f2cf3e65a2 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 21 May 2019 19:47:36 -0700 Subject: Add configuration options within getter and setter --- .../ca/allanwang/kau/kpref/activity/KPrefBinder.kt | 20 ++++++++++---------- .../allanwang/kau/kpref/activity/KPrefItemActions.kt | 11 +++++++++++ .../kau/kpref/activity/items/KPrefColorPicker.kt | 5 +++-- .../kau/kpref/activity/items/KPrefItemBase.kt | 13 +++++++------ .../kau/kpref/activity/items/KPrefItemCore.kt | 10 +++------- .../kau/kpref/activity/items/KPrefSeekbar.kt | 5 +++-- .../allanwang/kau/kpref/activity/items/KPrefText.kt | 9 +++++---- .../kau/kpref/activity/items/KPrefTimePicker.kt | 7 ++++--- 8 files changed, 46 insertions(+), 34 deletions(-) create mode 100644 kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefItemActions.kt (limited to 'kpref-activity/src/main') diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefBinder.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefBinder.kt index cf2a12f..ec3c69f 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefBinder.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefBinder.kt @@ -82,8 +82,8 @@ class KPrefAdapterBuilder(val globalOptions: GlobalOptions) { @KPrefMarker fun checkbox( @StringRes title: Int, - getter: (() -> Boolean), - setter: ((value: Boolean) -> Unit), + getter: KPrefItemActions.() -> Boolean, + setter: KPrefItemActions.(value: Boolean) -> Unit, builder: KPrefItemBase.BaseContract.() -> Unit = {} ) = list.add( KPrefCheckbox(KPrefItemBase.BaseBuilder(globalOptions, title, getter, setter) @@ -93,8 +93,8 @@ class KPrefAdapterBuilder(val globalOptions: GlobalOptions) { @KPrefMarker fun colorPicker( @StringRes title: Int, - getter: (() -> Int), - setter: ((value: Int) -> Unit), + getter: KPrefItemActions.() -> Int, + setter: KPrefItemActions.(value: Int) -> Unit, builder: KPrefColorPicker.KPrefColorContract.() -> Unit = {} ) = list.add( KPrefColorPicker(KPrefColorPicker.KPrefColorBuilder(globalOptions, title, getter, setter) @@ -104,8 +104,8 @@ class KPrefAdapterBuilder(val globalOptions: GlobalOptions) { @KPrefMarker fun text( @StringRes title: Int, - getter: (() -> T), - setter: ((value: T) -> Unit), + getter: KPrefItemActions.() -> T, + setter: KPrefItemActions.(value: T) -> Unit, builder: KPrefText.KPrefTextContract.() -> Unit = {} ) = list.add( KPrefText(KPrefText.KPrefTextBuilder(globalOptions, title, getter, setter) @@ -134,8 +134,8 @@ class KPrefAdapterBuilder(val globalOptions: GlobalOptions) { @KPrefMarker fun seekbar( @StringRes title: Int, - getter: (() -> Int), - setter: ((value: Int) -> Unit), + getter: KPrefItemActions.() -> Int, + setter: KPrefItemActions.(value: Int) -> Unit, builder: KPrefSeekbar.KPrefSeekbarContract.() -> Unit = {} ) = list.add( KPrefSeekbar(KPrefSeekbar.KPrefSeekbarBuilder(globalOptions, title, getter, setter) @@ -145,8 +145,8 @@ class KPrefAdapterBuilder(val globalOptions: GlobalOptions) { @KPrefMarker fun timePicker( @StringRes title: Int, - getter: (() -> Int), - setter: ((value: Int) -> Unit), + getter: KPrefItemActions.() -> Int, + setter: KPrefItemActions.(value: Int) -> Unit, builder: KPrefTimePicker.KPrefTimeContract.() -> Unit = {} ) = list.add( KPrefTimePicker(KPrefTimePicker.KPrefTimeBuilder(globalOptions, title, getter, setter) diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefItemActions.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefItemActions.kt new file mode 100644 index 0000000..e28bac6 --- /dev/null +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefItemActions.kt @@ -0,0 +1,11 @@ +package ca.allanwang.kau.kpref.activity + +/** + * Applicable actions + */ +interface KPrefItemActions { + /** + * Attempts to reload current item by identifying it with its titleId + */ + fun reloadSelf() +} \ No newline at end of file diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefColorPicker.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefColorPicker.kt index 675e387..07a6bbf 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefColorPicker.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefColorPicker.kt @@ -21,6 +21,7 @@ import ca.allanwang.kau.colorpicker.ColorContract import ca.allanwang.kau.colorpicker.colorPickerDialog 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 /** @@ -71,8 +72,8 @@ open class KPrefColorPicker(open val builder: KPrefColorContract) : KPrefItemBas class KPrefColorBuilder( globalOptions: GlobalOptions, titleId: Int, - getter: () -> Int, - setter: (value: Int) -> Unit + getter: KPrefItemActions.() -> Int, + setter: KPrefItemActions.(value: Int) -> Unit ) : KPrefColorContract, BaseContract by BaseBuilder(globalOptions, titleId, getter, setter), ColorContract by ColorBuilder() { override var showPreview: Boolean = true diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemBase.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemBase.kt index 6690574..a445de5 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemBase.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemBase.kt @@ -19,6 +19,7 @@ import android.view.View import androidx.annotation.CallSuper 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.resolveDrawable @@ -30,9 +31,9 @@ import ca.allanwang.kau.utils.resolveDrawable abstract class KPrefItemBase(protected val base: BaseContract) : KPrefItemCore(base) { open var pref: T - get() = base.getter() + get() = base.getter(this) set(value) { - base.setter(value) + base.setter(this, value) } private var _enabled: Boolean = true @@ -92,8 +93,8 @@ abstract class KPrefItemBase(protected val base: BaseContract) : KPrefItem var enabler: () -> Boolean var onClick: (KClick.() -> Unit)? var onDisabledClick: (KClick.() -> Unit)? - val getter: () -> T - val setter: (value: T) -> Unit + val getter: KPrefItemActions.() -> T + val setter: KPrefItemActions.(value: T) -> Unit } /** @@ -102,8 +103,8 @@ abstract class KPrefItemBase(protected val base: BaseContract) : KPrefItem class BaseBuilder( globalOptions: GlobalOptions, titleId: Int, - override val getter: () -> T, - override val setter: (value: T) -> Unit + override val getter: KPrefItemActions.() -> T, + override val setter: KPrefItemActions.(value: T) -> Unit ) : CoreContract by CoreBuilder(globalOptions, titleId), BaseContract { override var enabler: () -> Boolean = { true } override var onClick: (KClick.() -> Unit)? = null diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemCore.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemCore.kt index fc632ce..36bf670 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemCore.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemCore.kt @@ -30,6 +30,7 @@ import androidx.recyclerview.widget.RecyclerView import ca.allanwang.kau.adapters.ThemableIItem import ca.allanwang.kau.adapters.ThemableIItemDelegate import ca.allanwang.kau.kpref.activity.GlobalOptions +import ca.allanwang.kau.kpref.activity.KPrefItemActions import ca.allanwang.kau.kpref.activity.KPrefMarker import ca.allanwang.kau.kpref.activity.R import ca.allanwang.kau.utils.INVALID_ID @@ -47,7 +48,7 @@ import com.mikepenz.iconics.typeface.IIcon * Core class containing nothing but the view items */ -abstract class KPrefItemCore(val core: CoreContract) : AbstractItem(), +abstract class KPrefItemCore(val core: CoreContract) : AbstractItem(), KPrefItemActions by core, ThemableIItem by ThemableIItemDelegate() { final override fun getViewHolder(v: View) = ViewHolder(v) @@ -119,7 +120,7 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem Int @@ -128,11 +129,6 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem Int var iicon: IIcon? var visible: () -> Boolean - - /** - * Attempts to reload current item by identifying it with its [id] - */ - fun reloadSelf() } /** diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSeekbar.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSeekbar.kt index e70a374..d49fe16 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSeekbar.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSeekbar.kt @@ -19,6 +19,7 @@ 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 @@ -84,8 +85,8 @@ open class KPrefSeekbar(val builder: KPrefSeekbarContract) : KPrefItemBase( class KPrefSeekbarBuilder( globalOptions: GlobalOptions, titleId: Int, - getter: () -> Int, - setter: (value: Int) -> Unit + getter: KPrefItemActions.() -> Int, + setter: KPrefItemActions.(value: Int) -> Unit ) : KPrefSeekbarContract, BaseContract by BaseBuilder(globalOptions, titleId, getter, setter) { override var min: Int = 0 diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefText.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefText.kt index ac8416d..5f6eefa 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefText.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefText.kt @@ -18,6 +18,7 @@ package ca.allanwang.kau.kpref.activity.items 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.toast @@ -35,9 +36,9 @@ open class KPrefText(open val builder: KPrefTextContract) : KPrefItemBase< * Automatically reload on set */ override var pref: T - get() = base.getter() + get() = base.getter(this) set(value) { - base.setter(value) + base.setter(this, value) builder.reloadSelf() } @@ -65,8 +66,8 @@ open class KPrefText(open val builder: KPrefTextContract) : KPrefItemBase< class KPrefTextBuilder( globalOptions: GlobalOptions, titleId: Int, - getter: () -> T, - setter: (value: T) -> Unit + getter: KPrefItemActions.() -> T, + setter: KPrefItemActions.(value: T) -> Unit ) : KPrefTextContract, BaseContract by BaseBuilder(globalOptions, titleId, getter, setter) { override var textGetter: (T) -> String? = { it?.toString() } } diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefTimePicker.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefTimePicker.kt index 7242bbe..f2824a7 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefTimePicker.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefTimePicker.kt @@ -19,6 +19,7 @@ import android.app.TimePickerDialog import android.widget.TimePicker 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 java.util.Locale @@ -47,9 +48,9 @@ open class KPrefTimePicker(override val builder: KPrefTimeContract) : KPrefText< class KPrefTimeBuilder( globalOptions: GlobalOptions, titleId: Int, - getter: () -> Int, - setter: (value: Int) -> Unit - ) : KPrefTimeContract, BaseContract by BaseBuilder(globalOptions, titleId, getter, setter) { + getter: KPrefItemActions.() -> Int, + setter: KPrefItemActions.(value: Int) -> Unit + ) : KPrefTimeContract, BaseContract by BaseBuilder(globalOptions, titleId, getter, setter) { override var use24HourFormat: Boolean = false -- cgit v1.2.3