-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathNavAutoModule.m
More file actions
408 lines (361 loc) · 12.3 KB
/
NavAutoModule.m
File metadata and controls
408 lines (361 loc) · 12.3 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "NavAutoModule.h"
#import "NavAutoEventDispatcher.h"
#import "NavViewController.h"
@implementation NavAutoModule
RCT_EXPORT_MODULE(NavAutoModule);
// Static instance of the NavAutoModule to allow access from another modules.
static NavAutoModule *sharedInstance = nil;
static NavAutoModuleReadyCallback _navAutoModuleReadyCallback;
static NavAutoEventDispatcher *_eventDispatcher;
+ (id)allocWithZone:(NSZone *)zone {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_eventDispatcher = [NavAutoEventDispatcher allocWithZone:zone];
sharedInstance = [super allocWithZone:zone];
});
if (_navAutoModuleReadyCallback) {
_navAutoModuleReadyCallback();
}
return sharedInstance;
}
// Method to get the shared instance
+ (instancetype)sharedInstance {
return sharedInstance;
}
// Get or create the shared instance
+ (instancetype)getOrCreateSharedInstance {
if (sharedInstance == nil) {
sharedInstance = [[NavAutoModule allocWithZone:nil] init];
}
return sharedInstance;
}
- (void)registerViewController:(NavViewController *)vc {
self.viewController = vc;
[self onScreenStateChange:true];
}
- (void)unRegisterViewController {
self.viewController = nil;
[self onScreenStateChange:false];
}
+ (void)registerNavAutoModuleReadyCallback:(NavAutoModuleReadyCallback)callback {
_navAutoModuleReadyCallback = [callback copy];
}
+ (void)unregisterNavAutoModuleReadyCallback {
_navAutoModuleReadyCallback = nil;
}
RCT_EXPORT_METHOD(setMapType : (NSInteger)mapType) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
GMSMapViewType mapViewType;
switch (mapType) {
case 1:
mapViewType = kGMSTypeNormal;
break;
case 2:
mapViewType = kGMSTypeSatellite;
break;
case 3:
mapViewType = kGMSTypeTerrain;
break;
case 4:
mapViewType = kGMSTypeHybrid;
break;
default:
mapViewType = kGMSTypeNone;
break;
}
[self->_viewController setMapType:mapViewType];
}
});
}
RCT_EXPORT_METHOD(setMapStyle : (NSString *)jsonStyleString) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
NSError *error;
GMSMapStyle *mapStyle = [GMSMapStyle styleWithJSONString:jsonStyleString error:&error];
if (!mapStyle) {
return;
}
[self->_viewController setMapStyle:mapStyle];
}
});
}
RCT_EXPORT_METHOD(clearMapView) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController clearMapView];
}
});
}
RCT_EXPORT_METHOD(addMarker : (NSDictionary *)markerOptions resolver : (RCTPromiseResolveBlock)
resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController addMarker:markerOptions
result:^(NSDictionary *result) {
resolve(result);
}];
} else {
reject(@"no_view_controller", @"No viewController found", nil);
}
});
}
RCT_EXPORT_METHOD(addCircle : (NSDictionary *)circleOptions resolver : (RCTPromiseResolveBlock)
resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController addCircle:circleOptions
result:^(NSDictionary *result) {
resolve(result);
}];
} else {
reject(@"no_view_controller", @"No viewController found", nil);
}
});
}
RCT_EXPORT_METHOD(addPolyline : (NSDictionary *)polylineOptions resolver : (RCTPromiseResolveBlock)
resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController addPolyline:polylineOptions
result:^(NSDictionary *result) {
resolve(result);
}];
} else {
reject(@"no_view_controller", @"No viewController found", nil);
}
});
}
RCT_EXPORT_METHOD(addPolygon : (NSDictionary *)polygonOptions resolver : (RCTPromiseResolveBlock)
resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController addPolygon:polygonOptions
result:^(NSDictionary *result) {
resolve(result);
}];
} else {
reject(@"no_view_controller", @"No viewController found", nil);
}
});
}
RCT_EXPORT_METHOD(removeMarker : (NSString *)markerId) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController removeMarker:markerId];
}
});
}
RCT_EXPORT_METHOD(removePolygon : (NSString *)polygonId) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController removePolygon:polygonId];
}
});
}
RCT_EXPORT_METHOD(removePolyline : (NSString *)polylineId) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController removePolyline:polylineId];
}
});
}
RCT_EXPORT_METHOD(removeCircle : (NSString *)circleId) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController removeCircle:circleId];
}
});
}
RCT_EXPORT_METHOD(setIndoorEnabled : (BOOL *)enabled) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController setIndoorEnabled:enabled];
}
});
}
RCT_EXPORT_METHOD(setTrafficEnabled : (BOOL *)enabled) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController setTrafficEnabled:enabled];
}
});
}
RCT_EXPORT_METHOD(setCompassEnabled : (BOOL *)enabled) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController setCompassEnabled:enabled];
}
});
}
RCT_EXPORT_METHOD(setMyLocationButtonEnabled : (BOOL *)enabled) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController setMyLocationEnabled:enabled];
}
});
}
RCT_EXPORT_METHOD(setMyLocationEnabled : (BOOL *)enabled) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController setMyLocationEnabled:enabled];
}
});
}
RCT_EXPORT_METHOD(setRotateGesturesEnabled : (BOOL *)enabled) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController setRotateGesturesEnabled:enabled];
}
});
}
RCT_EXPORT_METHOD(setScrollGesturesEnabled : (BOOL *)enabled) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController setScrollGesturesEnabled:enabled];
}
});
}
RCT_EXPORT_METHOD(setScrollGesturesEnabledDuringRotateOrZoom : (BOOL *)enabled) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController setScrollGesturesEnabledDuringRotateOrZoom:enabled];
}
});
}
RCT_EXPORT_METHOD(setZoomLevel : (nonnull NSNumber *)level resolver : (RCTPromiseResolveBlock)
resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController setZoomLevel:level];
} else {
reject(@"no_view_controller", @"No viewController found", nil);
}
});
}
RCT_EXPORT_METHOD(setTiltGesturesEnabled : (BOOL *)enabled) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController setTiltGesturesEnabled:enabled];
}
});
}
RCT_EXPORT_METHOD(setZoomGesturesEnabled : (BOOL *)enabled) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController setZoomGesturesEnabled:enabled];
}
});
}
RCT_EXPORT_METHOD(setBuildingsEnabled : (BOOL *)enabled) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController setBuildingsEnabled:enabled];
}
});
}
RCT_EXPORT_METHOD(getCameraPosition : (RCTPromiseResolveBlock)
resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController getCameraPosition:^(NSDictionary *result) {
resolve(result);
}];
} else {
reject(@"no_view_controller", @"No viewController found", nil);
}
});
}
RCT_EXPORT_METHOD(getMyLocation : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)
reject) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController getMyLocation:^(NSDictionary *_Nullable result) {
resolve(result);
}];
} else {
reject(@"no_view_controller", @"No viewController found", nil);
}
});
}
RCT_EXPORT_METHOD(getUiSettings : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)
reject) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController getUiSettings:^(NSDictionary *_Nullable result) {
resolve(result);
}];
} else {
reject(@"no_view_controller", @"No viewController found", nil);
}
});
}
RCT_EXPORT_METHOD(isMyLocationEnabled : (RCTPromiseResolveBlock)
resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController isMyLocationEnabled:^(BOOL result) {
resolve([NSNumber numberWithBool:result]);
}];
} else {
reject(@"no_view_controller", @"No viewController found", nil);
}
});
}
RCT_EXPORT_METHOD(moveCamera : (NSDictionary *)cameraPosition) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController moveCamera:cameraPosition];
}
});
}
RCT_EXPORT_METHOD(isAutoScreenAvailable : (RCTPromiseResolveBlock)
resolve rejecter : (RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
BOOL hasViewController = self->_viewController != nil;
resolve([NSNumber numberWithBool:hasViewController]);
});
}
RCT_EXPORT_METHOD(setPadding : (nonnull NSNumber *)top left : (nonnull NSNumber *)
left bottom : (nonnull NSNumber *)bottom right : (nonnull NSNumber *)right) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController setPadding:UIEdgeInsetsMake(top.floatValue, left.floatValue,
bottom.floatValue, right.floatValue)];
}
});
}
- (void)onScreenStateChange:(BOOL)available {
[self sendCommandToReactNative:@"onAutoScreenAvailabilityChanged"
args:[NSNumber numberWithBool:available]];
}
- (void)onCustomNavigationAutoEvent:(NSString *)type data:(nullable NSDictionary *)data {
NSMutableDictionary *map = [NSMutableDictionary dictionary];
[map setObject:type forKey:@"type"];
[map setObject:(data != nil ? data : [NSNull null]) forKey:@"data"];
[self sendCommandToReactNative:@"onCustomNavigationAutoEvent" args:map];
}
/*
* Method to send command to React Native using the NavEventDispatcher instance.
*/
- (void)sendCommandToReactNative:(NSString *)command args:(NSObject *)args {
if (_eventDispatcher != NULL) {
[_eventDispatcher sendEventName:command body:args];
}
}
@end