-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
200 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,29 @@ | ||
import { NativeModules } from 'react-native'; | ||
import { NativeModules, NativeEventEmitter } from 'react-native'; | ||
|
||
const { CompassHeading } = NativeModules; | ||
|
||
let listener; | ||
|
||
//Monkey patching | ||
let _start = CompassHeading.start; | ||
CompassHeading.start = (update_rate, callback) => { | ||
if (listener) { | ||
CompassHeading.stop(); | ||
} | ||
|
||
const compassEventEmitter = new NativeEventEmitter(CompassHeading); | ||
listener = compassEventEmitter.addListener('HeadingUpdated', (degree) => { | ||
callback(degree); | ||
}); | ||
|
||
_start(update_rate === null ? 0 : update_rate); | ||
} | ||
|
||
let _stop = CompassHeading.stop; | ||
CompassHeading.stop = () => { | ||
listener && listener.remove(); | ||
listener = null; | ||
_stop(); | ||
} | ||
|
||
export default CompassHeading; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#import <React/RCTBridgeModule.h> | ||
#import <React/RCTEventEmitter.h> | ||
|
||
@interface CompassHeading : NSObject <RCTBridgeModule> | ||
@interface CompassHeading : RCTEventEmitter <RCTBridgeModule> | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,76 @@ | ||
#import "CompassHeading.h" | ||
#import <React/RCTEventDispatcher.h> | ||
#import <Corelocation/CoreLocation.h> | ||
|
||
#define kHeadingUpdated @"HeadingUpdated" | ||
|
||
@interface CompassHeading() <CLLocationManagerDelegate> | ||
@property (strong, nonatomic) CLLocationManager *locationManager; | ||
@end | ||
|
||
@implementation CompassHeading | ||
|
||
RCT_EXPORT_MODULE() | ||
|
||
RCT_EXPORT_METHOD(sampleMethod:(NSString *)stringArgument numberParameter:(nonnull NSNumber *)numberArgument callback:(RCTResponseSenderBlock)callback) | ||
- (instancetype)init { | ||
if (self = [super init]) { | ||
if ([CLLocationManager headingAvailable]) { | ||
self.locationManager = [[CLLocationManager alloc] init]; | ||
self.locationManager.delegate = self; | ||
} | ||
else { | ||
NSLog(@"Heading not available"); | ||
} | ||
} | ||
|
||
return self; | ||
} | ||
|
||
#pragma mark - RCTEventEmitter | ||
|
||
- (NSArray<NSString *> *)supportedEvents { | ||
return @[kHeadingUpdated]; | ||
} | ||
|
||
#pragma mark - CLLocationManagerDelegate | ||
|
||
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { | ||
if (newHeading.headingAccuracy < 0) { | ||
return; | ||
} | ||
[self sendEventWithName:kHeadingUpdated body:@(newHeading.trueHeading)]; | ||
} | ||
|
||
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { | ||
NSLog(@"AuthoriationStatus changed: %i", status); | ||
} | ||
|
||
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { | ||
NSLog(@"Location manager failed: %@", error); | ||
} | ||
|
||
- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager | ||
{ | ||
CLLocationDirection accuracy = [[manager heading] headingAccuracy]; | ||
return false; //accuracy <= 0.0f || accuracy > 10.0f; | ||
} | ||
|
||
#pragma mark - React | ||
|
||
RCT_EXPORT_METHOD(start: (NSInteger) headingFilter) { | ||
self.locationManager.headingFilter = headingFilter; | ||
[self.locationManager startUpdatingHeading]; | ||
} | ||
|
||
RCT_EXPORT_METHOD(stop) { | ||
[self.locationManager stopUpdatingHeading]; | ||
} | ||
|
||
RCT_EXPORT_MODULE() | ||
|
||
+ (BOOL)requiresMainQueueSetup | ||
{ | ||
// TODO: Implement some actually useful functionality | ||
callback(@[[NSString stringWithFormat: @"numberArgument: %@ stringArgument: %@", numberArgument, stringArgument]]); | ||
return NO; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters