Skip to content

Commit eca4ff0

Browse files
committed
refactor: refactored AddToSiriButton
BREAKING CHANGE: `supportsSiriButton` has been deprecated. The component now returns null when not supported. BREAKING CHANGE: AddToSiriButton now returns `null` on Android. Added `automatic` and `automaticOutline` styles to AddToSiriButton fix: Removed observer from `init` in RNSSSiriShortcuts.m, so an observer is only created when there are listeners. BREAKING CHANGE: RNSiriShortcuts has been renamed to RNSSSiriShortcuts. Prefixed files with RNSS for namespacing purposes. Exporting AddToSiriButton and SiriButtonStyles from `index.js`. fix: Added TS types for AddToSiriButton and SiriButtonStyles. Removed Swift setup from podspec. SiriButtonStyles is not exported natively to ensure the values match.
1 parent d4a557a commit eca4ff0

26 files changed

+483
-333
lines changed

AddToSiriButton.ios.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// @flow
2+
import type { ShortcutOptions } from ".";
3+
4+
import * as React from "react";
5+
import {
6+
requireNativeComponent,
7+
View,
8+
Platform,
9+
UIManager,
10+
NativeModules,
11+
} from "react-native";
12+
13+
const RNTAddToSiriButton = requireNativeComponent("RNSSAddToSiriButton");
14+
const Constants = (
15+
UIManager.getViewManagerConfig
16+
? UIManager.getViewManagerConfig("RNSSAddToSiriButton")
17+
: UIManager["RNSSAddToSiriButton"]
18+
).Constants;
19+
20+
export const SiriButtonStyles = Constants.AvailableStyles;
21+
22+
type ViewProps = React.ElementConfig<typeof View>;
23+
type ViewStyleProp = $PropertyType<ViewProps, "style">;
24+
25+
type Props = {
26+
buttonStyle?: 0 | 1 | 2 | 3 | 4 | 5,
27+
style?: ViewStyleProp,
28+
shortcut: ShortcutOptions,
29+
onPress?: () => void,
30+
};
31+
32+
/** @deprecated The component itself now returns `null` when the device does not support the AddToSiriButton */
33+
export const supportsSiriButton = Number.parseFloat(Platform.Version, 10) >= 12;
34+
35+
const AddToSiriButton = ({ buttonStyle, onPress, shortcut, style }: Props) => {
36+
if (!supportsSiriButton) {
37+
return null;
38+
}
39+
40+
return (
41+
<RNTAddToSiriButton
42+
buttonStyle={buttonStyle}
43+
onPress={onPress}
44+
shortcut={shortcut}
45+
style={[
46+
{
47+
height: Constants.ComponentHeight,
48+
width: Constants.ComponentWidth,
49+
},
50+
style,
51+
]}
52+
/>
53+
)};
54+
55+
export default AddToSiriButton;

AddToSiriButton.js

+4-57
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,5 @@
1-
// @flow
2-
import type { ShortcutOptions } from ".";
1+
export const SiriButtonStyles = {};
32

4-
import * as React from "react";
5-
import {
6-
requireNativeComponent,
7-
View,
8-
StyleSheet,
9-
Platform
10-
} from "react-native";
11-
12-
const RNTAddToSiriButton = requireNativeComponent("RNTAddToSiriButton");
13-
14-
export const SiriButtonStyles = {
15-
white: 0,
16-
whiteOutline: 1,
17-
black: 2,
18-
blackOutline: 3
19-
};
20-
21-
type ViewProps = React.ElementConfig<typeof View>;
22-
type ViewStyleProp = $PropertyType<ViewProps, "style">;
23-
24-
type Props = {
25-
buttonStyle?: 0 | 1 | 2 | 3,
26-
style?: ViewStyleProp,
27-
shortcut: ShortcutOptions,
28-
onPress?: () => void
29-
};
30-
31-
export const supportsSiriButton =
32-
Platform.OS === "ios" && Number.parseFloat(Platform.Version, 10) >= 12;
33-
34-
const AddToSiriButton = ({
35-
buttonStyle = SiriButtonStyles.white,
36-
style = {},
37-
onPress = () => {},
38-
shortcut
39-
}: Props) => (
40-
<View
41-
style={[
42-
{
43-
width: 149,
44-
height: 50
45-
},
46-
style
47-
]}
48-
>
49-
<RNTAddToSiriButton
50-
buttonStyle={buttonStyle}
51-
style={{ flex: 1 }}
52-
onPress={onPress}
53-
shortcut={shortcut}
54-
/>
55-
</View>
56-
);
57-
58-
export default AddToSiriButton;
3+
export default function AddToSiriButton() {
4+
return null;
5+
}

RNSiriShortcuts.podspec

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ Pod::Spec.new do |s|
1111
s.authors = package['author']
1212
s.homepage = package['homepage']
1313
s.platform = :ios, "9.0"
14-
s.swift_version = "5.2"
15-
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
1614

1715
s.source = { :git => "https://github.com/Gustash/react-native-siri-shortcut.git", :tag => "v#{s.version}" }
1816
s.source_files = "ios/**/*.{h,m,swift}"

example/App.js

+16-22
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ import {
3030
getShortcuts,
3131
addShortcutListener,
3232
getInitialShortcut,
33-
} from 'react-native-siri-shortcut';
34-
import AddToSiriButton, {
33+
AddToSiriButton,
3534
SiriButtonStyles,
36-
supportsSiriButton,
37-
} from 'react-native-siri-shortcut/AddToSiriButton';
35+
} from 'react-native-siri-shortcut';
3836

3937
const opts1: ShortcutOptions = {
4038
activityType: 'com.github.gustash.SiriShortcutsModuleExample.sayHello',
@@ -66,7 +64,7 @@ const opts2: ShortcutOptions = {
6664
type State = {
6765
shortcutInfo: ?any,
6866
shortcutActivityType: ?string,
69-
addToSiriStyle: 0 | 1 | 2 | 3,
67+
addToSiriStyle: 0 | 1 | 2 | 3 | 4 | 5,
7068
shortcuts: Array<ShortcutData>,
7169
};
7270
export default class App extends Component<void, State> {
@@ -229,23 +227,19 @@ export default class App extends Component<void, State> {
229227
title="Update list of shortcuts"
230228
onPress={this.updateShortcutList.bind(this)}
231229
/>
232-
{supportsSiriButton && (
233-
<>
234-
<AddToSiriButton
235-
buttonStyle={addToSiriStyle}
236-
onPress={() => {
237-
presentShortcut(opts1, ({status}) => {
238-
console.log(`I was ${status}`);
239-
});
240-
}}
241-
shortcut={opts1}
242-
/>
243-
<Button
244-
title="Swap Siri Button Theme"
245-
onPress={this.swapSiriButtonTheme.bind(this)}
246-
/>
247-
</>
248-
)}
230+
<AddToSiriButton
231+
buttonStyle={addToSiriStyle}
232+
onPress={() => {
233+
presentShortcut(opts1, ({status}) => {
234+
console.log(`I was ${status}`);
235+
});
236+
}}
237+
shortcut={opts1}
238+
/>
239+
<Button
240+
title="Swap Siri Button Theme"
241+
onPress={this.swapSiriButtonTheme.bind(this)}
242+
/>
249243
{shortcuts.length ? (
250244
shortcuts.map(({identifier, phrase, options}, i) => (
251245
<View key={identifier}>

example/ios/Podfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ SPEC CHECKSUMS:
540540
React-RCTVibration: 9e344c840176b0af9c84d5019eb4fed8b3c105a1
541541
React-runtimeexecutor: 7285b499d0339104b2813a1f58ad1ada4adbd6c0
542542
ReactCommon: bf2888a826ceedf54b99ad1b6182d1bc4a8a3984
543-
RNSiriShortcuts: 93d7607c6e1dd693657395ce6d705f6005e88240
543+
RNSiriShortcuts: 25b8d989565561f96a6a8791b474226f916fef72
544544
SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608
545545
Yoga: 17cd9a50243093b547c1e539c749928dd68152da
546546
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

example/ios/example/AppDelegate.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ - (BOOL)application:(UIApplication *)application
110110
continueUserActivity:(NSUserActivity *)userActivity
111111
restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
112112
{
113-
return [RNSiriShortcuts application:application continueUserActivity:userActivity restorationHandler:restorationHandler];
113+
return [RNSSSiriShortcuts application:application continueUserActivity:userActivity restorationHandler:restorationHandler];
114114
}
115115

116116
@end

index.d.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { EmitterSubscription, NativeEventEmitter } from "react-native";
1+
import React from "react";
2+
import { EmitterSubscription, NativeEventEmitter, StyleProp, ViewStyle } from "react-native";
23

34
export type ShortcutOptions = {
45
activityType: string;
@@ -59,3 +60,21 @@ export function clearShortcutsWithIdentifiers(
5960
): Promise<void>;
6061

6162
export const supportsPresentShortcut: boolean;
63+
64+
export enum SiriButtonStyles {
65+
white = 0,
66+
whiteOutline = 1,
67+
black = 2,
68+
blackOutline = 3,
69+
/** Only supported on iOS >= 13. On iOS 12 this defaults to `white` */
70+
automatic = 4,
71+
/** Only supported on iOS >= 13. On iOS 12 this defaults to `whiteOutline` */
72+
automaticOutline = 5,
73+
}
74+
export interface AddToSiriButtonProps {
75+
buttonStyle?: SiriButtonStyles;
76+
style?: StyleProp<ViewStyle>;
77+
shortcut: ShortcutOptions;
78+
onPress?: () => void;
79+
}
80+
export const AddToSiriButton: (props: AddToSiriButtonProps) => JSX.Element | null;

index.js

+5
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,8 @@ export const getInitialShortcut = safeCall(() => RNSiriShortcuts.getInitialShort
111111
export const addShortcutListener = (callback: (shortcut: ShortcutInfo) => void) => {
112112
return SiriShortcutsEvent.addListener("SiriShortcutListener", callback);
113113
}
114+
115+
export {
116+
default as AddToSiriButton,
117+
SiriButtonStyles,
118+
} from "./AddToSiriButton";

ios/NSUserActivity+ShortcutOptions.h

-21
This file was deleted.

ios/NSUserActivity+ShortcutOptions.m

-48
This file was deleted.

ios/RCTConvert+INShortcut.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// RCTConvert+INShortcut.h
3+
// Pods
4+
//
5+
// Created by Gustavo Parreira on 21/04/2022.
6+
//
7+
8+
#ifndef RCTConvert_INShortcut_h
9+
#define RCTConvert_INShortcut_h
10+
11+
#import <React/RCTConvert.h>
12+
#import <Intents/Intents.h>
13+
14+
API_AVAILABLE(ios(12.0))
15+
@interface RCTConvert (INShortcut)
16+
17+
+ (INShortcut *)INShortcut:(id)json;
18+
19+
@end
20+
21+
#endif /* RCTConvert_INShortcut_h */

ios/RCTConvert+INShortcut.m

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// RCTConvert+INShortcut.m
3+
// RNSiriShortcuts
4+
//
5+
// Created by Gustavo Parreira on 21/04/2022.
6+
//
7+
8+
#import "RCTConvert+INShortcut.h"
9+
#import "RCTConvert+NSUserActivity.h"
10+
11+
API_AVAILABLE(ios(12.0))
12+
@implementation RCTConvert (INShortcut)
13+
14+
+ (INShortcut *)INShortcut:(id)json
15+
{
16+
return [[INShortcut alloc] initWithUserActivity:[self NSUserActivity:json]];
17+
}
18+
19+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// RCTConvert+INUIAddVoiceShortcutButtonStyle.h
3+
// Pods
4+
//
5+
// Created by Gustavo Parreira on 21/04/2022.
6+
//
7+
8+
#ifndef RCTConvert_INUIAddVoiceShortcutButtonStyle_h
9+
#define RCTConvert_INUIAddVoiceShortcutButtonStyle_h
10+
11+
#import <React/RCTConvert.h>
12+
#import <IntentsUI/IntentsUI.h>
13+
14+
API_AVAILABLE(ios(12.0))
15+
@interface RCTConvert (INUIAddVoiceShortcutButtonStyle)
16+
17+
+ (INUIAddVoiceShortcutButtonStyle)INUIAddVoiceShortcutButtonStyle:(id)json;
18+
19+
@end
20+
21+
#endif /* RCTConvert_INUIAddVoiceShortcutButtonStyle_h */

0 commit comments

Comments
 (0)