Skip to content
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

location bias feature implementation #33334

Merged
merged 8 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/components/AddressSearch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ const propTypes = {
/** Information about the network */
network: networkPropTypes.isRequired,

/** location bias based on rectangular format */
rojiphil marked this conversation as resolved.
Show resolved Hide resolved
locationBias: PropTypes.string,

...withLocalizePropTypes,
};

Expand Down Expand Up @@ -138,6 +141,7 @@ const defaultProps = {
maxInputLength: undefined,
predefinedPlaces: [],
resultTypes: 'address',
locationBias: undefined,
};

function AddressSearch({
Expand All @@ -162,6 +166,7 @@ function AddressSearch({
shouldSaveDraft,
translate,
value,
locationBias,
}) {
const theme = useTheme();
const styles = useThemeStyles();
Expand All @@ -179,11 +184,11 @@ function AddressSearch({
language: preferredLocale,
types: resultTypes,
components: isLimitedToUSA ? 'country:us' : undefined,
locationbias: locationBias || 'ipbias',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should pass ipbias here. Let this be controlled by the caller. Can you please explain this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get your point. Instead of passing ipbias, it may be better to let the default behavior occur by not passing the locationbias property if we do not have one. We can do something like this below.
Let me know what you think on this.

...(locationBias && {locationbias: locationBias}),

}),
[preferredLocale, resultTypes, isLimitedToUSA],
[preferredLocale, resultTypes, isLimitedToUSA, locationBias],
);
const shouldShowCurrentLocationButton = canUseCurrentLocation && searchValue.trim().length === 0 && isFocused;

const saveLocationDetails = (autocompleteData, details) => {
const addressComponents = details.address_components;
if (!addressComponents) {
Expand Down
76 changes: 76 additions & 0 deletions src/pages/iou/request/step/IOURequestStepWaypoint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useNavigation} from '@react-navigation/native';
import lodashGet from 'lodash/get';
import lodashIsNil from 'lodash/isNil';
import PropTypes from 'prop-types';
import React, {useMemo, useRef, useState} from 'react';
import {View} from 'react-native';
Expand Down Expand Up @@ -38,6 +39,15 @@ const propTypes = {
/** The optimistic transaction for this request */
transaction: transactionPropTypes,

/* Current location coordinates of the user */
userLocation: PropTypes.shape({
/** Latitude of the location */
latitude: PropTypes.number,

/** Longitude of the location */
longitude: PropTypes.number,
}),

/** Recent waypoints that the user has selected */
recentWaypoints: PropTypes.arrayOf(
PropTypes.shape({
Expand Down Expand Up @@ -65,6 +75,7 @@ const propTypes = {
const defaultProps = {
recentWaypoints: [],
transaction: {},
userLocation: undefined,
};

function IOURequestStepWaypoint({
Expand All @@ -73,6 +84,7 @@ function IOURequestStepWaypoint({
params: {iouType, pageIndex, reportID, transactionID},
},
transaction,
userLocation,
}) {
const styles = useThemeStyles();
const {windowWidth} = useWindowDimensions();
Expand All @@ -83,6 +95,7 @@ function IOURequestStepWaypoint({
const {isOffline} = useNetwork();
const textInput = useRef(null);
const parsedWaypointIndex = parseInt(pageIndex, 10);
const directionCoordinates = lodashGet(transaction, 'routes.route0.geometry.coordinates', []);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this? I don't see this defined in transactionPropTypes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I also notice that routes are not there in transactionPropTypes. But, these are route coordinates that get populated in transaction via getRouteForDraft when the start and finish valid waypoints are filled.

Screenshot 2023-12-21 at 5 59 17 PM

const allWaypoints = lodashGet(transaction, 'comment.waypoints', {});
const currentWaypoint = lodashGet(allWaypoints, `waypoint${pageIndex}`, {});

Expand All @@ -100,6 +113,65 @@ function IOURequestStepWaypoint({
}
}, [parsedWaypointIndex, waypointCount]);

// Construct the rectangular boundary based on user location, waypoints and direction coordinates
const locationBias = useMemo(() => {
// If there are no filled wayPoints and if user's current location cannot be retrieved,
// it is futile to arrive at a biased location. Let's return
if (filledWaypointCount === 0 && _.isEmpty(userLocation)) {
return null;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parasharrajat
If there are no waypoints and also user's current location cannot be determined, do you think we should consider the default coordinates in the app i.e. [-122.4021, 37.7911] as set here?
or returning null as it is done currently is good enough?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be better to skip locationBias in that case.

// Gather the longitudes and latitudes from filled waypoints.
const longitudes = _.filter(
_.map(allWaypoints, (waypoint) => {
if (!waypoint || lodashIsNil(waypoint.lng)) {
return;
}
return waypoint.lng;
}),
(lng) => lng,
);
const latitudes = _.filter(
_.map(allWaypoints, (waypoint) => {
if (!waypoint || lodashIsNil(waypoint.lat)) {
return;
}
return waypoint.lat;
}),
(lat) => lat,
);

// We will get direction coordinates when user is adding a stop after filling the Start and Finish waypoints.
// Include direction coordinates when available.
if (_.size(directionCoordinates) > 0) {
longitudes.push(..._.map(directionCoordinates, (coordinate) => coordinate[0]));
latitudes.push(..._.map(directionCoordinates, (coordinate) => coordinate[1]));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should worry about this. This is just too specific. Also, it depends on the internal state which can change. What is the benefit of adding these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the route between waypoints could have detours, I think considering direction coordinates gives us a more realistic boundary. While I too think this to be too specific, we do consider direction coordinates in arriving at the map bounds as seen here. I think the user will naturally expect to find finer address locations within the visual map bounds. So, I think we may want to keep this. But, if you think this is way too specific, we can remove this too. Let me know what you think.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will leave this for the internal Engineer to decide.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thienlnam
We can either ignore direction coordinates or consider them to arrive at the rectangular boundary for location bias. Please help us decide on this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to worry about this right now - let's start with the most straightforward implementation first and then see if there's an issue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rojiphil Please let me know when changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parasharrajat
I have committed the changes.


// When no filled waypoints are available but the current location of the user is available,
// let us consider the current user's location to construct a rectangular bound
if (filledWaypointCount === 0 && !_.isEmpty(userLocation)) {
longitudes.push(userLocation.longitude);
latitudes.push(userLocation.latitude);
}

// Extend the rectangular bound by 0.5 degree (roughly around 25-30 miles in US)
const minLat = Math.min(...latitudes) - 0.5;
const minLng = Math.min(...longitudes) - 0.5;
const maxLat = Math.max(...latitudes) + 0.5;
const maxLng = Math.max(...longitudes) + 0.5;

// Ensuring coordinates do not go out of range.
const south = minLat > -90 ? minLat : -90;
const west = minLng > -180 ? minLng : -180;
const north = maxLat < 90 ? maxLat : 90;
const east = maxLng < 180 ? maxLng : 180;

// Format: rectangle:south,west|north,east
const rectFormat = `rectangle:${south},${west}|${north},${east}`;
return rectFormat;
}, [userLocation, directionCoordinates, filledWaypointCount, allWaypoints]);

const waypointAddress = lodashGet(currentWaypoint, 'address', '');
// Hide the menu when there is only start and finish waypoint
const shouldShowThreeDotsButton = waypointCount > 2;
Expand Down Expand Up @@ -219,6 +291,7 @@ function IOURequestStepWaypoint({
<View>
<InputWrapperWithRef
InputComponent={AddressSearch}
locationBias={locationBias}
canUseCurrentLocation
inputID={`waypoint${pageIndex}`}
ref={(e) => (textInput.current = e)}
Expand Down Expand Up @@ -257,6 +330,9 @@ export default compose(
withWritableReportOrNotFound,
withFullTransactionOrNotFound,
withOnyx({
userLocation: {
key: ONYXKEYS.USER_LOCATION,
},
recentWaypoints: {
key: ONYXKEYS.NVP_RECENT_WAYPOINTS,

Expand Down
Loading