Created
June 9, 2022 06:20
-
-
Save dmersiyanov/64b895663178bc9385460aac69e8d80c to your computer and use it in GitHub Desktop.
Kotlin android bundle extensions
This file contains 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.content.Intent | |
import android.os.Bundle | |
import android.os.Parcelable | |
import timber.log.Timber | |
import java.io.Serializable | |
inline fun <reified T : Parcelable> Bundle.putWithClassNameKey(parcelable: T?) { | |
parcelable?.let { putParcelable(T::class.java.simpleName, parcelable) } | |
} | |
inline fun <reified T : Parcelable> Bundle.getWithClassNameKey(): T? { | |
return getParcelable(T::class.java.simpleName) | |
} | |
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 Map<String, Any>.toBundle(): Bundle { | |
val bundle = Bundle() | |
for ((key, value) in this) { | |
bundle.put(key, value) | |
} | |
return bundle | |
} | |
fun Bundle.toMap(): Map<String, String> { | |
val map: MutableMap<String, String> = mutableMapOf() | |
val iterator = keySet().iterator() | |
while (iterator.hasNext()) { | |
val key = iterator.next() | |
map[key] = get(key).toString() | |
} | |
return map | |
} | |
fun Bundle.prettyPrint() { | |
fun Bundle.prettyPrintRec(parentKey: String) { | |
if (keySet().isEmpty()) { | |
Timber.d("Bundle with parentKey: $parentKey is empty") | |
} else { | |
for (key in keySet()) { | |
when (val value = this[key]) { | |
is Bundle -> value.prettyPrintRec(key) | |
is Array<*> -> Timber.d("parentKey: $parentKey, $key - ${value.joinToString()}") | |
else -> Timber.d("parentKey: $parentKey, $key - $value") | |
} | |
} | |
} | |
} | |
if (keySet().isEmpty()) { | |
Timber.d("Bundle is empty") | |
} else { | |
for (key in keySet()) { | |
when (val value = this[key]) { | |
is Bundle -> value.prettyPrintRec(key) | |
is Array<*> -> Timber.d("$key - ${value.joinToString()}") | |
else -> Timber.d("$key - $value") | |
} | |
} | |
} | |
} | |
fun Bundle.equalBundles(two: Bundle): Boolean { | |
var isEquals = true | |
if (this.size() != two.size()) | |
isEquals = false | |
if (!this.keySet().containsAll(two.keySet())) | |
isEquals = false | |
for (key in this.keySet()) { | |
val valueOne = this.get(key) | |
val valueTwo = two.get(key) | |
if (valueOne is Bundle && valueTwo is Bundle) { | |
if (!valueOne.equalBundles(valueTwo)) { | |
isEquals = false | |
} | |
} else if (valueOne != valueTwo) { | |
isEquals = false | |
} | |
} | |
return isEquals | |
} | |
fun Intent.printBundle() { | |
extras?.prettyPrint() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment