replace deprecated usage of PersistableBundle with kotlin-parcelize plugin

This commit is contained in:
Jakob Haider 2024-03-04 13:35:42 +01:00
parent 53d1e3423e
commit 33a99196f3
4 changed files with 17 additions and 37 deletions

View File

@ -20,6 +20,7 @@ def isNewArchitectureEnabled() {
apply plugin: "com.android.library"
apply plugin: "kotlin-android"
apply plugin: "kotlin-parcelize"
if (isNewArchitectureEnabled()) {
apply plugin: "com.facebook.react"

View File

@ -6,7 +6,7 @@ import android.content.Intent
import android.content.pm.ShortcutManager
import android.net.Uri
import android.os.Build
import android.os.PersistableBundle
import androidx.core.content.IntentCompat
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat
@ -22,7 +22,6 @@ import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.WritableMap
import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.modules.core.DeviceEventManagerModule
import com.reactNativeQuickActions.ShortcutItem.Companion.fromPersistableBundle
import com.reactNativeQuickActions.ShortcutItem.Companion.fromReadableArray
@ReactModule(name = AppShortcutsModule.REACT_NAME)
@ -52,10 +51,9 @@ internal class AppShortcutsModule(reactContext: ReactApplicationContext) : React
if (currentActivity != null) {
val intent = currentActivity.intent
if (ACTION_SHORTCUT == intent.action) {
val bundle = intent.getParcelableExtra<PersistableBundle>(SHORTCUT_ITEM)
if (bundle != null) {
val item = fromPersistableBundle(bundle)
map = item.toWritableMap()
val shortcutItem: ShortcutItem? = IntentCompat.getParcelableExtra(intent, SHORTCUT_ITEM, ShortcutItem::class.java)
if (shortcutItem != null) {
map = shortcutItem.toWritableMap()
}
}
}
@ -95,8 +93,11 @@ internal class AppShortcutsModule(reactContext: ReactApplicationContext) : React
Intent(Intent.ACTION_VIEW, uri)
} else {
Intent(context, activity.javaClass)
.setAction(ACTION_SHORTCUT)
}
intent.putExtra(SHORTCUT_ITEM, item)
return ShortcutInfoCompat.Builder(context, id)
.setShortLabel(item.title)
.setLongLabel(item.title)
@ -126,11 +127,7 @@ internal class AppShortcutsModule(reactContext: ReactApplicationContext) : React
if (ACTION_SHORTCUT != intent.action || !isShortcutSupported) {
return
}
var item: ShortcutItem? = null
val bundle = intent.getParcelableExtra<PersistableBundle>(SHORTCUT_ITEM)
if (bundle != null) {
item = fromPersistableBundle(bundle)
}
val item: ShortcutItem? = IntentCompat.getParcelableExtra(intent, SHORTCUT_ITEM, ShortcutItem::class.java)
if (item != null) {
reactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)

View File

@ -1,26 +1,19 @@
package com.reactNativeQuickActions
import android.os.PersistableBundle
import android.os.Parcelable
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableMap
import kotlinx.parcelize.Parcelize
@Parcelize
data class ShortcutItem(
val type: String? = null,
val title: String,
val icon: String? = null,
val userInfo: UserInfo? = null
) {
fun toPersistableBundle(): PersistableBundle {
val bundle = PersistableBundle()
bundle.putString("type", type)
bundle.putString("title", title)
bundle.putString("icon", icon)
bundle.putPersistableBundle("userInfo", userInfo!!.toPersistableBundle())
return bundle
}
) : Parcelable {
fun toWritableMap(): WritableMap {
val map = Arguments.createMap()
@ -57,14 +50,5 @@ data class ShortcutItem(
)
}
@JvmStatic
fun fromPersistableBundle(bundle: PersistableBundle): ShortcutItem {
return ShortcutItem(
type = bundle.getString("type"),
title = bundle.requireString("title"),
icon = bundle.getString("icon"),
userInfo = UserInfo.fromPersistableBundle(bundle.getPersistableBundle("userInfo"))
)
}
}
}

View File

@ -1,18 +1,16 @@
package com.reactNativeQuickActions
import android.os.Parcelable
import android.os.PersistableBundle
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableMap
import kotlinx.parcelize.Parcelize
@Parcelize
data class UserInfo(
val url: String? = null
) {
fun toPersistableBundle(): PersistableBundle {
val bundle = PersistableBundle()
bundle.putString("url", url)
return bundle
}
) : Parcelable {
fun toWritableMap(): WritableMap {
val map = Arguments.createMap()