-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwithRNOrientationAppDelegate.ts
More file actions
133 lines (115 loc) · 4.17 KB
/
withRNOrientationAppDelegate.ts
File metadata and controls
133 lines (115 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import {
type ConfigPlugin,
type ExportedConfigWithProps,
withAppDelegate,
} from '@expo/config-plugins';
import { type AppDelegateProjectFile } from '@expo/config-plugins/build/ios/Paths';
import { mergeContents } from '@expo/config-plugins/build/utils/generateCode';
export const withRNOrientationAppDelegate: ConfigPlugin = (config) => {
return withAppDelegate(config, readAppDelegateFileAndUpdateContents);
};
async function readAppDelegateFileAndUpdateContents(
config: ExportedConfigWithProps<AppDelegateProjectFile>
): Promise<ExportedConfigWithProps<AppDelegateProjectFile>> {
const { modResults: appDelegateFile } = config;
const fileUpdater = getCompatibleFileUpdater(appDelegateFile.language);
if (fileUpdater.language === 'swift') {
const { worker } = fileUpdater;
appDelegateFile.contents = worker(
appDelegateFile.contents,
config.sdkVersion
);
} else {
const { worker } = fileUpdater;
appDelegateFile.contents = worker(appDelegateFile.contents);
}
return config;
}
function getCompatibleFileUpdater(
language: AppDelegateProjectFile['language']
):
| { language: 'swift'; worker: typeof swiftFileUpdater }
| { language: 'objc' | 'objcpp'; worker: typeof objCFileUpdater } {
switch (language) {
case 'objc':
case 'objcpp': {
return {
language,
worker: objCFileUpdater,
};
}
case 'swift':
return {
language,
worker: swiftFileUpdater,
};
default:
throw new Error(
`Cannot add React Native Orientation Director code to AppDelegate of language "${language}"`
);
}
}
export function swiftFileUpdater(
originalContents: string,
sdkVersion?: string
): string {
const methodPrefix = computeMethodPrefix(sdkVersion);
const supportedInterfaceOrientationsForCodeBlock = `\n ${methodPrefix} func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return OrientationDirector.getSupportedInterfaceOrientationsForWindow()
}\n`;
const rightBeforeLastClosingBrace =
/didFinishLaunchingWithOptions:\s*launchOptions\)/g;
const pasteInTheListJustAfterTheClosingBracket = 2;
const results = mergeContents({
tag: '@react-native-orientation-director/supportedInterfaceOrientationsFor-implementation',
src: originalContents,
newSrc: supportedInterfaceOrientationsForCodeBlock,
anchor: rightBeforeLastClosingBrace,
offset: pasteInTheListJustAfterTheClosingBracket,
comment: '// React Native Orientation Director',
});
return results.contents;
function computeMethodPrefix(_sdkVersion?: string) {
if (!_sdkVersion) {
return '';
}
const rawMajor = _sdkVersion.split('.').at(0);
if (!rawMajor) {
return '';
}
const major = Number(rawMajor);
if (Number.isNaN(major)) {
return '';
}
if (major === 53) {
return '';
}
return 'public override';
}
}
export function objCFileUpdater(originalContents: string): string {
const libraryHeaderImportCodeBlock = '#import "OrientationDirector.h"\n';
const rightBeforeAppDelegateImplementation = /@implementation\s+\w+/g;
const headerImportMergeResults = mergeContents({
tag: '@react-native-orientation-director/library-header-import',
src: originalContents,
newSrc: libraryHeaderImportCodeBlock,
anchor: rightBeforeAppDelegateImplementation,
offset: 0,
comment: '// React Native Orientation Director',
});
const supportedInterfaceOrientationsForCodeBlock = `- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return [OrientationDirector getSupportedInterfaceOrientationsForWindow];
}\n`;
const rightBeforeLastClosingEnd = /@end[^@]*$/g;
const implementationMergeResults = mergeContents({
tag: '@react-native-orientation-director/supportedInterfaceOrientationsFor-implementation',
src: headerImportMergeResults.contents,
newSrc: supportedInterfaceOrientationsForCodeBlock,
anchor: rightBeforeLastClosingEnd,
offset: 0,
comment: '// React Native Orientation Director',
});
return implementationMergeResults.contents;
}