Last active
May 17, 2021 12:49
-
-
Save green-nick/2f32b6177ffb90f256ca1e3b29c6c830 to your computer and use it in GitHub Desktop.
Bundle arguments delegates for Activites and Fragments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package * | |
import android.app.Activity | |
import androidx.fragment.app.Fragment | |
import kotlin.properties.ReadOnlyProperty | |
inline fun <reified T> argNamed() = | |
ReadOnlyProperty<Activity, T> { thisRef, prop -> | |
val data = thisRef.intent.extras?.get(prop.name) | |
data as T | |
} | |
inline fun <reified T> argNamed(default: T) = | |
ReadOnlyProperty<Activity, T> { thisRef, prop -> | |
val data = thisRef.intent.extras?.get(prop.name) | |
data as? T ?: default | |
} | |
inline fun <reified T> arg(key: String) = | |
ReadOnlyProperty<Activity, T> { thisRef, _ -> | |
val data = thisRef.intent.extras?.get(key) | |
data as T | |
} | |
inline fun <reified T> arg(key: String, default: T) = | |
ReadOnlyProperty<Activity, T> { thisRef, _ -> | |
val data = thisRef.intent.extras?.get(key) | |
data as? T ?: default | |
} | |
inline fun <reified T> Fragment.argNamed() = | |
ReadOnlyProperty<Fragment, T> { thisRef, prop -> | |
val data = thisRef.arguments?.get(prop.name) | |
data as T | |
} | |
inline fun <reified T> Fragment.argNamed(default: T) = | |
ReadOnlyProperty<Fragment, T> { thisRef, prop -> | |
val data = thisRef.arguments?.get(prop.name) | |
data as? T ?: default | |
} | |
inline fun <reified T> Fragment.arg(key: String) = | |
ReadOnlyProperty<Fragment, T> { thisRef, _ -> | |
val data = thisRef.arguments?.get(key) | |
data as T | |
} | |
inline fun <reified T> Fragment.arg(key: String, default: T) = | |
ReadOnlyProperty<Fragment, T> { thisRef, _ -> | |
val data = thisRef.arguments?.get(key) | |
data as? T ?: default | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment