From 97694e639a43d83ea32e488b59ad92f2f2627ddc Mon Sep 17 00:00:00 2001 From: Abandoned Cart Date: Fri, 21 Apr 2023 09:06:49 -0400 Subject: [PATCH] Add a Parcelable helper to handle deprecation --- .../applet/swkbd/SoftwareKeyboardDialog.kt | 3 +- .../emu/skyline/utils/ParcelableHelper.kt | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/emu/skyline/utils/ParcelableHelper.kt diff --git a/app/src/main/java/emu/skyline/applet/swkbd/SoftwareKeyboardDialog.kt b/app/src/main/java/emu/skyline/applet/swkbd/SoftwareKeyboardDialog.kt index a00c4d35..817302ad 100644 --- a/app/src/main/java/emu/skyline/applet/swkbd/SoftwareKeyboardDialog.kt +++ b/app/src/main/java/emu/skyline/applet/swkbd/SoftwareKeyboardDialog.kt @@ -14,13 +14,14 @@ import android.view.ViewGroup import androidx.core.widget.doOnTextChanged import androidx.fragment.app.DialogFragment import emu.skyline.databinding.KeyboardDialogBinding +import emu.skyline.utils.parcelable import emu.skyline.utils.stringFromChars import java.util.concurrent.FutureTask data class SoftwareKeyboardResult(val cancelled : Boolean, val text : String) class SoftwareKeyboardDialog : DialogFragment() { - private val config by lazy { requireArguments().getParcelable("config")!! } + private val config by lazy { requireArguments().parcelable("config")!! } private val initialText by lazy { requireArguments().getString("initialText")!! } private var stopped = false diff --git a/app/src/main/java/emu/skyline/utils/ParcelableHelper.kt b/app/src/main/java/emu/skyline/utils/ParcelableHelper.kt new file mode 100644 index 00000000..fefa9d03 --- /dev/null +++ b/app/src/main/java/emu/skyline/utils/ParcelableHelper.kt @@ -0,0 +1,28 @@ +/* + * SPDX-License-Identifier: MPL-2.0 + * Copyright © 2023 Skyline Team and Contributors (https://github.com/skyline-emu/) + */ + +package emu.skyline.utils + +import android.content.Intent +import android.os.Build +import android.os.Bundle +import android.os.Parcelable + +inline fun Bundle.parcelable(key: String): T? = when { + Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getParcelable(key, T::class.java) + else -> @Suppress("DEPRECATION") getParcelable(key) as? T +} + +@Suppress("unused") +inline fun Intent.parcelable(key: String): T? = when { + Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getParcelableExtra(key, T::class.java) + else -> @Suppress("DEPRECATION") getParcelableExtra(key) as? T +} + +@Suppress("unused") +inline fun Intent.parcelableArrayList(key: String): ArrayList? = when { + Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getParcelableArrayListExtra(key, T::class.java) + else -> @Suppress("DEPRECATION") getParcelableArrayListExtra(key) +} \ No newline at end of file