Android: Create extension functions for serializables

Getting serializables without explicitly stating the type is deprecated in Android 13. However we still need a way to get a serializable in versions below that. Now with these we can avoid repeating the same if block.
This commit is contained in:
Charles Lombardo 2023-03-15 14:25:31 -04:00
parent 4281606838
commit 98ab893be7
2 changed files with 26 additions and 6 deletions

View File

@ -2,7 +2,6 @@
package org.dolphinemu.dolphinemu.features.input.ui
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
@ -14,6 +13,7 @@ import com.google.android.material.divider.MaterialDividerItemDecoration
import org.dolphinemu.dolphinemu.R
import org.dolphinemu.dolphinemu.databinding.DialogInputProfilesBinding
import org.dolphinemu.dolphinemu.features.settings.ui.MenuTag
import org.dolphinemu.dolphinemu.utils.SerializableHelper.serializable
class ProfileDialog : BottomSheetDialogFragment() {
private var presenter: ProfileDialogPresenter? = null
@ -22,11 +22,7 @@ class ProfileDialog : BottomSheetDialogFragment() {
private val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
val menuTag: MenuTag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requireArguments().getSerializable(KEY_MENU_TAG, MenuTag::class.java) as MenuTag
} else {
requireArguments().getSerializable(KEY_MENU_TAG) as MenuTag
}
val menuTag = requireArguments().serializable<MenuTag>(KEY_MENU_TAG)
presenter = ProfileDialogPresenter(this, menuTag)

View File

@ -0,0 +1,24 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.utils
import android.content.Intent
import android.os.Build
import android.os.Bundle
import java.io.Serializable
object SerializableHelper {
inline fun <reified T : Serializable> Intent.serializable(key: String): T? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
getSerializableExtra(key, T::class.java)
else
getSerializableExtra(key) as T?
}
inline fun <reified T : Serializable> Bundle.serializable(key: String): T? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
getSerializable(key, T::class.java)
else
getSerializable(key) as T?
}
}