Fixed crash on android in view sessions

- time formatting wasn't defined
- fixes issue 6
This commit is contained in:
Martin Bauer 2023-10-29 12:55:13 +01:00
parent 6ed968a8c5
commit 96db91f027
2 changed files with 50 additions and 30 deletions

View File

@ -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 };

View File

@ -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) => <SessionCardBehindSwipe onDelete={() => { deleteSession(data.item.name) }} />}
leftOpenValue={0}