[gnome-calculator/wip/rstrode/no-network-plz] math-converter: Hide currency conversion UI if there isn't a loaded currency provider




commit 14766d808d88f266405351b729c5e2377353da98
Author: Ray Strode <rstrode redhat com>
Date:   Thu May 13 11:29:33 2021 -0400

    math-converter: Hide currency conversion UI if there isn't a loaded currency provider
    
    If the admin sets a currency refresh-interval of 0, then the currency
    conversion data will be missing or woefully out of date.
    
    This commit changes the code to hide the conversion UI in this case.

 lib/currency-provider.vala |   8 +-
 lib/currency.vala          |  18 ++-
 src/math-converter.vala    |  18 ++-
 src/ui/math-converter.ui   | 315 +++++++++++++++++++++++----------------------
 4 files changed, 203 insertions(+), 156 deletions(-)
---
diff --git a/lib/currency-provider.vala b/lib/currency-provider.vala
index 244ca152..bd471598 100644
--- a/lib/currency-provider.vala
+++ b/lib/currency-provider.vala
@@ -7,6 +7,8 @@ public interface CurrencyProvider : Object {
     public abstract void set_refresh_interval (int interval, bool asyncLoad = true);
 
     public abstract void clear ();
+
+    public abstract bool is_loaded();
 }
 
 public abstract class AbstractCurrencyProvider : Object, CurrencyProvider {
@@ -27,6 +29,10 @@ public abstract class AbstractCurrencyProvider : Object, CurrencyProvider {
         update_rates (asyncLoad);
     }
 
+    public bool is_loaded() {
+        return loaded;
+    }
+
     protected bool loading;
     protected bool loaded;
     protected List<Currency> currencies;
@@ -35,7 +41,7 @@ public abstract class AbstractCurrencyProvider : Object, CurrencyProvider {
     public void clear () {
         FileUtils.remove (rate_filepath);
     }
-    
+
     public Currency register_currency(string symbol, string source) {
         Currency currency = currency_manager.add_currency (symbol, source);
         currencies.append(currency);
diff --git a/lib/currency.vala b/lib/currency.vala
index e0d4f108..fdf44647 100644
--- a/lib/currency.vala
+++ b/lib/currency.vala
@@ -26,17 +26,21 @@ public class CurrencyManager : Object
 
     public signal void updated ();
 
+    public bool loaded { get; private set; }
+
     public void add_provider (CurrencyProvider provider) {
         providers.append (provider);
     }
 
     public void refresh_sync () {
+        loaded = false;
         foreach (var p in providers) {
             p.set_refresh_interval(_refresh_interval, false);
         }
     }
 
     public void refresh_async () {
+        loaded = false;
         foreach (var p in providers) {
             p.set_refresh_interval(_refresh_interval, true);
         }
@@ -118,11 +122,23 @@ public class CurrencyManager : Object
         return default_currency_manager;
     }
 
+    private void update ()
+    {
+        loaded = false;
+        foreach (var p in providers) {
+            if (p.is_loaded ()) {
+                loaded = true;
+                break;
+            }
+        }
+        updated ();
+    }
+
     public void initialize_providers (bool asyncLoad = true)
     {
         /* Start downloading the rates if they are outdated. */
         foreach (var p in providers) {
-            p.updated.connect ( () => { updated (); });
+            p.updated.connect ( () => { update (); });
             p.update_rates (asyncLoad);
         }
     }
diff --git a/src/math-converter.vala b/src/math-converter.vala
index 128ba149..e43d71fa 100644
--- a/src/math-converter.vala
+++ b/src/math-converter.vala
@@ -27,6 +27,7 @@ public class MathConverter : Gtk.Grid
     [GtkChild]
     private unowned Gtk.Label to_label;
 
+    public bool outer_box_visible { set; get; default = false; }
     public bool view_more_visible { set; get; default = false; }
     public bool view_more_active { set; get; default = false; }
 
@@ -39,8 +40,12 @@ public class MathConverter : Gtk.Grid
     construct
     {
         from_combo.set_cell_data_func (from_renderer, from_cell_data_func);
-        CurrencyManager.get_default ().updated.connect (() => { update_result_label (); });
+        CurrencyManager.get_default ().updated.connect (() => {
+            update_visibility ();
+            update_result_label ();
+        });
 
+        update_visibility ();
         update_from_model ();
     }
 
@@ -61,6 +66,7 @@ public class MathConverter : Gtk.Grid
             return;
         this.category = category;
 
+        update_visibility ();
         update_from_model ();
     }
 
@@ -103,6 +109,16 @@ public class MathConverter : Gtk.Grid
         to_combo.get_model ().get (to_iter, 2, out to_unit, -1);
     }
 
+    private void update_visibility ()
+    {
+        if (category != "currency") {
+            this.outer_box_visible = true;
+            return;
+        }
+
+        this.outer_box_visible = CurrencyManager.get_default ().loaded;
+    }
+
     private void update_result_label ()
     {
         var x = equation.number;
diff --git a/src/ui/math-converter.ui b/src/ui/math-converter.ui
index dd95751a..513a49b0 100644
--- a/src/ui/math-converter.ui
+++ b/src/ui/math-converter.ui
@@ -3,207 +3,216 @@
 <interface>
   <requires lib="gtk+" version="3.16"/>
   <template class="MathConverter" parent="GtkGrid">
-    <property name="visible">False</property>
-    <property name="can_focus">False</property>
-    <property name="row_spacing">6</property>
     <child>
-      <object class="GtkComboBox" id="from_combo">
-        <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <property name="hexpand">True</property>
-        <signal name="changed" handler="from_combobox_changed_cb" swapped="no"/>
-        <child>
-          <object class="GtkCellRendererText" id="from_renderer">
-            <property name="ellipsize">end</property>
-          </object>
-          <attributes>
-            <attribute name="text">0</attribute>
-          </attributes>
-        </child>
-      </object>
-    </child>
-    <child>
-      <object class="GtkButton" id="in_button">
-        <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <property name="label" translatable="yes"> to </property>
-        <signal name="clicked" handler="convert_button_clicked_cb" swapped="no"/>
-        <style>
-          <class name="flat"/>
-        </style>
-      </object>
-    </child>
-    <child>
-      <object class="GtkComboBox" id="to_combo">
-        <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <property name="opacity">0.88</property>
-        <property name="hexpand">True</property>
-        <signal name="changed" handler="to_combobox_changed_cb" swapped="no"/>
-        <child>
-          <object class="GtkCellRendererText" id="to_renderer">
-            <property name="ellipsize">end</property>
-          </object>
-          <attributes>
-            <attribute name="text">0</attribute>
-          </attributes>
-        </child>
-      </object>
-    </child>
-    <child>
-      <object class="GtkButton" id="swap_button">
-        <property name="label">⇆</property>
-        <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <property name="receives_default">False</property>
-        <property name="tooltip_text" translatable="yes">Switch conversion units</property>
-        <property name="relief">none</property>
-        <signal name="clicked" handler="swap_button_clicked_cb" swapped="no"/>
-      </object>
-    </child>
-    <child>
-      <object class="GtkBox" id="result_holder">
-        <property name="visible">True</property>
+      <object class="GtkBox" id="outer_box">
+        <property name="visible" bind-source="MathConverter" bind-property="outer-box-visible" 
bind-flags="sync-create|bidirectional"/>
         <property name="orientation">horizontal</property>
         <property name="sensitive">True</property>
-        <property name="spacing">6</property>
-        <property name="margin-end">2</property>
         <property name="can_focus">False</property>
-        <property name="halign">end</property>
+        <property name="halign">center</property>
         <property name="valign">center</property>
         <property name="hexpand">True</property>
         <property name="vexpand">False</property>
-        <property name="visible" bind-source="MathConverter" bind-property="view-more-visible" 
bind-flags="sync-create|bidirectional|invert-boolean"/>
         <child>
-          <object class="GtkLabel" id="from_label">
+          <object class="GtkComboBox" id="from_combo">
             <property name="visible">True</property>
-            <property name="sensitive">True</property>
-            <property name="selectable">True</property>
             <property name="can_focus">False</property>
-            <property name="halign">start</property>
-            <property name="valign">center</property>
             <property name="hexpand">True</property>
-            <property name="vexpand">False</property>
-            <property name="justify">center</property>
-            <property name="ellipsize">end</property>
-            <property name="xalign">0</property>
-            <property name="yalign">0</property>
+            <signal name="changed" handler="from_combobox_changed_cb" swapped="no"/>
+            <child>
+              <object class="GtkCellRendererText" id="from_renderer">
+                <property name="ellipsize">end</property>
+              </object>
+              <attributes>
+                <attribute name="text">0</attribute>
+              </attributes>
+            </child>
           </object>
         </child>
         <child>
-          <object class="GtkLabel" id="convert_equals">
+          <object class="GtkButton" id="in_button">
             <property name="visible">True</property>
-            <property name="sensitive">True</property>
             <property name="can_focus">False</property>
-            <property name="halign">center</property>
-            <property name="valign">center</property>
-            <property name="hexpand">False</property>
-            <property name="vexpand">False</property>
-            <property name="justify">center</property>
-            <property name="xalign">0</property>
-            <property name="yalign">0</property>
-            <property name="label" translatable="yes" context="convertion equals label">=</property>
+            <property name="label" translatable="yes"> to </property>
+            <signal name="clicked" handler="convert_button_clicked_cb" swapped="no"/>
+            <style>
+              <class name="flat"/>
+            </style>
           </object>
         </child>
         <child>
-          <object class="GtkLabel" id="to_label">
+          <object class="GtkComboBox" id="to_combo">
             <property name="visible">True</property>
-            <property name="sensitive">True</property>
-            <property name="selectable">True</property>
             <property name="can_focus">False</property>
-            <property name="halign">fill</property>
-            <property name="valign">center</property>
+            <property name="opacity">0.88</property>
             <property name="hexpand">True</property>
-            <property name="vexpand">False</property>
-            <property name="justify">center</property>
-            <property name="ellipsize">end</property>
-            <property name="xalign">0</property>
-            <property name="yalign">0</property>
+            <signal name="changed" handler="to_combobox_changed_cb" swapped="no"/>
+            <child>
+              <object class="GtkCellRendererText" id="to_renderer">
+                <property name="ellipsize">end</property>
+              </object>
+              <attributes>
+                <attribute name="text">0</attribute>
+              </attributes>
+            </child>
           </object>
-          <packing>
-            <property name="expand">false</property>
-          </packing>
         </child>
-      </object>
-    </child>
-    <child>
-      <object class="GtkBox">
-        <property name="orientation">horizontal</property>
-        <property name="sensitive">True</property>
-        <property name="spacing">6</property>
-        <property name="can_focus">False</property>
-        <property name="halign">end</property>
-        <property name="valign">center</property>
-        <property name="hexpand">True</property>
-        <property name="vexpand">False</property>
-        <property name="visible" bind-source="result_holder" bind-property="visible" 
bind-flags="sync-create|invert-boolean"/>
         <child>
-          <object class="GtkLabel">
+          <object class="GtkButton" id="swap_button">
+            <property name="label">⇆</property>
             <property name="visible">True</property>
-            <property name="sensitive">True</property>
-            <property name="selectable">True</property>
             <property name="can_focus">False</property>
-            <property name="halign">start</property>
-            <property name="valign">center</property>
-            <property name="hexpand">True</property>
-            <property name="vexpand">False</property>
-            <property name="justify">center</property>
-            <property name="ellipsize">end</property>
-            <property name="xalign">0</property>
-            <property name="yalign">0</property>
-            <property name="label" bind-source="from_label" bind-property="label" 
bind-flags="sync-create|bidirectional"/>
+            <property name="receives_default">False</property>
+            <property name="tooltip_text" translatable="yes">Switch conversion units</property>
+            <property name="relief">none</property>
+            <signal name="clicked" handler="swap_button_clicked_cb" swapped="no"/>
           </object>
         </child>
         <child>
-          <object class="GtkLabel">
+          <object class="GtkBox" id="result_holder">
             <property name="visible">True</property>
+            <property name="orientation">horizontal</property>
             <property name="sensitive">True</property>
+            <property name="spacing">6</property>
+            <property name="margin-end">2</property>
             <property name="can_focus">False</property>
-            <property name="halign">center</property>
+            <property name="halign">end</property>
             <property name="valign">center</property>
-            <property name="hexpand">False</property>
+            <property name="hexpand">True</property>
             <property name="vexpand">False</property>
-            <property name="justify">center</property>
-            <property name="xalign">0</property>
-            <property name="yalign">0</property>
-            <property name="label" bind-source="convert_equals" bind-property="label" 
bind-flags="sync-create|bidirectional"/>
+            <property name="visible" bind-source="MathConverter" bind-property="view-more-visible" 
bind-flags="sync-create|bidirectional|invert-boolean"/>
+            <child>
+              <object class="GtkLabel" id="from_label">
+                <property name="visible">True</property>
+                <property name="sensitive">True</property>
+                <property name="selectable">True</property>
+                <property name="can_focus">False</property>
+                <property name="halign">start</property>
+                <property name="valign">center</property>
+                <property name="hexpand">True</property>
+                <property name="vexpand">False</property>
+                <property name="justify">center</property>
+                <property name="ellipsize">end</property>
+                <property name="xalign">0</property>
+                <property name="yalign">0</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkLabel" id="convert_equals">
+                <property name="visible">True</property>
+                <property name="sensitive">True</property>
+                <property name="can_focus">False</property>
+                <property name="halign">center</property>
+                <property name="valign">center</property>
+                <property name="hexpand">False</property>
+                <property name="vexpand">False</property>
+                <property name="justify">center</property>
+                <property name="xalign">0</property>
+                <property name="yalign">0</property>
+                <property name="label" translatable="yes" context="convertion equals label">=</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkLabel" id="to_label">
+                <property name="visible">True</property>
+                <property name="sensitive">True</property>
+                <property name="selectable">True</property>
+                <property name="can_focus">False</property>
+                <property name="halign">fill</property>
+                <property name="valign">center</property>
+                <property name="hexpand">True</property>
+                <property name="vexpand">False</property>
+                <property name="justify">center</property>
+                <property name="ellipsize">end</property>
+                <property name="xalign">0</property>
+                <property name="yalign">0</property>
+              </object>
+              <packing>
+                <property name="expand">false</property>
+              </packing>
+            </child>
           </object>
         </child>
         <child>
-          <object class="GtkLabel">
-            <property name="visible">True</property>
+          <object class="GtkBox">
+            <property name="orientation">horizontal</property>
             <property name="sensitive">True</property>
-            <property name="selectable">True</property>
+            <property name="spacing">6</property>
             <property name="can_focus">False</property>
-            <property name="halign">fill</property>
+            <property name="halign">end</property>
             <property name="valign">center</property>
             <property name="hexpand">True</property>
             <property name="vexpand">False</property>
-            <property name="justify">center</property>
-            <property name="ellipsize">end</property>
-            <property name="xalign">0</property>
-            <property name="yalign">0</property>
-            <property name="label" bind-source="to_label" bind-property="label" 
bind-flags="sync-create|bidirectional"/>
-          </object>
-        </child>
-        <child>
-          <object class="GtkToggleButton" id="view_more_button">
-            <property name="visible">True</property>
-            <property name="can_focus">False</property>
-            <property name="receives_default">False</property>
-            <property name="active" bind-source="MathConverter" bind-property="view-more-active" 
bind-flags="sync-create|bidirectional"/>
+            <property name="visible" bind-source="result_holder" bind-property="visible" 
bind-flags="sync-create|invert-boolean"/>
             <child>
-              <object class="GtkImage">
+              <object class="GtkLabel">
                 <property name="visible">True</property>
+                <property name="sensitive">True</property>
+                <property name="selectable">True</property>
                 <property name="can_focus">False</property>
-                <property name="icon_name">view-more-horizontal-symbolic</property>
-                <property name="icon_size">1</property>
+                <property name="halign">start</property>
+                <property name="valign">center</property>
+                <property name="hexpand">True</property>
+                <property name="vexpand">False</property>
+                <property name="justify">center</property>
+                <property name="ellipsize">end</property>
+                <property name="xalign">0</property>
+                <property name="yalign">0</property>
+                <property name="label" bind-source="from_label" bind-property="label" 
bind-flags="sync-create|bidirectional"/>
+              </object>
+            </child>
+            <child>
+              <object class="GtkLabel">
+                <property name="visible">True</property>
+                <property name="sensitive">True</property>
+                <property name="can_focus">False</property>
+                <property name="halign">center</property>
+                <property name="valign">center</property>
+                <property name="hexpand">False</property>
+                <property name="vexpand">False</property>
+                <property name="justify">center</property>
+                <property name="xalign">0</property>
+                <property name="yalign">0</property>
+                <property name="label" bind-source="convert_equals" bind-property="label" 
bind-flags="sync-create|bidirectional"/>
+              </object>
+            </child>
+            <child>
+              <object class="GtkLabel">
+                <property name="visible">True</property>
+                <property name="sensitive">True</property>
+                <property name="selectable">True</property>
+                <property name="can_focus">False</property>
+                <property name="halign">fill</property>
+                <property name="valign">center</property>
+                <property name="hexpand">True</property>
+                <property name="vexpand">False</property>
+                <property name="justify">center</property>
+                <property name="ellipsize">end</property>
+                <property name="xalign">0</property>
+                <property name="yalign">0</property>
+                <property name="label" bind-source="to_label" bind-property="label" 
bind-flags="sync-create|bidirectional"/>
+              </object>
+            </child>
+            <child>
+              <object class="GtkToggleButton" id="view_more_button">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="receives_default">False</property>
+                <property name="active" bind-source="MathConverter" bind-property="view-more-active" 
bind-flags="sync-create|bidirectional"/>
+                <child>
+                  <object class="GtkImage">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="icon_name">view-more-horizontal-symbolic</property>
+                    <property name="icon_size">1</property>
+                  </object>
+                </child>
+                <style>
+                  <class name="image-button"/>
+                  <class name="view-more-button"/>
+                </style>
               </object>
             </child>
-            <style>
-              <class name="image-button"/>
-              <class name="view-more-button"/>
-            </style>
           </object>
         </child>
       </object>


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