Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/ktextension/BundleExtensions.kt

48 lines
1.2 KiB
Kotlin
Raw Normal View History

2016-08-30 14:23:59 +02:00
package org.mariotaku.ktextension
import android.os.Bundle
import android.os.Parcelable
inline fun Bundle(action: Bundle.() -> Unit): Bundle {
val bundle = Bundle()
action(bundle)
return bundle
}
operator fun Bundle.set(key: String, value: Boolean) {
return putBoolean(key, value)
}
operator fun Bundle.set(key: String, value: Int) {
return putInt(key, value)
}
operator fun Bundle.set(key: String, value: Long) {
return putLong(key, value)
}
2016-12-03 06:48:40 +01:00
operator fun Bundle.set(key: String, value: String?) {
2016-08-30 14:23:59 +02:00
return putString(key, value)
}
operator fun Bundle.set(key: String, value: Parcelable?) {
return putParcelable(key, value)
}
operator fun Bundle.set(key: String, value: Array<out Parcelable>?) {
return putParcelableArray(key, value)
2016-12-13 04:06:07 +01:00
}
operator fun Bundle.set(key: String, value: Array<String>?) {
return putStringArray(key, value)
2017-02-02 16:50:30 +01:00
}
2017-04-14 11:01:45 +02:00
inline fun <reified T> Bundle.getTypedArray(key: String): Array<T> {
val parcelable = getParcelableArray(key)
return Array(parcelable.size) { parcelable[it] as T }
2017-04-09 08:15:01 +02:00
}
2017-04-14 11:01:45 +02:00
inline fun <reified T> Bundle.getNullableTypedArray(key: String): Array<T>? {
val parcelable = getParcelableArray(key) ?: return null
return Array(parcelable.size) { parcelable[it] as T }
2016-08-30 14:23:59 +02:00
}