| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- //
- // CParam.m
- // CParam
- //
- // Created by breeze on 2017/6/20.
- // Copyright © 2017年 Breeze. All rights reserved.
- //
- #import "CParam.h"
- #import <React/RCTBridge.h>
- #import <React/RCTEventDispatcher.h>
- #import "DataController.h"
- #import "LocationManager.h"
- #import <Foundation/NSTimer.h>
- static CParam *_instance;
- @interface CParam () <LocationManagerDelegate>
- @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<NSString *> *)supportedEvents {
- return @[self.eventName];
- }
- - (NSDictionary<NSString *, id> *)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
|