// // CParam.m // CParam // // Created by breeze on 2017/6/20. // Copyright © 2017年 Breeze. All rights reserved. // #import "CParam.h" #import #import #import "DataController.h" #import "LocationManager.h" #import static CParam *_instance; @interface CParam () @property (nonatomic) bool hasListeners; @property (nonatomic, strong) NSString *eventName; @property (nonatomic, strong) NSTimer *timer; @property (nonatomic, strong) NSDictionary *cparams; @end @implementation CParam RCT_EXPORT_MODULE(); +(instancetype)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ if (_instance == nil) { _instance = [super allocWithZone:zone]; } }); return _instance; } +(instancetype)shareInstance { return [[self alloc] init]; } // 为了严谨,也要重写copyWithZone 和 mutableCopyWithZone -(id)copyWithZone:(NSZone *)zone { return _instance; } -(id)mutableCopyWithZone:(NSZone *)zone { return _instance; } - (void)dealloc { if (_timer.valid) { [_timer invalidate]; } } + (BOOL)requiresMainQueueSetup { return YES; } - (instancetype)init { if (self = [super init]) { // [LocationManager shareInstance].delegate = self; // // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // [[LocationManager shareInstance] startUpdatingLocation]; // }); // // self.timer = [NSTimer scheduledTimerWithTimeInterval: 300 // target: self // selector: @selector(timeout) // userInfo: nil // repeats: YES]; } return self; } - (void)timeout { [[LocationManager shareInstance] startUpdatingLocation]; } - (NSString *)eventName { return @"GPS_UPDATE"; } - (NSArray *)supportedEvents { return @[self.eventName]; } - (NSDictionary *)constantsToExport { return @{@"GPSUpdateEventName": self.eventName}; } // 在添加第一个监听函数时触发 -(void)startObserving { self.hasListeners = YES; // Set up any upstream listeners or background tasks as necessary } // Will be called when this module's last listener is removed, or on dealloc. -(void)stopObserving { self.hasListeners = NO; // Remove upstream listeners, stop unnecessary background tasks } - (void)updateLocationWithCoordinate:(CLLocationCoordinate2D)coordinate { if (self.hasListeners) { // Only send events if anyone is listening NSNumber *latitude = [NSNumber numberWithFloat:coordinate.latitude]; NSNumber *longitude = [NSNumber numberWithFloat:coordinate.longitude]; [self sendEventWithName: self.eventName body:@{@"lat": latitude, @"lng": longitude}]; } } RCT_EXPORT_METHOD(setNativeCparams: (nonnull NSDictionary *)cparams) { NSMutableDictionary *resDict = [[NSMutableDictionary alloc] init]; for (NSString *key in cparams) { id val = cparams[key]; if (val) { [resDict setValue: val forKey: key]; } } _cparams = [resDict copy]; } - (NSDictionary *)cparams { return _cparams; } @end