Skip to content

Instantly share code, notes, and snippets.

@dmersiyanov
Last active June 9, 2022 06:22
Show Gist options
  • Save dmersiyanov/815cb6218607ceab64e335dac966e74d to your computer and use it in GitHub Desktop.
Save dmersiyanov/815cb6218607ceab64e335dac966e74d to your computer and use it in GitHub Desktop.
Delegate for fragment arguments
import android.os.Bundle
import android.os.Parcelable
import java.io.Serializable
import androidx.fragment.app.Fragment
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class FragmentArgumentDelegate<T : Any> :
ReadWriteProperty<Fragment, T> {
@Suppress("UNCHECKED_CAST")
override fun getValue(
thisRef: Fragment,
property: KProperty<*>
): T {
val key = property.name
return thisRef.arguments
?.get(key) as? T
?: throw IllegalStateException("Property ${property.name} could not be read")
}
override fun setValue(
thisRef: Fragment,
property: KProperty<*>,
value: T
) {
val args = thisRef.arguments
?: Bundle().also(thisRef::setArguments)
val key = property.name
args.put(key, value)
}
}
class FragmentNullableArgumentDelegate<T : Any?> :
ReadWriteProperty<Fragment, T?> {
@Suppress("UNCHECKED_CAST")
override fun getValue(
thisRef: Fragment,
property: KProperty<*>
): T? {
val key = property.name
return thisRef.arguments?.get(key) as? T
}
override fun setValue(
thisRef: Fragment,
property: KProperty<*>,
value: T?
) {
val args = thisRef.arguments
?: Bundle().also(thisRef::setArguments)
val key = property.name
value?.let { args.put(key, it) } ?: args.remove(key)
}
}
fun <T> Bundle.put(key: String, value: T) {
when (value) {
is Boolean -> putBoolean(key, value)
is String -> putString(key, value)
is Int -> putInt(key, value)
is Short -> putShort(key, value)
is Long -> putLong(key, value)
is Byte -> putByte(key, value)
is ByteArray -> putByteArray(key, value)
is Char -> putChar(key, value)
is CharArray -> putCharArray(key, value)
is CharSequence -> putCharSequence(key, value)
is Float -> putFloat(key, value)
is Bundle -> putBundle(key, value)
is Parcelable -> putParcelable(key, value)
is Serializable -> putSerializable(key, value)
else -> throw IllegalStateException("Type of property $key is not supported")
}
}
fun <T : Any> argument(): ReadWriteProperty<Fragment, T> =
FragmentArgumentDelegate()
fun <T : Any> argumentNullable(): ReadWriteProperty<Fragment, T?> =
FragmentNullableArgumentDelegate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment