Skip to content

Commit

Permalink
Merge pull request yihong0618#398 from ben-29/pr_fix_moving_time
Browse files Browse the repository at this point in the history
fix: RunTime using `moving_time`
  • Loading branch information
yihong0618 authored Apr 17, 2023
2 parents 332ad3f + 141fb40 commit 530d912
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/components/RunTable/RunRow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const RunRow = ({ runs, run, locateActivity, runIndex, setRunIndex }) => {

const heartRate = run.average_heartrate;

const runTime = formatRunTime(distance,pace);
const runTime = formatRunTime(run.moving_time);

// change click color
const handleClick = (e, runs, run) => {
Expand Down
20 changes: 6 additions & 14 deletions src/components/RunTable/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from 'react';
import { MAIN_COLOR } from 'src/utils/const';
import { sortDateFunc, sortDateFuncReverse } from 'src/utils/utils';
import { sortDateFunc, sortDateFuncReverse, convertMovingTime2Sec } from 'src/utils/utils';
import RunRow from './RunRow';
import styles from './style.module.scss';

Expand All @@ -24,19 +24,11 @@ const RunTable = ({
? a.average_heartrate - b.average_heartrate
: b.average_heartrate - a.average_heartrate;
const sortRunTimeFunc = (a, b) => {
if (Number.isNaN(a.distance) || Number.isNaN(b.distance)
|| Number.isNaN(a.average_speed) || Number.isNaN(b.average_speed)) {
return 0;
}
const aDistance = (a.distance / 1000.0).toFixed(1);
const bDistance = (b.distance / 1000.0).toFixed(1);
const aPace = (1000.0 / 60.0) * (1.0 / a.average_speed);
const bPace = (1000.0 / 60.0) * (1.0 / b.average_speed);
if (sortFuncInfo === 'Time') {
return aDistance * aPace - bDistance * bPace;
} else {
return bDistance * bPace - aDistance * aPace;
}
const aTotalSeconds = convertMovingTime2Sec(a.moving_time)
const bTotalSeconds = convertMovingTime2Sec(b.moving_time)
return sortFuncInfo === 'Time'
? aTotalSeconds - bTotalSeconds
: bTotalSeconds - aTotalSeconds
};
const sortDateFuncClick =
sortFuncInfo === 'Date' ? sortDateFunc : sortDateFuncReverse;
Expand Down
25 changes: 18 additions & 7 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@ const formatPace = (d) => {
return `${minutes}'${seconds.toFixed(0).toString().padStart(2, '0')}"`;
};

const formatRunTime = (distance,pace) => {
if (Number.isNaN(distance) || Number.isNaN(pace)) {
return '0min';
}
const formatPace = (1000.0 / 60.0) * (1.0 / pace);
const minutes = Math.floor(formatPace * distance);
const convertMovingTime2Sec = (moving_time) => {
if (!moving_time) {
return 0;
}
// moving_time : '2 days, 12:34:56' or '12:34:56';
const splits = moving_time.split(', ');
const days = splits.length == 2 ? parseInt(splits[0]) : 0;
const time = splits.splice(-1)[0]
const [hours, minutes, seconds] = time.split(':').map(Number);
const totalSeconds = (((days * 24) + hours) * 60 + minutes) * 60 + seconds;
return totalSeconds;
}

const formatRunTime = (moving_time) => {
const totalSeconds = convertMovingTime2Sec(moving_time)
const seconds = totalSeconds % 60
const minutes = (totalSeconds-seconds) / 60
if (minutes === 0) {
const seconds = Math.floor((formatPace * distance - minutes) * 60.0);
return seconds + 's';
}
return minutes + 'min';
Expand Down Expand Up @@ -241,4 +251,5 @@ export {
sortDateFuncReverse,
getBoundsForGeoData,
formatRunTime,
convertMovingTime2Sec,
};

0 comments on commit 530d912

Please sign in to comment.