Skip to content

Commit

Permalink
Merge pull request #73 from naoufal/updates
Browse files Browse the repository at this point in the history
Native promises and updated android stub
  • Loading branch information
koenpunt authored Oct 1, 2017
2 parents 1b451f0 + aea0324 commit 1e328ae
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 44 deletions.
19 changes: 3 additions & 16 deletions SafariViewManager.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,8 @@
*/
'use strict';

const {NativeModules} = require('react-native');
const NativeSafariViewManager = NativeModules.SafariViewManager;

var SafariViewManager = {
isAvailable: function() {
return new Promise(function(resolve, reject) {
NativeSafariViewManager.isAvailable(function(error) {
if (error) {
return reject(error);
}

resolve(false);
});
});
export default {
isAvailable() {
return Promise.reject(new Error('SafariView is unavailable on Android'));
}
};

module.exports = SafariViewManager;
20 changes: 2 additions & 18 deletions SafariViewManager.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,15 @@ export default {
options.barTintColor = processColor(options.barTintColor);
}

return new Promise((resolve, reject) => {
NativeSafariViewManager.show(options, (error) => {
if (error) {
return reject(error);
}

resolve(true);
});
});
return NativeSafariViewManager.show(options);
},

dismiss() {
NativeSafariViewManager.dismiss();
},

isAvailable() {
return new Promise((resolve, reject) => {
NativeSafariViewManager.isAvailable((error) => {
if (error) {
return reject(error);
}

resolve(true);
});
});
return NativeSafariViewManager.isAvailable();
},

addEventListener(event, listener) {
Expand Down
24 changes: 14 additions & 10 deletions SafariViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,22 @@ - (void)stopObserving
return @[@"SafariViewOnShow", @"SafariViewOnDismiss"];
}

RCT_EXPORT_METHOD(show:(NSDictionary *)args callback:(RCTResponseSenderBlock)callback)
RCT_EXPORT_METHOD(show:(NSDictionary *)args resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
UIColor *tintColorString = args[@"tintColor"];
UIColor *barTintColorString = args[@"barTintColor"];
BOOL fromBottom = [args[@"fromBottom"] boolValue];

// Error if no url is passed
if (!args[@"url"]) {
RCTLogError(@"[SafariView] You must specify a url.");
reject(@"E_SAFARI_VIEW_NO_URL", @"You must specify a url.", nil);
return;
}

NSURL *url = [NSURL URLWithString:args[@"url"]];
BOOL readerMode = [args[@"readerMode"] boolValue];
UIColor *tintColorString = args[@"tintColor"];
UIColor *barTintColorString = args[@"barTintColor"];
BOOL fromBottom = [args[@"fromBottom"] boolValue];

// Initialize the Safari View
self.safariView = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:args[@"url"]] entersReaderIfAvailable:[args[@"readerMode"] boolValue]];
self.safariView = [[SFSafariViewController alloc] initWithURL:url entersReaderIfAvailable:readerMode];
self.safariView.delegate = self;

// Set tintColor if available
Expand Down Expand Up @@ -83,15 +85,17 @@ - (void)stopObserving
if (hasListeners) {
[self sendEventWithName:@"SafariViewOnShow" body:nil];
}

resolve(@YES);
}

RCT_EXPORT_METHOD(isAvailable:(RCTResponseSenderBlock)callback)
RCT_EXPORT_METHOD(isAvailable:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
if ([SFSafariViewController class]) {
// SafariView is available
callback(@[[NSNull null], @true]);
resolve(@YES);
} else {
callback(@[RCTMakeError(@"[SafariView] SafariView is unavailable.", nil, nil)]);
reject(@"E_SAFARI_VIEW_UNAVAILABLE", @"SafariView is unavailable", nil);
}
}

Expand Down

0 comments on commit 1e328ae

Please sign in to comment.