-
Notifications
You must be signed in to change notification settings - Fork 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
Chrome mWeb - scan button provides no feedback #30094
Merged
tgolen
merged 18 commits into
Expensify:main
from
lukemorawski:29990-chrome_mweb_scan_button_provides_no_feedback
Nov 15, 2023
Merged
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
04476a3
chrome camera fix
lukemorawski c09a37c
little fix
lukemorawski e8664c0
linting
lukemorawski 7307db8
default tab focused to false
lukemorawski 710cee1
fixed infinite loader on scan tab refresh
lukemorawski 9e87798
const name change
lukemorawski b3f0a85
delayed show camera state
lukemorawski ab6ad33
Merge branch 'main' into 29990-chrome_mweb_scan_button_provides_no_fe…
lukemorawski afc709c
Merge branch 'main' into 29990-chrome_mweb_scan_button_provides_no_fe…
lukemorawski e846414
prettier
lukemorawski 6788075
useTabNavigationFocus hook refactoring
lukemorawski 6f76928
added JSDocs for useTabNavigatorFocus hook
lukemorawski 0b23ac2
native camera fix and useTabNavigatorFocus refactor
lukemorawski f21f204
Merge branch 'main' into 29990-chrome_mweb_scan_button_provides_no_fe…
lukemorawski 1349a1e
JSDocs types improvements
lukemorawski 96f76c5
Merge branch 'main' into 29990-chrome_mweb_scan_button_provides_no_fe…
lukemorawski 6080787
removed unnecessary HOC and unused prop
lukemorawski 5da6f16
linting
lukemorawski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import {useTabAnimation} from '@react-navigation/material-top-tabs'; | ||
import {useIsFocused} from '@react-navigation/native'; | ||
import {useEffect, useState} from 'react'; | ||
import DomUtils from '@libs/DomUtils'; | ||
|
||
/** | ||
* Custom React hook to determine the focus status of a tab in a Material Top Tab Navigator. | ||
* It evaluates whether the current tab is focused based on the tab's animation position and | ||
* the screen's focus status within a React Navigation environment. | ||
* | ||
* This hook is designed for use with the Material Top Tabs provided by '@react-navigation/material-top-tabs'. | ||
* It leverages the `useTabAnimation` hook from the same package to track the animated position of tabs | ||
* and the `useIsFocused` hook from '@react-navigation/native' to ascertain if the current screen is in focus. | ||
* | ||
* Note: This hook contains a conditional invocation of another hook (`useTabAnimation`), | ||
* which is typically an anti-pattern in React. This is done to account for scenarios where the hook | ||
* might not be used within a Material Top Tabs Navigator context. Proper usage should ensure that | ||
* this hook is only used where appropriate. | ||
* | ||
* @param {Object} params - The parameters object. | ||
* @param {number} params.tabIndex - The index of the tab for which focus status is being determined. | ||
* @returns {boolean} Returns `true` if the tab is both animation-focused and screen-focused, otherwise `false`. | ||
* | ||
* @example | ||
* const isTabFocused = useTabNavigatorFocus({ tabIndex: 1 }); | ||
*/ | ||
function useTabNavigatorFocus({tabIndex}) { | ||
let tabPositionAnimation = null; | ||
try { | ||
// Retrieve the animation value from the tab navigator, which ranges from 0 to the total number of pages displayed. | ||
// Even a minimal scroll towards the camera page (e.g., a value of 0.001 at start) should activate the camera for immediate responsiveness. | ||
// STOP!!!!!!! This is not a pattern to be followed! We are conditionally rendering this hook becase when used in the edit flow we'll never be inside a tab navigator. | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
tabPositionAnimation = useTabAnimation(); | ||
} catch (error) { | ||
tabPositionAnimation = null; | ||
} | ||
const isPageFocused = useIsFocused(); | ||
// set to true if the hook is not used within the MaterialTopTabs context | ||
// the hook will then return true if the screen is focused | ||
const [isTabFocused, setIsTabFocused] = useState(!tabPositionAnimation); | ||
|
||
useEffect(() => { | ||
if (!tabPositionAnimation) { | ||
return; | ||
} | ||
const index = Number(tabIndex); | ||
|
||
const listenerId = tabPositionAnimation.addListener(({value}) => { | ||
// Activate camera as soon the index is animating towards the `tabIndex` | ||
DomUtils.requestAnimationFrame(() => { | ||
setIsTabFocused(value > index - 1 && value < index + 1); | ||
}); | ||
}); | ||
|
||
// We need to get the position animation value on component initialization to determine | ||
// if the tab is focused or not. Since it's an Animated.Value the only synchronous way | ||
// to retrieve the value is to use a private method. | ||
// eslint-disable-next-line no-underscore-dangle | ||
const initialTabPositionValue = tabPositionAnimation.__getValue(); | ||
|
||
if (typeof initialTabPositionValue === 'number') { | ||
DomUtils.requestAnimationFrame(() => { | ||
setIsTabFocused(initialTabPositionValue > index - 1 && initialTabPositionValue < index + 1); | ||
}); | ||
} | ||
|
||
return () => { | ||
if (!tabPositionAnimation) { | ||
return; | ||
} | ||
tabPositionAnimation.removeListener(listenerId); | ||
}; | ||
}, [tabIndex, tabPositionAnimation]); | ||
|
||
return isTabFocused && isPageFocused; | ||
} | ||
|
||
export default useTabNavigatorFocus; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 4 additions & 56 deletions
60
src/pages/iou/ReceiptSelector/NavigationAwareCamera.native.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
Would you mind applying this change, then I think it should be all ready to merge.