From 1edb6e1ac1297f6feb229d8f89e07a88de1ae2e9 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 24 Sep 2017 16:56:11 -0400 Subject: v3.4.3 (#65) * fix/mediapicker (#50) * Bring all glide request managers to one instance * Switch to test implementation * Check if parent is null for searchview * Ensure open close runs on ui thread * Make glide contract internal * Update changelog * Update version Update changelog for previous prs * v3.4.1 (#63) * Check browser intent before launching (#54) * Update changelog * fix/misc (#55) * Add kapt plugin * Fix kau vector * Debug lintRelease * Revert debug * Update dependencies * Check context finishing state before showing dialog (#61) * Keep copy of shared pref rather than application context (#60) * Keep copy of shared pref rather than application context * Add back preference name * Add resolver checks (#62) Squashed commit of the following: commit 7fe57d4ab1dbfe8bfb4d4a15bd0fbf636da491fa Author: Allan Wang Date: Sat Sep 23 15:25:18 2017 -0400 Add missing quote commit ffc3ac99248c2250a7f14ef709f37d787cbe0d83 Author: Allan Wang Date: Sat Sep 23 15:20:54 2017 -0400 Update changelog Update gradle Update version name * Fix bundle NPE for activity creation Update changelog * Feature/kpref time picker (#64) * Init kpref time builder and open up other builders * Enable self refresh * Add readme * Update changelog * Update readme --- kpref-activity/README.md | 2 + .../ca/allanwang/kau/kpref/activity/KPrefBinder.kt | 8 +++ .../kau/kpref/activity/items/KPrefColorPicker.kt | 2 +- .../kau/kpref/activity/items/KPrefPlainText.kt | 2 +- .../kau/kpref/activity/items/KPrefSubItems.kt | 2 +- .../kau/kpref/activity/items/KPrefText.kt | 2 +- .../kau/kpref/activity/items/KPrefTimePicker.kt | 64 ++++++++++++++++++++++ 7 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefTimePicker.kt (limited to 'kpref-activity') diff --git a/kpref-activity/README.md b/kpref-activity/README.md index a9aac89..69d4892 100644 --- a/kpref-activity/README.md +++ b/kpref-activity/README.md @@ -30,6 +30,7 @@ Contract | Mandatory | Optional | Description `KPrefSeekbarContract` | `NA` | `min` `max` `increments` `toText` `textViewConfigs` | Addtional configurations for a seekbar, as well as text to be displayed on the side. `KPrefSubItemsContract` | `itemBuilder` | `NA` | Contains a new list for the adapter to load when clicked `KPrefTextContract` | `NA` | `textGetter` | Additional configurations for the text item +`KPrefTimeContract` | `NA` | `use24HourFormat` | Additional configurations for time picker The kpref items are as followed: @@ -41,6 +42,7 @@ Item | Implements | Description `header` | `CoreContract` | Header; just a title that isn't clickable `text` | `CoreContract` `BaseContract` `KPrefTextContract` | Text item; displays the kpref as a String on the right; does not have click implementation by default `plainText` | `CoreContract` `BaseContract` | Plain text item; like `text` but does not deal with any preferences directly, so it doesn't need a getter or setter +`timePicker` | `CoreContract` `BaseContract` `KPrefTextContract`, `KPrefTimeContract` | Extension ot `text` that will open and save a time picker This can be used to display text or deal with preference that are completely handed within the click event (eg a dialog). `subItems` | `CoreContract` `KPrefSubItemsContract` | Sub items; contains a new page for the activity to load when clicked 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 35821fd..637af03 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 @@ -101,6 +101,14 @@ class KPrefAdapterBuilder(val globalOptions: GlobalOptions) { = list.add(KPrefSeekbar(KPrefSeekbar.KPrefSeekbarBuilder(globalOptions, title, getter, setter) .apply { builder() })) + @KPrefMarker + fun timePicker(@StringRes title: Int, + getter: (() -> Int), + setter: ((value: Int) -> Unit), + builder: KPrefTimePicker.KPrefTimeContract.() -> Unit = {}) + = list.add(KPrefTimePicker(KPrefTimePicker.KPrefTimeBuilder(globalOptions, title, getter, setter) + .apply { builder() })) + @KPrefMarker val list: MutableList = mutableListOf() } \ 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 315d67b..1950589 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 @@ -14,7 +14,7 @@ import ca.allanwang.kau.kpref.activity.R * ColorPicker preference * When a color is successfully selected in the dialog, it will be saved as an int */ -open class KPrefColorPicker(val builder: KPrefColorContract) : KPrefItemBase(builder) { +open class KPrefColorPicker(open val builder: KPrefColorContract) : KPrefItemBase(builder) { override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { super.onPostBindView(viewHolder, textColor, accentColor) diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefPlainText.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefPlainText.kt index b83df69..d2e49d8 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefPlainText.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefPlainText.kt @@ -12,7 +12,7 @@ import ca.allanwang.kau.kpref.activity.R * and when the preference is completely handled by the click * */ -open class KPrefPlainText(val builder: KPrefPlainTextBuilder) : KPrefItemBase(builder) { +open class KPrefPlainText(open val builder: KPrefPlainTextBuilder) : KPrefItemBase(builder) { override fun defaultOnClick(itemView: View, innerContent: View?): Boolean { //nothing diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSubItems.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSubItems.kt index 1bf0ffc..7c2979c 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSubItems.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSubItems.kt @@ -12,7 +12,7 @@ import ca.allanwang.kau.kpref.activity.R * When clicked, will navigate to a new set of preferences and add the old list to a stack * */ -open class KPrefSubItems(val builder: KPrefSubItemsContract) : KPrefItemCore(builder) { +open class KPrefSubItems(open val builder: KPrefSubItemsContract) : KPrefItemCore(builder) { override fun onClick(itemView: View, innerContent: View?): Boolean { builder.globalOptions.showNextPrefs(builder.titleRes, builder.itemBuilder) 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 45c9a5d..29dd007 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 @@ -14,7 +14,7 @@ import ca.allanwang.kau.utils.toast * This is still a generic preference * */ -open class KPrefText(val builder: KPrefTextContract) : KPrefItemBase(builder) { +open class KPrefText(open val builder: KPrefTextContract) : KPrefItemBase(builder) { /** * Automatically reload on set 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 new file mode 100644 index 0000000..d4f854b --- /dev/null +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefTimePicker.kt @@ -0,0 +1,64 @@ +package ca.allanwang.kau.kpref.activity.items + +import android.app.TimePickerDialog +import android.view.View +import android.widget.TimePicker +import ca.allanwang.kau.kpref.activity.GlobalOptions +import ca.allanwang.kau.kpref.activity.R +import java.util.* + +/** + * Created by Allan Wang on 2017-06-14. + * + * Text preference + * Holds a textview to display data on the right + * This is still a generic preference + * + */ +open class KPrefTimePicker(override val builder: KPrefTimeContract) : KPrefText(builder) { + + interface KPrefTimeContract : KPrefText.KPrefTextContract { + var use24HourFormat: Boolean + } + + /** + * Default implementation of [KPrefTimeContract] + */ + class KPrefTimeBuilder( + globalOptions: GlobalOptions, + titleRes: Int, + getter: () -> Int, + setter: (value: Int) -> Unit + ) : KPrefTimeContract, BaseContract by BaseBuilder(globalOptions, titleRes, getter, setter), TimePickerDialog.OnTimeSetListener { + + override var use24HourFormat: Boolean = false + + override fun onTimeSet(view: TimePicker, hourOfDay: Int, minute: Int) { + setter((hourOfDay to minute).mergeTime) + reloadSelf() + } + + override var textGetter: (Int) -> String? = { + val (hour, min) = it.splitTime + if (use24HourFormat) + String.format(Locale.CANADA, "%d:%02d", hour, min) + else + String.format(Locale.CANADA, "%d:%02d %s", hour % 12, min, if (hour >= 12) "PM" else "AM") + } + + override var onClick: ((itemView: View, innerContent: View?, item: KPrefItemBase) -> Boolean)? = { itemView, _, item -> + val (hour, min) = item.pref.splitTime + TimePickerDialog(itemView.context, this, hour, min, use24HourFormat).show() + true + } + + private val Int.splitTime: Pair + get() = Pair(this / 100, this % 100) + + private val Pair.mergeTime: Int + get() = first * 100 + second + } + + override fun getType(): Int = R.id.kau_item_pref_time_picker + +} \ No newline at end of file -- cgit v1.2.3