Skip to content

Commit

Permalink
Merge pull request #4795 from Isira-Seneviratne/Improve_extensions
Browse files Browse the repository at this point in the history
refactor: Improve getSerializable extensions
  • Loading branch information
Isira-Seneviratne authored Sep 17, 2023
2 parents 00220d5 + e2ee776 commit 835339d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
11 changes: 8 additions & 3 deletions app/src/main/java/com/github/libretube/extensions/Bundle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
7 changes: 1 addition & 6 deletions app/src/main/java/com/github/libretube/extensions/Intent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 835339d

Please sign in to comment.