Skip to content

Update RN & Refactoring #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
44 changes: 23 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,64 +1,66 @@
# React Native Heading
Get device heading information on iOS or Android

##What
## What
Report back device orientation in degrees, 0-360, with 0 being North.

####Example
```java
const { DeviceEventEmitter } = require('react-native');
const ReactNativeHeading = require('react-native-heading');
#### Example
```javascript
import { NativeEventEmitter } from 'react-native';
import ReactNativeHeading from '@zsajjad/react-native-heading';

//....
componentDidMount() {
this.listener = new NativeEventEmitter(ReactNativeHeading)
ReactNativeHeading.start(1)
.then(didStart => {
this.setState({
headingIsSupported: didStart,
})
})

DeviceEventEmitter.addListener('headingUpdated', data => {
console.log('New heading is:', data.heading);
this.listener.addListener('headingUpdated', heading => {
console.log('New heading is:', heading);
});

}
componentWillUnmount() {
ReactNativeHeading.stop();
DeviceEventEmitter.removeAllListeners('headingUpdated');
this.listener.removeAllListeners('headingUpdated');
}
//...
```


####API
#### API

`start(filter)` - Start receiving heading updates. Accepts an optional filter param (int) to ignore heading changes less than the spcified threshold. The default value is 5. Returns a promise which can be used to determine if heading updates are suported by the device.

`stop` - Stop receiving heaing updates (don't forget to remove the `headingUpdated` listener)


##Setup
## Setup

````
npm install --save react-native-heading
````
```
yarn add https://github.com/zsajjad/react-native-heading.git
```

###iOS
* Run open node_modules/react-native-heading
* Drag ReactNativeHeading.xcodeproj into your Libraries group
### iOS
* Run open node_modules/@zsajjad/react-native-heading
* Drag ReactNativeHeading.xcodeproj into your Libraries group of XCode's project navigator
* In XCode add Libraries/ReactNativeHeading.xcodeproj/Products/libReactNativeHeading.a to the "Link Binary with Libraries" section of the Build Phases

###Android
#####Step 1 - Update Gradle Settings
### Android
##### Step 1 - Update Gradle Settings

```
// file: android/settings.gradle
...

include ':react-native-heading'
project(':react-native-heading').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-heading/android')
project(':react-native-heading').projectDir = new File(rootProject.projectDir, '../node_modules/@zsajjad/react-native-heading/android')
```
#####Step 2 - Update Gradle Build
##### Step 2 - Update Gradle Build

```
// file: android/app/build.gradle
Expand All @@ -69,7 +71,7 @@ dependencies {
compile project(':react-native-heading')
}
```
#####Step 3 - Register React Package
##### Step 3 - Register React Package
```
...
import com.joshblour.reactnativeheading.ReactNativeHeadingPackage; // <--- import
Expand Down
10 changes: 0 additions & 10 deletions ReactNativeHeading.android.js

This file was deleted.

7 changes: 3 additions & 4 deletions ReactNativeHeading.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
// Created by Yonah Forst on 18/02/16.
// Copyright © 2016 Yonah Forst. All rights reserved.
//
#import "RCTBridgeModule.h"
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>

#import <Foundation/Foundation.h>

@interface ReactNativeHeading : NSObject <RCTBridgeModule>
@interface ReactNativeHeading : RCTEventEmitter <RCTBridgeModule>

@end
6 changes: 0 additions & 6 deletions ReactNativeHeading.ios.js

This file was deleted.

12 changes: 12 additions & 0 deletions ReactNativeHeading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
*
* @providesModule ReactNativeHeading
*
*/
'use strict';

import {
NativeModules
} from 'react-native';

export default NativeModules.ReactNativeHeading;
14 changes: 8 additions & 6 deletions ReactNativeHeading.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

#import "ReactNativeHeading.h"

#import "RCTBridge.h"
#import "RCTConvert.h"
#import "RCTEventDispatcher.h"
#import <React/RCTBridge.h>
#import <React/RCTConvert.h>
#import <React/RCTEventEmitter.h>
#import <CoreLocation/CoreLocation.h>


Expand Down Expand Up @@ -54,6 +54,10 @@ - (instancetype)init
[self.locManager stopUpdatingHeading];
}

- (NSArray<NSString *> *)supportedEvents {
return @[@"headingUpdated"];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
if (newHeading.headingAccuracy < 0)
return;
Expand All @@ -62,9 +66,7 @@ - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading
CLLocationDirection heading = ((newHeading.trueHeading > 0) ?
newHeading.trueHeading : newHeading.magneticHeading);

NSDictionary *headingEvent = @{@"heading": @(heading)};

[self.bridge.eventDispatcher sendDeviceEventWithName:@"headingUpdated" body:headingEvent];
[self sendEventWithName:@"headingUpdated" body:@(heading)];
}

@end
11 changes: 4 additions & 7 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ buildscript {
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

compileSdkVersion 26
defaultConfig {
minSdkVersion 18
targetSdkVersion 22
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
Expand All @@ -30,6 +28,5 @@ repositories {
}

dependencies {
compile 'com.facebook.react:react-native:0.20.+'
compile "com.joshblour.discovery:discovery:0.0.3"
implementation 'com.facebook.react:react-native:+'
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.joshblour.discovery.BLEUser;
import com.joshblour.discovery.Discovery;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand All @@ -36,6 +34,7 @@ public class ReactNativeHeadingModule extends ReactContextBaseJavaModule impleme

private static Context mApplicationContext;
private int mAzimuth = 0; // degree
private int newAzimuth = 0; // degree
private int mFilter = 5;
private SensorManager mSensorManager;
private Sensor mSensor;
Expand All @@ -52,9 +51,6 @@ public String getName() {
return "ReactNativeHeading";
}




@ReactMethod
public void start(int filter, Promise promise) {

Expand Down Expand Up @@ -82,21 +78,21 @@ public void onSensorChanged(SensorEvent event) {
// calculate th rotation matrix
SensorManager.getRotationMatrixFromVector(rMat, event.values);
// get the azimuth value (orientation[0]) in degree
int newAzimuth = (int) ( Math.toDegrees( SensorManager.getOrientation( rMat, orientation )[0] ) + 360 ) % 360;

newAzimuth = (int) (((( Math.toDegrees( SensorManager.getOrientation( rMat, orientation )[0] ) + 360 ) % 360) -
( Math.toDegrees( SensorManager.getOrientation( rMat, orientation )[2] ))) +360) % 360;
//dont react to changes smaller than the filter value
if (Math.abs(mAzimuth - newAzimuth) < mFilter) {
return;
}

mAzimuth = newAzimuth;
Log.e("TAG", String.valueOf(newAzimuth));
WritableMap params = Arguments.createMap();
params.putInt("heading", mAzimuth);

getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("headingUpdated", params);
.emit("headingUpdated", (int) newAzimuth);

mAzimuth = newAzimuth;
// Log.e("Azimuth",
// String.valueOf(newAzimuth));

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
modules.add(new ReactNativeHeadingModule(reactContext));
return modules; }

@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Arrays.asList();
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"name": "react-native-heading",
"version": "1.0.0",
"name": "@zsajjad/react-native-heading",
"version": "1.1.0",
"repository": {
"type": "git",
"url": "https://github.com/joshblour/react-native-heading.git"
"url": "https://github.com/zsajjad/react-native-heading.git"
},
"homepage": "https://github.com/zsajjad/react-native-heading.git",
"summary": "RN Package for head sensor detection",
"license": "MIT",
"keywords": ["react-native", "react-component"],
"keywords": ["react-native", "react-component", "heading", "azimuth"],
"main": "ReactNativeHeading",
"author": "Yonah Forst <yonaforst@hotmail.com>"
"author": "Zain Sajjad <zsajjad93@gmail.com>"
}
22 changes: 22 additions & 0 deletions react-native-heading.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'json'

package = JSON.parse(File.read(File.join(__dir__, 'package.json')))

Pod::Spec.new do |s|
s.name = 'react-native-heading'
s.version = package['version']
s.summary = package['summary']
s.description = package['description']
s.license = package['license']
s.author = package['author']
s.homepage = package['homepage']
s.source = { :git => 'https://github.com/zsajjad/react-native-heading', :tag => s.version }

s.requires_arc = true
s.platform = :ios, '8.0'

s.preserve_paths = 'LICENSE', 'README.md', 'package.json', 'ReactNativeHeading.ios.js'
s.source_files = '*.{h,m}'

s.dependency 'React'
end