From c161a1a28cf01fd70cd6f36fec125ce3873642db Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Sun, 1 Oct 2017 17:05:17 +0200 Subject: [PATCH 1/4] update isAvailable to use native promise --- SafariViewManager.ios.js | 10 +--------- SafariViewManager.m | 6 +++--- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/SafariViewManager.ios.js b/SafariViewManager.ios.js index cb0e5a9..4898935 100644 --- a/SafariViewManager.ios.js +++ b/SafariViewManager.ios.js @@ -36,15 +36,7 @@ export default { }, isAvailable() { - return new Promise((resolve, reject) => { - NativeSafariViewManager.isAvailable((error) => { - if (error) { - return reject(error); - } - - resolve(true); - }); - }); + return NativeSafariViewManager.isAvailable(); }, addEventListener(event, listener) { diff --git a/SafariViewManager.m b/SafariViewManager.m index 2716802..2a4795e 100644 --- a/SafariViewManager.m +++ b/SafariViewManager.m @@ -85,13 +85,13 @@ - (void)stopObserving } } -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); } } From 4b0529f1b6946ffd334a4f2c55231290b5c2efae Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Sun, 1 Oct 2017 17:17:20 +0200 Subject: [PATCH 2/4] simplify android stub --- SafariViewManager.android.js | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/SafariViewManager.android.js b/SafariViewManager.android.js index 833d367..1c2b751 100644 --- a/SafariViewManager.android.js +++ b/SafariViewManager.android.js @@ -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; From f5336d3ad676a287554312fc9c796af4e224dadb Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Sun, 1 Oct 2017 17:30:46 +0200 Subject: [PATCH 3/4] update show to use native promise fixes #59 --- SafariViewManager.ios.js | 10 +--------- SafariViewManager.m | 6 ++++-- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/SafariViewManager.ios.js b/SafariViewManager.ios.js index 4898935..d2c0042 100644 --- a/SafariViewManager.ios.js +++ b/SafariViewManager.ios.js @@ -20,15 +20,7 @@ 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() { diff --git a/SafariViewManager.m b/SafariViewManager.m index 2a4795e..d7fea23 100644 --- a/SafariViewManager.m +++ b/SafariViewManager.m @@ -31,7 +31,7 @@ - (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"]; @@ -39,7 +39,7 @@ - (void)stopObserving // 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; } @@ -83,6 +83,8 @@ - (void)stopObserving if (hasListeners) { [self sendEventWithName:@"SafariViewOnShow" body:nil]; } + + resolve(@YES); } RCT_EXPORT_METHOD(isAvailable:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) From aea0324ea624047fa5f4d8cc06f6b6a6075825ca Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Sun, 1 Oct 2017 17:45:17 +0200 Subject: [PATCH 4/4] do not assign variables before checking url --- SafariViewManager.m | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/SafariViewManager.m b/SafariViewManager.m index d7fea23..0bb7c0c 100644 --- a/SafariViewManager.m +++ b/SafariViewManager.m @@ -33,18 +33,20 @@ - (void)stopObserving 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"]) { 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