Skip to content

Feat: add support for passing custom data to iOS shortcuts #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export const App = () => {
Shortcuts.addShortcut({
id,
title,
iconName
iconName,
userInfo: {
key: 'test'
}
})
.then((response) =>
Alert.alert(
Expand Down
1 change: 1 addition & 0 deletions ios/RNShortcuts.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
23 changes: 18 additions & 5 deletions ios/RNShortcutsImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand All @@ -116,14 +116,27 @@ 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 as? any NSSecureCoding
}

return secureCodingDict
}
}

private extension UIApplicationShortcutItem {
func toDictionary() -> [String: String?] {
func toDictionary() -> [String: Any?] {
return [
"id": type,
"title": localizedTitle,
"subtitle": localizedSubtitle
"subtitle": localizedSubtitle,
"userInfo": userInfo
]
}
}
3 changes: 3 additions & 0 deletions lib/typescript/commonjs/src/NativeShortcuts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions lib/typescript/module/src/NativeShortcuts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/NativeShortcuts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export interface ShortcutResponseType {
title: string;
subTitle?: string;
longLabel?: string;
userInfo?: {
[key: string]: string | number;
};
}

export interface ShortcutParamsType extends ShortcutResponseType {
Expand All @@ -20,8 +23,8 @@ export interface Spec extends TurboModule {
isShortcutExists(id: string): Promise<boolean>;
isShortcutSupported(): Promise<boolean>;
getInitialShortcutId(): Promise<string>;
addListener: (eventType: string) => void;
addListener: (eventType: string) => void;
removeListeners: (count: number) => void;
}

export default TurboModuleRegistry.getEnforcing<Spec>("RNShortcuts");
export default TurboModuleRegistry.getEnforcing<Spec>("RNShortcuts");