From b51ce53886415bb4a6b320a3a53eee587ec8c288 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 25 Jun 2017 00:05:05 -0700 Subject: Add annotations --- .../kau/dialogs/color/ColorPickerDialog.kt | 17 +++++--- .../ca/allanwang/kau/kotlin/LazyResettable.kt | 51 ++++++++++++++++++++++ .../kau/kotlin/NonReadablePropertyException.kt | 12 +++++ .../kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt | 13 +++++- .../ca/allanwang/kau/kpref/items/KPrefCheckbox.kt | 1 + .../allanwang/kau/kpref/items/KPrefColorPicker.kt | 1 + .../ca/allanwang/kau/kpref/items/KPrefHeader.kt | 1 + .../ca/allanwang/kau/kpref/items/KPrefItemBase.kt | 1 - .../ca/allanwang/kau/kpref/items/KPrefItemCore.kt | 8 +++- .../ca/allanwang/kau/searchview/SearchView.kt | 23 ++++++---- .../ca/allanwang/kau/utils/LazyResettable.kt | 51 ---------------------- library/src/main/res/layout/kau_search_view.xml | 18 +------- 12 files changed, 109 insertions(+), 88 deletions(-) create mode 100644 library/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt create mode 100644 library/src/main/kotlin/ca/allanwang/kau/kotlin/NonReadablePropertyException.kt delete mode 100644 library/src/main/kotlin/ca/allanwang/kau/utils/LazyResettable.kt (limited to 'library/src') diff --git a/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerDialog.kt b/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerDialog.kt index 53d94ef..07f5e17 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerDialog.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerDialog.kt @@ -2,6 +2,8 @@ package ca.allanwang.kau.dialogs.color import android.content.Context import android.graphics.Color +import android.support.annotation.DimenRes +import android.support.annotation.StringRes import ca.allanwang.kau.R import ca.allanwang.kau.utils.string import com.afollestad.materialdialogs.MaterialDialog @@ -30,18 +32,19 @@ class ColorBuilder : ColorContract { interface ColorContract { var title: String? - var titleRes: Int + var titleRes: Int @StringRes set var allowCustom: Boolean var allowCustomAlpha: Boolean var isAccent: Boolean - var defaultColor: Int - var doneText: Int - var backText: Int - var cancelText: Int + var defaultColor: Int @StringRes set + var doneText: Int @StringRes set + var backText: Int @StringRes set + var cancelText: Int @StringRes set var presetText: Int - var customText: Int + @StringRes set + var customText: Int @StringRes set var dynamicButtonColors: Boolean - var circleSizeRes: Int + var circleSizeRes: Int @DimenRes set var colorCallback: ((selectedColor: Int) -> Unit)? var colorsTop: IntArray? var colorsSub: Array? diff --git a/library/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt b/library/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt new file mode 100644 index 0000000..b74c5c7 --- /dev/null +++ b/library/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt @@ -0,0 +1,51 @@ +package ca.allanwang.kau.kotlin + +import java.io.Serializable +import kotlin.reflect.KProperty + +/** + * Created by Allan Wang on 2017-05-30. + * + * Lazy delegate that can be invalidated if needed + * https://stackoverflow.com/a/37294840/4407321 + */ +private object UNINITIALIZED + +fun lazyResettable(initializer: () -> T): LazyResettable = LazyResettable(initializer) + +class LazyResettable(private val initializer: () -> T, lock: Any? = null) : Lazy, Serializable { + @Volatile private var _value: Any = UNINITIALIZED + private val lock = lock ?: this + + fun invalidate() { + _value = UNINITIALIZED + } + + override val value: T + get() { + val _v1 = _value + if (_v1 !== UNINITIALIZED) + @Suppress("UNCHECKED_CAST") + return _v1 as T + + return synchronized(lock) { + val _v2 = _value + if (_v2 !== UNINITIALIZED) { + @Suppress("UNCHECKED_CAST") + _v2 as T + } else { + val typedValue = initializer() + _value = typedValue + typedValue + } + } + } + + override fun isInitialized(): Boolean = _value !== UNINITIALIZED + + override fun toString(): String = if (isInitialized()) value.toString() else "Lazy value not initialized yet." + + operator fun setValue(any: Any, property: KProperty<*>, t: T) { + _value = t + } +} \ No newline at end of file diff --git a/library/src/main/kotlin/ca/allanwang/kau/kotlin/NonReadablePropertyException.kt b/library/src/main/kotlin/ca/allanwang/kau/kotlin/NonReadablePropertyException.kt new file mode 100644 index 0000000..f3add48 --- /dev/null +++ b/library/src/main/kotlin/ca/allanwang/kau/kotlin/NonReadablePropertyException.kt @@ -0,0 +1,12 @@ +package ca.allanwang.kau.kotlin + +/** + * Created by Allan Wang on 2017-06-24. + * + * Credits to @zsmb13 + * + * https://github.com/zsmb13/MaterialDrawerKt/blob/master/library/src/main/java/co/zsmb/materialdrawerkt/NonReadablePropertyException.kt + */ +class NonReadablePropertyException : Exception() + +fun nonReadable(): Nothing = throw NonReadablePropertyException() \ No newline at end of file diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt index 723af45..5fdb6bc 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt @@ -34,9 +34,13 @@ fun RecyclerView.setKPrefAdapter(globalOptions: GlobalOptions, builder: KPrefAda return adapter } +@DslMarker +annotation class KPrefMarker + /** * Contains attributes shared amongst all kpref items */ +@KPrefMarker interface CoreAttributeContract { var textColor: (() -> Int)? var accentColor: (() -> Int)? @@ -67,11 +71,14 @@ class GlobalOptions(core: CoreAttributeContract, activity: KPrefActivityContract * The arguments are all the mandatory values plus an optional builder housing all the possible configurations * The mandatory values are final so they cannot be edited in the builder */ +@KPrefMarker class KPrefAdapterBuilder(internal val globalOptions: GlobalOptions) { + @KPrefMarker fun header(@StringRes title: Int) = list.add(KPrefHeader(KPrefItemCore.CoreBuilder(globalOptions, title))) + @KPrefMarker fun checkbox(@StringRes title: Int, getter: (() -> Boolean), setter: ((value: Boolean) -> Unit), @@ -79,7 +86,7 @@ class KPrefAdapterBuilder(internal val globalOptions: GlobalOptions) { = list.add(KPrefCheckbox(KPrefItemBase.BaseBuilder(globalOptions, title, getter, setter) .apply { builder() })) - + @KPrefMarker fun colorPicker(@StringRes title: Int, getter: (() -> Int), setter: ((value: Int) -> Unit), @@ -87,6 +94,7 @@ class KPrefAdapterBuilder(internal val globalOptions: GlobalOptions) { = list.add(KPrefColorPicker(KPrefColorPicker.KPrefColorBuilder(globalOptions, title, getter, setter) .apply { builder() })) + @KPrefMarker fun text(@StringRes title: Int, getter: (() -> T), setter: ((value: T) -> Unit), @@ -94,16 +102,19 @@ class KPrefAdapterBuilder(internal val globalOptions: GlobalOptions) { = list.add(KPrefText(KPrefText.KPrefTextBuilder(globalOptions, title, getter, setter) .apply { builder() })) + @KPrefMarker fun subItems(@StringRes title: Int, itemBuilder: KPrefAdapterBuilder.() -> Unit, builder: KPrefSubItems.KPrefSubItemsContract.() -> Unit) = list.add(KPrefSubItems(KPrefSubItems.KPrefSubItemsBuilder(globalOptions, title, itemBuilder) .apply { builder() })) + @KPrefMarker fun plainText(@StringRes title: Int, builder: KPrefItemBase.BaseContract.() -> Unit = {}) = list.add(KPrefPlainText(KPrefPlainText.KPrefPlainTextBuilder(globalOptions, title) .apply { builder() })) + @KPrefMarker internal val list: MutableList = mutableListOf() } \ No newline at end of file diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt index 4af837b..22cc927 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt @@ -3,6 +3,7 @@ package ca.allanwang.kau.kpref.items import android.view.View import android.widget.CheckBox import ca.allanwang.kau.R +import ca.allanwang.kau.kpref.KPrefMarker import ca.allanwang.kau.utils.tint /** diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt index b22c4b3..c573939 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt @@ -8,6 +8,7 @@ import ca.allanwang.kau.dialogs.color.ColorContract import ca.allanwang.kau.dialogs.color.colorPickerDialog import ca.allanwang.kau.kpref.CoreAttributeContract import ca.allanwang.kau.kpref.GlobalOptions +import ca.allanwang.kau.kpref.KPrefMarker /** * Created by Allan Wang on 2017-06-07. diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt index c50556d..fa8efff 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt @@ -2,6 +2,7 @@ package ca.allanwang.kau.kpref.items import android.view.View import ca.allanwang.kau.R +import ca.allanwang.kau.kpref.KPrefMarker /** * Created by Allan Wang on 2017-06-07. diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt index a0718a0..bb0f0a3 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt @@ -12,7 +12,6 @@ import ca.allanwang.kau.utils.resolveDrawable * * Base class for pref setters that include the Shared Preference hooks */ - abstract class KPrefItemBase(val base: BaseContract) : KPrefItemCore(base) { open var pref: T diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt index 9c0ad79..7d0f9f7 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt @@ -3,6 +3,7 @@ package ca.allanwang.kau.kpref.items import android.support.annotation.CallSuper import android.support.annotation.IdRes import android.support.annotation.LayoutRes +import android.support.annotation.StringRes import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.View @@ -12,6 +13,7 @@ import android.widget.LinearLayout import android.widget.TextView import ca.allanwang.kau.R import ca.allanwang.kau.kpref.GlobalOptions +import ca.allanwang.kau.kpref.KPrefMarker import ca.allanwang.kau.utils.* import com.mikepenz.fastadapter.items.AbstractItem import com.mikepenz.iconics.typeface.IIcon @@ -69,10 +71,12 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem private val progress: ProgressBar by bindView(R.id.search_progress) - private val iconMic: ImageView by bindView(R.id.search_mic) private val iconClear: ImageView by bindView(R.id.search_clear) private val divider: View by bindView(R.id.search_divider) private val recycler: RecyclerView by bindView(R.id.search_recycler) @@ -158,7 +165,6 @@ class SearchView @JvmOverloads constructor( init { View.inflate(context, R.layout.kau_search_view, this) iconNav.setSearchIcon(configs.navIcon) - iconMic.setSearchIcon(configs.micIcon) iconClear.setSearchIcon(configs.clearIcon).setOnClickListener { editText.text.clear() } @@ -239,7 +245,6 @@ class SearchView @JvmOverloads constructor( fun tintForeground(@ColorInt color: Int) { iconNav.drawable.setTint(color) - iconMic.drawable.setTint(color) iconClear.drawable.setTint(color) SearchItem.foregroundColor = color divider.setBackgroundColor(color.adjustAlpha(0.1f)) diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/LazyResettable.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/LazyResettable.kt deleted file mode 100644 index 6e7e43e..0000000 --- a/library/src/main/kotlin/ca/allanwang/kau/utils/LazyResettable.kt +++ /dev/null @@ -1,51 +0,0 @@ -package ca.allanwang.kau.utils - -import java.io.Serializable -import kotlin.reflect.KProperty - -/** - * Created by Allan Wang on 2017-05-30. - * - * Lazy delegate that can be invalidated if needed - * https://stackoverflow.com/a/37294840/4407321 - */ -private object UNINITIALIZED - -fun lazyResettable(initializer: () -> T): LazyResettable = LazyResettable(initializer) - -class LazyResettable(private val initializer: () -> T, lock: Any? = null) : Lazy, Serializable { - @Volatile private var _value: Any = UNINITIALIZED - private val lock = lock ?: this - - fun invalidate() { - _value = UNINITIALIZED - } - - override val value: T - get() { - val _v1 = _value - if (_v1 !== UNINITIALIZED) - @Suppress("UNCHECKED_CAST") - return _v1 as T - - return synchronized(lock) { - val _v2 = _value - if (_v2 !== UNINITIALIZED) { - @Suppress("UNCHECKED_CAST") - _v2 as T - } else { - val typedValue = initializer() - _value = typedValue - typedValue - } - } - } - - override fun isInitialized(): Boolean = _value !== UNINITIALIZED - - override fun toString(): String = if (isInitialized()) value.toString() else "Lazy value not initialized yet." - - operator fun setValue(any: Any, property: KProperty<*>, t: T) { - _value = t - } -} \ No newline at end of file diff --git a/library/src/main/res/layout/kau_search_view.xml b/library/src/main/res/layout/kau_search_view.xml index 5c2ad27..ddde753 100644 --- a/library/src/main/res/layout/kau_search_view.xml +++ b/library/src/main/res/layout/kau_search_view.xml @@ -68,24 +68,10 @@ android:layout_height="match_parent" android:visibility="gone" /> - - - - -