aboutsummaryrefslogtreecommitdiff
path: root/core/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kotlin/Utils.kt6
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt15
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt89
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt5
4 files changed, 86 insertions, 29 deletions
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 <reified T: Any> javaClass(): Class<T> = 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 <V : View> Activity.bindView(id: Int)
fun <V : View> Dialog.bindView(id: Int)
: ReadOnlyProperty<Dialog, V> = required(id, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindViewResettable<V>(id)"),
+ DeprecationLevel.WARNING)
fun <V : View> DialogFragment.bindView(id: Int)
: ReadOnlyProperty<DialogFragment, V> = required(id, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindViewResettable<V>(id)"),
+ DeprecationLevel.WARNING)
fun <V : View> SupportDialogFragment.bindView(id: Int)
: ReadOnlyProperty<android.support.v4.app.DialogFragment, V> = required(id, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindViewResettable<V>(id)"),
+ DeprecationLevel.WARNING)
fun <V : View> Fragment.bindView(id: Int)
: ReadOnlyProperty<Fragment, V> = required(id, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindViewResettable<V>(id)"),
+ DeprecationLevel.WARNING)
fun <V : View> SupportFragment.bindView(id: Int)
: ReadOnlyProperty<android.support.v4.app.Fragment, V> = required(id, viewFinder)
@@ -54,15 +66,27 @@ fun <V : View> Activity.bindOptionalView(id: Int)
fun <V : View> Dialog.bindOptionalView(id: Int)
: ReadOnlyProperty<Dialog, V?> = optional(id, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindOptionalViewResettable<V>(id)"),
+ DeprecationLevel.WARNING)
fun <V : View> DialogFragment.bindOptionalView(id: Int)
: ReadOnlyProperty<DialogFragment, V?> = optional(id, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindOptionalViewResettable<V>(id)"),
+ DeprecationLevel.WARNING)
fun <V : View> SupportDialogFragment.bindOptionalView(id: Int)
: ReadOnlyProperty<android.support.v4.app.DialogFragment, V?> = optional(id, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindOptionalViewResettable<V>(id)"),
+ DeprecationLevel.WARNING)
fun <V : View> Fragment.bindOptionalView(id: Int)
: ReadOnlyProperty<Fragment, V?> = optional(id, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindOptionalViewResettable<V>(id)"),
+ DeprecationLevel.WARNING)
fun <V : View> SupportFragment.bindOptionalView(id: Int)
: ReadOnlyProperty<android.support.v4.app.Fragment, V?> = optional(id, viewFinder)
@@ -78,15 +102,27 @@ fun <V : View> Activity.bindViews(vararg ids: Int)
fun <V : View> Dialog.bindViews(vararg ids: Int)
: ReadOnlyProperty<Dialog, List<V>> = required(ids, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewsResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindViewsResettable<V>(*ids)"),
+ DeprecationLevel.WARNING)
fun <V : View> DialogFragment.bindViews(vararg ids: Int)
: ReadOnlyProperty<DialogFragment, List<V>> = required(ids, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewsResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindViewsResettable<V>(*ids)"),
+ DeprecationLevel.WARNING)
fun <V : View> SupportDialogFragment.bindViews(vararg ids: Int)
: ReadOnlyProperty<android.support.v4.app.DialogFragment, List<V>> = required(ids, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewsResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindViewsResettable<V>(*ids)"),
+ DeprecationLevel.WARNING)
fun <V : View> Fragment.bindViews(vararg ids: Int)
: ReadOnlyProperty<Fragment, List<V>> = required(ids, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindViewsResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindViewsResettable<V>(*ids)"),
+ DeprecationLevel.WARNING)
fun <V : View> SupportFragment.bindViews(vararg ids: Int)
: ReadOnlyProperty<android.support.v4.app.Fragment, List<V>> = required(ids, viewFinder)
@@ -102,15 +138,27 @@ fun <V : View> Activity.bindOptionalViews(vararg ids: Int)
fun <V : View> Dialog.bindOptionalViews(vararg ids: Int)
: ReadOnlyProperty<Dialog, List<V>> = optional(ids, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewsResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindOptionalViewsResettable<V>(*ids)"),
+ DeprecationLevel.WARNING)
fun <V : View> DialogFragment.bindOptionalViews(vararg ids: Int)
: ReadOnlyProperty<DialogFragment, List<V>> = optional(ids, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewsResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindOptionalViewsResettable<V>(*ids)"),
+ DeprecationLevel.WARNING)
fun <V : View> SupportDialogFragment.bindOptionalViews(vararg ids: Int)
: ReadOnlyProperty<android.support.v4.app.DialogFragment, List<V>> = optional(ids, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewsResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindOptionalViewsResettable<V>(*ids)"),
+ DeprecationLevel.WARNING)
fun <V : View> Fragment.bindOptionalViews(vararg ids: Int)
: ReadOnlyProperty<Fragment, List<V>> = optional(ids, viewFinder)
+@Deprecated("Fragments operate on a different lifecycle. Consider using bindOptionalViewsResettable and resetting using KotterKnife.reset(this)",
+ ReplaceWith("bindOptionalViewsResettable<V>(*ids)"),
+ DeprecationLevel.WARNING)
fun <V : View> SupportFragment.bindOptionalViews(vararg ids: Int)
: ReadOnlyProperty<android.support.v4.app.Fragment, List<V>> = 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 <T, V : View> required(id: Int, finder: T.(Int) -> View?)
- = Lazy { t: T, desc -> (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc) }
+private fun <T, V : View> required(id: Int, finder: T.(Int) -> View?) = Lazy { t: T, desc ->
+ (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc)
+}
-private fun <T, V : View> optional(id: Int, finder: T.(Int) -> View?)
- = Lazy { t: T, _ -> t.finder(id) as V? }
+private fun <T, V : View> optional(id: Int, finder: T.(Int) -> View?) = Lazy { t: T, _ -> t.finder(id) as V? }
-private fun <T, V : View> required(ids: IntArray, finder: T.(Int) -> View?)
- = Lazy { t: T, desc -> ids.map { t.finder(it) as V? ?: viewNotFound(it, desc) } }
+private fun <T, V : View> required(ids: IntArray, finder: T.(Int) -> View?) = Lazy { t: T, desc ->
+ ids.map {
+ t.finder(it) as V? ?: viewNotFound(it, desc)
+ }
+}
-private fun <T, V : View> optional(ids: IntArray, finder: T.(Int) -> View?)
- = Lazy { t: T, _ -> ids.map { t.finder(it) as V? }.filterNotNull() }
+private fun <T, V : View> 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<in T, out V>(private val initializer: (T, KProperty<*>) -> V) : ReadOnlyProperty<T, V> {
@@ -269,17 +319,19 @@ fun <V : View> SupportFragment.bindOptionalViewsResettable(vararg ids: Int)
fun <V : View> ViewHolder.bindOptionalViewsResettable(vararg ids: Int)
: ReadOnlyProperty<ViewHolder, List<V>> = optionalResettable(ids, viewFinder)
-private fun <T, V : View> requiredResettable(id: Int, finder: T.(Int) -> View?)
- = LazyResettable { t: T, desc -> (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc) }
+private fun <T, V : View> requiredResettable(id: Int, finder: T.(Int) -> View?) = LazyResettable { t: T, desc ->
+ (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc)
+}
-private fun <T, V : View> optionalResettable(id: Int, finder: T.(Int) -> View?)
- = LazyResettable { t: T, _ -> t.finder(id) as V? }
+private fun <T, V : View> optionalResettable(id: Int, finder: T.(Int) -> View?) = LazyResettable { t: T, _ -> t.finder(id) as V? }
-private fun <T, V : View> requiredResettable(ids: IntArray, finder: T.(Int) -> View?)
- = LazyResettable { t: T, desc -> ids.map { t.finder(it) as V? ?: viewNotFound(it, desc) } }
+private fun <T, V : View> requiredResettable(ids: IntArray, finder: T.(Int) -> View?) = LazyResettable { t: T, desc ->
+ ids.map {
+ t.finder(it) as V? ?: viewNotFound(it, desc)
+ }
+}
-private fun <T, V : View> optionalResettable(ids: IntArray, finder: T.(Int) -> View?)
- = LazyResettable { t: T, _ -> ids.map { t.finder(it) as V? }.filterNotNull() }
+private fun <T, V : View> 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<in T, out V>(initializer: (T, KProperty<*>) -> V) : Lazy<T, V>(initializer) {
@@ -302,8 +354,7 @@ object Kotterknife {
private object KotterknifeRegistry {
private val lazyMap = WeakHashMap<Any, MutableCollection<LazyResettable<*, *>>>()
- 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
@@ -73,11 +73,6 @@ fun View.snackbar(@StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG,
= 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
setImageDrawable(icon.toDrawable(context, sizeDp = sizeDp, color = color, builder = builder))