[gnome-sound-recorder/bilelmoussaoui/utils: 5/5] utils: stop using a Timer object



commit 26ac525fdf08922972f1074ea94fd6159804596c
Author: Bilal Elmoussaoui <bil elmoussaoui gmail com>
Date:   Mon Jun 15 12:19:18 2020 +0200

    utils: stop using a Timer object
    
    sperate the code into two small functions and clean up their code a bit.

 src/row.js    | 12 +++++----
 src/utils.js  | 84 +++++++++++++++++++++++------------------------------------
 src/window.js |  4 +--
 3 files changed, 42 insertions(+), 58 deletions(-)
---
diff --git a/src/row.js b/src/row.js
index d96b8bc..4610a85 100644
--- a/src/row.js
+++ b/src/row.js
@@ -1,7 +1,6 @@
 /* exported Row RowState */
 const { GObject, Handy } = imports.gi;
-
-var Utils = imports.utils;
+const { displayDateTime, formatTime } = imports.utils;
 
 const RowState = {
     PLAYING: 0,
@@ -21,11 +20,14 @@ var Row = GObject.registerClass({
         super._init({});
 
         this._action_row.title = recording.name;
-        this._action_row.subtitle = Utils.Time.getDisplayTime(
-            recording.timeCreated > 0 ? recording.timeCreated : recording.timeModified);
+
+        if (recording.timeCreated > 0)
+            this._action_row.subtitle = displayDateTime(recording.timeCreated);
+        else
+            this._action_row.subtitle = displayDateTime(recording.timeModified);
 
         recording.connect('notify::duration', () => {
-            this._duration.label = Utils.Time.formatTime(recording.duration);
+            this._duration.label = formatTime(recording.duration);
         });
 
         this._playButton.connect('clicked', () => {
diff --git a/src/utils.js b/src/utils.js
index 577756c..2d42663 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -1,4 +1,4 @@
-/* exported Time */
+/* exported displayDateTime formatTime */
 /*
  * Copyright 2013 Meg Ford
  * This library is free software; you can redistribute it and/or
@@ -17,62 +17,44 @@
  * Author: Meg Ford <megford gnome org>
  *
  */
-
 const Gettext = imports.gettext;
 const GLib = imports.gi.GLib;
 
-const _TIME_DIVISOR = 60;
+var formatTime = totalSeconds => {
+    totalSeconds = Math.floor(totalSeconds);
+    const hours = parseInt(totalSeconds / Math.pow(60, 2)).toString();
+    const minutes = (parseInt(totalSeconds / 60) % 60).toString();
+    const seconds = parseInt(totalSeconds % 60).toString();
 
+    return `${hours.padStart(2, '0')}∶${minutes.padStart(2, '0')}∶${seconds.padStart(2, '0')}`;
+};
 
-var Time = {
-    formatTime: unformattedTime => {
-        this.unformattedTime = unformattedTime;
-        let seconds = Math.floor(this.unformattedTime);
-        let hoursString = parseInt(seconds / Math.pow(_TIME_DIVISOR, 2));
-        let minuteString = parseInt(seconds / _TIME_DIVISOR) % _TIME_DIVISOR;
-        let secondString = parseInt(seconds % _TIME_DIVISOR);
-        let timeString = `${hoursString < 10 ? `0${hoursString}` : hoursString}:${minuteString < 10 ? 
`0${minuteString}` : minuteString}:${secondString < 10 ? `0${secondString}` : secondString}`;
+var displayDateTime = time => {
+    const DAY = 86400000000;
+    const now = GLib.DateTime.new_now_local();
+    const difference = now.difference(time);
 
-        return timeString;
-    },
+    const days = Math.floor(difference / DAY);
+    const weeks = Math.floor(difference / (7 * DAY));
+    const months = Math.floor(difference / (30 * DAY));
+    const years = Math.floor(difference / (365 * DAY));
 
-    getDisplayTime: mtime => {
-        let text = '';
-        let DAY = 86400000000;
-        let now = GLib.DateTime.new_now_local();
-        let difference = now.difference(mtime);
-        let days = Math.floor(difference / DAY);
-        let weeks = Math.floor(difference / (7 * DAY));
-        let months = Math.floor(difference / (30 * DAY));
-        let years = Math.floor(difference / (365 * DAY));
+    if (difference < DAY)
+        return time.format('%X');
+    else if (difference < 2 * DAY)
+        return _('Yesterday');
+    else if (difference < 7 * DAY)
+        return Gettext.ngettext('%d day ago', '%d days ago', days).format(days);
+    else if (difference < 14 * DAY)
+        return _('Last week');
+    else if (difference < 28 * DAY)
+        return Gettext.ngettext('%d week ago', '%d weeks ago',  weeks).format(weeks);
+    else if (difference < 60 * DAY)
+        return _('Last month');
+    else if (difference < 360 * DAY)
+        return Gettext.ngettext('%d month ago', '%d months ago', months).format(months);
+    else if (difference < 730 * DAY)
+        return _('Last year');
 
-        if (difference < DAY) {
-            text = mtime.format('%X');
-        } else if (difference < 2 * DAY) {
-            text = _('Yesterday');
-        } else if (difference < 7 * DAY) {
-            text = Gettext.ngettext('%d day ago',
-                '%d days ago',
-                days).format(days);
-        } else if (difference < 14 * DAY) {
-            text = _('Last week');
-        } else if (difference < 28 * DAY) {
-            text = Gettext.ngettext('%d week ago',
-                '%d weeks ago',
-                weeks).format(weeks);
-        } else if (difference < 60 * DAY) {
-            text = _('Last month');
-        } else if (difference < 360 * DAY) {
-            text = Gettext.ngettext('%d month ago',
-                '%d months ago',
-                months).format(months);
-        } else if (difference < 730 * DAY) {
-            text = _('Last year');
-        } else {
-            text = Gettext.ngettext('%d year ago',
-                '%d years ago',
-                years).format(years);
-        }
-        return text;
-    },
+    return Gettext.ngettext('%d year ago', '%d years ago', years).format(years);
 };
diff --git a/src/window.js b/src/window.js
index 2c3b260..2146f17 100644
--- a/src/window.js
+++ b/src/window.js
@@ -23,7 +23,7 @@ const { Player } = imports.player;
 const { Recorder } = imports.recorder;
 const { RecordingList } = imports.recordingList;
 const { Row, RowState } = imports.row;
-const Utils = imports.utils;
+const { formatTime } = imports.utils;
 const { WaveForm } = imports.waveform;
 
 
@@ -47,7 +47,7 @@ var Window = GObject.registerClass({
         });
 
         this.recorder.connect('notify::duration', _recorder => {
-            this._recorderTime.label = Utils.Time.formatTime(_recorder.duration);
+            this._recorderTime.label = formatTime(_recorder.duration);
         });
 
         this.connect('destroy', () => {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]