[gnome-weather] Add last updated time



commit c5efe33c25fd50a8263ebc17baa015af39e843dd
Author: Vitaly Dyachkov <obyknovenius me com>
Date:   Mon Jul 6 08:28:40 2020 +0000

    Add last updated time

 data/application.css   |  4 +++
 data/weather-widget.ui | 16 +++++++++++-
 src/app/city.js        | 71 +++++++++++++++++++++++++++++++++++++++++++++++++-
 src/app/main.js        |  4 ++-
 4 files changed, 92 insertions(+), 3 deletions(-)
---
diff --git a/data/application.css b/data/application.css
index f831ad1..af9a1b2 100644
--- a/data/application.css
+++ b/data/application.css
@@ -52,3 +52,7 @@
 
 @define-color temp_graph_border_color rgba(246, 211, 45, 1.0);
 @define-color temp_graph_background_color rgba(248, 228, 92, 0.5);
+
+#updated-time-label {
+    font-size: 9pt;
+}
diff --git a/data/weather-widget.ui b/data/weather-widget.ui
index 9bfb198..533ab73 100644
--- a/data/weather-widget.ui
+++ b/data/weather-widget.ui
@@ -22,7 +22,7 @@
               <object class="GtkOverlay" id="forecast-overlay">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
-                <property name="margin_bottom">50</property>
+                <property name="margin_bottom">24</property>
                 <child>
                   <object class="GtkStack" id="forecastStack">
                     <property name="visible">True</property>
@@ -243,6 +243,20 @@
                 <property name="top_attach">0</property>
               </packing>
             </child>
+            <child>
+              <object class="GtkLabel" id="updatedTimeLabel">
+                <property name="name">updated-time-label</property>
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="halign">start</property>
+                <property name="margin_start">18</property>
+                <property name="margin_bottom">20</property>
+              </object>
+              <packing>
+                <property name="left_attach">0</property>
+                <property name="top_attach">2</property>
+              </packing>
+            </child>
           </object>
         </child>
       </object>
diff --git a/src/app/city.js b/src/app/city.js
index 9885e1f..a862fbd 100644
--- a/src/app/city.js
+++ b/src/app/city.js
@@ -32,13 +32,16 @@ const SPINNER_SIZE = 128;
 
 const SCROLLING_ANIMATION_TIME = 400000; //us
 
+const UPDATED_TIME_TIMEOUT = 60; //s
+
 var WeatherWidget = GObject.registerClass({
     Template: 'resource:///org/gnome/Weather/weather-widget.ui',
     InternalChildren: ['contentFrame', 'outerGrid', 'conditionsImage',
                        'placesButton', 'placesLabel','temperatureLabel',
                        'forecastStack','leftButton', 'rightButton',
                        'forecast-hourly', 'forecast-hourly-alignment',
-                       'forecast-daily', 'forecast-daily-alignment'],
+                       'forecast-daily', 'forecast-daily-alignment',
+                       'updatedTimeLabel'],
 }, class WeatherWidget extends Gtk.Frame {
 
     _init(application, window, params) {
@@ -106,6 +109,18 @@ var WeatherWidget = GObject.registerClass({
 
             this._beginScrollAnimation(target);
         });
+
+        this._updatedTime = null;
+        this._updatedTimeTimeoutId = 0;
+
+        this.connect('destroy', () => this._onDestroy());
+    }
+
+    _onDestroy() {
+        if (this._updatedTimeTimeoutId) {
+            GLib.Source.remove(this._updatedTimeTimeoutId);
+            this._updatedTimeTimeoutId = 0;
+        }
     }
 
     _syncLeftRightButtons() {
@@ -193,6 +208,60 @@ var WeatherWidget = GObject.registerClass({
         let tz = GLib.TimeZone.new(info.location.get_timezone().get_tzid());
         for (let t of ['hourly', 'daily'])
             this._forecasts[t].update(forecasts, tz);
+
+        if (this._updatedTimeTimeoutId)
+            GLib.Source.remove(this._updatedTimeTimeoutId);
+
+        this._updatedTime = Date.now();
+        this._updatedTimeLabel.label = this._formatUpdatedTime();
+
+        this._updatedTimeTimeoutId = GLib.timeout_add_seconds(
+            GLib.PRIORITY_DEFAULT,
+            UPDATED_TIME_TIMEOUT, () => {
+                this._updatedTimeLabel.label = this._formatUpdatedTime();
+                return GLib.SOURCE_CONTINUE;
+            }
+        );
+    }
+
+    _formatUpdatedTime() {
+        if (this._updatedTime == null)
+            return '';
+
+        const milliseconds = Date.now() - this._updatedTime;
+
+        const seconds = milliseconds / 1000;
+        if (seconds < 60)
+            return _('Updated just now.');
+
+        const minutes = seconds / 60;
+        if (minutes < 60)
+            return ngettext(
+                'Updated %d minute ago.',
+                'Updated %d minutes ago.', minutes).format(minutes);
+
+        const hours = minutes / 60;
+        if (hours < 24)
+            return ngettext(
+                'Updated %d hour ago.',
+                'Updated %d hours ago.', hours).format(hours);
+
+        const days = hours / 24;
+        if (days < 7)
+            return ngettext(
+                'Updated %d day ago.',
+                'Updated %d days ago.', days).format(days);
+
+        const weeks = days / 7;
+        if (days < 30)
+            return ngettext(
+                'Updated %d week ago.',
+                'Updated %d weeks ago.', weeks).format(weeks);
+
+        const months = days / 30;
+        return ngettext(
+            'Updated %d month ago.',
+            '%d months ago.', months).format(months);
     }
 });
 
diff --git a/src/app/main.js b/src/app/main.js
index 6e93fa9..9f5fdf5 100644
--- a/src/app/main.js
+++ b/src/app/main.js
@@ -16,8 +16,10 @@
 // with Gnome Weather; if not, write to the Free Software Foundation,
 // Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
-pkg.initGettext();
 pkg.initFormat();
+pkg.initGettext();
+window.ngettext = imports.gettext.ngettext;
+
 pkg.require({ 'Gdk': '3.0',
               'Gio': '2.0',
               'GLib': '2.0',


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