Skip to content

Commit

Permalink
Merge pull request #1183 from JGreenlee/fix-timeline-refresh
Browse files Browse the repository at this point in the history
🐛 Fix timeline range issues
  • Loading branch information
shankari authored Oct 9, 2024
2 parents 7988b05 + 6d1fcad commit 655f843
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.cordovabuild.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"cordova-plugin-app-version": "0.1.14",
"cordova-plugin-customurlscheme": "5.0.2",
"cordova-plugin-device": "2.1.0",
"cordova-plugin-em-datacollection": "git+https://github.com/e-mission/e-mission-data-collection.git#v1.9.2",
"cordova-plugin-em-datacollection": "git+https://github.com/e-mission/e-mission-data-collection.git#v1.9.3",
"cordova-plugin-em-opcodeauth": "git+https://github.com/e-mission/cordova-jwt-auth.git#v1.7.2",
"cordova-plugin-em-server-communication": "git+https://github.com/e-mission/cordova-server-communication.git#v1.2.7",
"cordova-plugin-em-serversync": "git+https://github.com/e-mission/cordova-server-sync.git#v1.3.3",
Expand Down
23 changes: 14 additions & 9 deletions www/js/TimelineContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ import useAppStateChange from './useAppStateChange';
import { isoDateRangeToTsRange, isoDateWithOffset } from './datetimeUtil';
import { base_modes } from 'e-mission-common';

const TODAY_DATE = DateTime.now().toISODate();
const getTodayDate = () => DateTime.now().toISODate();
// initial date range is the past week: [TODAY - 6 days, TODAY]
const INITIAL_DATE_RANGE: [string, string] = [isoDateWithOffset(TODAY_DATE, -6), TODAY_DATE];
const getPastWeekDateRange = (): [string, string] => {
const todayDate = getTodayDate();
return [isoDateWithOffset(todayDate, -6), todayDate];
};

type ContextProps = {
labelOptions: LabelOptions | null;
Expand Down Expand Up @@ -60,7 +63,7 @@ export const useTimelineContext = (): ContextProps => {
// date range (inclusive) that has been loaded into the UI [YYYY-MM-DD, YYYY-MM-DD]
const [queriedDateRange, setQueriedDateRange] = useState<[string, string] | null>(null);
// date range (inclusive) chosen by datepicker [YYYY-MM-DD, YYYY-MM-DD]
const [dateRange, setDateRange] = useState<[string, string]>(INITIAL_DATE_RANGE);
const [dateRange, setDateRange] = useState<[string, string]>(getPastWeekDateRange);
// map of timeline entries (trips, places, untracked time), ids to objects
const [timelineMap, setTimelineMap] = useState<TimelineMap | null>(null);
const [timelineIsLoading, setTimelineIsLoading] = useState<string | false>('replace');
Expand Down Expand Up @@ -176,15 +179,15 @@ export const useTimelineContext = (): ContextProps => {

function loadDateRange(range: [string, string]) {
logDebug('Timeline: loadDateRange with newDateRange = ' + range);
if (!pipelineRange) {
logWarn('No pipelineRange yet - early return from loadDateRange');
if (!pipelineRange?.start_ts) {
logWarn('No pipelineRange start_ts yet - early return from loadDateRange');
return;
}
const pipelineStartDate = DateTime.fromSeconds(pipelineRange.start_ts).toISODate();
// clamp range to ensure it is within [pipelineStartDate, TODAY_DATE]
const clampedDateRange: [string, string] = [
new Date(range[0]) < new Date(pipelineStartDate) ? pipelineStartDate : range[0],
new Date(range[1]) > new Date(TODAY_DATE) ? TODAY_DATE : range[1],
new Date(range[1]) > new Date(getTodayDate()) ? getTodayDate() : range[1],
];
if (clampedDateRange[0] != dateRange?.[0] || clampedDateRange[1] != dateRange?.[1]) {
logDebug('Timeline: loadDateRange setting new date range = ' + clampedDateRange);
Expand Down Expand Up @@ -220,8 +223,10 @@ export const useTimelineContext = (): ContextProps => {
}

async function fetchTripsInRange(dateRange: [string, string]) {
if (!pipelineRange?.start_ts || !pipelineRange?.end_ts)
return logWarn('No pipelineRange yet - early return');
if (!pipelineRange?.start_ts || !pipelineRange?.end_ts) {
logDebug('No pipelineRange yet, returning empty lists');
return [[], []];
}
logDebug('Timeline: fetchTripsInRange from ' + dateRange[0] + ' to ' + dateRange[1]);

const [startTs, endTs] = isoDateRangeToTsRange(dateRange);
Expand Down Expand Up @@ -257,7 +262,7 @@ export const useTimelineContext = (): ContextProps => {
try {
logDebug('timelineContext: refreshTimeline');
setTimelineIsLoading('replace');
setDateRange(INITIAL_DATE_RANGE);
setDateRange(getPastWeekDateRange());
setQueriedDateRange(null);
setTimelineMap(null);
setRefreshTime(new Date());
Expand Down
3 changes: 2 additions & 1 deletion www/js/diary/list/TimelineScrollList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ const TimelineScrollList = ({ listEntries }: Props) => {
</LoadMoreButton>
);

const pipelineEndDate = pipelineRange && DateTime.fromSeconds(pipelineRange.end_ts).toISODate();
const pipelineEndDate =
pipelineRange?.end_ts && DateTime.fromSeconds(pipelineRange.end_ts).toISODate();
const noTravelBanner = (
<Banner visible={true} icon={({ size }) => <Icon source="alert-circle" size={size} />}>
<View style={{ width: '100%' }}>
Expand Down
23 changes: 14 additions & 9 deletions www/js/usePermissionStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { useTranslation } from 'react-i18next';
import { useAppTheme } from './appTheme';
import { logDebug, logWarn } from './plugin/logger';
import { AlertManager } from './components/AlertBar';
import { storageGet } from './plugin/storage';

const HAS_REQUESTED_NOTIFS_KEY = 'HasRequestedNotificationPermission';

let DEVICE_PLATFORM: 'android' | 'ios';
let DEVICE_VERSION: number;
Expand Down Expand Up @@ -224,7 +227,7 @@ const usePermissionStatus = () => {
}
}

function setupNotificationChecks() {
function setupNotificationChecks(hasRequestedNotifs) {
let fixPerms = () => {
logDebug('fix and refresh notification permissions');
appAndChannelNotificationsCheck.wasRequested = true;
Expand Down Expand Up @@ -252,7 +255,7 @@ const usePermissionStatus = () => {
fix: fixPerms,
refresh: checkPerms,
isOptional: true,
wasRequested: false,
wasRequested: hasRequestedNotifs,
};
let tempChecks = checkList;
tempChecks.push(appAndChannelNotificationsCheck);
Expand Down Expand Up @@ -344,7 +347,7 @@ const usePermissionStatus = () => {
logDebug('Explanation = ' + explanationList);
}

function createChecklist() {
function createChecklist(hasRequestedNotifs) {
setupLocChecks();
setupFitnessChecks();
if (DEVICE_PLATFORM == 'android') {
Expand All @@ -353,7 +356,7 @@ const usePermissionStatus = () => {
}
setupAndroidBackgroundRestrictionChecks();
}
setupNotificationChecks();
setupNotificationChecks(hasRequestedNotifs);
refreshAllChecks(checkList);
}

Expand All @@ -365,11 +368,13 @@ const usePermissionStatus = () => {
//load when ready
useEffect(() => {
if (appConfig && window['device']?.platform) {
DEVICE_PLATFORM = window['device'].platform.toLowerCase();
DEVICE_VERSION = window['device'].version.split('.')[0];
setupPermissionText();
logDebug('setting up permissions');
createChecklist();
storageGet(HAS_REQUESTED_NOTIFS_KEY).then((hasRequestedNotifs) => {
DEVICE_PLATFORM = window['device'].platform.toLowerCase();
DEVICE_VERSION = window['device'].version.split('.')[0];
setupPermissionText();
logDebug('setting up permissions');
createChecklist(hasRequestedNotifs);
});
}
}, [appConfig]);

Expand Down

0 comments on commit 655f843

Please sign in to comment.