-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Move BackAndroid -> BackHandler, add Apple TV support for back nav #12571
Changes from all commits
cfacc0f
901af84
480c694
529bde3
f20012f
c266c80
19ad6af
d481d41
d577afb
24b0d4d
819355d
e4f00f0
027ce13
441e60c
2656bdc
1f233dc
c2406cc
2e0376f
db26995
883d255
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* BackAndroid has been moved to BackHandler. This stub calls BackHandler methods | ||
* after generating a warning to remind users to move to the new BackHandler module. | ||
* | ||
* @providesModule BackAndroid | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var BackHandler = require('BackHandler'); | ||
|
||
var warning = require('fbjs/lib/warning'); | ||
|
||
/** | ||
* Deprecated. Use BackHandler instead. | ||
*/ | ||
var BackAndroid = { | ||
|
||
exitApp: function() { | ||
warning(false, 'BackAndroid is deprecated. Please use BackHandler instead.'); | ||
BackHandler.exitApp(); | ||
}, | ||
|
||
addEventListener: function ( | ||
eventName: BackPressEventName, | ||
handler: Function | ||
): {remove: () => void} { | ||
warning(false, 'BackAndroid is deprecated. Please use BackHandler instead.'); | ||
return BackHandler.addEventListener(eventName, handler); | ||
}, | ||
|
||
removeEventListener: function( | ||
eventName: BackPressEventName, | ||
handler: Function | ||
): void { | ||
warning(false, 'BackAndroid is deprecated. Please use BackHandler instead.'); | ||
BackHandler.removeEventListener(eventName, handler); | ||
}, | ||
|
||
}; | ||
|
||
module.exports = BackAndroid; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* On Apple TV, this implements back navigation using the TV remote's menu button. | ||
* On iOS, this just implements a stub. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-trailing-spaces: Trailing spaces not allowed. |
||
* | ||
* @providesModule BackHandler | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Platform = require('Platform'); | ||
const TVEventHandler = require('TVEventHandler'); | ||
|
||
type BackPressEventName = $Enum<{ | ||
backPress: string, | ||
}>; | ||
|
||
function emptyFunction() {} | ||
|
||
/** | ||
* Detect hardware button presses for back navigation. | ||
* | ||
* Android: Detect hardware back button presses, and programmatically invoke the default back button | ||
* functionality to exit the app if there are no listeners or if none of the listeners return true. | ||
* | ||
* tvOS: Detect presses of the menu button on the TV remote. (Still to be implemented: | ||
* programmatically disable menu button handling | ||
* functionality to exit the app if there are no listeners or if none of the listeners return true.) | ||
* | ||
* iOS: Not applicable. | ||
* | ||
* The event subscriptions are called in reverse order (i.e. last registered subscription first), | ||
* and if one subscription returns true then subscriptions registered earlier will not be called. | ||
* | ||
* Example: | ||
* | ||
* ```javascript | ||
* BackHandler.addEventListener('hardwareBackPress', function() { | ||
* // this.onMainScreen and this.goBack are just examples, you need to use your own implementation here | ||
* // Typically you would use the navigator here to go to the last state. | ||
* | ||
* if (!this.onMainScreen()) { | ||
* this.goBack(); | ||
* return true; | ||
* } | ||
* return false; | ||
* }); | ||
* ``` | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: new line after comment |
||
let BackHandler; | ||
|
||
if (Platform.isTVOS) { | ||
const _tvEventHandler = new TVEventHandler(); | ||
var _backPressSubscriptions = new Set(); | ||
|
||
_tvEventHandler.enable(this, function(cmp, evt) { | ||
if (evt && evt.eventType === 'menu') { | ||
var backPressSubscriptions = new Set(_backPressSubscriptions); | ||
var invokeDefault = true; | ||
var subscriptions = [...backPressSubscriptions].reverse(); | ||
for (var i = 0; i < subscriptions.length; ++i) { | ||
if (subscriptions[i]()) { | ||
invokeDefault = false; | ||
break; | ||
} | ||
} | ||
|
||
if (invokeDefault) { | ||
BackHandler.exitApp(); | ||
} | ||
} | ||
}); | ||
|
||
BackHandler = { | ||
exitApp: emptyFunction, | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: unnecessary new line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this makes the code more readable. |
||
addEventListener: function ( | ||
eventName: BackPressEventName, | ||
handler: Function | ||
): {remove: () => void} { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type should be extracted out to the top, type BackHandlerSubscription = {
remove: () => void
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm leaving this as is because it's copied from the existing code in the previous |
||
_backPressSubscriptions.add(handler); | ||
return { | ||
remove: () => BackHandler.removeEventListener(eventName, handler), | ||
}; | ||
}, | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: unnecessary new line |
||
removeEventListener: function( | ||
eventName: BackPressEventName, | ||
handler: Function | ||
): void { | ||
_backPressSubscriptions.delete(handler); | ||
}, | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: unnecessary new line |
||
}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: unnecessary new lines |
||
} else { | ||
|
||
BackHandler = { | ||
exitApp: emptyFunction, | ||
addEventListener() { | ||
return { | ||
remove: emptyFunction, | ||
}; | ||
}, | ||
removeEventListener: emptyFunction, | ||
}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: unnecessary new lines |
||
} | ||
|
||
module.exports = BackHandler; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,9 @@ const apis = [ | |
'../Libraries/ReactNative/AppRegistry.js', | ||
'../Libraries/AppState/AppState.js', | ||
'../Libraries/Storage/AsyncStorage.js', | ||
'../Libraries/Utilities/BackAndroid.android.js', | ||
'../Libraries/Utilities/BackAndroid.js', | ||
'../Libraries/Utilities/BackHandler.ios.js', | ||
'../Libraries/Utilities/BackHandler.android.js', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this cause there to be 3 separate pages? Lets consolidate it so that all the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There were only two pages when I tested this, one for BackHandler, and one for BackAndroid to let users know that it is deprecated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, fair enough |
||
'../Libraries/CameraRoll/CameraRoll.js', | ||
'../Libraries/Components/Clipboard/Clipboard.js', | ||
'../Libraries/Components/DatePickerAndroid/DatePickerAndroid.android.js', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no-trailing-spaces: Trailing spaces not allowed.