Last active
October 18, 2021 19:02
-
-
Save VincentJoshuaET/343450d170b07509c92c260563f31a5b to your computer and use it in GitHub Desktop.
Building a type safe bundleOf
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
import android.os.Bundle | |
import android.os.IBinder | |
import android.os.Parcelable | |
import android.util.Size | |
import android.util.SizeF | |
import android.util.SparseArray | |
import kotlinx.parcelize.Parcelize | |
import java.io.Serializable | |
inline fun bundleOf(block: BundleBuilder.() -> Unit): Bundle { | |
return BundleBuilder(Bundle()).apply(block).build() | |
} | |
@Parcelize | |
object SampleParcel : Parcelable | |
object SampleSerializable : Serializable { | |
private const val serialVersionUID = 420931057304L | |
} | |
fun main() { | |
bundleOf { | |
+bundleOf { | |
"first_key" to true | |
"second_key" to "value" | |
"third_key" to booleanArrayOf(false, true) | |
"fourth_key" to bundleOf { | |
"empty" to true | |
} | |
"fifth_key" to 1.toByte() | |
"sixth_key" to byteArrayOf(2, 2) | |
"seventh_key" to 'f' | |
"eighth_key" to charArrayOf('s', 'z') | |
"ninth_key" to "text" as CharSequence | |
"tenth_key" to arrayOf("text" as CharSequence) | |
"eleventh_key" to arrayListOf("text" as CharSequence) | |
"twelveth_key" to 1.0 | |
"thirteenth_key" to doubleArrayOf(1.0) | |
"fourteenth_key" to 1F | |
"fifteenth_key" to floatArrayOf(1F) | |
"sixteenth_key" to 1 | |
"seventeenth_key" to intArrayOf(1) | |
"eighteenth_key" to arrayListOf(1) | |
"nineteenth_key" to 1L | |
"twentieth_key" to longArrayOf(1) | |
"twenty_first_key" to SampleParcel | |
"twenty_second_key" to arrayOf(SampleParcel) | |
"twenty_third_key" to arrayListOf(SampleParcel) | |
"twenty_fourth_key" to SampleSerializable | |
"twenty_fifth_key" to 2.toShort() | |
"twenty_sixth_key" to shortArrayOf(2) | |
"twenty_seventh_key" to Size(1, 1) | |
"twenty_eighth_key" to SizeF(1F, 1F) | |
"twenty_ninth_key" to SparseArray<Parcelable>().apply { | |
put(0, SampleParcel) | |
} | |
"thirtieth_key" to "text" | |
"thirty_first_key" to arrayOf("text") | |
"thirty_second_key" to arrayListOf("text") | |
} | |
} | |
} | |
@JvmInline | |
@Suppress("INAPPLICABLE_JVM_NAME") | |
value class BundleBuilder(private val bundle: Bundle) { | |
@JvmName("putAll") | |
operator fun Bundle?.unaryPlus() { | |
bundle.putAll(this) | |
} | |
@JvmName("putBinder") | |
infix fun String.to(binder: IBinder?) { | |
bundle.putBinder(this, binder) | |
} | |
@JvmName("putBoolean") | |
infix fun String.to(boolean: Boolean) { | |
bundle.putBoolean(this, boolean) | |
} | |
@JvmName("putBooleanArray") | |
infix fun String.to(booleanArray: BooleanArray?) { | |
bundle.putBooleanArray(this, booleanArray) | |
} | |
@JvmName("putBundle") | |
infix fun String.to(value: Bundle?) { | |
bundle.putBundle(this, value) | |
} | |
@JvmName("putByte") | |
infix fun String.to(byte: Byte) { | |
bundle.putByte(this, byte) | |
} | |
@JvmName("putByteArray") | |
infix fun String.to(byteArray: ByteArray?) { | |
bundle.putByteArray(this, byteArray) | |
} | |
@JvmName("putChar") | |
infix fun String.to(char: Char) { | |
bundle.putChar(this, char) | |
} | |
@JvmName("putCharArray") | |
infix fun String.to(charArray: CharArray?) { | |
bundle.putCharArray(this, charArray) | |
} | |
@JvmName("putCharSequence") | |
infix fun String.to(charSequence: CharSequence?) { | |
bundle.putCharSequence(this, charSequence) | |
} | |
@JvmName("putCharSequenceArray") | |
infix fun String.to(charSequenceArray: Array<CharSequence>?) { | |
bundle.putCharSequenceArray(this, charSequenceArray) | |
} | |
@JvmName("putCharSequenceArrayList") | |
infix fun String.to(charSequenceArrayList: ArrayList<CharSequence>?) { | |
bundle.putCharSequenceArrayList(this, charSequenceArrayList) | |
} | |
@JvmName("putDouble") | |
infix fun String.to(double: Double) { | |
bundle.putDouble(this, double) | |
} | |
@JvmName("putDoubleArray") | |
infix fun String.to(doubleArray: DoubleArray?) { | |
bundle.putDoubleArray(this, doubleArray) | |
} | |
@JvmName("putFloat") | |
infix fun String.to(float: Float) { | |
bundle.putFloat(this, float) | |
} | |
@JvmName("putFloatArray") | |
infix fun String.to(floatArray: FloatArray?) { | |
bundle.putFloatArray(this, floatArray) | |
} | |
@JvmName("putInt") | |
infix fun String.to(int: Int) { | |
bundle.putInt(this, int) | |
} | |
@JvmName("putIntArray") | |
infix fun String.to(intArray: IntArray?) { | |
bundle.putIntArray(this, intArray) | |
} | |
@JvmName("putIntegerArrayList") | |
infix fun String.to(intArrayList: ArrayList<Int>?) { | |
bundle.putIntegerArrayList(this, intArrayList) | |
} | |
@JvmName("putLong") | |
infix fun String.to(long: Long) { | |
bundle.putLong(this, long) | |
} | |
@JvmName("putLongArray") | |
infix fun String.to(longArray: LongArray?) { | |
bundle.putLongArray(this, longArray) | |
} | |
@JvmName("putParcelable") | |
infix fun String.to(parcelable: Parcelable?) { | |
bundle.putParcelable(this, parcelable) | |
} | |
@JvmName("putParcelableArray") | |
infix fun String.to(parcelableArray: Array<Parcelable>?) { | |
bundle.putParcelableArray(this, parcelableArray) | |
} | |
@JvmName("putParcelableArrayList") | |
infix fun String.to(parcelableArrayList: ArrayList<out Parcelable>?) { | |
bundle.putParcelableArrayList(this, parcelableArrayList) | |
} | |
@JvmName("putSerializable") | |
infix fun String.to(serializable: Serializable?) { | |
bundle.putSerializable(this, serializable) | |
} | |
@JvmName("putShort") | |
infix fun String.to(short: Short) { | |
bundle.putShort(this, short) | |
} | |
@JvmName("putShortArray") | |
infix fun String.to(shortArray: ShortArray?) { | |
bundle.putShortArray(this, shortArray) | |
} | |
@JvmName("putSize") | |
infix fun String.to(size: Size?) { | |
bundle.putSize(this, size) | |
} | |
@JvmName("putSizeF") | |
infix fun String.to(sizeF: SizeF?) { | |
bundle.putSizeF(this, sizeF) | |
} | |
@JvmName("putSparseParcelableArray") | |
infix fun String.to(sparseParcelableArray: SparseArray<out Parcelable>?) { | |
bundle.putSparseParcelableArray(this, sparseParcelableArray) | |
} | |
@JvmName("putString") | |
infix fun String.to(string: String?) { | |
bundle.putString(this, string) | |
} | |
fun build() = bundle | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment