From 96db91f027ed571fe352f95db593827ffa4f670d Mon Sep 17 00:00:00 2001 From: Martin Bauer Date: Sun, 29 Oct 2023 12:55:13 +0100 Subject: [PATCH] Fixed crash on android in view sessions - time formatting wasn't defined - fixes issue 6 --- SwimTracker/utility/TimeUtils.js | 76 +++++++++++++++++---------- SwimTracker/views/LastSessionsView.js | 4 +- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/SwimTracker/utility/TimeUtils.js b/SwimTracker/utility/TimeUtils.js index 328e1fd..1295a20 100644 --- a/SwimTracker/utility/TimeUtils.js +++ b/SwimTracker/utility/TimeUtils.js @@ -1,37 +1,58 @@ - -import moment from 'moment/min/moment-with-locales'; import {i18n} from './i18n'; +const locale = i18n.locale; + +const fullDateFormat = Intl.DateTimeFormat(locale, { + weekday: "short", + year: "2-digit", + month: "2-digit", + day: "2-digit", + hour: "2-digit", + minute: "2-digit", +}); +const timeFormatter = Intl.DateTimeFormat(locale, { + hour: "2-digit", + minute: "2-digit", +}); + +/* +// Doesn't work on android yet (so use full time for now instead of relative time) +const relativeTimeFormatter = new Intl.RelativeTimeFormat(locale, { numeric: "auto"}); + +const DIVISIONS = [ + { amount: 60, name: "seconds" }, + { amount: 60, name: "minutes" }, + { amount: 24, name: "hours" }, + { amount: 7, name: "days" }, + { amount: 4.34524, name: "weeks" }, + { amount: 12, name: "months" }, + { amount: Number.POSITIVE_INFINITY, name: "years" }, +]; + +function formatTimeAgo(date) { + let duration = (date - new Date()) / 1000 + for (let i = 0; i < DIVISIONS.length; i++) { + const division = DIVISIONS[i] + if (Math.abs(duration) < division.amount) { + const suffix = (division.name === "days") ? ", " + timeFormatter.format(date) : ""; + return relativeTimeFormatter.format(Math.round(duration), division.name) + suffix + } + duration /= division.amount + } +} +*/ function timeSince(timeStamp) { - moment.locale(i18n.locale); - const now = Math.floor((new Date()).getTime() / 1000); - const secondsPast = now - timeStamp; - if (secondsPast <= 6 * 3600) { - return moment().seconds(-secondsPast).fromNow(); - } - else{ - const timeStampDate = new Date(timeStamp * 1000); - const dateNow = new Date(); - - const timeStampMoment = moment.unix(timeStamp); - - let dateStr = ""; - - if (timeStampDate.getDate() == dateNow.getDate()) - dateStr = "Heute, " + timeStampMoment.format("HH:mm"); - else if (timeStampDate.getDate() + 1 == dateNow.getDate()) - dateStr = "Gestern, " + timeStampMoment.format("HH:mm"); - else { - dateStr = timeStampMoment.format("llll"); - } - return dateStr; - } - + const secondsPassed = now - timeStamp; + const daysPassed = secondsPassed / 60 / 60 / 24; + const timeStampDate = new Date(timeStamp * 1000); + //if (daysPassed < 2) + // return formatTimeAgo(timeStampDate); + //else + return fullDateFormat.format(timeStampDate); } - const toTimeStr = seconds => { let minuteStr = String(Math.floor(seconds / 60)); if (minuteStr.length < 2) @@ -42,5 +63,4 @@ const toTimeStr = seconds => { return minuteStr + ":" + secondStr; } - export { toTimeStr, timeSince }; diff --git a/SwimTracker/views/LastSessionsView.js b/SwimTracker/views/LastSessionsView.js index cb9af50..a06b33b 100644 --- a/SwimTracker/views/LastSessionsView.js +++ b/SwimTracker/views/LastSessionsView.js @@ -180,7 +180,7 @@ async function getFullData(swimTrackerHost, analysisSettings) { const da = new DataAnalysis(); e.analysis = da.analyze(analysisSettings, e.startTime, e.values); } - console.log("full data", parsed); + //console.log("full data", parsed); return parsed.reverse(); } @@ -227,7 +227,7 @@ class LastSessionsView extends React.Component { textFirstLine={timeSince(data.item.startTime)} laps={(data.item.analysis.peaks.size / this.props.peaksPerLap).toFixed(1)} momentum={Math.trunc(data.item.analysis.totalMomentum * this.props.kgFactor / 10 / 60)} - activeTime={data.item.analysis.activeTime} /> + activeTime={Math.round(data.item.analysis.activeTime / 60, 0)} /> )} renderHiddenItem={(data, rowMap) => { deleteSession(data.item.name) }} />} leftOpenValue={0}