mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-13 22:00:30 +05:30
Merge pull request #4795 from Isira-Seneviratne/Improve_extensions
refactor: Improve getSerializable extensions
This commit is contained in:
commit
835339d85d
@ -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))
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user