Skip to content

Instantly share code, notes, and snippets.

@rlac
Created December 23, 2014 05:11
Show Gist options
  • Save rlac/5e56d6565334129aeaed to your computer and use it in GitHub Desktop.
Save rlac/5e56d6565334129aeaed to your computer and use it in GitHub Desktop.
import android.os.Bundle
import kotlin.properties.ReadOnlyProperty
import kotlin.properties.ReadWriteProperty
import android.app.Fragment
import android.support.v4.app.Fragment as SupportFragment
import kotlin.properties.Delegates
public fun bundledBoolean(bundle: Bundle, key: String, default: Boolean = false): ReadWriteProperty<Any, Boolean> = bundleVar({ bundle }, key, default)
public fun bundledString(bundle: Bundle, key: String, default: String = ""): ReadWriteProperty<Any, String> = bundleVar({ bundle }, key, default)
public fun bundledInt(bundle: Bundle, key: String, default: Int = 0): ReadWriteProperty<Any, Int> = bundleVar({ bundle }, key, default)
public fun Fragment.booleanArgument(key: String, default: Boolean = false): ReadOnlyProperty<Any, Boolean> = bundleVal({ this.getArguments() }, key, default)
public fun Fragment.stringArgument(key: String, default: String = ""): ReadOnlyProperty<Any, String> = bundleVal({ this.getArguments() }, key, default)
public fun Fragment.intArgument(key: String, default: Int = 0): ReadOnlyProperty<Any, Int> = bundleVal({ this.getArguments() }, key, default)
public fun SupportFragment.booleanArgument(key: String, default: Boolean = false): ReadOnlyProperty<Any, Boolean> = bundleVal({ this.getArguments() }, key, default)
public fun SupportFragment.stringArgument(key: String, default: String = ""): ReadOnlyProperty<Any, String> = bundleVal({ this.getArguments() }, key, default)
public fun SupportFragment.intArgument(key: String, default: Int = 0): ReadOnlyProperty<Any, Int> = bundleVal({ this.getArguments() }, key, default)
private fun bundleVal(b: () -> Bundle, k: String, d: Boolean): ReadOnlyProperty<Any, Boolean> = BundleVal(b, k, d, { getBoolean(k) })
private fun bundleVar(b: () -> Bundle, k: String, d: Boolean): ReadWriteProperty<Any, Boolean> = BundleVar(b, k, d, { getBoolean(k) }, { v -> putBoolean(k, v) })
private fun bundleVal(b: () -> Bundle, k: String, d: String): ReadOnlyProperty<Any, String> = BundleVal(b, k, d, { getString(k) })
private fun bundleVar(b: () -> Bundle, k: String, d: String): ReadWriteProperty<Any, String> = BundleVar(b, k, d, { getString(k) }, { v -> putString(k, v) })
private fun bundleVal(b: () -> Bundle, k: String, d: Int): ReadOnlyProperty<Any, Int> = BundleVal(b, k, d, { getInt(k) })
private fun bundleVar(b: () -> Bundle, k: String, d: Int): ReadWriteProperty<Any, Int> = BundleVar(b, k, d, { getInt(k) }, { v -> putInt(k, v) })
private class BundleVal<T>(val bundleSource: () -> Bundle,
val key: String,
val default: T,
val read: Bundle.() -> T) : ReadOnlyProperty<Any, T> {
val bundle: Bundle by Delegates.lazy { bundleSource() }
override fun get(thisRef: Any, desc: PropertyMetadata): T =
if (bundle.containsKey(key)) bundle.read()
else default
}
private class BundleVar<T>(val bundleSource: () -> Bundle,
val key: String,
val default: T,
val read: Bundle.() -> T,
val write: Bundle.(v: T) -> Unit) : ReadWriteProperty<Any, T> {
val bundle: Bundle by Delegates.lazy { bundleSource() }
override fun set(thisRef: Any, desc: PropertyMetadata, value: T) = bundle.write(value)
override fun get(thisRef: Any, desc: PropertyMetadata): T =
if (bundle.containsKey(key)) bundle.read()
else default
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment