Merge pull request #6343 from Isira-Seneviratne/Simplify-extensions

refactor: Simplify serializable extensions
This commit is contained in:
Isira Seneviratne 2024-08-12 16:06:35 +05:30 committed by GitHub
commit 43a9ba3f06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 2 additions and 13 deletions

View File

@ -1,11 +1,9 @@
package com.github.libretube.extensions package com.github.libretube.extensions
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Parcelable import android.os.Parcelable
import androidx.core.os.BundleCompat import androidx.core.os.BundleCompat
import java.io.Serializable import java.io.Serializable
import kotlin.reflect.safeCast
inline fun <reified T : Parcelable> Bundle.parcelable(key: String?): T? { inline fun <reified T : Parcelable> Bundle.parcelable(key: String?): T? {
return BundleCompat.getParcelable(this, key, T::class.java) return BundleCompat.getParcelable(this, key, T::class.java)
@ -16,14 +14,5 @@ inline fun <reified T : Parcelable> Bundle.parcelableArrayList(key: String?): Ar
} }
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) return BundleCompat.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) {
bundle.getSerializable(key, clazz)
} else {
@Suppress("DEPRECATION")
clazz.kotlin.safeCast(bundle.getSerializable(key))
}
} }

View File

@ -10,5 +10,5 @@ inline fun <reified T : Parcelable> Intent.parcelableExtra(name: String?): T? {
} }
inline fun <reified T : Serializable> Intent.serializableExtra(name: String?): T? { inline fun <reified T : Serializable> Intent.serializableExtra(name: String?): T? {
return extras?.serializable(name) return IntentCompat.getSerializableExtra(this, name, T::class.java)
} }