Skip to content
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

added constants, use Promises instead of Callbacks #365

Open
wants to merge 1 commit 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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default class AppScreen extends Component {
},

componentWillUnmount() {
Orientation.getOrientation((err, orientation) => {
Orientation.getOrientation().then(orientation => {
console.log(`Current Device Orientation: ${orientation}`);
});

Expand Down Expand Up @@ -217,5 +217,7 @@ removeSpecificOrientationListener((specificOrientation) => {});
- `lockToLandscapeLeft()`
- `lockToLandscapeRight()`
- `unlockAllOrientations()`
- `getOrientation((err, orientation) => {})`
- `getSpecificOrientation((err, specificOrientation) => {})`
- `getOrientation().then(orientation => {})`
- `getSpecificOrientation().then(orientation => {})` iOS only
- `LANDSCAPE` constant
- `PORTRAIT` constant
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.Promise;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -27,6 +27,8 @@

public class OrientationModule extends ReactContextBaseJavaModule implements LifecycleEventListener{
final BroadcastReceiver receiver;
final String LANDSCAPE = "LANDSCAPE";
final String PORTRAIT = "PORTRAIT";

public OrientationModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand All @@ -38,7 +40,7 @@ public void onReceive(Context context, Intent intent) {
Configuration newConfig = intent.getParcelableExtra("newConfig");
Log.d("receiver", String.valueOf(newConfig.orientation));

String orientationValue = newConfig.orientation == 1 ? "PORTRAIT" : "LANDSCAPE";
String orientationValue = newConfig.orientation == 1 ? PORTRAIT : LANDSCAPE;

WritableMap params = Arguments.createMap();
params.putString("orientation", orientationValue);
Expand All @@ -58,16 +60,10 @@ public String getName() {
}

@ReactMethod
public void getOrientation(Callback callback) {
public void getOrientation(final Promise promise) {
final int orientationInt = getReactApplicationContext().getResources().getConfiguration().orientation;

String orientation = this.getOrientationString(orientationInt);

if (orientation == "null") {
callback.invoke(orientationInt, null);
} else {
callback.invoke(null, orientation);
}
promise.resolve(orientation);
}

@ReactMethod
Expand Down Expand Up @@ -127,14 +123,17 @@ public void unlockAllOrientations() {
constants.put("initialOrientation", orientation);
}

constants.put("LANDSCAPE", LANDSCAPE);
constants.put("PORTRAIT", PORTRAIT);

return constants;
}

private String getOrientationString(int orientation) {
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
return "LANDSCAPE";
return LANDSCAPE;
} else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
return "PORTRAIT";
return PORTRAIT;
} else if (orientation == Configuration.ORIENTATION_UNDEFINED) {
return "UNKNOWN";
} else {
Expand Down
12 changes: 6 additions & 6 deletions demo/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export default class Demo extends Component {
}

_getOrientation() {
Orientation.getOrientation((err, orientation) => {
Alert.alert(`Orientation is ${orientation}`);
});
Orientation.getOrientation().then(orientation =>
Alert.alert(`Orientation is ${orientation}`)
);
}

_getSpecificOrientation() {
Orientation.getSpecificOrientation((err, orientation) => {
Alert.alert(`Specific orientation is ${orientation}`);
});
Orientation.getSpecificOrientation().then(orientation =>
Alert.alert(`Specific orientation is ${orientation}`)
);
}

_updateOrientation = (orientation) => this.setState({ orientation });
Expand Down
22 changes: 14 additions & 8 deletions iOS/RCTOrientation/Orientation.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ @implementation Orientation
@synthesize bridge = _bridge;

static UIInterfaceOrientationMask _orientation = UIInterfaceOrientationMaskAllButUpsideDown;
NSString *LANDSCAPE = @"LANDSCAPE";
NSString *PORTRAIT = @"PORTRAIT";
+ (void)setOrientation: (UIInterfaceOrientationMask)orientation {
_orientation = orientation;
}
Expand Down Expand Up @@ -59,7 +61,7 @@ - (NSString *)getOrientationStr: (UIDeviceOrientation)orientation {
case UIDeviceOrientationLandscapeLeft:
case UIDeviceOrientationLandscapeRight:

orientationStr = @"LANDSCAPE";
orientationStr = LANDSCAPE;
break;

case UIDeviceOrientationPortraitUpsideDown:
Expand All @@ -75,7 +77,7 @@ - (NSString *)getOrientationStr: (UIDeviceOrientation)orientation {
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:

orientationStr = @"LANDSCAPE";
orientationStr = LANDSCAPE;
break;

case UIInterfaceOrientationPortraitUpsideDown:
Expand Down Expand Up @@ -119,7 +121,7 @@ - (NSString *)getSpecificOrientationStr: (UIDeviceOrientation)orientation {
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:

orientationStr = @"LANDSCAPE";
orientationStr = LANDSCAPE;
break;

case UIInterfaceOrientationPortraitUpsideDown:
Expand All @@ -137,18 +139,20 @@ - (NSString *)getSpecificOrientationStr: (UIDeviceOrientation)orientation {

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(getOrientation:(RCTResponseSenderBlock)callback)
RCT_REMAP_METHOD(getOrientation,
findEventsWithResolver:(RCTPromiseResolveBlock)resolve
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSString *orientationStr = [self getOrientationStr:orientation];
callback(@[[NSNull null], orientationStr]);
resolve(orientationStr);
}

RCT_EXPORT_METHOD(getSpecificOrientation:(RCTResponseSenderBlock)callback)
RCT_REMAP_METHOD(getSpecificOrientation,
findEventsWithResolver:(RCTPromiseResolveBlock)resolve
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSString *orientationStr = [self getSpecificOrientationStr:orientation];
callback(@[[NSNull null], orientationStr]);
resolve(orientationStr);
}

RCT_EXPORT_METHOD(lockToPortrait)
Expand Down Expand Up @@ -230,7 +234,9 @@ - (NSDictionary *)constantsToExport
NSString *orientationStr = [self getOrientationStr:orientation];

return @{
@"initialOrientation": orientationStr
@"initialOrientation": orientationStr,
@"PORTRAIT": PORTRAIT,
@"LANDSCAPE": LANDSCAPE
};
}

Expand Down
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Orientation = require('react-native').NativeModules.Orientation;
var DeviceEventEmitter = require('react-native').DeviceEventEmitter;
var Platform = require('react-native').Platform;

var listeners = {};
var orientationDidChangeEvent = 'orientationDidChange';
Expand All @@ -23,16 +24,16 @@ function getKey(listener) {
};

module.exports = {
getOrientation(cb) {
Orientation.getOrientation((error,orientation) =>{
cb(error, orientation);
});
getOrientation() {
return Orientation.getOrientation();
},

getSpecificOrientation(cb) {
Orientation.getSpecificOrientation((error,orientation) =>{
cb(error, orientation);
});
getSpecificOrientation() {
if (Platform.OS === 'ios') {
return Orientation.getSpecificOrientation();
}
alert('getSpecificOrientation method is not supported on android');
return Promise.reject();
},

lockToPortrait() {
Expand Down