Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions ios/LiveActivity/GpsTripAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ struct GpsTripAttributes: ActivityAttributes {
var deepLink: String
var buttonText: String
var subtitle: String
var distanceUnit: String
var distanceUnitLong: String


public struct ContentState: Codable, Hashable {
var distance: Double
var distanceUnit: String
var distanceUnitLong: String
}
}
6 changes: 3 additions & 3 deletions ios/LiveActivity/GpsTripLiveActivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct GpsTripLiveActivity: Widget {
Text(distanceString(distance: context.state.distance))
.font(.custom("ExpensifyNeue-Bold", size: 45))
.foregroundColor(.expensifyGreen)
Text(context.attributes.distanceUnitLong)
Text(context.state.distanceUnitLong)
.font(.custom("ExpensifyNeue-Regular", size: 15))
.foregroundColor(.white)
}
Expand Down Expand Up @@ -76,7 +76,7 @@ struct GpsTripLiveActivity: Widget {
.foregroundColor(.expensifyGreen)
.lineLimit(1)
.minimumScaleFactor(0.3)
Text(context.attributes.distanceUnitLong)
Text(context.state.distanceUnitLong)
.font(.custom("ExpensifyNeue-Regular", size: 15))
.foregroundColor(.white)
}
Expand All @@ -99,7 +99,7 @@ struct GpsTripLiveActivity: Widget {
.frame(maxWidth: 23, maxHeight: 23)
.clipShape(RoundedRectangle(cornerRadius: 7))
} compactTrailing: {
Text(distanceStringShort(distance: context.state.distance, distanceUnit: context.attributes.distanceUnit))
Text(distanceStringShort(distance: context.state.distance, distanceUnit: context.state.distanceUnit))
.font(.custom("ExpensifyNeue-Bold", size: 15))
.foregroundColor(.white)
.minimumScaleFactor(0.5)
Expand Down
2 changes: 2 additions & 0 deletions src/components/GPSTripStateChecker/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import {useSplashScreenState} from '@src/SplashScreenStateContext';
import useUpdateGpsNotificationOnUnitChange from './useUpdateGpsNotificationOnUnitChange';
import useUpdateGpsTripOnReconnect from './useUpdateGpsTripOnReconnect';

function GPSTripStateChecker() {
Expand All @@ -27,6 +28,7 @@ function GPSTripStateChecker() {
const reportID = gpsDraftDetails?.reportID ?? generateReportID();

useUpdateGpsTripOnReconnect();
useUpdateGpsNotificationOnUnitChange();

useEffect(() => {
async function handleGpsTripInProgressOnAppRestart() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {useEffect} from 'react';
import useDefaultExpensePolicy from '@hooks/useDefaultExpensePolicy';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import DistanceRequestUtils from '@libs/DistanceRequestUtils';
import shouldUseDefaultExpensePolicyUtil from '@libs/shouldUseDefaultExpensePolicy';
import {shouldUpdateGpsNotificationUnit, updateGpsTripNotificationUnit} from '@pages/iou/request/step/IOURequestStepDistanceGPS/GPSNotifications';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';

function useUpdateGpsNotificationOnUnitChange() {
const {translate} = useLocalize();

const [amountOwed] = useOnyx(ONYXKEYS.NVP_PRIVATE_AMOUNT_OWED);
const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${CONST.IOU.OPTIMISTIC_TRANSACTION_ID}`);

const defaultExpensePolicy = useDefaultExpensePolicy();
const shouldUseDefaultExpensePolicy = shouldUseDefaultExpensePolicyUtil(CONST.IOU.TYPE.CREATE, defaultExpensePolicy, amountOwed);

const unit = DistanceRequestUtils.getRate({transaction, policy: shouldUseDefaultExpensePolicy ? defaultExpensePolicy : undefined}).unit;
Comment thread
GCyganek marked this conversation as resolved.
Resolved

useEffect(() => {
if (!shouldUpdateGpsNotificationUnit()) {
return;
}

updateGpsTripNotificationUnit(translate, unit);
}, [unit, translate]);
}

export default useUpdateGpsNotificationOnUnitChange;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Only iOS needs notification updates on unit change, as Android GPS notification doesn't
// have unit-dependent data
function useUpdateGpsNotificationOnUnitChange() {}

export default useUpdateGpsNotificationOnUnitChange;
4 changes: 2 additions & 2 deletions src/libs/actions/GPSDraftDetails.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Onyx from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import {GPS_DISTANCE_INTERVAL_METERS} from '@pages/iou/request/step/IOURequestStepDistanceGPS/const';
import {updateGpsTripNotification} from '@pages/iou/request/step/IOURequestStepDistanceGPS/GPSNotifications';
import {updateGpsTripNotificationDistance} from '@pages/iou/request/step/IOURequestStepDistanceGPS/GPSNotifications';
import ONYXKEYS from '@src/ONYXKEYS';
import type {GpsDraftDetails} from '@src/types/onyx';
import type {Unit} from '@src/types/onyx/Policy';
Expand Down Expand Up @@ -73,7 +73,7 @@ function addGpsPoints(gpsDraftDetails: OnyxEntry<GpsDraftDetails>, newGpsPoints:
const updatedGpsPoints = [...capturedPoints, ...gpsPointsToAdd];

if (updatedDistance > 0) {
updateGpsTripNotification(updatedDistance);
updateGpsTripNotificationDistance(updatedDistance);
}

Onyx.merge(ONYXKEYS.GPS_DRAFT_DETAILS, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ async function checkAndCleanGpsNotification() {
}
}

export {startGpsTripNotification, stopGpsTripNotification, checkAndCleanGpsNotification};
function shouldUpdateGpsNotificationUnit() {
return false;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Comment thread
GCyganek marked this conversation as resolved.
Resolved
function updateGpsTripNotificationDistance(_distanceInMeters: number) {}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Comment thread
GCyganek marked this conversation as resolved.
Resolved
function updateGpsTripNotificationUnit(_translate: LocalizedTranslate, _unit: Unit) {}

export {startGpsTripNotification, stopGpsTripNotification, updateGpsTripNotificationDistance, updateGpsTripNotificationUnit, checkAndCleanGpsNotification, shouldUpdateGpsNotificationUnit};
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@ import type {Unit} from '@src/types/onyx/Policy';
const ATTRIBUTES_TYPE = 'GpsTripAttributes';

let activityId: string | null = null;
let distanceUnit: Unit | null = null;
let lastDistanceUnit: Unit | null = null;
let lastDistanceUnitLong: string | null = null;
let lastDistanceInMeters: number | null = null;

function getDistanceUnitLong(translate: LocalizedTranslate, unit: Unit) {
return unit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES ? translate('common.miles') : translate('common.kilometers');
}

function startGpsTripNotification(translate: LocalizedTranslate, reportID: string, unit: Unit, distanceInMeters = 0) {
const subtitle = translate('gps.liveActivity.subtitle');
const buttonText = translate('gps.liveActivity.button');
const distanceUnitLong = unit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES ? translate('common.miles') : translate('common.kilometers');

lastDistanceUnitLong = getDistanceUnitLong(translate, unit);
lastDistanceUnit = unit;
lastDistanceInMeters = distanceInMeters;

const deepLink = ROUTES.DISTANCE_REQUEST_CREATE_TAB_GPS.getRoute(CONST.IOU.ACTION.CREATE, CONST.IOU.TYPE.CREATE, CONST.IOU.OPTIMISTIC_TRANSACTION_ID, reportID);

Expand All @@ -23,38 +32,62 @@ function startGpsTripNotification(translate: LocalizedTranslate, reportID: strin
.start({
attributesType: ATTRIBUTES_TYPE,
content: {
state: {distance},
state: {distance, distanceUnit: unit, distanceUnitLong: lastDistanceUnitLong},
relevanceScore: 100,
},
attributes: {deepLink, subtitle, distanceUnit: unit, distanceUnitLong, buttonText},
attributes: {deepLink, subtitle, buttonText},
})
.then((activity) => {
activityId = activity.id;
distanceUnit = unit;
})
.catch((error: unknown) => console.error('[GPS Live Activity] Failed to start', error));
}

function updateGpsTripNotification(distanceInMeters: number) {
if (activityId === null || distanceUnit === null) {
console.error('[GPS Live Activity] Failed to start update: activityId or distanceUnit is null');
return;
}

const distance = DistanceRequestUtils.convertDistanceUnit(distanceInMeters, distanceUnit);

function updateGpsTripNotification(nonNullActivityId: string, distance: number, distanceUnit: Unit, distanceUnitLong: string) {
Airship.iOS.liveActivityManager
.update({
attributesType: ATTRIBUTES_TYPE,
activityId,
activityId: nonNullActivityId,
content: {
state: {distance},
state: {distance, distanceUnit, distanceUnitLong},
relevanceScore: 100,
},
})
.catch((error: unknown) => console.error('[GPS Live Activity] Failed to update', error));
}

function updateGpsTripNotificationDistance(distanceInMeters: number) {
if (activityId === null || lastDistanceUnit === null || lastDistanceUnitLong === null) {
console.error('[GPS Live Activity] Failed to start update: activityId or lastDistanceUnit/lastDistanceUnitLong is null');
return;
}

lastDistanceInMeters = distanceInMeters;

const distance = DistanceRequestUtils.convertDistanceUnit(distanceInMeters, lastDistanceUnit);

updateGpsTripNotification(activityId, distance, lastDistanceUnit, lastDistanceUnitLong);
}

function updateGpsTripNotificationUnit(translate: LocalizedTranslate, unit: Unit) {
if (activityId === null || lastDistanceInMeters === null) {
console.error('[GPS Live Activity] Failed to start update: activityId or lastDistanceInMeters is null');
return;
}

// Update is not needed if the distance unit will stay the same
if (lastDistanceUnit === unit) {
return;
}

lastDistanceUnitLong = getDistanceUnitLong(translate, unit);
lastDistanceUnit = unit;

const distance = DistanceRequestUtils.convertDistanceUnit(lastDistanceInMeters, unit);

updateGpsTripNotification(activityId, distance, lastDistanceUnit, lastDistanceUnitLong);
}

function stopGpsTripNotification() {
if (activityId === null) {
return;
Expand All @@ -69,7 +102,9 @@ function stopGpsTripNotification() {
.catch((error: unknown) => console.error('[GPS Live Activity] Failed to end', error));

activityId = null;
distanceUnit = null;
lastDistanceUnit = null;
lastDistanceInMeters = null;
lastDistanceUnitLong = null;
}

async function checkAndCleanGpsNotification() {
Expand All @@ -90,4 +125,8 @@ async function checkAndCleanGpsNotification() {
}
}

export {startGpsTripNotification, updateGpsTripNotification, stopGpsTripNotification, checkAndCleanGpsNotification};
function shouldUpdateGpsNotificationUnit() {
return activityId !== null;
}

export {startGpsTripNotification, updateGpsTripNotificationDistance, updateGpsTripNotificationUnit, stopGpsTripNotification, checkAndCleanGpsNotification, shouldUpdateGpsNotificationUnit};
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ function startGpsTripNotification(_translate: LocalizedTranslate, _reportID: str
function stopGpsTripNotification() {}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function updateGpsTripNotification(_distanceInMeters: number) {}
function updateGpsTripNotificationDistance(_distanceInMeters: number) {}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function updateGpsTripNotificationUnit(_translate: LocalizedTranslate, _unit: Unit) {}
Comment thread
GCyganek marked this conversation as resolved.
Resolved

async function checkAndCleanGpsNotification(): Promise<void> {
// no-op
}

export {startGpsTripNotification, stopGpsTripNotification, updateGpsTripNotification, checkAndCleanGpsNotification};
function shouldUpdateGpsNotificationUnit(): boolean {
return false;
}

export {startGpsTripNotification, stopGpsTripNotification, updateGpsTripNotificationDistance, updateGpsTripNotificationUnit, checkAndCleanGpsNotification, shouldUpdateGpsNotificationUnit};
Loading