aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-01-01 23:11:23 -0500
committerAllan Wang <me@allanwang.ca>2018-01-01 23:11:23 -0500
commitf94d6f9694973c2a323e565794d948002593df0a (patch)
treea4fd25f982e91ec4cb39e317fb3fd1e6b50674bf /core/src/main/kotlin/ca
parent332d633ce32eb2d686a6342e39c0f7d4cb31edc0 (diff)
downloadkau-f94d6f9694973c2a323e565794d948002593df0a.tar.gz
kau-f94d6f9694973c2a323e565794d948002593df0a.tar.bz2
kau-f94d6f9694973c2a323e565794d948002593df0a.zip
Allow usage of reified generics instead of passing class
Diffstat (limited to 'core/src/main/kotlin/ca')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt16
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt31
2 files changed, 34 insertions, 13 deletions
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<out Activity>,
+inline fun <reified T : Activity> 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<T>(requestCode, bundleBuilder, intentBuilder)"),
+ DeprecationLevel.WARNING)
+inline fun <reified T : Activity> Activity.startActivityForResult(
+ clazz: Class<T>,
+ requestCode: Int,
+ bundleBuilder: Bundle.() -> Unit = {},
+ intentBuilder: Intent.() -> Unit = {}) {
+ startActivityForResult<T>(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<out Activity>,
+inline fun <reified T : Activity> 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<T>(clearStack, bundleBuilder, intentBuilder)"),
+ DeprecationLevel.WARNING)
+inline fun <reified T : Activity> Context.startActivity(
+ clazz: Class<T>,
+ clearStack: Boolean = false,
+ bundleBuilder: Bundle.() -> Unit = {},
+ intentBuilder: Intent.() -> Unit = {}) {
+ startActivity<T>(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 {