[gnome-contacts] ContactPane: split set_edit_mode in 2 methods.



commit aec895206f9ee308f75f3ffa4008d7fc1ec853b0
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Sat Apr 14 16:10:31 2018 +0200

    ContactPane: split set_edit_mode in 2 methods.
    
    * A start_editing() method
    * A stop_editing() method
    
    Also clean up those methods a bit.

 src/contacts-contact-pane.vala | 113 +++++++++++++++++++----------------------
 src/contacts-contact.vala      |   9 ++--
 src/contacts-window.vala       |   7 ++-
 3 files changed, 58 insertions(+), 71 deletions(-)
---
diff --git a/src/contacts-contact-pane.vala b/src/contacts-contact-pane.vala
index f404b51..e2ee36e 100644
--- a/src/contacts-contact-pane.vala
+++ b/src/contacts-contact-pane.vala
@@ -183,8 +183,8 @@ public class Contacts.ContactPane : Stack {
     var dialog = new LinkedPersonasDialog (this.parent_window, this.store, contact);
     if (dialog.run () == ResponseType.CLOSE && dialog.any_unlinked) {
       /* update edited contact if any_unlinked */
-      set_edit_mode (false);
-      set_edit_mode (true);
+      stop_editing ();
+      start_editing ();
     }
     dialog.destroy ();
   }
@@ -196,73 +196,62 @@ public class Contacts.ContactPane : Stack {
     }
   }
 
-  public void set_edit_mode (bool on_edit, bool drop_changes = false) {
-    if (on_edit == on_edit_mode)
+  public void start_editing() {
+    if (this.on_edit_mode || this.contact == null)
       return;
 
-    if (on_edit) {
-      if (contact == null) {
-       return;
-      }
+    this.on_edit_mode = true;
 
-      on_edit_mode = true;
+    remove_contact_sheet ();
 
-      remove_contact_sheet ();
+    this.editor.clear ();
+    this.editor.edit (this.contact);
+    this.editor.show_all ();
+    set_visible_child (this.contact_editor_page);
+  }
 
-      editor.clear ();
-      editor.edit (contact);
-      editor.show_all ();
-      set_visible_child (this.contact_editor_page);
-    } else {
-      on_edit_mode = false;
-      /* saving changes */
-      if (!drop_changes) {
-       foreach (var prop in editor.properties_changed ().entries) {
-         Contact.set_persona_property.begin (prop.value.persona, prop.key, prop.value.value,
-                                             (obj, result) => {
-                                               try {
-                                                 Contact.set_persona_property.end (result);
-                                               } catch (Error e2) {
-                                                 show_message (e2.message);
-                                               }
-                                             });
-       }
-
-       if (editor.name_changed ()) {
-         var v = editor.get_full_name_value ();
-         Contact.set_individual_property.begin (contact,
-                                                "full-name", v,
-                                                (obj, result) => {
-                                                  try {
-                                                    Contact.set_individual_property.end (result);
-                                                    display_name_changed (v.get_string ());
-                                                  } catch (Error e) {
-                                                    show_message (e.message);
-                                                    /* FIXME: add this back */
-                                                    /* l.set_markup (Markup.printf_escaped ("<span 
font='16'>%s</span>", contact.display_name)); */
-                                                  }
-                                                });
-       }
-       if (editor.avatar_changed ()) {
-         var v = editor.get_avatar_value ();
-         Contact.set_individual_property.begin (contact,
-                                                "avatar", v,
-                                                (obj, result) => {
-                                                  try {
-                                                    Contact.set_individual_property.end (result);
-                                                  } catch (GLib.Error e) {
-                                                    show_message (e.message);
-                                                  }
-                                                });
-       }
+  public void stop_editing (bool drop_changes = false) {
+    if (!this.on_edit_mode)
+      return;
+
+    this.on_edit_mode = false;
+    /* saving changes */
+    if (!drop_changes)
+      save_editor_changes.begin ();
+
+    this.editor.clear ();
+
+    if (this.contact != null)
+      show_contact_sheet ();
+    else
+      set_visible_child (this.none_selected_page);
+  }
+
+  private async void save_editor_changes () {
+    foreach (var prop in this.editor.properties_changed ().entries) {
+      try {
+        yield Contact.set_persona_property (prop.value.persona, prop.key, prop.value.value);
+      } catch (Error e) {
+        show_message (e.message);
       }
+    }
 
-      editor.clear ();
+    if (this.editor.name_changed ()) {
+      var v = this.editor.get_full_name_value ();
+      try {
+        yield this.contact.set_individual_property ("full-name", v);
+        display_name_changed (v.get_string ());
+      } catch (Error e) {
+        show_message (e.message);
+      }
+    }
 
-      if (contact != null) {
-        show_contact_sheet ();
-      } else {
-        set_visible_child (this.none_selected_page);
+    if (this.editor.avatar_changed ()) {
+      var v = this.editor.get_avatar_value ();
+      try {
+        yield this.contact.set_individual_property ("avatar", v);
+      } catch (Error e) {
+        show_message (e.message);
       }
     }
   }
@@ -292,7 +281,7 @@ public class Contacts.ContactPane : Stack {
       details[prop.key] = prop.value.value;
 
     // Leave edit mode
-    set_edit_mode (false, true);
+    stop_editing (true);
 
     if (details.size () == 0) {
       show_message_dialog (_("You need to enter some data"));
diff --git a/src/contacts-contact.vala b/src/contacts-contact.vala
index 9d63c60..44ba196 100644
--- a/src/contacts-contact.vala
+++ b/src/contacts-contact.vala
@@ -502,12 +502,11 @@ public class Contacts.Contact : GLib.Object  {
    * if none, creates a new persona and writes to it, returning the new
    * persona.
    */
-  public static async Persona? set_individual_property (Contact contact,
-                                                       string property_name,
-                                                       Value value) throws GLib.Error, PropertyError {
+  public async Persona? set_individual_property (string property_name, Value value)
+      throws GLib.Error, PropertyError {
     bool did_set = false;
     // Need to make a copy here as it could change during the yields
-    var personas_copy = contact.individual.personas.to_array ();
+    var personas_copy = this.individual.personas.to_array ();
     foreach (var p in personas_copy) {
       if (property_name in p.writeable_properties) {
        did_set = true;
@@ -516,7 +515,7 @@ public class Contacts.Contact : GLib.Object  {
     }
 
     if (!did_set) {
-      var fake = new FakePersona (contact.store, contact);
+      var fake = new FakePersona (this.store, this);
       return yield fake.make_real_and_set (property_name, value);
     }
     return null;
diff --git a/src/contacts-window.vala b/src/contacts-window.vala
index f74a5a7..59c6d8b 100644
--- a/src/contacts-window.vala
+++ b/src/contacts-window.vala
@@ -231,8 +231,7 @@ public class Contacts.Window : Gtk.ApplicationWindow {
 
     var name = this.contact_pane.contact.individual.display_name;
     this.right_header.title = _("Editing %s").printf (name);
-
-    this.contact_pane.set_edit_mode (true);
+    this.contact_pane.start_editing ();
   }
 
   [GtkCallback]
@@ -249,13 +248,13 @@ public class Contacts.Window : Gtk.ApplicationWindow {
     if (this.state == UiState.CREATING) {
 
       if (drop_changes) {
-        this.contact_pane.set_edit_mode (false, drop_changes);
+        this.contact_pane.stop_editing (drop_changes);
       } else {
         this.contact_pane.create_contact.begin ();
       }
       this.state = UiState.NORMAL;
     } else {
-      this.contact_pane.set_edit_mode (false, drop_changes);
+      this.contact_pane.stop_editing (drop_changes);
       this.state = UiState.SHOWING;
     }
 


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