From 56f423dfe0ad9605907d8ba4d13482378c208ba3 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 17 Jun 2017 12:09:28 -0700 Subject: Add more readme --- .../kotlin/ca/allanwang/kau/utils/Kotterknife.kt | 35 ++++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'library') diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt index fca5677..247bbc7 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt @@ -20,69 +20,97 @@ import android.support.v4.app.Fragment as SupportFragment fun View.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) + fun Activity.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) + fun Dialog.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) + fun DialogFragment.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) + fun android.support.v4.app.DialogFragment.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) + fun Fragment.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) + fun android.support.v4.app.Fragment.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) + fun ViewHolder.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) fun View.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) + fun Activity.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) + fun Dialog.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) + fun DialogFragment.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) + fun android.support.v4.app.DialogFragment.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) + fun Fragment.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) + fun android.support.v4.app.Fragment.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) + fun ViewHolder.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) fun View.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) + fun Activity.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) + fun Dialog.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) + fun DialogFragment.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) + fun android.support.v4.app.DialogFragment.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) + fun Fragment.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) + fun android.support.v4.app.Fragment.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) + fun ViewHolder.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) fun View.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) + fun Activity.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) + fun Dialog.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) + fun DialogFragment.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) + fun android.support.v4.app.DialogFragment.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) + fun Fragment.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) + fun android.support.v4.app.Fragment.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) + fun ViewHolder.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) @@ -103,16 +131,16 @@ private val android.support.v4.app.Fragment.viewFinder: android.support.v4.app.F private val ViewHolder.viewFinder: ViewHolder.(Int) -> View? get() = { itemView.findViewById(it) } -private fun viewNotFound(id:Int, desc: KProperty<*>): Nothing = +private fun viewNotFound(id: Int, desc: KProperty<*>): Nothing = throw IllegalStateException("View ID $id for '${desc.name}' not found.") @Suppress("UNCHECKED_CAST") private fun required(id: Int, finder: T.(Int) -> View?) - = Lazy { t: T, desc -> (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc) } + = Lazy { t: T, desc -> (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc) } @Suppress("UNCHECKED_CAST") private fun optional(id: Int, finder: T.(Int) -> View?) - = Lazy { t: T, _ -> t.finder(id) as V? } + = Lazy { t: T, _ -> t.finder(id) as V? } @Suppress("UNCHECKED_CAST") private fun required(ids: IntArray, finder: T.(Int) -> View?) @@ -125,6 +153,7 @@ private fun optional(ids: IntArray, finder: T.(Int) -> View?) // Like Kotlin's lazy delegate but the initializer gets the target and metadata passed to it private class Lazy(private val initializer: (T, KProperty<*>) -> V) : ReadOnlyProperty { private object EMPTY + private var value: Any? = EMPTY override fun getValue(thisRef: T, property: KProperty<*>): V { -- cgit v1.2.3