Skip to content

Commit 066c2d9

Browse files
author
alvaromb
committed
Starting to add native libs for iOS
1 parent 4306179 commit 066c2d9

14 files changed

+576
-0
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pbxproj -text

.gitignore

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
# OSX
3+
#
4+
.DS_Store
5+
6+
# node.js
7+
#
8+
node_modules/
9+
npm-debug.log
10+
yarn-error.log
11+
12+
13+
# Xcode
14+
#
15+
build/
16+
*.pbxuser
17+
!default.pbxuser
18+
*.mode1v3
19+
!default.mode1v3
20+
*.mode2v3
21+
!default.mode2v3
22+
*.perspectivev3
23+
!default.perspectivev3
24+
xcuserdata
25+
*.xccheckout
26+
*.moved-aside
27+
DerivedData
28+
*.hmap
29+
*.ipa
30+
*.xcuserstate
31+
project.xcworkspace
32+
33+
34+
# Android/IntelliJ
35+
#
36+
build/
37+
.idea
38+
.gradle
39+
local.properties
40+
*.iml
41+
42+
# BUCK
43+
buck-out/
44+
\.buckd/
45+
*.keystore
46+

README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
# react-native-interactive-image-library
3+
4+
## Getting started
5+
6+
`$ npm install react-native-interactive-image-library --save`
7+
8+
### Mostly automatic installation
9+
10+
`$ react-native link react-native-interactive-image-library`
11+
12+
### Manual installation
13+
14+
15+
#### iOS
16+
17+
1. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
18+
2. Go to `node_modules``react-native-interactive-image-library` and add `RNIKInteractiveImageLibrary.xcodeproj`
19+
3. In XCode, in the project navigator, select your project. Add `libRNIKInteractiveImageLibrary.a` to your project's `Build Phases``Link Binary With Libraries`
20+
4. Run your project (`Cmd+R`)<
21+
22+
#### Android
23+
24+
1. Open up `android/app/src/main/java/[...]/MainActivity.java`
25+
- Add `import com.apsl.interfacekit.RNIKInteractiveImageLibraryPackage;` to the imports at the top of the file
26+
- Add `new RNIKInteractiveImageLibraryPackage()` to the list returned by the `getPackages()` method
27+
2. Append the following lines to `android/settings.gradle`:
28+
```
29+
include ':react-native-interactive-image-library'
30+
project(':react-native-interactive-image-library').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-interactive-image-library/android')
31+
```
32+
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
33+
```
34+
compile project(':react-native-interactive-image-library')
35+
```
36+
37+
38+
## Usage
39+
```javascript
40+
import RNIKInteractiveImageLibrary from 'react-native-interactive-image-library';
41+
42+
// TODO: What to do with the module?
43+
RNIKInteractiveImageLibrary;
44+
```
45+

android/build.gradle

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
buildscript {
3+
repositories {
4+
jcenter()
5+
}
6+
7+
dependencies {
8+
classpath 'com.android.tools.build:gradle:1.3.1'
9+
}
10+
}
11+
12+
apply plugin: 'com.android.library'
13+
14+
android {
15+
compileSdkVersion 23
16+
buildToolsVersion "23.0.1"
17+
18+
defaultConfig {
19+
minSdkVersion 16
20+
targetSdkVersion 22
21+
versionCode 1
22+
versionName "1.0"
23+
}
24+
lintOptions {
25+
abortOnError false
26+
}
27+
}
28+
29+
repositories {
30+
mavenCentral()
31+
}
32+
33+
dependencies {
34+
compile 'com.facebook.react:react-native:+'
35+
}
36+

android/src/main/AndroidManifest.xml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.apsl.interfacekit">
4+
5+
</manifest>
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
package com.apsl.interfacekit;
3+
4+
import com.facebook.react.bridge.ReactApplicationContext;
5+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
6+
import com.facebook.react.bridge.ReactMethod;
7+
import com.facebook.react.bridge.Callback;
8+
9+
public class RNIKInteractiveImageLibraryModule extends ReactContextBaseJavaModule {
10+
11+
private final ReactApplicationContext reactContext;
12+
13+
public RNIKInteractiveImageLibraryModule(ReactApplicationContext reactContext) {
14+
super(reactContext);
15+
this.reactContext = reactContext;
16+
}
17+
18+
@Override
19+
public String getName() {
20+
return "RNIKInteractiveImageLibrary";
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
package com.apsl.interfacekit;
3+
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
import com.facebook.react.ReactPackage;
9+
import com.facebook.react.bridge.NativeModule;
10+
import com.facebook.react.bridge.ReactApplicationContext;
11+
import com.facebook.react.uimanager.ViewManager;
12+
import com.facebook.react.bridge.JavaScriptModule;
13+
public class RNIKInteractiveImageLibraryPackage implements ReactPackage {
14+
@Override
15+
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
16+
return Arrays.<NativeModule>asList(new RNIKInteractiveImageLibraryModule(reactContext));
17+
}
18+
19+
// Deprecated from RN 0.47
20+
public List<Class<? extends JavaScriptModule>> createJSModules() {
21+
return Collections.emptyList();
22+
}
23+
24+
@Override
25+
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
26+
return Collections.emptyList();
27+
}
28+
}

index.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
import { NativeModules } from 'react-native';
3+
4+
const { RNIKInteractiveImageLibrary } = NativeModules;
5+
6+
export default RNIKInteractiveImageLibrary;

ios/RNIKInteractiveImageLibrary.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
#if __has_include("RCTBridgeModule.h")
3+
#import "RCTBridgeModule.h"
4+
#else
5+
#import <React/RCTBridgeModule.h>
6+
#endif
7+
#import <CoreMotion/CoreMotion.h>
8+
#import <QuartzCore/QuartzCore.h>
9+
10+
@interface RNIKInteractiveImageLibrary : NSObject <RCTBridgeModule>
11+
12+
@property (strong, nonatomic) CMMotionManager *motionManager;
13+
@property (strong, nonatomic) CADisplayLink *motionDisplayLink;
14+
@property (nonatomic) double motionLastYaw;
15+
16+
@end
17+

ios/RNIKInteractiveImageLibrary.m

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
#import "RNIKInteractiveImageLibrary.h"
3+
#import <React/RCTBridge.h>
4+
#import <React/RCTEventDispatcher.h>
5+
6+
@implementation RNIKInteractiveImageLibrary
7+
8+
- (dispatch_queue_t)methodQueue
9+
{
10+
return dispatch_get_main_queue();
11+
}
12+
13+
RCT_EXPORT_MODULE();
14+
15+
- (id)init {
16+
if (self = [super init]) {
17+
self.motionManager = [[CMMotionManager alloc] init];
18+
self.motionManager.deviceMotionUpdateInterval = 0.02; // 50 Hz
19+
}
20+
return self;
21+
}
22+
23+
RCT_EXPORT_METHOD(startYawUpdates) {
24+
self.motionDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(motionRefresh:)];
25+
[self.motionDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
26+
if ([self.motionManager isDeviceMotionActive]) {
27+
// To avoid using more CPU than necessary we use
28+
// `CMAttitudeReferenceFrameXArbitraryZVertical`
29+
[self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical];
30+
}
31+
}
32+
33+
- (void)motionRefresh:(id)sender {
34+
CMQuaternion quat = self.motionManager.deviceMotion.attitude.quaternion;
35+
double yaw = asin(2*(quat.x*quat.z - quat.w*quat.y));
36+
37+
if (self.motionLastYaw == 0) {
38+
self.motionLastYaw = yaw;
39+
}
40+
41+
// Kalman filtering
42+
static float q = 0.1; // process noise
43+
static float r = 0.1; // sensor noise
44+
static float p = 0.1; // estimated error
45+
static float k = 0.5; // kalman filter gain
46+
47+
float x = self.motionLastYaw;
48+
p = p + q;
49+
k = p / (p + r);
50+
x = x + k*(yaw - x);
51+
p = (1 - k)*p;
52+
self.motionLastYaw = x;
53+
[self.bridge.eventDispatcher sendDeviceEventWithName:@"MotionManager"
54+
body:@{@"yaw": [NSNumber numberWithDouble:self.motionLastYaw]}];
55+
}
56+
57+
RCT_EXPORT_METHOD(stopYawUpdates) {
58+
[self.motionDisplayLink invalidate];
59+
}
60+
61+
@end
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
Pod::Spec.new do |s|
3+
s.name = "RNIKInteractiveImageLibrary"
4+
s.version = "1.0.0"
5+
s.summary = "RNIKInteractiveImageLibrary"
6+
s.description = <<-DESC
7+
RNIKInteractiveImageLibrary
8+
DESC
9+
s.homepage = ""
10+
s.license = "MIT"
11+
# s.license = { :type => "MIT", :file => "FILE_LICENSE" }
12+
s.author = { "author" => "[email protected]" }
13+
s.platform = :ios, "7.0"
14+
s.source = { :git => "https://github.com/author/RNIKInteractiveImageLibrary.git", :tag => "master" }
15+
s.source_files = "RNIKInteractiveImageLibrary/**/*.{h,m}"
16+
s.requires_arc = true
17+
18+
19+
s.dependency "React"
20+
#s.dependency "others"
21+
22+
end
23+
24+

0 commit comments

Comments
 (0)