package com.fireworksdk.bridge.utils import android.os.Bundle import android.os.Parcelable object FWBundleUtils { @Suppress("UNCHECKED_CAST") fun mapToBundle(map: Map?): Bundle? { if (map == null) { return null } val bundle = Bundle() for ((key, value) in map) { when (value) { is Double -> { bundle.putDouble(key, value) } is Int -> { bundle.putInt(key, value) } is String -> { bundle.putString(key, value) } is Boolean -> { bundle.putBoolean(key, value) } is List<*> -> { val list = value as List when (list[0]) { is Double -> { bundle.putDoubleArray(key, listToDoubleArray(list)) } is Int -> { bundle.putIntArray(key, listToIntArray(list)) } is String -> { bundle.putStringArray(key, listToStringArray(list)) } is Map<*, *> -> { bundle.putParcelableArrayList(key, listToParcelableArrayList(list)) } else -> {} } } is Map<*, *> -> { bundle.putBundle(key, mapToBundle(value as Map)) } else -> {} } } return bundle } private fun listToDoubleArray(list: List?): DoubleArray? { if (list == null) { return null } val doubles = DoubleArray(list.size) for (i in list.indices) { doubles[i] = list[i] as Double } return doubles } private fun listToIntArray(list: List?): IntArray? { if (list == null) { return null } val integers = IntArray(list.size) for (i in list.indices) { integers[i] = list[i] as Int } return integers } private fun listToStringArray(list: List?): Array? { if (list == null) { return null } val strings = arrayOfNulls(list.size) for (i in list.indices) { strings[i] = list[i].toString() } return strings } @Suppress("UNCHECKED_CAST") private fun listToParcelableArrayList(list: List?): ArrayList? { if (list == null) { return null } val arrayList: ArrayList = ArrayList() for (item in list) { val map = item as Map arrayList.add(mapToBundle(map)) } return arrayList } }