Merge pull request #4795 from Isira-Seneviratne/Improve_extensions

refactor: Improve getSerializable extensions
This commit is contained in:
Isira Seneviratne 2023-09-17 19:28:38 +05:30 committed by GitHub
commit 835339d85d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 9 deletions

View File

@ -5,16 +5,21 @@ import android.os.Bundle
import android.os.Parcelable
import androidx.core.os.BundleCompat
import java.io.Serializable
import kotlin.reflect.safeCast
inline fun <reified T : Parcelable> Bundle.parcelable(key: String?): T? {
return BundleCompat.getParcelable(this, key, T::class.java)
}
inline fun <reified T : Serializable> Bundle.serializable(key: String): T? {
inline fun <reified T : Serializable> Bundle.serializable(key: String?): T? {
return getSerializable(this, key, T::class.java)
}
fun <T : Serializable> getSerializable(bundle: Bundle, key: String?, clazz: Class<T>): T? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
getSerializable(key, T::class.java)
bundle.getSerializable(key, clazz)
} else {
@Suppress("DEPRECATION")
getSerializable(key) as? T
clazz.kotlin.safeCast(bundle.getSerializable(key))
}
}

View File

@ -11,10 +11,5 @@ inline fun <reified T : Parcelable> Intent.parcelableExtra(name: String?): T? {
}
inline fun <reified T : Serializable> Intent.serializableExtra(name: String?): T? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
getSerializableExtra(name, T::class.java)
} else {
@Suppress("DEPRECATION")
getSerializableExtra(name) as? T
}
return extras?.serializable(name)
}