Skip to content

Commit eb7644a

Browse files
committed
Initial commit
0 parents  commit eb7644a

30 files changed

+1483
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pbxproj -text

.gitignore

Lines changed: 46 additions & 0 deletions
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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
# react-native-launch-darkly
3+
4+
React Native wrapper over LaunchDarkly SDK's for iOS and Android.
5+
6+
[LaunchDarkly](https://launchdarkly.com)
7+
8+
[Native iOS SDK](https://github.com/launchdarkly/ios-client)
9+
10+
[Native Android SDK](https://github.com/launchdarkly/android-client)
11+
12+
## Getting started
13+
14+
`$ npm install react-native-launch-darkly --save`
15+
16+
or
17+
18+
``$ yarn add react-native-launch-darkly --save``
19+
20+
### Mostly automatic installation
21+
22+
`$ react-native link react-native-launch-darkly`
23+
24+
#### iOS:
25+
26+
1) In XCode in project navigator right click on application name => Add files to... => navigate to node_modules/react-native-launch-darkly/ios and select Darkly.framework.
27+
2) Go to you project target and add Darkly.framework to Embedded Binaries
28+
29+
#### Android:
30+
31+
Add line bellow at the bottom of your app/build.gradle
32+
```
33+
configurations.all { resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.4.1' }
34+
```
35+
36+
### Manual installation
37+
38+
39+
#### iOS
40+
41+
1. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
42+
2. Go to `node_modules``react-native-launch-darkly` and add `RNLaunchDarkly.xcodeproj`
43+
3. In XCode, in the project navigator, select your project. Add `libRNLaunchDarkly.a` to your project's `Build Phases``Link Binary With Libraries`
44+
4. Run your project (`Cmd+R`)<
45+
46+
#### Android
47+
48+
1. Open up `android/app/src/main/java/[...]/MainActivity.java`
49+
- Add `import com.reactlibrary.RNLaunchDarklyPackage;` to the imports at the top of the file
50+
- Add `new RNLaunchDarklyPackage()` to the list returned by the `getPackages()` method
51+
2. Append the following lines to `android/settings.gradle`:
52+
```
53+
include ':react-native-launch-darkly'
54+
project(':react-native-launch-darkly').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-launch-darkly/android')
55+
```
56+
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
57+
```
58+
compile project(':react-native-launch-darkly')
59+
```
60+
4. Add line bellow at the bottom of your app/build.gradle
61+
```
62+
configurations.all { resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.4.1' }
63+
```
64+
65+
66+
## Usage
67+
```javascript
68+
import LaunchDarkly from 'react-native-launch-darkly';
69+
70+
type User = {
71+
key: string,
72+
email?: string,
73+
firstName?: string,
74+
lastName?: string,
75+
isAnonymous?: bool
76+
};
77+
78+
// init native SDK with api key and user object
79+
LaunchDarkly.configure(apiKey: string, user: User);
80+
81+
// get feature flag value
82+
LaunchDarkly.boolVariation(featureFlagName: string, callback: function): bool
83+
84+
// adds listener which is called every time feature flag value is changed
85+
LaunchDarkly.addFeatureFlagChangeListener(callback: function): void
86+
87+
// removes onFeatureFlagChange listener
88+
LaunchDarkly.addFeatureFlagChangeListener(): void
89+
```

android/build.gradle

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
compile('com.launchdarkly:launchdarkly-android-client:2.0.1') {
36+
exclude group: 'com.squareup.okhttp', module: 'okhttp'
37+
}
38+
39+
}

android/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
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.reactlibrary">
4+
5+
</manifest>
6+
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
2+
package com.reactlibrary;
3+
4+
import android.app.Application;
5+
import android.util.Log;
6+
7+
import com.facebook.react.bridge.Arguments;
8+
import com.facebook.react.bridge.ReactApplicationContext;
9+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
10+
import com.facebook.react.bridge.ReactMethod;
11+
import com.facebook.react.bridge.Callback;
12+
import com.facebook.react.bridge.ReadableMap;
13+
import com.facebook.react.bridge.WritableMap;
14+
import com.facebook.react.modules.core.DeviceEventManagerModule;
15+
import com.launchdarkly.android.FeatureFlagChangeListener;
16+
import com.launchdarkly.android.LDClient;
17+
import com.launchdarkly.android.LDConfig;
18+
import com.launchdarkly.android.LDUser;
19+
import com.launchdarkly.android.LaunchDarklyException;
20+
21+
import java.util.Collections;
22+
23+
public class RNLaunchDarklyModule extends ReactContextBaseJavaModule {
24+
25+
private final ReactApplicationContext reactContext;
26+
private LDClient ldClient;
27+
private LDUser user;
28+
29+
public RNLaunchDarklyModule(ReactApplicationContext reactContext) {
30+
super(reactContext);
31+
this.reactContext = reactContext;
32+
}
33+
34+
@Override
35+
public String getName() {
36+
return "RNLaunchDarkly";
37+
}
38+
39+
@ReactMethod
40+
public void configure(String apiKey, ReadableMap options) {
41+
LDConfig ldConfig = new LDConfig.Builder()
42+
.setMobileKey(apiKey)
43+
.build();
44+
45+
LDUser.Builder userBuilder = new LDUser.Builder(options.getString("key"));
46+
47+
if (options.hasKey("email")) {
48+
userBuilder = userBuilder.email(options.getString("getString"));
49+
}
50+
51+
if (options.hasKey("firstName")) {
52+
userBuilder = userBuilder.firstName(options.getString("firstName"));
53+
}
54+
55+
if (options.hasKey("lastName")) {
56+
userBuilder = userBuilder.lastName(options.getString("lastName"));
57+
}
58+
59+
if (options.hasKey("isAnonymous")) {
60+
userBuilder = userBuilder.anonymous(options.getBoolean("isAnonymous"));
61+
}
62+
63+
if (user != null && ldClient != null) {
64+
user = userBuilder.build();
65+
ldClient.identify(user);
66+
67+
return;
68+
}
69+
70+
user = userBuilder.build();
71+
72+
Application application = reactContext.getCurrentActivity().getApplication();
73+
74+
if (application != null) {
75+
ldClient = LDClient.init(application, ldConfig, user, 0);
76+
} else {
77+
Log.d("RNLaunchDarklyModule", "Couldn't init RNLaunchDarklyModule cause application was null");
78+
}
79+
}
80+
81+
@ReactMethod
82+
public void addFeatureFlagChangeListener (String flagName) {
83+
FeatureFlagChangeListener listener = new FeatureFlagChangeListener() {
84+
@Override
85+
public void onFeatureFlagChange(String flagKey) {
86+
WritableMap result = Arguments.createMap();
87+
result.putString("flagName", flagKey);
88+
89+
getReactApplicationContext()
90+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
91+
.emit("FeatureFlagChanged", result);
92+
}
93+
};
94+
95+
try {
96+
LDClient.get().registerFeatureFlagListener(flagName, listener);
97+
} catch (LaunchDarklyException e) {
98+
Log.d("RNLaunchDarklyModule", e.getMessage());
99+
e.printStackTrace();
100+
}
101+
}
102+
103+
@ReactMethod
104+
public void boolVariation(String flagName, Callback callback) {
105+
Boolean variationResult = ldClient.boolVariation(flagName, false);
106+
callback.invoke(variationResult);
107+
}
108+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
package com.reactlibrary;
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 RNLaunchDarklyPackage implements ReactPackage {
14+
@Override
15+
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
16+
return Arrays.<NativeModule>asList(new RNLaunchDarklyModule(reactContext));
17+
}
18+
19+
@Override
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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { NativeModules, NativeEventEmitter } from 'react-native';
2+
3+
const { RNLaunchDarkly } = NativeModules;
4+
5+
class LaunchDarkly {
6+
constructor () {
7+
this.emitter = new NativeEventEmitter(RNLaunchDarkly);
8+
}
9+
10+
configure (apiKey, options) {
11+
RNLaunchDarkly.configure(apiKey, options);
12+
}
13+
14+
boolVariation (featureName, callback) {
15+
RNLaunchDarkly.boolVariation(featureName, callback);
16+
}
17+
18+
addFeatureFlagChangeListener (callback) {
19+
this.featureFlagChangeListener = this.emitter.addListener(
20+
'FeatureFlagChanged',
21+
result => callback(result.flagName),
22+
);
23+
}
24+
25+
removeFeatureFlagChangeListener () {
26+
if (this.featureFlagChangeListener) {
27+
this.featureFlagChangeListener.remove();
28+
this.featureFlagChangeListener = null;
29+
}
30+
}
31+
}
32+
33+
export default new LaunchDarkly();

ios/Darkly.framework/Darkly

1.67 MB
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//
2+
// Copyright © 2015 Catamorphic Co. All rights reserved.
3+
//
4+
5+
#ifdef __OBJC__
6+
#import <Foundation/Foundation.h>
7+
#import "DarklyConstants.h"
8+
#import "NSDictionary+JSON.h"
9+
#endif

0 commit comments

Comments
 (0)