[gitg/wip/adwait/add-remotes: 77/77] Refactor Add remotes



commit a7f357462354ce67e52df18a7fffb35b24729781
Author: Alberto Fanjul <albertofanjul gmail com>
Date:   Thu Jul 4 13:20:17 2019 +0200

    Refactor Add remotes
    
    decoupling Refheader and actions
    keep user input on add remote fail

 gitg/gitg-add-remote-action-dialog.vala  | 10 ++++++
 gitg/gitg-add-remote-action.vala         | 21 ++++++++-----
 gitg/history/gitg-history-refs-list.vala | 34 +++++++++-----------
 gitg/history/gitg-history.vala           | 53 ++++++++++++--------------------
 4 files changed, 57 insertions(+), 61 deletions(-)
---
diff --git a/gitg/gitg-add-remote-action-dialog.vala b/gitg/gitg-add-remote-action-dialog.vala
index f1008503..7a5c85aa 100644
--- a/gitg/gitg-add-remote-action-dialog.vala
+++ b/gitg/gitg-add-remote-action-dialog.vala
@@ -45,6 +45,11 @@ class AddRemoteActionDialog : Gtk.Dialog
                {
                        return d_entry_remote_name.text.strip();
                }
+
+               set
+               {
+                  d_entry_remote_name.text = value.strip();
+               }
        }
 
        public string remote_url
@@ -53,6 +58,11 @@ class AddRemoteActionDialog : Gtk.Dialog
                {
                        return d_entry_remote_url.text.strip();
                }
+
+               set
+               {
+                  d_entry_remote_url.text = value.strip();
+               }
        }
 }
 
diff --git a/gitg/gitg-add-remote-action.vala b/gitg/gitg-add-remote-action.vala
index 4b12bee6..5679cffc 100644
--- a/gitg/gitg-add-remote-action.vala
+++ b/gitg/gitg-add-remote-action.vala
@@ -7,9 +7,10 @@ class AddRemoteAction : GitgExt.UIElement, GitgExt.Action, Object
        private const string version = Gitg.Config.VERSION;
 
        public GitgExt.Application? application { owned get; construct set; }
-       Gitg.Remote? d_remote;
-       Gitg.Repository? repo;
-       string? remote_name;
+       private Gitg.Remote? d_remote;
+       private Gitg.Repository? repo;
+       private string? remote_name;
+       private string? remote_url;
 
        public AddRemoteAction(GitgExt.Application application)
        {
@@ -70,23 +71,26 @@ class AddRemoteAction : GitgExt.UIElement, GitgExt.Action, Object
                if (updates.size != 0)
                {
                        notification.success(_("Fetched from %s: %s").printf(d_remote.get_url(), 
string.joinv(", ", updates.to_array())));
+                       ((Gtk.ApplicationWindow)application).activate_action("reload", null);
                }
                else
                {
-                       add_remote();
+                       add_remote(remote_name, remote_url);
 
                        return false;
                }
 
-               ((Gtk.ApplicationWindow)application).activate_action("reload", null);
                return true;
        }
 
-       public void add_remote()
+       public void add_remote(owned string? remote_name = null, owned string? remote_url = null)
        {
                var dlg = new AddRemoteActionDialog((Gtk.Window)application);
                var remote_added = true;
 
+               dlg.remote_name = remote_name;
+               dlg.remote_url = remote_url;
+
                dlg.response.connect((d, resp) => {
                        if (resp == Gtk.ResponseType.OK)
                        {
@@ -95,16 +99,17 @@ class AddRemoteAction : GitgExt.UIElement, GitgExt.Action, Object
 
                                repo = application.repository;
                                remote_name = dlg.remote_name;
+                               remote_url = dlg.remote_url;
 
                                try
                                {
                                        remote = repo.create_remote(remote_name,
-                                                                   dlg.remote_url);
+                                                                   remote_url);
                                }
                                catch (Error e)
                                {
                                        remote_added = false;
-                                       add_remote();
+                                       add_remote(remote_name, remote_url);
                                        application.show_infobar(_("Failed to add remote"),
                                                                 e.message,
                                                                 Gtk.MessageType.ERROR);
diff --git a/gitg/history/gitg-history-refs-list.vala b/gitg/history/gitg-history-refs-list.vala
index a9559ff0..5ae7a57f 100644
--- a/gitg/history/gitg-history-refs-list.vala
+++ b/gitg/history/gitg-history-refs-list.vala
@@ -43,7 +43,7 @@ private enum RefAnimation
        ANIMATE
 }
 
-private interface RefTyped : Object
+public interface RefTyped : Object
 {
        public abstract Gitg.RefType ref_type { get; }
 }
@@ -402,11 +402,12 @@ private class RefRow : RefTyped, Gtk.ListBoxRow
 }
 
 [GtkTemplate (ui = "/org/gnome/gitg/ui/gitg-history-ref-header.ui")]
-private class RefHeader : RefTyped, Gtk.ListBoxRow
+public class RefHeader : RefTyped, Gtk.ListBoxRow
 {
        private Gitg.RefType d_rtype;
        private bool d_is_sub_header_remote;
        private string d_name;
+       public Gee.LinkedList<GitgExt.Action> actions { get; set; }
 
        public Gitg.RemoteState remote_state
        {
@@ -456,20 +457,6 @@ private class RefHeader : RefTyped, Gtk.ListBoxRow
                get { return d_name; }
        }
 
-       public Gee.LinkedList<GitgExt.Action> get_actions (GitgExt.Application application)
-       {
-               var actions = new Gee.LinkedList<GitgExt.Action>();
-               if (d_rtype == Gitg.RefType.REMOTE)
-               {
-                       actions.add(new Gitg.AddRemoteAction(application));
-               }
-               else {
-                       actions = null;
-               }
-
-               return actions;
-       }
-
        public RefHeader(Gitg.RefType rtype, string name)
        {
                var escaped = Markup.escape_text(name);
@@ -550,6 +537,12 @@ public class RefsList : Gtk.ListBox
        private RefHeader? d_all_tags;
        private RefRow.SortOrder d_ref_sort_order;
        private HeaderState[] d_expanded;
+       public RefHeader? branches_header { get { return d_all_branches; } }
+       public RefHeader? remotes_header { get { return d_all_remotes; } }
+       public RefHeader? tags_header { get { return d_all_tags; } }
+       public Gee.LinkedList<GitgExt.Action> branches_actions { get; set; default = null; }
+       public Gee.LinkedList<GitgExt.Action> remotes_actions { get; set; default = null; }
+       public Gee.LinkedList<GitgExt.Action> tags_actions { get; set; default = null; }
 
        public signal void changed();
 
@@ -881,10 +874,11 @@ public class RefsList : Gtk.ListBox
                }
        }
 
-       private RefHeader add_header(Gitg.RefType ref_type, string name)
+       private RefHeader add_header(Gitg.RefType ref_type, string name, Gee.LinkedList<GitgExt.Action>? 
actions)
        {
                var header = new RefHeader(ref_type, name);
                init_header(header);
+               header.actions = actions;
 
                add(header);
                return header;
@@ -1188,9 +1182,9 @@ public class RefsList : Gtk.ListBox
                }
 
                d_all_commits = add_ref_row(null);
-               d_all_branches = add_header(Gitg.RefType.BRANCH, _("Branches"));
-               d_all_remotes = add_header(Gitg.RefType.REMOTE, _("Remotes"));
-               d_all_tags = add_header(Gitg.RefType.TAG, _("Tags"));
+               d_all_branches = add_header(Gitg.RefType.BRANCH, _("Branches"), branches_actions);
+               d_all_remotes = add_header(Gitg.RefType.REMOTE, _("Remotes"), remotes_actions);
+               d_all_tags = add_header(Gitg.RefType.TAG, _("Tags"), tags_actions);
 
                RefRow? head = null;
 
diff --git a/gitg/history/gitg-history.vala b/gitg/history/gitg-history.vala
index d1050e00..99b1333c 100644
--- a/gitg/history/gitg-history.vala
+++ b/gitg/history/gitg-history.vala
@@ -603,6 +603,10 @@ namespace GitgHistory
 
                        d_commit_list_model.begin_clear.connect(on_commit_model_begin_clear);
                        d_commit_list_model.end_clear.connect(on_commit_model_end_clear);
+
+                       var actions = new Gee.LinkedList<GitgExt.Action>();
+                       actions.add(new Gitg.AddRemoteAction(application));
+                       d_main.refs_list.remotes_actions = actions;
                }
 
                private void update_walker_idle()
@@ -828,16 +832,6 @@ namespace GitgHistory
                        return populate_menu_for_commit(commit);
                }
 
-               private Gtk.Menu? popup_menu_for_remote() {
-                       var action = new Gitg.AddRemoteAction(application);
-                       var menu = new Gtk.Menu();
-
-                       action.populate_menu(menu);
-                       menu.set_data("gitg-ext-actions", action);
-
-                       return menu;
-               }
-
                private Gtk.Menu? popup_menu_for_ref(Gitg.Ref reference)
                {
                        var actions = new Gee.LinkedList<GitgExt.RefAction?>();
@@ -984,37 +978,30 @@ namespace GitgHistory
                        }
 
                        var references = d_main.refs_list.selection;
-                       var actions = new Gee.LinkedList<GitgExt.Action>();
 
                        if (references.is_empty || references.first() != references.last())
                        {
-                               if (selection != null && selection.get_type () == typeof(RefHeader)) {
-                                       actions = ((RefHeader)selection).get_actions (application);
+                               Gee.LinkedList<GitgExt.Action> actions = null;
+                               if (selection != null && selection.get_type () == typeof(RefHeader)
+                                       && (actions = ((RefHeader)selection).actions) != null && actions.size 
0) {
+                                       var menu = new Gtk.Menu();
 
-                                       if (actions != null)
+                                       foreach (var ac in actions)
                                        {
-                                               var menu = new Gtk.Menu();
-
-                                               foreach (var ac in actions)
+                                               if (ac != null)
                                                {
-                                                       if (ac != null)
-                                                       {
-                                                               ac.populate_menu(menu);
-                                                       }
-                                                       else
-                                                       {
-                                                               var sep = new Gtk.SeparatorMenuItem();
-                                                               sep.show();
-                                                               menu.append(sep);
-                                                       }
+                                                       ac.populate_menu(menu);
+                                               }
+                                               else
+                                               {
+                                                       var sep = new Gtk.SeparatorMenuItem();
+                                                       sep.show();
+                                                       menu.append(sep);
                                                }
-
-                                               menu.set_data("gitg-ext-actions", actions);
-                                               return menu;
-                                       }
-                                       else{
-                                               return null;
                                        }
+
+                                       menu.set_data("gitg-ext-actions", actions);
+                                       return menu;
                                } else {
                                        return null;
                                }


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