[geary/wip/draft-management: 2/7] Clean up async parts of loading the composer



commit c5903c17cc8b8d353d4a9ed3514a17c8e7de209b
Author: Michael Gratton <mike vee net>
Date:   Fri Aug 2 11:02:17 2019 +1000

    Clean up async parts of loading the composer
    
    Construct new composer widgets synchronously so we have some guarantees
    of when the widget will be added to the main window. Only load referred
    message from the store and into the composer asynchronously.

 src/client/application/application-controller.vala | 56 ++++++++++++++--------
 src/client/components/main-window.vala             |  2 +-
 src/client/composer/composer-widget.vala           | 29 ++++++-----
 3 files changed, 53 insertions(+), 34 deletions(-)
---
diff --git a/src/client/application/application-controller.vala 
b/src/client/application/application-controller.vala
index aa2198a3..627e7b67 100644
--- a/src/client/application/application-controller.vala
+++ b/src/client/application/application-controller.vala
@@ -2019,24 +2019,26 @@ public class Application.Controller : Geary.BaseObject {
         }
     }
 
-    private void create_compose_widget(ComposerWidget.ComposeType compose_type,
-        Geary.Email? referred = null, string? quote = null, string? mailto = null,
-        bool is_draft = false) {
-        create_compose_widget_async.begin(compose_type, referred, quote, mailto, is_draft);
-    }
-
     /**
-     * Creates a composer widget. Depending on the arguments, this can be inline in the
+     * Creates a composer widget.
+     *
+     * Depending on the arguments, this can be inline in the
      * conversation or as a new window.
-     * @param compose_type - Whether it's a new message, a reply, a forwarded mail, ...
-     * @param referred - The mail of which we should copy the from/to/... addresses
+     *
+     * @param compose_type - Whether it's a new message, a reply, a
+     * forwarded mail, ...
+     * @param referred - The mail of which we should copy the from/to/...
+     * addresses
      * @param quote - The quote after the mail body
      * @param mailto - A "mailto:"-link
-     * @param is_draft - Whether we're starting from a draft (true) or a new mail (false)
+     * @param is_draft - Whether we're starting from a draft (true) or
+     * a new mail (false)
      */
-    private async void create_compose_widget_async(ComposerWidget.ComposeType compose_type,
-        Geary.Email? referred = null, string? quote = null, string? mailto = null,
-        bool is_draft = false) {
+    private void create_compose_widget(ComposerWidget.ComposeType compose_type,
+                                       Geary.Email? referred = null,
+                                       string? quote = null,
+                                       string? mailto = null,
+                                       bool is_draft = false) {
         if (current_account == null)
             return;
 
@@ -2102,25 +2104,39 @@ public class Application.Controller : Geary.BaseObject {
             this.main_window.show_composer(widget);
         }
 
-        // Load the widget's content
+        this.load_composer.begin(
+            this.current_account,
+            widget,
+            referred,
+            quote,
+            is_draft,
+            this.cancellable_folder
+        );
+    }
+
+    private async void load_composer(Geary.Account account,
+                                     ComposerWidget widget,
+                                     Geary.Email? referred = null,
+                                     string? quote = null,
+                                     bool is_draft = false,
+                                     GLib.Cancellable? cancellable) {
         Geary.Email? full = null;
         if (referred != null) {
-            Geary.App.EmailStore? store = get_email_store_for_folder(current_folder);
-            if (store != null) {
+            AccountContext? context = this.accounts.get(account.information);
+            if (context != null) {
                 try {
-                    full = yield store.fetch_email_async(
+                    full = yield context.emails.fetch_email_async(
                         referred.id,
                         Geary.ComposedEmail.REQUIRED_REPLY_FIELDS,
                         Geary.Folder.ListFlags.NONE,
-                        cancellable_folder
+                        cancellable
                     );
                 } catch (Error e) {
                     message("Could not load full message: %s", e.message);
                 }
             }
         }
-        yield widget.load(full, quote, is_draft);
-
+        yield widget.load(full, quote, is_draft, cancellable);
         widget.set_focus();
     }
 
diff --git a/src/client/components/main-window.vala b/src/client/components/main-window.vala
index c38e1b97..b3de97cc 100644
--- a/src/client/components/main-window.vala
+++ b/src/client/components/main-window.vala
@@ -223,7 +223,7 @@ public class MainWindow : Gtk.ApplicationWindow, Geary.BaseInterface {
         composer.to = to.to_full_display();
         controller.add_composer(composer);
         show_composer(composer);
-        composer.load.begin(null, null, false);
+        composer.load.begin(null, null, false, null);
     }
 
     /** Displays a composer in the window if possible, else in a new window. */
diff --git a/src/client/composer/composer-widget.vala b/src/client/composer/composer-widget.vala
index de225573..48ddbcba 100644
--- a/src/client/composer/composer-widget.vala
+++ b/src/client/composer/composer-widget.vala
@@ -634,7 +634,7 @@ public class ComposerWidget : Gtk.EventBox, Geary.BaseInterface {
     public async void load(Geary.Email? referred = null,
                            string? quote = null,
                            bool is_referred_draft = false,
-                           Cancellable? cancellable = null) {
+                           GLib.Cancellable? cancellable) {
         this.last_quote = quote;
         string referred_quote = "";
         if (referred != null) {
@@ -664,7 +664,10 @@ public class ComposerWidget : Gtk.EventBox, Geary.BaseInterface {
         );
 
         try {
-            yield open_draft_manager_async(is_referred_draft ? referred.id : null);
+            yield open_draft_manager_async(
+                is_referred_draft ? referred.id : null,
+                cancellable
+            );
         } catch (Error e) {
             debug("Could not open draft manager: %s", e.message);
         }
@@ -1378,9 +1381,9 @@ public class ComposerWidget : Gtk.EventBox, Geary.BaseInterface {
     /**
      * Creates and opens the composer's draft manager.
      */
-    private async void
-        open_draft_manager_async(Geary.EmailIdentifier? editing_draft_id = null)
-    throws Error {
+    private async void open_draft_manager_async(Geary.EmailIdentifier? editing_draft_id,
+                                                GLib.Cancellable? cancellable)
+        throws GLib.Error {
         if (!this.account.information.save_drafts) {
             this.header.save_and_close_button.hide();
             return;
@@ -1390,16 +1393,17 @@ public class ComposerWidget : Gtk.EventBox, Geary.BaseInterface {
         if (this.draft_manager_opening != null) {
             this.draft_manager_opening.cancel();
         }
-        this.draft_manager_opening = new GLib.Cancellable();
+
+        GLib.Cancellable internal_cancellable = new GLib.Cancellable();
+        cancellable.cancelled.connect(() => { internal_cancellable.cancel(); });
+        this.draft_manager_opening = internal_cancellable;
 
         Geary.App.DraftManager new_manager = new Geary.App.DraftManager(account);
         try {
-            yield new_manager.open_async(editing_draft_id, this.draft_manager_opening);
+            yield new_manager.open_async(editing_draft_id, internal_cancellable);
             debug("Draft manager opened");
-        } catch (Error err) {
-            debug("Unable to open draft manager %s: %s",
-                  new_manager.to_string(), err.message);
-            throw err;
+        } finally {
+            this.draft_manager_opening = null;
         }
 
         new_manager.notify[Geary.App.DraftManager.PROP_DRAFT_STATE]
@@ -1408,7 +1412,6 @@ public class ComposerWidget : Gtk.EventBox, Geary.BaseInterface {
             .connect(on_draft_id_changed);
         new_manager.fatal.connect(on_draft_manager_fatal);
 
-        this.draft_manager_opening = null;
         this.draft_manager = new_manager;
 
         update_draft_state();
@@ -1429,7 +1432,7 @@ public class ComposerWidget : Gtk.EventBox, Geary.BaseInterface {
             this.draft_manager.discard_on_close = true;
             yield close_draft_manager_async(null);
         }
-        yield open_draft_manager_async();
+        yield open_draft_manager_async(null, null);
     }
 
     private async void close_draft_manager_async(Cancellable? cancellable)


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