aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt25
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/BundleUtils.kt46
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt40
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/FileUtils.kt1
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt10
5 files changed, 65 insertions, 57 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 0e418ec..90961c5 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt
@@ -2,7 +2,6 @@ package ca.allanwang.kau.utils
import android.annotation.SuppressLint
import android.app.Activity
-import android.app.ActivityOptions
import android.content.Context
import android.content.Intent
import android.graphics.Color
@@ -20,35 +19,35 @@ import org.jetbrains.anko.contentView
* Created by Allan Wang on 2017-06-21.
*/
+@DslMarker
+@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE, AnnotationTarget.FUNCTION)
+annotation class KauActivity
+
/**
* Helper class to launch an activity for result
* Counterpart of [Activity.startActivityForResult]
* For starting activities without result, see [startActivity]
*/
-@SuppressLint("NewApi")
-fun Activity.startActivityForResult(
+inline fun Activity.startActivityForResult(
clazz: Class<out Activity>,
requestCode: Int,
- transition: Boolean = false,
- bundle: Bundle? = null,
+ bundleBuilder: Bundle.() -> Unit = {},
intentBuilder: Intent.() -> Unit = {}) {
val intent = Intent(this, clazz)
- val fullBundle = Bundle()
- if (transition && buildIsLollipopAndUp)
- fullBundle.with(ActivityOptions.makeSceneTransitionAnimation(this).toBundle())
- if (bundle != null) fullBundle.putAll(bundle)
intent.intentBuilder()
- startActivityForResult(intent, requestCode, if (fullBundle.isEmpty) null else fullBundle)
+ val bundle = Bundle()
+ bundle.bundleBuilder()
+ startActivityForResult(intent, requestCode, bundle)
}
/**
* Restarts an activity from itself with a fade animation
- * Keeps its existing extra bundles and has a builder to accept other parameters
+ * Keeps its existing extra bundles and has a intentBuilder to accept other parameters
*/
-fun Activity.restart(builder: Intent.() -> Unit = {}) {
+inline fun Activity.restart(intentBuilder: Intent.() -> Unit = {}) {
val i = Intent(this, this::class.java)
i.putExtras(intent.extras)
- i.builder()
+ i.intentBuilder()
startActivity(i)
overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_fade_out) //No transitions
finish()
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/BundleUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/BundleUtils.kt
new file mode 100644
index 0000000..98fceaf
--- /dev/null
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/BundleUtils.kt
@@ -0,0 +1,46 @@
+package ca.allanwang.kau.utils
+
+import android.annotation.SuppressLint
+import android.app.Activity
+import android.app.ActivityOptions
+import android.content.Context
+import android.os.Bundle
+import android.support.annotation.AnimRes
+import android.support.v4.app.ActivityOptionsCompat
+import ca.allanwang.kau.R
+
+/**
+ * Created by Allan Wang on 10/12/17.
+ */
+/**
+ * Similar to [Bundle.putAll], but checks for a null insert and returns the parent bundle
+ */
+infix fun Bundle.with(bundle: Bundle?): Bundle {
+ if (bundle != null) putAll(bundle)
+ return this
+}
+
+/**
+ * Adds transition bundle if context is activity and build is lollipop+
+ */
+@SuppressLint("NewApi")
+fun Bundle.withSceneTransitionAnimation(context: Context) {
+ if (context !is Activity || !buildIsLollipopAndUp) return
+ this with ActivityOptions.makeSceneTransitionAnimation(context).toBundle()
+}
+
+fun Bundle.withCustomAnimation(context: Context,
+ @AnimRes enterResId: Int,
+ @AnimRes exitResId: Int) {
+ this with ActivityOptionsCompat.makeCustomAnimation(context,
+ enterResId, exitResId).toBundle()
+}
+
+fun Bundle.withSlideIn(context: Context) = withCustomAnimation(context,
+ R.anim.kau_slide_in_right, R.anim.kau_fade_out)
+
+fun Bundle.withSlideOut(context: Context) = withCustomAnimation(context,
+ R.anim.kau_fade_in, R.anim.kau_slide_out_right_top)
+
+fun Bundle.withFade(context: Context) = withCustomAnimation(context,
+ android.R.anim.fade_in, android.R.anim.fade_out) \ 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 545a3f0..06b9601 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
@@ -2,9 +2,7 @@
package ca.allanwang.kau.utils
-import android.annotation.SuppressLint
import android.app.Activity
-import android.app.ActivityOptions
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
@@ -14,7 +12,6 @@ import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.support.annotation.*
-import android.support.v4.app.ActivityOptionsCompat
import android.support.v4.content.ContextCompat
import android.util.TypedValue
import android.view.View
@@ -34,47 +31,20 @@ import com.afollestad.materialdialogs.MaterialDialog
* Counterpart of [ContextCompat.startActivity]
* For starting activities for results, see [startActivityForResult]
*/
-@SuppressLint("NewApi")
-fun Context.startActivity(
+inline fun Context.startActivity(
clazz: Class<out Activity>,
clearStack: Boolean = false,
- transition: Boolean = false,
- bundle: Bundle? = null,
+ bundleBuilder: Bundle.() -> Unit = {},
intentBuilder: Intent.() -> Unit = {}) {
val intent = Intent(this, clazz)
if (clearStack) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
- val fullBundle = Bundle()
- if (transition && this is Activity && buildIsLollipopAndUp)
- fullBundle.with(ActivityOptions.makeSceneTransitionAnimation(this).toBundle())
- if (transition && this !is Activity) KL.d("Cannot make scene transition when context is not an instance of an Activity")
- if (bundle != null) fullBundle.putAll(bundle)
intent.intentBuilder()
- ContextCompat.startActivity(this, intent, if (fullBundle.isEmpty) null else fullBundle)
+ val bundle = Bundle()
+ bundle.bundleBuilder()
+ ContextCompat.startActivity(this, intent, bundle)
if (this is Activity && clearStack) finish()
}
-/**
- * Bring in activity from the right
- */
-fun Context.startActivitySlideIn(clazz: Class<out Activity>, clearStack: Boolean = false, intentBuilder: Intent.() -> Unit = {}, bundleBuilder: Bundle.() -> Unit = {}) {
- val fullBundle = Bundle()
- fullBundle.with(ActivityOptionsCompat.makeCustomAnimation(this, R.anim.kau_slide_in_right, R.anim.kau_fade_out).toBundle())
- fullBundle.bundleBuilder()
- startActivity(clazz, clearStack, intentBuilder = intentBuilder, bundle = if (fullBundle.isEmpty) null else fullBundle)
-}
-
-/**
- * Bring in activity from behind while pushing the current activity to the right
- * This replicates the exit animation of a sliding activity, but is a forward creation
- * For the animation to work, the previous activity should not be in the stack (otherwise you wouldn't need this in the first place)
- * Consequently, the stack will be cleared by default
- */
-fun Context.startActivitySlideOut(clazz: Class<out Activity>, clearStack: Boolean = true, intentBuilder: Intent.() -> Unit = {}, bundleBuilder: Bundle.() -> Unit = {}) {
- val fullBundle = Bundle()
- fullBundle.with(ActivityOptionsCompat.makeCustomAnimation(this, R.anim.kau_fade_in, R.anim.kau_slide_out_right_top).toBundle())
- fullBundle.bundleBuilder()
- startActivity(clazz, clearStack, intentBuilder = intentBuilder, bundle = if (fullBundle.isEmpty) null else fullBundle)
-}
fun Context.startPlayStoreLink(@StringRes packageIdRes: Int) = startPlayStoreLink(string(packageIdRes))
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/FileUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/FileUtils.kt
index b97f4aa..65c28f7 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/FileUtils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/FileUtils.kt
@@ -1,5 +1,6 @@
package ca.allanwang.kau.utils
+import android.os.Bundle
import java.io.File
import java.io.InputStream
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
index 954bedc..8b32894 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
@@ -126,12 +126,4 @@ class KauException(message: String) : RuntimeException(message)
fun String.withMaxLength(n: Int): String =
if (length <= n) this
- else substring(0, n - 1) + KAU_ELLIPSIS
-
-/**
- * Similar to [Bundle.putAll], but checks for a null insert and returns the parent bundle
- */
-fun Bundle.with(bundle: Bundle?): Bundle {
- if (bundle != null) putAll(bundle)
- return this
-} \ No newline at end of file
+ else substring(0, n - 1) + KAU_ELLIPSIS \ No newline at end of file