|
1 | | -import {ManagerRequestOptions, RequestOptions} from "./common"; |
| 1 | +import { ManagerRequestOptions, RequestOptions } from './common'; |
2 | 2 |
|
3 | 3 | export function toSerializeRequestOptions(requestOptions?: RequestOptions, isManager: boolean = false) { |
4 | | - if (global.isAndroid) { |
5 | | - return null; |
6 | | - } |
7 | | - const request = isManager ? GADRequest.request() : GAMRequest.request(); |
| 4 | + if (global.isAndroid) { |
| 5 | + return null; |
| 6 | + } |
| 7 | + const request = isManager ? GADRequest.request() : GAMRequest.request(); |
8 | 8 |
|
9 | | - if (requestOptions?.contentUrl) { |
10 | | - request.contentURL = requestOptions?.contentUrl; |
11 | | - } |
| 9 | + if (requestOptions?.contentUrl) { |
| 10 | + request.contentURL = requestOptions?.contentUrl; |
| 11 | + } |
12 | 12 |
|
13 | | - if (requestOptions?.keywords) { |
14 | | - request.keywords = requestOptions?.keywords as any; |
15 | | - } |
| 13 | + if (requestOptions?.keywords) { |
| 14 | + request.keywords = requestOptions?.keywords as any; |
| 15 | + } |
16 | 16 |
|
17 | | - if (requestOptions?.location) { |
18 | | - request.setLocationWithLatitudeLongitudeAccuracy(requestOptions?.location[0], requestOptions?.location[1], requestOptions?.locationAccuracy); |
19 | | - } |
| 17 | + const nativeExtras = GADExtras.new(); |
| 18 | + let extras = {}; |
20 | 19 |
|
21 | | - const nativeExtras = GADExtras.new(); |
22 | | - let extras = {}; |
| 20 | + if (requestOptions?.requestNonPersonalizedAdsOnly) { |
| 21 | + extras['npa'] = '1'; |
| 22 | + } |
23 | 23 |
|
24 | | - if (requestOptions?.requestNonPersonalizedAdsOnly) { |
25 | | - extras['npa'] = '1'; |
26 | | - } |
| 24 | + if (requestOptions?.networkExtras) { |
| 25 | + extras = Object.assign(extras, requestOptions?.networkExtras); |
| 26 | + } |
27 | 27 |
|
28 | | - if (requestOptions?.networkExtras) { |
29 | | - extras = Object.assign(extras, requestOptions?.networkExtras); |
30 | | - } |
| 28 | + nativeExtras.additionalParameters = extras as any; |
| 29 | + request.registerAdNetworkExtras(nativeExtras); |
31 | 30 |
|
32 | | - nativeExtras.additionalParameters = extras as any; |
33 | | - request.registerAdNetworkExtras(nativeExtras); |
34 | | - |
35 | | - if (requestOptions?.requestAgent) { |
36 | | - request.requestAgent = requestOptions?.requestAgent; |
37 | | - } |
38 | | - return request; |
| 31 | + if (requestOptions?.requestAgent) { |
| 32 | + request.requestAgent = requestOptions?.requestAgent; |
| 33 | + } |
| 34 | + return request; |
39 | 35 | } |
40 | 36 |
|
41 | 37 | export function toSerializeManagerRequestOptions(requestOptions?: ManagerRequestOptions) { |
| 38 | + if (global.isAndroid) { |
| 39 | + return null; |
| 40 | + } |
42 | 41 |
|
43 | | - if (global.isAndroid) { |
44 | | - return null; |
45 | | - } |
46 | | - |
47 | | - const request = toSerializeRequestOptions(requestOptions, true) as GAMRequest; |
| 42 | + const request = toSerializeRequestOptions(requestOptions, true) as GAMRequest; |
48 | 43 |
|
49 | | - if (requestOptions.publisherProvidedId) { |
50 | | - request.publisherProvidedID = requestOptions.publisherProvidedId; |
51 | | - } |
| 44 | + if (requestOptions.publisherProvidedId) { |
| 45 | + request.publisherProvidedID = requestOptions.publisherProvidedId; |
| 46 | + } |
52 | 47 |
|
53 | | - if (requestOptions.customTargeting) { |
54 | | - request.customTargeting = requestOptions.customTargeting as any; |
55 | | - } |
| 48 | + if (requestOptions.customTargeting) { |
| 49 | + request.customTargeting = requestOptions.customTargeting as any; |
| 50 | + } |
56 | 51 |
|
57 | | - if (Array.isArray(requestOptions.categoryExclusions)) { |
58 | | - request.categoryExclusions = requestOptions.categoryExclusions as any; |
59 | | - } |
| 52 | + if (Array.isArray(requestOptions.categoryExclusions)) { |
| 53 | + request.categoryExclusions = requestOptions.categoryExclusions as any; |
| 54 | + } |
60 | 55 |
|
61 | | - return request; |
| 56 | + return request; |
62 | 57 | } |
63 | 58 |
|
64 | | - |
65 | 59 | export const topViewController = (): UIViewController | undefined => { |
66 | | - if (global.isAndroid) { |
67 | | - return undefined; |
68 | | - } |
69 | | - const root = rootViewController(); |
70 | | - if (root == null) { |
71 | | - return undefined; |
72 | | - } |
73 | | - return findTopViewController(root); |
| 60 | + if (global.isAndroid) { |
| 61 | + return undefined; |
| 62 | + } |
| 63 | + const root = rootViewController(); |
| 64 | + if (root == null) { |
| 65 | + return undefined; |
| 66 | + } |
| 67 | + return findTopViewController(root); |
74 | 68 | }; |
75 | 69 |
|
76 | | - |
77 | 70 | const rootViewController = (): UIViewController | undefined => { |
78 | | - const keyWindow = UIApplication.sharedApplication.keyWindow; |
79 | | - return keyWindow != null ? keyWindow.rootViewController : undefined; |
| 71 | + const keyWindow = UIApplication.sharedApplication.keyWindow; |
| 72 | + return keyWindow != null ? keyWindow.rootViewController : undefined; |
80 | 73 | }; |
81 | 74 |
|
82 | | - |
83 | 75 | const findTopViewController = (root: UIViewController): UIViewController | undefined => { |
84 | | - const presented = root.presentedViewController; |
85 | | - if (presented != null) { |
86 | | - return findTopViewController(presented); |
87 | | - } |
88 | | - if (root instanceof UISplitViewController) { |
89 | | - const last = root.viewControllers.lastObject; |
90 | | - if (last == null) { |
91 | | - return root; |
92 | | - } |
93 | | - return findTopViewController(last); |
94 | | - } else if (root instanceof UINavigationController) { |
95 | | - const top = root.topViewController; |
96 | | - if (top == null) { |
97 | | - return root; |
98 | | - } |
99 | | - return findTopViewController(top); |
100 | | - } else if (root instanceof UITabBarController) { |
101 | | - const selected = root.selectedViewController; |
102 | | - if (selected == null) { |
103 | | - return root; |
104 | | - } |
105 | | - return findTopViewController(selected); |
106 | | - } else { |
107 | | - return root; |
108 | | - } |
| 76 | + const presented = root.presentedViewController; |
| 77 | + if (presented != null) { |
| 78 | + return findTopViewController(presented); |
| 79 | + } |
| 80 | + if (root instanceof UISplitViewController) { |
| 81 | + const last = root.viewControllers.lastObject; |
| 82 | + if (last == null) { |
| 83 | + return root; |
| 84 | + } |
| 85 | + return findTopViewController(last); |
| 86 | + } else if (root instanceof UINavigationController) { |
| 87 | + const top = root.topViewController; |
| 88 | + if (top == null) { |
| 89 | + return root; |
| 90 | + } |
| 91 | + return findTopViewController(top); |
| 92 | + } else if (root instanceof UITabBarController) { |
| 93 | + const selected = root.selectedViewController; |
| 94 | + if (selected == null) { |
| 95 | + return root; |
| 96 | + } |
| 97 | + return findTopViewController(selected); |
| 98 | + } else { |
| 99 | + return root; |
| 100 | + } |
109 | 101 | }; |
0 commit comments