From a0377be622f21b4c6a7d8828505c1e95efab1254 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 7 Apr 2018 20:23:17 -0400 Subject: Update/android studio 3.1 (#146) * Update dependencies * Add default invalid id value * Extend appcompat views only * Update migration * Remove setTextIfValid * Remove nosibling warning * Add deprecation warnings to fragment view binding * Bring back glide 4.6.1 * Use proper distribution type setter --- .../main/kotlin/ca/allanwang/kau/kotlin/Utils.kt | 6 ++ .../kotlin/ca/allanwang/kau/utils/ContextUtils.kt | 15 ++-- .../kotlin/ca/allanwang/kau/utils/Kotterknife.kt | 89 +++++++++++++++++----- .../kotlin/ca/allanwang/kau/utils/ViewUtils.kt | 5 -- 4 files changed, 86 insertions(+), 29 deletions(-) create mode 100644 core/src/main/kotlin/ca/allanwang/kau/kotlin/Utils.kt (limited to 'core') diff --git a/core/src/main/kotlin/ca/allanwang/kau/kotlin/Utils.kt b/core/src/main/kotlin/ca/allanwang/kau/kotlin/Utils.kt new file mode 100644 index 0000000..db28914 --- /dev/null +++ b/core/src/main/kotlin/ca/allanwang/kau/kotlin/Utils.kt @@ -0,0 +1,6 @@ +package ca.allanwang.kau.kotlin + +/** + * Created by Allan Wang on 07/04/18. + */ +inline fun javaClass(): Class = T::class.java \ No newline at end of file diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt index 67da3f9..fdee4d8 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt @@ -93,24 +93,29 @@ inline fun Context.toast(text: String, duration: Int = Toast.LENGTH_LONG, log: B if (log) KL.i { "Toast: $text" } } +const val INVALID_ID = 0 + //Resource retrievers inline fun Context.string(@StringRes id: Int): String = getString(id) -inline fun Context.string(@StringRes id: Int, fallback: String?): String? = if (id > 0) string(id) else fallback +inline fun Context.string(@StringRes id: Int, fallback: String?): String? = if (id != INVALID_ID) string(id) else fallback +inline fun Context.string(@StringRes id: Int, fallback: () -> String?): String? = if (id != INVALID_ID) string(id) else fallback() inline fun Context.color(@ColorRes id: Int): Int = ContextCompat.getColor(this, id) inline fun Context.boolean(@BoolRes id: Int): Boolean = resources.getBoolean(id) inline fun Context.integer(@IntegerRes id: Int): Int = resources.getInteger(id) inline fun Context.dimen(@DimenRes id: Int): Float = resources.getDimension(id) inline fun Context.dimenPixelSize(@DimenRes id: Int): Int = resources.getDimensionPixelSize(id) -inline fun Context.drawable(@DrawableRes id: Int): Drawable = ContextCompat.getDrawable(this, id) ?: throw KauException("Drawable with id $id not found") -inline fun Context.drawable(@DrawableRes id: Int, fallback: Drawable?): Drawable? = if (id > 0) drawable(id) else fallback +inline fun Context.drawable(@DrawableRes id: Int): Drawable = ContextCompat.getDrawable(this, id) + ?: throw KauException("Drawable with id $id not found") + +inline fun Context.drawable(@DrawableRes id: Int, fallback: Drawable?): Drawable? = if (id != INVALID_ID) drawable(id) else fallback +inline fun Context.drawable(@DrawableRes id: Int, fallback: () -> Drawable?): Drawable? = if (id != INVALID_ID) drawable(id) else fallback() inline fun Context.interpolator(@InterpolatorRes id: Int) = AnimationUtils.loadInterpolator(this, id)!! inline fun Context.animation(@AnimRes id: Int) = AnimationUtils.loadAnimation(this, id)!! /** * Returns plural form of res. The quantity is also passed to the formatter as an int */ -inline fun Context.plural(@PluralsRes id: Int, quantity: Number) - = resources.getQuantityString(id, quantity.toInt(), quantity.toInt())!! +inline fun Context.plural(@PluralsRes id: Int, quantity: Number) = resources.getQuantityString(id, quantity.toInt(), quantity.toInt())!! //Attr retrievers fun Context.resolveColor(@AttrRes attr: Int, fallback: Int = 0): Int { diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt index f3c08bd..b0448d1 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt @@ -30,15 +30,27 @@ fun Activity.bindView(id: Int) fun Dialog.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindViewResettable(id)"), + DeprecationLevel.WARNING) fun DialogFragment.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindViewResettable(id)"), + DeprecationLevel.WARNING) fun SupportDialogFragment.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindViewResettable(id)"), + DeprecationLevel.WARNING) fun Fragment.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindViewResettable(id)"), + DeprecationLevel.WARNING) fun SupportFragment.bindView(id: Int) : ReadOnlyProperty = required(id, viewFinder) @@ -54,15 +66,27 @@ fun Activity.bindOptionalView(id: Int) fun Dialog.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindOptionalViewResettable(id)"), + DeprecationLevel.WARNING) fun DialogFragment.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindOptionalViewResettable(id)"), + DeprecationLevel.WARNING) fun SupportDialogFragment.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindOptionalViewResettable(id)"), + DeprecationLevel.WARNING) fun Fragment.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindOptionalViewResettable(id)"), + DeprecationLevel.WARNING) fun SupportFragment.bindOptionalView(id: Int) : ReadOnlyProperty = optional(id, viewFinder) @@ -78,15 +102,27 @@ fun Activity.bindViews(vararg ids: Int) fun Dialog.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewsResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindViewsResettable(*ids)"), + DeprecationLevel.WARNING) fun DialogFragment.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewsResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindViewsResettable(*ids)"), + DeprecationLevel.WARNING) fun SupportDialogFragment.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewsResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindViewsResettable(*ids)"), + DeprecationLevel.WARNING) fun Fragment.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewsResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindViewsResettable(*ids)"), + DeprecationLevel.WARNING) fun SupportFragment.bindViews(vararg ids: Int) : ReadOnlyProperty> = required(ids, viewFinder) @@ -102,15 +138,27 @@ fun Activity.bindOptionalViews(vararg ids: Int) fun Dialog.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewsResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindOptionalViewsResettable(*ids)"), + DeprecationLevel.WARNING) fun DialogFragment.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewsResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindOptionalViewsResettable(*ids)"), + DeprecationLevel.WARNING) fun SupportDialogFragment.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewsResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindOptionalViewsResettable(*ids)"), + DeprecationLevel.WARNING) fun Fragment.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) +@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewsResettable and resetting using KotterKnife.reset(this)", + ReplaceWith("bindOptionalViewsResettable(*ids)"), + DeprecationLevel.WARNING) fun SupportFragment.bindOptionalViews(vararg ids: Int) : ReadOnlyProperty> = optional(ids, viewFinder) @@ -137,17 +185,19 @@ private inline val ViewHolder.viewFinder: ViewHolder.(Int) -> View? private fun viewNotFound(id: Int, desc: KProperty<*>): Nothing = throw IllegalStateException("View ID $id for '${desc.name}' not found.") -private fun required(id: Int, finder: T.(Int) -> View?) - = Lazy { t: T, desc -> (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc) } +private fun required(id: Int, finder: T.(Int) -> View?) = Lazy { t: T, desc -> + (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc) +} -private fun optional(id: Int, finder: T.(Int) -> View?) - = Lazy { t: T, _ -> t.finder(id) as V? } +private fun optional(id: Int, finder: T.(Int) -> View?) = Lazy { t: T, _ -> t.finder(id) as V? } -private fun required(ids: IntArray, finder: T.(Int) -> View?) - = Lazy { t: T, desc -> ids.map { t.finder(it) as V? ?: viewNotFound(it, desc) } } +private fun required(ids: IntArray, finder: T.(Int) -> View?) = Lazy { t: T, desc -> + ids.map { + t.finder(it) as V? ?: viewNotFound(it, desc) + } +} -private fun optional(ids: IntArray, finder: T.(Int) -> View?) - = Lazy { t: T, _ -> ids.map { t.finder(it) as V? }.filterNotNull() } +private fun optional(ids: IntArray, finder: T.(Int) -> View?) = Lazy { t: T, _ -> ids.map { t.finder(it) as V? }.filterNotNull() } // Like Kotlin's lazy delegate but the initializer gets the target and metadata passed to it private open class Lazy(private val initializer: (T, KProperty<*>) -> V) : ReadOnlyProperty { @@ -269,17 +319,19 @@ fun SupportFragment.bindOptionalViewsResettable(vararg ids: Int) fun ViewHolder.bindOptionalViewsResettable(vararg ids: Int) : ReadOnlyProperty> = optionalResettable(ids, viewFinder) -private fun requiredResettable(id: Int, finder: T.(Int) -> View?) - = LazyResettable { t: T, desc -> (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc) } +private fun requiredResettable(id: Int, finder: T.(Int) -> View?) = LazyResettable { t: T, desc -> + (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc) +} -private fun optionalResettable(id: Int, finder: T.(Int) -> View?) - = LazyResettable { t: T, _ -> t.finder(id) as V? } +private fun optionalResettable(id: Int, finder: T.(Int) -> View?) = LazyResettable { t: T, _ -> t.finder(id) as V? } -private fun requiredResettable(ids: IntArray, finder: T.(Int) -> View?) - = LazyResettable { t: T, desc -> ids.map { t.finder(it) as V? ?: viewNotFound(it, desc) } } +private fun requiredResettable(ids: IntArray, finder: T.(Int) -> View?) = LazyResettable { t: T, desc -> + ids.map { + t.finder(it) as V? ?: viewNotFound(it, desc) + } +} -private fun optionalResettable(ids: IntArray, finder: T.(Int) -> View?) - = LazyResettable { t: T, _ -> ids.map { t.finder(it) as V? }.filterNotNull() } +private fun optionalResettable(ids: IntArray, finder: T.(Int) -> View?) = LazyResettable { t: T, _ -> ids.map { t.finder(it) as V? }.filterNotNull() } //Like Kotterknife's lazy delegate but is resettable private class LazyResettable(initializer: (T, KProperty<*>) -> V) : Lazy(initializer) { @@ -302,8 +354,7 @@ object Kotterknife { private object KotterknifeRegistry { private val lazyMap = WeakHashMap>>() - fun register(target: Any, lazy: LazyResettable<*, *>) - = lazyMap.getOrPut(target, { Collections.newSetFromMap(WeakHashMap()) }).add(lazy) + fun register(target: Any, lazy: LazyResettable<*, *>) = lazyMap.getOrPut(target, { Collections.newSetFromMap(WeakHashMap()) }).add(lazy) - fun reset(target: Any) = lazyMap[target]?.forEach { it.reset() } + fun reset(target: Any) = lazyMap[target]?.forEach(LazyResettable<*, *>::reset) } \ No newline at end of file diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt index 484e5bd..9a1e9b0 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt @@ -72,11 +72,6 @@ fun View.snackbar(text: String, duration: Int = Snackbar.LENGTH_LONG, builder: S fun View.snackbar(@StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {}) = snackbar(context.string(textId), duration, builder) -@KauUtils -fun TextView.setTextIfValid(@StringRes id: Int) { - if (id > 0) text = context.string(id) -} - @KauUtils fun ImageView.setIcon(icon: IIcon?, sizeDp: Int = 24, @ColorInt color: Int = Color.WHITE, builder: IconicsDrawable.() -> Unit = {}) { if (icon == null) return -- cgit v1.2.3