-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathutils.ts
102 lines (85 loc) · 2.72 KB
/
utils.ts
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
import { ManagerRequestOptions, RequestOptions } from '.';
export function toSerializeRequestOptions(requestOptions?: RequestOptions, isManager: boolean = false) {
if (global.isAndroid) {
return null;
}
const request = isManager ? GADRequest.request() : GAMRequest.request();
if (requestOptions?.contentUrl) {
request.contentURL = requestOptions?.contentUrl;
}
if (requestOptions?.keywords) {
request.keywords = requestOptions?.keywords as any;
}
const nativeExtras = GADExtras.new();
let extras = {};
if (requestOptions?.requestNonPersonalizedAdsOnly) {
extras['npa'] = '1';
}
if (requestOptions?.networkExtras) {
extras = Object.assign(extras, requestOptions?.networkExtras);
}
nativeExtras.additionalParameters = extras as any;
request.registerAdNetworkExtras(nativeExtras);
if (requestOptions?.requestAgent) {
request.requestAgent = requestOptions?.requestAgent;
}
return request;
}
export function toSerializeManagerRequestOptions(requestOptions?: ManagerRequestOptions) {
if (global.isAndroid) {
return null;
}
const request = toSerializeRequestOptions(requestOptions, true) as GAMRequest;
if (requestOptions.publisherProvidedId) {
request.publisherProvidedID = requestOptions.publisherProvidedId;
}
if (requestOptions.customTargeting) {
request.customTargeting = requestOptions.customTargeting as any;
}
if (Array.isArray(requestOptions.categoryExclusions)) {
request.categoryExclusions = requestOptions.categoryExclusions as any;
}
return request;
}
export const topViewController = (): UIViewController | undefined => {
if (global.isAndroid) {
return undefined;
}
const root = rootViewController();
if (root == null) {
return undefined;
}
return findTopViewController(root);
};
const rootViewController = (): UIViewController | undefined => {
const app = UIApplication.sharedApplication;
const window = app.keyWindow || (app.windows.count > 0 && app.windows[0]);
return window != null ? window.rootViewController : undefined;
};
const findTopViewController = (root: UIViewController): UIViewController | undefined => {
const presented = root.presentedViewController;
if (presented != null) {
return findTopViewController(presented);
}
if (root instanceof UISplitViewController) {
const last = root.viewControllers.lastObject;
if (last == null) {
return root;
}
return findTopViewController(last);
} else if (root instanceof UINavigationController) {
const top = root.topViewController;
if (top == null) {
return root;
}
return findTopViewController(top);
} else if (root instanceof UITabBarController) {
const selected = root.selectedViewController;
if (selected == null) {
return root;
}
return findTopViewController(selected);
} else {
return root;
}
};