From 50f01c05619a55bc9c2f9058c6561324354d6362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A0=20Qu=E1=BB=91c=20L=C6=B0=C6=A1ng?= Date: Wed, 16 Apr 2025 00:41:19 +0700 Subject: [PATCH 1/3] feat: add custom userInfo to shortcut --- example/src/App.tsx | 5 ++++- ios/RNShortcuts.mm | 1 + ios/RNShortcutsImpl.swift | 21 +++++++++++++++++---- src/NativeShortcuts.tsx | 7 +++++-- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/example/src/App.tsx b/example/src/App.tsx index 4de350c..84f1d95 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -55,7 +55,10 @@ export const App = () => { Shortcuts.addShortcut({ id, title, - iconName + iconName, + userInfo: { + key: 'test' + } }) .then((response) => Alert.alert( diff --git a/ios/RNShortcuts.mm b/ios/RNShortcuts.mm index 77f1f1c..78d0462 100644 --- a/ios/RNShortcuts.mm +++ b/ios/RNShortcuts.mm @@ -51,6 +51,7 @@ - (NSDictionary *)convertShortcutParamsToDictionary:(JS::NativeShortcuts::Shortc dict[@"title"] = params.title() ?: @""; dict[@"subTitle"] = params.subTitle() ?: @""; dict[@"longLabel"] = params.longLabel() ?: @""; + dict[@"userInfo"] = params.userInfo() ?: nil; return [dict copy]; } diff --git a/ios/RNShortcutsImpl.swift b/ios/RNShortcutsImpl.swift index 9786206..f2c87e8 100644 --- a/ios/RNShortcutsImpl.swift +++ b/ios/RNShortcutsImpl.swift @@ -78,20 +78,20 @@ public class RNShortcutsImpl: NSObject { return } - let shortcutItem = createShortcutItem(id: id, title: title, subtitle: params["subTitle"] as? String, iconName: params["iconName"] as? String) + let shortcutItem = createShortcutItem(id: id, title: title, subtitle: params["subTitle"] as? String, iconName: params["iconName"] as? String, userInfo: params["userInfo"] as? [String : any NSSecureCoding]) DispatchQueue.main.async { operation(shortcutItem) } } - private func createShortcutItem(id: String, title: String, subtitle: String?, iconName: String?) -> UIApplicationShortcutItem { + private func createShortcutItem(id: String, title: String, subtitle: String?, iconName: String?, userInfo: [String : any NSSecureCoding]?) -> UIApplicationShortcutItem { return UIApplicationShortcutItem( type: id, localizedTitle: title, localizedSubtitle: subtitle, icon: getUIApplicationShortcutIcon(iconName: iconName), - userInfo: nil + userInfo: userInfo ) } @@ -116,6 +116,18 @@ public class RNShortcutsImpl: NSObject { guard let iconName = iconName else { return nil} return UIApplicationShortcutIcon(templateImageName: iconName) } + + private func convertToSecureCodingDictionary(from object: NSObject) -> [String: any NSSecureCoding]? { + guard let dictionary = object as? [String: Any] else { return nil } + + var secureCodingDict = [String: any NSSecureCoding]() + + for (key, value) in dictionary { + secureCodingDict[key] = value + } + + return secureCodingDict + } } private extension UIApplicationShortcutItem { @@ -123,7 +135,8 @@ private extension UIApplicationShortcutItem { return [ "id": type, "title": localizedTitle, - "subtitle": localizedSubtitle + "subtitle": localizedSubtitle, + "userInfo": userInfo ] } } diff --git a/src/NativeShortcuts.tsx b/src/NativeShortcuts.tsx index ad100a8..73b7d02 100644 --- a/src/NativeShortcuts.tsx +++ b/src/NativeShortcuts.tsx @@ -5,6 +5,9 @@ export interface ShortcutResponseType { title: string; subTitle?: string; longLabel?: string; + userInfo?: { + [key: string]: string | number; + }; } export interface ShortcutParamsType extends ShortcutResponseType { @@ -20,8 +23,8 @@ export interface Spec extends TurboModule { isShortcutExists(id: string): Promise; isShortcutSupported(): Promise; getInitialShortcutId(): Promise; - addListener: (eventType: string) => void; + addListener: (eventType: string) => void; removeListeners: (count: number) => void; } -export default TurboModuleRegistry.getEnforcing("RNShortcuts"); \ No newline at end of file +export default TurboModuleRegistry.getEnforcing("RNShortcuts"); From 35a7185f6a56261f5a48c4b752321ae7dfaef651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A0=20Qu=E1=BB=91c=20L=C6=B0=C6=A1ng?= Date: Wed, 16 Apr 2025 15:14:24 +0700 Subject: [PATCH 2/3] feat: update type assertion --- ios/RNShortcutsImpl.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ios/RNShortcutsImpl.swift b/ios/RNShortcutsImpl.swift index f2c87e8..c72e3f5 100644 --- a/ios/RNShortcutsImpl.swift +++ b/ios/RNShortcutsImpl.swift @@ -123,7 +123,7 @@ public class RNShortcutsImpl: NSObject { var secureCodingDict = [String: any NSSecureCoding]() for (key, value) in dictionary { - secureCodingDict[key] = value + secureCodingDict[key] = value as? any NSSecureCoding } return secureCodingDict @@ -131,7 +131,7 @@ public class RNShortcutsImpl: NSObject { } private extension UIApplicationShortcutItem { - func toDictionary() -> [String: String?] { + func toDictionary() -> [String: Any?] { return [ "id": type, "title": localizedTitle, From 9555b12dc92822289b2012c840f712d71237eeba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A0=20Qu=E1=BB=91c=20L=C6=B0=C6=A1ng?= Date: Wed, 16 Apr 2025 15:51:17 +0700 Subject: [PATCH 3/3] feat: update lib typescript folder --- lib/typescript/commonjs/src/NativeShortcuts.d.ts | 3 +++ lib/typescript/module/src/NativeShortcuts.d.ts | 3 +++ 2 files changed, 6 insertions(+) diff --git a/lib/typescript/commonjs/src/NativeShortcuts.d.ts b/lib/typescript/commonjs/src/NativeShortcuts.d.ts index a373157..569e6f2 100644 --- a/lib/typescript/commonjs/src/NativeShortcuts.d.ts +++ b/lib/typescript/commonjs/src/NativeShortcuts.d.ts @@ -4,6 +4,9 @@ export interface ShortcutResponseType { title: string; subTitle?: string; longLabel?: string; + userInfo?: { + [key: string]: string | number; + }; } export interface ShortcutParamsType extends ShortcutResponseType { iconName?: string; diff --git a/lib/typescript/module/src/NativeShortcuts.d.ts b/lib/typescript/module/src/NativeShortcuts.d.ts index a373157..569e6f2 100644 --- a/lib/typescript/module/src/NativeShortcuts.d.ts +++ b/lib/typescript/module/src/NativeShortcuts.d.ts @@ -4,6 +4,9 @@ export interface ShortcutResponseType { title: string; subTitle?: string; longLabel?: string; + userInfo?: { + [key: string]: string | number; + }; } export interface ShortcutParamsType extends ShortcutResponseType { iconName?: string;