[gnome-contacts/wip/nielsdg/converge-editor-and-sheet] ContactForm: create a common base structure.



commit 0a0f2eb06a4b831604f3962c6d3510aada173c4c
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Sun Apr 22 18:43:50 2018 +0200

    ContactForm: create a common base structure.
    
    ContactEditor and ContactSheet share a lot of common code. By having a
    ContactForm class, we can exploit this to remove some copy-pasta.

 data/ui/contacts-contact-editor.ui |  4 +--
 data/ui/contacts-contact-sheet.ui  |  2 +-
 po/POTFILES.in                     |  1 +
 po/POTFILES.skip                   |  1 +
 src/contacts-contact-editor.vala   |  8 ++---
 src/contacts-contact-form.vala     | 67 ++++++++++++++++++++++++++++++++++++++
 src/contacts-contact-sheet.vala    | 10 ++----
 src/contacts-contact.vala          | 23 -------------
 src/meson.build                    |  1 +
 9 files changed, 77 insertions(+), 40 deletions(-)
---
diff --git a/data/ui/contacts-contact-editor.ui b/data/ui/contacts-contact-editor.ui
index 57a862f..4f181e8 100644
--- a/data/ui/contacts-contact-editor.ui
+++ b/data/ui/contacts-contact-editor.ui
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
-  <requires lib="gtk+" version="3.20"/>
+  <requires lib="gtk+" version="3.22"/>
 
   <menu id="edit-contact-menu">
     <item>
@@ -49,7 +49,7 @@
     </item>
   </menu>
 
-  <template class="ContactsContactEditor" parent="GtkGrid">
+  <template class="ContactsContactEditor" parent="ContactsContactForm">
     <property name="visible">True</property>
     <property name="orientation">vertical</property>
     <child>
diff --git a/data/ui/contacts-contact-sheet.ui b/data/ui/contacts-contact-sheet.ui
index a65ece5..835beb6 100644
--- a/data/ui/contacts-contact-sheet.ui
+++ b/data/ui/contacts-contact-sheet.ui
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <!-- interface-requires gtk+ 3.22 -->
-  <template class="ContactsContactSheet" parent="GtkGrid">
+  <template class="ContactsContactSheet" parent="ContactsContactForm">
     <property name="visible">True</property>
     <property name="hexpand">True</property>
     <property name="vexpand">True</property>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 09289c9..f600768 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -20,6 +20,7 @@ src/contacts-app.vala
 src/contacts-avatar-selector.vala
 src/contacts-avatar.vala
 src/contacts-contact-editor.vala
+src/contacts-contact-form.vala
 src/contacts-contact-list.vala
 src/contacts-contact-pane.vala
 src/contacts-contact-sheet.vala
diff --git a/po/POTFILES.skip b/po/POTFILES.skip
index 8fae880..6d83981 100644
--- a/po/POTFILES.skip
+++ b/po/POTFILES.skip
@@ -8,6 +8,7 @@ src/contacts-avatar.c
 src/contacts-avatar-selector.c
 src/contacts-contact.c
 src/contacts-contact-editor.c
+src/contacts-contact-form.c
 src/contacts-contact-list.c
 src/contacts-contact-pane.c
 src/contacts-contact-sheet.c
diff --git a/src/contacts-contact-editor.vala b/src/contacts-contact-editor.vala
index 8e224e6..8b71448 100644
--- a/src/contacts-contact-editor.vala
+++ b/src/contacts-contact-editor.vala
@@ -63,7 +63,7 @@ public class Contacts.AddressEditor : Box {
  * A widget that allows the user to edit a given {@link Contact}.
  */
 [GtkTemplate (ui = "/org/gnome/Contacts/ui/contacts-contact-editor.ui")]
-public class Contacts.ContactEditor : Grid {
+public class Contacts.ContactEditor : ContactForm {
 
   private const string[] DEFAULT_PROPS_NEW_CONTACT = {
     "email-addresses.personal",
@@ -71,9 +71,6 @@ public class Contacts.ContactEditor : Grid {
     "postal-addresses.home"
   };
 
-  private Contact contact;
-  private Store store;
-
   [GtkChild]
   private Grid container_grid;
   private weak Widget focus_widget;
@@ -108,7 +105,6 @@ public class Contacts.ContactEditor : Grid {
     HashMap<int, RowData?> rows;
   }
 
-  private int last_row;
   /* the key of the hash_map is the uid of the persona */
   private HashMap<string, HashMap<string, Field?>> writable_personas;
 
@@ -176,7 +172,7 @@ public class Contacts.ContactEditor : Grid {
         last_store_position = ++i;
       }
 
-      var rw_props = Contact.sort_persona_properties (p.writeable_properties);
+      var rw_props = sort_persona_properties (p.writeable_properties);
       if (rw_props.length != 0) {
         this.writable_personas[p.uid] = new HashMap<string, Field?> ();
         foreach (var prop in rw_props)
diff --git a/src/contacts-contact-form.vala b/src/contacts-contact-form.vala
new file mode 100644
index 0000000..c07634e
--- /dev/null
+++ b/src/contacts-contact-form.vala
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2018 Niels De Graef <nielsdegraef gmail com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Gee;
+using Gtk;
+
+/**
+ * A parent class for the {@link ContactEditor} and the {@link ContactSheet}.
+ *
+ * This exploits the common structure of both widgets: they both display a
+ * (possibly empty) contact, starting with a header and subsequently iterating
+ * over the several {@link Folks.Persona}s, displaying their properties.
+ */
+public abstract class Contacts.ContactForm : Grid {
+
+  protected const string[] SORTED_PROPERTIES = {
+    "email-addresses",
+    "phone-numbers",
+    "im-addresses",
+    "urls",
+    "nickname",
+    "birthday",
+    "postal-addresses",
+    "notes"
+  };
+
+  protected Contact? contact;
+
+  protected Store store;
+
+  protected int last_row = 0;
+
+  protected string[] sort_persona_properties (string[] props) {
+    CompareDataFunc<string> compare_properties = (a, b) => {
+        foreach (var prop in SORTED_PROPERTIES) {
+          if (a == prop)
+            return (b == prop)? 0 : -1;
+
+          if (b == prop)
+            return 1;
+        }
+
+        return 0;
+      };
+
+    var sorted_props = new ArrayList<string> ();
+    foreach (var s in props)
+      sorted_props.add (s);
+
+    sorted_props.sort ((owned) compare_properties);
+    return sorted_props.to_array ();
+  }
+}
diff --git a/src/contacts-contact-sheet.vala b/src/contacts-contact-sheet.vala
index 36bd4da..8d9ed2b 100644
--- a/src/contacts-contact-sheet.vala
+++ b/src/contacts-contact-sheet.vala
@@ -25,13 +25,7 @@ using Gee;
  * (Note: to edit a contact, use the {@link ContactEditor} instead.
  */
 [GtkTemplate (ui = "/org/gnome/Contacts/ui/contacts-contact-sheet.ui")]
-public class Contacts.ContactSheet : Grid {
-
-  private Contact? contact;
-
-  private Store store;
-
-  private int last_row = 0;
+public class Contacts.ContactSheet : ContactForm {
 
   [GtkChild]
   private Label name_label;
@@ -144,7 +138,7 @@ public class Contacts.ContactSheet : Grid {
       }
       is_first_persona = false;
 
-      foreach (var prop in Contact.SORTED_PROPERTIES)
+      foreach (var prop in ContactForm.SORTED_PROPERTIES)
         add_row_for_property (p, prop);
 
       // Nothing to show in the persona: don't mention it
diff --git a/src/contacts-contact.vala b/src/contacts-contact.vala
index 6bf98f4..b537e73 100644
--- a/src/contacts-contact.vala
+++ b/src/contacts-contact.vala
@@ -475,29 +475,6 @@ public class Contacts.Contact : GLib.Object  {
     return store.display_name;
   }
 
-  public const string[] SORTED_PROPERTIES = { "email-addresses" , "phone-numbers" , "im-addresses", "urls", 
"nickname", "birthday", "postal-addresses", "notes" };
-
-  public static string[] sort_persona_properties (string[] props) {
-    CompareDataFunc<string> compare_properties = (a, b) => {
-        foreach (var prop in SORTED_PROPERTIES) {
-          if (a == prop)
-            return (b == prop)? 0 : -1;
-
-          if (b == prop)
-            return 1;
-        }
-
-        return 0;
-      };
-
-    var sorted_props = new ArrayList<string> ();
-    foreach (var s in props)
-      sorted_props.add (s);
-
-    sorted_props.sort ((owned) compare_properties);
-    return sorted_props.to_array ();
-  }
-
   /* Tries to set the property on all persons that have it writeable, and
    * if none, creates a new persona and writes to it, returning the new
    * persona.
diff --git a/src/meson.build b/src/meson.build
index d05d1f5..5669450 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -11,6 +11,7 @@ contacts_vala_sources = files(
   'contacts-avatar.vala',
   'contacts-avatar-selector.vala',
   'contacts-contact-editor.vala',
+  'contacts-contact-form.vala',
   'contacts-contact-list.vala',
   'contacts-contact-pane.vala',
   'contacts-contact-sheet.vala',


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