From a429a5e0dbc96ae67bc68e27527239a43390e3f1 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 12 Jun 2017 16:24:29 -0700 Subject: Add fragment bundler --- README.md | 4 ++-- .../main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt | 5 +++-- .../main/kotlin/ca/allanwang/kau/utils/FragmentUtils.kt | 17 +++-------------- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 17145d5..3aff56d 100644 --- a/README.md +++ b/README.md @@ -152,11 +152,11 @@ The canvas also supports color fading and direct color setting so it can effecti ### FragmentUtils > Extends Fragment -* Directly put extras into a fragment; if a bundle does not exist, it will be created +* `withBundle` Directly put extras into a fragment; if a bundle does not exist, it will be created ### IIconUtils > Extends [IIcon](https://github.com/mikepenz/Android-Iconics) -* toDrawable method that only requires a context; defaults to a white icon of size 24dp and uses a ColorStateList to allow for dimming +* `toDrawable` method that only requires a context; defaults to a white icon of size 24dp and uses a ColorStateList to allow for dimming ### Utils [Misc] > Extends Int diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt index a856ed2..e2c1eb8 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt @@ -6,6 +6,7 @@ import android.content.Intent import android.graphics.Color import android.graphics.drawable.Drawable import android.net.ConnectivityManager +import android.os.Bundle import android.os.Handler import android.support.annotation.* import android.support.v4.content.ContextCompat @@ -30,11 +31,11 @@ fun Activity.restart(extras: ((Intent) -> Unit)? = null) { overridePendingTransition(0, 0) } -fun Context.startActivity(clazz: Class, clearStack: Boolean = false, intentBuilder: Intent.() -> Unit = {}) { +fun Context.startActivity(clazz: Class, clearStack: Boolean = false, intentBuilder: Intent.() -> Unit = {}, bundle: Bundle? = null) { val intent = (Intent(this, clazz)) if (clearStack) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) intent.intentBuilder() - startActivity(intent) + ContextCompat.startActivity(this, intent, bundle) if (this is Activity && clearStack) finish() } diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/FragmentUtils.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/FragmentUtils.kt index 2ef1232..f97b4d3 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/utils/FragmentUtils.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/utils/FragmentUtils.kt @@ -6,19 +6,8 @@ import android.support.v4.app.Fragment /** * Created by Allan Wang on 2017-05-29. */ - -private fun Fragment.bundle(): Bundle { - if (this.arguments == null) - this.arguments = Bundle() - return this.arguments -} - -fun T.putString(key: String, value: String): T { - this.bundle().putString(key, value) - return this -} - -fun T.putInt(key: String, value: Int): T { - this.bundle().putInt(key, value) +fun T.withBundle(builder: Bundle.() -> Unit = {}): T { + if (this.arguments == null) this.arguments = Bundle() + this.arguments.builder() return this } \ No newline at end of file -- cgit v1.2.3