-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
feat: Add distance label on map route #55517
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
935591a
feat: add distance label
truph01 593653d
merge main
truph01 619a121
merge main
truph01 988005f
fix: style web
truph01 53a14ec
fix: conflicts
truph01 1e83c74
merge main
truph01 75d6124
merge main
truph01 cbc63c1
fix: toggle distance unit onpress
truph01 3a6a683
merge main
truph01 b1dd8ba
fix: lint
truph01 e08c0e3
merge main
truph01 2c4d3b2
fix: lint
truph01 aff2281
fix: lint
truph01 6f7a06f
fix: remove unused change
truph01 c79d896
merge main
truph01 9f403a7
fix: update label color to green500
truph01 4d3610a
merge main
truph01 dd95e24
fix: use usePolicy instead of getPolicy
truph01 5632ffd
merge main
truph01 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,10 +12,12 @@ import {View} from 'react-native'; | |
import {useOnyx} from 'react-native-onyx'; | ||
import Button from '@components/Button'; | ||
import * as Expensicons from '@components/Icon/Expensicons'; | ||
import {PressableWithoutFeedback} from '@components/Pressable'; | ||
import usePrevious from '@hooks/usePrevious'; | ||
import useStyleUtils from '@hooks/useStyleUtils'; | ||
import useTheme from '@hooks/useTheme'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import DistanceRequestUtils from '@libs/DistanceRequestUtils'; | ||
import type {GeolocationErrorCallback} from '@libs/getCurrentPosition/getCurrentPosition.types'; | ||
import {GeolocationErrorCode} from '@libs/getCurrentPosition/getCurrentPosition.types'; | ||
import {clearUserLocation, setUserLocation} from '@userActions/UserLocation'; | ||
|
@@ -42,13 +44,28 @@ const MapViewImpl = forwardRef<MapViewHandle, MapViewProps>( | |
directionCoordinates, | ||
initialState = {location: CONST.MAPBOX.DEFAULT_COORDINATE, zoom: CONST.MAPBOX.DEFAULT_ZOOM}, | ||
interactive = true, | ||
distanceInMeters, | ||
unit, | ||
}, | ||
ref, | ||
) => { | ||
const [userLocation] = useOnyx(ONYXKEYS.USER_LOCATION); | ||
|
||
const {isOffline} = useNetwork(); | ||
const {translate} = useLocalize(); | ||
const [distanceUnit, setDistanceUnit] = useState(unit); | ||
useEffect(() => { | ||
if (!unit || distanceUnit) { | ||
return; | ||
} | ||
setDistanceUnit(unit); | ||
}, [unit, distanceUnit]); | ||
|
||
const toggleDistanceUnit = useCallback(() => { | ||
setDistanceUnit((currentUnit) => | ||
currentUnit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_KILOMETERS ? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES : CONST.CUSTOM_UNITS.DISTANCE_UNIT_KILOMETERS, | ||
); | ||
}, []); | ||
|
||
const theme = useTheme(); | ||
const styles = useThemeStyles(); | ||
|
@@ -232,6 +249,20 @@ const MapViewImpl = forwardRef<MapViewHandle, MapViewProps>( | |
}; | ||
}, [waypoints, directionCoordinates, interactive, currentPosition, initialState.zoom]); | ||
|
||
const distanceSymbolCoorinate = useMemo(() => { | ||
const length = directionCoordinates?.length; | ||
// If the array is empty, return undefined | ||
if (!length) { | ||
return undefined; | ||
} | ||
|
||
// Find the index of the middle element | ||
const middleIndex = Math.floor(length / 2); | ||
|
||
// Return the middle element | ||
return directionCoordinates.at(middleIndex); | ||
}, [directionCoordinates]); | ||
|
||
return !isOffline && !!accessToken && !!initialViewState ? ( | ||
<View | ||
style={style} | ||
|
@@ -257,6 +288,24 @@ const MapViewImpl = forwardRef<MapViewHandle, MapViewProps>( | |
<View style={styles.currentPositionDot} /> | ||
</Marker> | ||
)} | ||
{!!distanceSymbolCoorinate && !!distanceInMeters && !!distanceUnit && ( | ||
<Marker | ||
key="distance" | ||
longitude={distanceSymbolCoorinate.at(0) ?? 0} | ||
latitude={distanceSymbolCoorinate.at(1) ?? 0} | ||
> | ||
<PressableWithoutFeedback | ||
accessibilityLabel={CONST.ROLE.BUTTON} | ||
role={CONST.ROLE.BUTTON} | ||
onPress={toggleDistanceUnit} | ||
style={{marginRight: 100}} | ||
> | ||
<View style={styles.distanceLabelWrapper}> | ||
<View style={styles.distanceLabelText}> {DistanceRequestUtils.getDistanceForDisplayLabel(distanceInMeters, distanceUnit)}</View> | ||
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. This was supposed to display a text value, wrapping it in a view caused #56603 |
||
</View> | ||
</PressableWithoutFeedback> | ||
</Marker> | ||
)} | ||
{waypoints?.map(({coordinate, markerComponent, id}) => { | ||
const MarkerComponent = markerComponent; | ||
if (utils.areSameCoordinate([coordinate[0], coordinate[1]], [currentPosition?.longitude ?? 0, currentPosition?.latitude ?? 0]) && interactive) { | ||
|
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
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
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.
Coming from #56531 checklist: We can enhance the logic by finding the closest point to the center of the waypoints instead of using the middle index, which may hide the distance label in some cases.