From 770c641f1bffba3914701cbb76c43ad626dc2e40 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 2 Oct 2021 16:11:45 +0300 Subject: [PATCH] refactor: ETA calculation (#2055) * Refactor ETA calculation * Update src/renderer/pages/player-page.js Co-authored-by: Alex * Fix lint issue Co-authored-by: Alex --- src/renderer/lib/time.js | 20 ++++++++++++++++++++ src/renderer/pages/player-page.js | 12 +++--------- src/renderer/pages/torrent-list-page.js | 14 +++----------- 3 files changed, 26 insertions(+), 20 deletions(-) create mode 100644 src/renderer/lib/time.js diff --git a/src/renderer/lib/time.js b/src/renderer/lib/time.js new file mode 100644 index 00000000..ee1a831d --- /dev/null +++ b/src/renderer/lib/time.js @@ -0,0 +1,20 @@ +module.exports = { + calculateEta +} + +function calculateEta (missing, downloadSpeed) { + if (downloadSpeed === 0 || missing === 0) return + + const rawEta = missing / downloadSpeed + const hours = Math.floor(rawEta / 3600) % 24 + const minutes = Math.floor(rawEta / 60) % 60 + const seconds = Math.floor(rawEta % 60) + + // Only display hours and minutes if they are greater than 0 but always + // display minutes if hours is being displayed + const hoursStr = hours ? hours + ' h' : '' + const minutesStr = (hours || minutes) ? minutes + ' min' : '' + const secondsStr = seconds + ' s' + + return `${hoursStr} ${minutesStr} ${secondsStr} remaining` +} diff --git a/src/renderer/pages/player-page.js b/src/renderer/pages/player-page.js index 9ebfa9ab..6b16cc8f 100644 --- a/src/renderer/pages/player-page.js +++ b/src/renderer/pages/player-page.js @@ -6,6 +6,7 @@ const TorrentSummary = require('../lib/torrent-summary') const Playlist = require('../lib/playlist') const { dispatch, dispatcher } = require('../lib/dispatcher') const config = require('../../config') +const { calculateEta } = require('../lib/time') // Shows a streaming video player. Standard features + Chromecast + Airplay module.exports = class Player extends React.Component { @@ -446,16 +447,9 @@ function renderCastScreen (state) { const downloadSpeed = prog.downloadSpeed || 0 if (downloadSpeed === 0 || missing === 0) return - const rawEta = missing / downloadSpeed - const hours = Math.floor(rawEta / 3600) % 24 - const minutes = Math.floor(rawEta / 60) % 60 - const seconds = Math.floor(rawEta % 60) + const etaStr = calculateEta(missing, downloadSpeed) - const hoursStr = hours ? hours + ' h' : '' - const minutesStr = (hours || minutes) ? minutes + ' min' : '' - const secondsStr = seconds + ' s' - - return ({hoursStr} {minutesStr} {secondsStr} remaining) + return ({etaStr}) } function renderDownloadProgress () { diff --git a/src/renderer/pages/torrent-list-page.js b/src/renderer/pages/torrent-list-page.js index e84dcd92..1a52897d 100644 --- a/src/renderer/pages/torrent-list-page.js +++ b/src/renderer/pages/torrent-list-page.js @@ -7,6 +7,7 @@ const LinearProgress = require('material-ui/LinearProgress').default const TorrentSummary = require('../lib/torrent-summary') const TorrentPlayer = require('../lib/torrent-player') const { dispatcher } = require('../lib/dispatcher') +const { calculateEta } = require('../lib/time') module.exports = class TorrentList extends React.Component { render () { @@ -195,18 +196,9 @@ module.exports = class TorrentList extends React.Component { const downloadSpeed = prog.downloadSpeed if (downloadSpeed === 0 || missing === 0) return - const rawEta = missing / downloadSpeed - const hours = Math.floor(rawEta / 3600) % 24 - const minutes = Math.floor(rawEta / 60) % 60 - const seconds = Math.floor(rawEta % 60) + const etaStr = calculateEta(missing, downloadSpeed) - // Only display hours and minutes if they are greater than 0 but always - // display minutes if hours is being displayed - const hoursStr = hours ? hours + ' h' : '' - const minutesStr = (hours || minutes) ? minutes + ' min' : '' - const secondsStr = seconds + ' s' - - return ({hoursStr} {minutesStr} {secondsStr} remaining) + return ({etaStr}) } function renderTorrentStatus () {