Skip to content

Commit

Permalink
implement DistanceMapView with blocking view for Android native
Browse files Browse the repository at this point in the history
  • Loading branch information
akinwale committed Sep 14, 2023
1 parent 86d37d6 commit 07abca9
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/components/ConfirmedRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import transactionPropTypes from './transactionPropTypes';
import BlockingView from './BlockingViews/BlockingView';
import useNetwork from '../hooks/useNetwork';
import useLocalize from '../hooks/useLocalize';
import MapView from './MapView';
import DistanceMapView from './DistanceMapView';

const propTypes = {
/** Transaction that stores the distance request data */
Expand Down Expand Up @@ -90,7 +90,7 @@ function ConfirmedRoute({mapboxAccessToken, transaction}) {
return (
<>
{!isOffline && Boolean(mapboxAccessToken.token) ? (
<MapView
<DistanceMapView
accessToken={mapboxAccessToken.token}
mapPadding={CONST.MAP_PADDING}
pitchEnabled={false}
Expand Down
55 changes: 55 additions & 0 deletions src/components/DistanceMapView/distanceMapViewPropTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import PropTypes from 'prop-types';

const propTypes = {
// Public access token to be used to fetch map data from Mapbox.
accessToken: PropTypes.string.isRequired,

// Style applied to MapView component. Note some of the View Style props are not available on ViewMap
style: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.object), PropTypes.object]),

// Link to the style JSON document.
styleURL: PropTypes.string,

// Whether map can tilt in the vertical direction.
pitchEnabled: PropTypes.bool,

// Padding to apply when the map is adjusted to fit waypoints and directions
mapPadding: PropTypes.number,

// Initial coordinate and zoom level
initialState: PropTypes.shape({
location: PropTypes.arrayOf(PropTypes.number).isRequired,
zoom: PropTypes.number.isRequired,
}),

// Locations on which to put markers
waypoints: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string,
coordinate: PropTypes.arrayOf(PropTypes.number),
markerComponent: PropTypes.oneOfType([PropTypes.element, PropTypes.func]),
}),
),

// List of coordinates which together forms a direction.
directionCoordinates: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number)),

// Callback to call when the map is idle / ready
onMapReady: PropTypes.func,

// Optional additional styles to be applied to the overlay
overlayStyle: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.object), PropTypes.object]),
};

const defaultProps = {
styleURL: undefined,
pitchEnabled: false,
mapPadding: 0,
initialState: undefined,
waypoints: undefined,
directionCoordinates: undefined,
onMapReady: () => {},
overlayStyle: undefined,
};

export {propTypes, defaultProps};
48 changes: 48 additions & 0 deletions src/components/DistanceMapView/index.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, {useState} from 'react';
import {View} from 'react-native';
import _ from 'underscore';
import BlockingView from '../BlockingViews/BlockingView';
import MapView from '../MapView';
import styles from '../../styles/styles';
import useNetwork from '../../hooks/useNetwork';
import useLocalize from '../../hooks/useLocalize';
import * as Expensicons from '../Icon/Expensicons';
import * as StyleUtils from '../../styles/StyleUtils';
import * as distanceMapViewPropTypes from './distanceMapViewPropTypes';

function DistanceMapView(props) {
const [isMapReady, setIsMapReady] = useState(false);
const {isOffline} = useNetwork();
const {translate} = useLocalize();

return (
<>
<MapView
// eslint-disable-next-line react/jsx-props-no-spreading
{..._.omit(props, 'overlayStyle')}
onMapReady={() => {
if (isMapReady) {
return;
}
setIsMapReady(true);
}}
/>
{!isMapReady && (
<View style={StyleUtils.combineStyles(styles.mapViewOverlay, props.overlayStyle)}>
<BlockingView
icon={Expensicons.EmptyStateRoutePending}
title={translate('distance.mapPending.title')}
subtitle={isOffline ? translate('distance.mapPending.subtitle') : translate('distance.mapPending.onlineSubtitle')}
shouldShowLink={false}
/>
</View>
)}
</>
);
}

DistanceMapView.propTypes = distanceMapViewPropTypes.propTypes;
DistanceMapView.defaultProps = distanceMapViewPropTypes.defaultProps;
DistanceMapView.displayName = 'DistanceMapView';

export default DistanceMapView;
15 changes: 15 additions & 0 deletions src/components/DistanceMapView/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import _ from 'underscore';
import MapView from '../MapView';
import * as distanceMapViewPropTypes from './distanceMapViewPropTypes';

function DistanceMapView(props) {
// eslint-disable-next-line react/jsx-props-no-spreading
return <MapView {..._.omit(props, 'overlayStyle')} />;
}

DistanceMapView.propTypes = distanceMapViewPropTypes.propTypes;
DistanceMapView.defaultProps = distanceMapViewPropTypes.defaultProps;
DistanceMapView.displayName = 'DistanceMapView';

export default DistanceMapView;
5 changes: 3 additions & 2 deletions src/components/DistanceRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import * as Transaction from '../libs/actions/Transaction';
import * as TransactionUtils from '../libs/TransactionUtils';

import Button from './Button';
import MapView from './MapView';
import DistanceMapView from './DistanceMapView';
import LinearGradient from './LinearGradient';
import * as Expensicons from './Icon/Expensicons';
import BlockingView from './BlockingViews/BlockingView';
Expand Down Expand Up @@ -238,7 +238,7 @@ function DistanceRequest({iou, iouType, report, transaction, mapboxAccessToken})
</View>
<View style={styles.mapViewContainer}>
{!isOffline && Boolean(mapboxAccessToken.token) ? (
<MapView
<DistanceMapView
accessToken={mapboxAccessToken.token}
mapPadding={CONST.MAPBOX.PADDING}
pitchEnabled={false}
Expand All @@ -250,6 +250,7 @@ function DistanceRequest({iou, iouType, report, transaction, mapboxAccessToken})
style={styles.mapView}
waypoints={waypointMarkers}
styleURL={CONST.MAPBOX.STYLE_URL}
overlayStyle={styles.m4}
/>
) : (
<View style={[styles.mapPendingView]}>
Expand Down
5 changes: 4 additions & 1 deletion src/components/MapView/MapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import CONST from '../../CONST';

import {MapViewProps, MapViewHandle} from './MapViewTypes';

const MapView = forwardRef<MapViewHandle, MapViewProps>(({accessToken, style, mapPadding, styleURL, pitchEnabled, initialState, waypoints, directionCoordinates}, ref) => {
const MapView = forwardRef<MapViewHandle, MapViewProps>(({accessToken, style, mapPadding, styleURL, pitchEnabled, initialState, waypoints, directionCoordinates, onMapReady}, ref) => {
const cameraRef = useRef<Mapbox.Camera>(null);
const [isIdle, setIsIdle] = useState(false);

Expand Down Expand Up @@ -56,6 +56,9 @@ const MapView = forwardRef<MapViewHandle, MapViewProps>(({accessToken, style, ma
const setMapIdle = (e: MapState) => {
if (e.gestures.isGestureActive) return;
setIsIdle(true);
if (onMapReady) {
onMapReady();
}
};

return (
Expand Down
2 changes: 2 additions & 0 deletions src/components/MapView/MapViewTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type MapViewProps = {
waypoints?: WayPoint[];
// List of coordinates which together forms a direction.
directionCoordinates?: Array<[number, number]>;
// Callback to call when the map is idle / ready.
onMapReady: () => void;
};

type DirectionProps = {
Expand Down
12 changes: 12 additions & 0 deletions src/styles/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3931,6 +3931,18 @@ const styles = {
overflow: 'hidden',
},

mapViewOverlay: {
flex: 1,
position: 'absolute',
left: 0,
top: 0,
borderRadius: variables.componentBorderRadiusLarge,
overflow: 'hidden',
backgroundColor: themeColors.highlightBG,
...sizing.w100,
...sizing.h100,
},

confirmationListMapItem: {
...spacing.m5,
height: 200,
Expand Down

0 comments on commit 07abca9

Please sign in to comment.