From f94d6f9694973c2a323e565794d948002593df0a Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 1 Jan 2018 23:11:23 -0500 Subject: Allow usage of reified generics instead of passing class --- .../kotlin/ca/allanwang/kau/utils/ActivityUtils.kt | 16 ++++++++--- .../kotlin/ca/allanwang/kau/utils/ContextUtils.kt | 31 +++++++++++++++------- 2 files changed, 34 insertions(+), 13 deletions(-) (limited to 'core/src') diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt index 90961c5..3484353 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt @@ -28,18 +28,28 @@ annotation class KauActivity * Counterpart of [Activity.startActivityForResult] * For starting activities without result, see [startActivity] */ -inline fun Activity.startActivityForResult( - clazz: Class, +inline fun Activity.startActivityForResult( requestCode: Int, bundleBuilder: Bundle.() -> Unit = {}, intentBuilder: Intent.() -> Unit = {}) { - val intent = Intent(this, clazz) + val intent = Intent(this, T::class.java) intent.intentBuilder() val bundle = Bundle() bundle.bundleBuilder() startActivityForResult(intent, requestCode, bundle) } +@Deprecated("Use reified generic instead of passing class", + ReplaceWith("startActivityForResult(requestCode, bundleBuilder, intentBuilder)"), + DeprecationLevel.WARNING) +inline fun Activity.startActivityForResult( + clazz: Class, + requestCode: Int, + bundleBuilder: Bundle.() -> Unit = {}, + intentBuilder: Intent.() -> Unit = {}) { + startActivityForResult(requestCode, bundleBuilder, intentBuilder) +} + /** * Restarts an activity from itself with a fade animation * Keeps its existing extra bundles and has a intentBuilder to accept other parameters 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 1b8d58c..db54282 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt @@ -28,21 +28,32 @@ import com.afollestad.materialdialogs.MaterialDialog /** * Helper class to launch an activity from a context - * Counterpart of [ContextCompat.startActivity] + * Counterpart of [Context.startActivity] * For starting activities for results, see [startActivityForResult] */ -inline fun Context.startActivity( - clazz: Class, +inline fun Context.startActivity( clearStack: Boolean = false, bundleBuilder: Bundle.() -> Unit = {}, - intentBuilder: Intent.() -> Unit = {}) { - val intent = Intent(this, clazz) + intentBuilder: Intent.() -> Unit = {} +) { + val intent = Intent(this, T::class.java) if (clearStack) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK) intent.intentBuilder() val bundle = Bundle() bundle.bundleBuilder() - ContextCompat.startActivity(this, intent, bundle) - if (this is Activity && clearStack) finish() + startActivity(intent, bundle) + if (clearStack && this is Activity) finish() +} + +@Deprecated("Use reified generic instead of passing class", + ReplaceWith("startActivity(clearStack, bundleBuilder, intentBuilder)"), + DeprecationLevel.WARNING) +inline fun Context.startActivity( + clazz: Class, + clearStack: Boolean = false, + bundleBuilder: Bundle.() -> Unit = {}, + intentBuilder: Intent.() -> Unit = {}) { + startActivity(clearStack, bundleBuilder, intentBuilder) } @@ -93,13 +104,13 @@ 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.interpolator(@InterpolatorRes id: Int) = AnimationUtils.loadInterpolator(this, id) -inline fun Context.animation(@AnimRes id: Int) = AnimationUtils.loadAnimation(this, id) +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()) + = resources.getQuantityString(id, quantity.toInt(), quantity.toInt())!! //Attr retrievers fun Context.resolveColor(@AttrRes attr: Int, fallback: Int = 0): Int { -- cgit v1.2.3