|
1 | 1 | // @flow |
2 | 2 |
|
3 | 3 | import invariant from 'invariant'; |
4 | | -import { Linking, NativeModules, Platform, processColor } from 'react-native'; |
| 4 | +import { |
| 5 | + Linking, |
| 6 | + NativeModules, |
| 7 | + Platform, |
| 8 | + processColor, |
| 9 | + AppState |
| 10 | +} from 'react-native'; |
5 | 11 |
|
6 | 12 | const { RNInAppBrowser } = NativeModules; |
7 | 13 |
|
@@ -66,28 +72,21 @@ async function open( |
66 | 72 | url: string, |
67 | 73 | options: InAppBrowserOptions = {} |
68 | 74 | ): Promise<BrowserResult> { |
69 | | - const { |
70 | | - animated, |
71 | | - readerMode, |
72 | | - modalEnabled, |
73 | | - dismissButtonStyle, |
74 | | - enableBarCollapsing, |
75 | | - preferredBarTintColor, |
76 | | - preferredControlTintColor, |
77 | | - ...optionalOptions |
78 | | - } = options; |
79 | 75 | const inAppBrowserOptions = { |
80 | | - ...optionalOptions, |
| 76 | + ...options, |
81 | 77 | url, |
82 | | - dismissButtonStyle: dismissButtonStyle || 'close', |
83 | | - readerMode: !!readerMode, |
84 | | - animated: animated !== undefined ? animated : true, |
85 | | - modalEnabled: modalEnabled !== undefined ? modalEnabled : true, |
86 | | - enableBarCollapsing: !!enableBarCollapsing, |
| 78 | + dismissButtonStyle: options.dismissButtonStyle || 'close', |
| 79 | + readerMode: !!options.readerMode, |
| 80 | + animated: options.animated !== undefined ? options.animated : true, |
| 81 | + modalEnabled: |
| 82 | + options.modalEnabled !== undefined ? options.modalEnabled : true, |
| 83 | + enableBarCollapsing: !!options.enableBarCollapsing, |
87 | 84 | preferredBarTintColor: |
88 | | - preferredBarTintColor && processColor(preferredBarTintColor), |
| 85 | + options.preferredBarTintColor && |
| 86 | + processColor(options.preferredBarTintColor), |
89 | 87 | preferredControlTintColor: |
90 | | - preferredControlTintColor && processColor(preferredControlTintColor) |
| 88 | + options.preferredControlTintColor && |
| 89 | + processColor(options.preferredControlTintColor) |
91 | 90 | }; |
92 | 91 | return RNInAppBrowser.open(inAppBrowserOptions); |
93 | 92 | } |
@@ -169,20 +168,32 @@ function _waitForRedirectAsync(returnUrl: string): Promise<RedirectResult> { |
169 | 168 |
|
170 | 169 | function _checkResultAndReturnUrl( |
171 | 170 | returnUrl: string, |
172 | | - result: RedirectResult |
173 | | -): Promise<RedirectResult> { |
| 171 | + result: AuthSessionResult |
| 172 | +): Promise<AuthSessionResult> { |
174 | 173 | return new Promise(function(resolve) { |
175 | | - Linking.getInitialURL() |
176 | | - .then(function(url) { |
177 | | - if (url && url.startsWith(returnUrl)) { |
178 | | - resolve({ url: url, type: 'success' }); |
179 | | - } else { |
180 | | - resolve(result); |
| 174 | + if (Platform.OS === 'android' && result.type !== 'cancel') { |
| 175 | + /** |
| 176 | + * Detect Android Activity OnResume event once |
| 177 | + */ |
| 178 | + const _handleAppStateChange = async nextAppState => { |
| 179 | + if (nextAppState === 'active') { |
| 180 | + try { |
| 181 | + const url = await Linking.getInitialURL(); |
| 182 | + if (url && url.startsWith(returnUrl)) { |
| 183 | + resolve({ url, type: 'success' }); |
| 184 | + } else { |
| 185 | + resolve(result); |
| 186 | + } |
| 187 | + } catch (error) { |
| 188 | + resolve(result); |
| 189 | + } |
| 190 | + AppState.removeEventListener('change', _handleAppStateChange); |
181 | 191 | } |
182 | | - }) |
183 | | - .catch(function() { |
184 | | - resolve(result); |
185 | | - }); |
| 192 | + }; |
| 193 | + AppState.addEventListener('change', _handleAppStateChange); |
| 194 | + } else { |
| 195 | + resolve(result); |
| 196 | + } |
186 | 197 | }); |
187 | 198 | } |
188 | 199 |
|
|
0 commit comments