From 696b7a72ffa48aa8ac18e9324daa87e6316a993a Mon Sep 17 00:00:00 2001 From: Alain Date: Mon, 9 Dec 2024 11:46:23 -0500 Subject: [PATCH] add cancellable --- src/Plug.vala | 7 ++++++ src/Views/OperatingSystemView.vala | 36 ++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/Plug.vala b/src/Plug.vala index d4fed1d8..f86d0747 100644 --- a/src/Plug.vala +++ b/src/Plug.vala @@ -27,6 +27,7 @@ public class About.Plug : Switchboard.Plug { private OperatingSystemView operating_system_view; private Adw.ToolbarView toolbarview; private Gtk.Stack stack; + private GLib.Cancellable sponsors_goal_cancellable; public Plug () { GLib.Intl.bindtextdomain (About.GETTEXT_PACKAGE, About.LOCALEDIR); @@ -91,9 +92,15 @@ public class About.Plug : Switchboard.Plug { public override void shown () { operating_system_view.load_logo.begin (); + + sponsors_goal_cancellable = new GLib.Cancellable (); + operating_system_view.load_sponsors_goal (sponsors_goal_cancellable); } public override void hidden () { + if (sponsors_goal_cancellable != null) { + sponsors_goal_cancellable.cancel (); + } } public override void search_callback (string location) { diff --git a/src/Views/OperatingSystemView.vala b/src/Views/OperatingSystemView.vala index 5ccda722..f675362b 100644 --- a/src/Views/OperatingSystemView.vala +++ b/src/Views/OperatingSystemView.vala @@ -93,6 +93,7 @@ public class About.OperatingSystemView : Gtk.Box { private Gtk.Label updates_description; private Gtk.Revealer details_button_revealer; private Gtk.Stack button_stack; + private SponsorUsRow sponsor_us_row; construct { add_css_class ("operating-system-view"); @@ -250,6 +251,8 @@ public class About.OperatingSystemView : Gtk.Box { updates_list.get_first_child ().focusable = false; updates_list.get_last_child ().focusable = false; + sponsor_us_row = new SponsorUsRow ("https://github.com/sponsors/elementary"); + var sponsor_list = new Gtk.ListBox () { margin_bottom = 12, margin_top = 12, @@ -261,7 +264,7 @@ public class About.OperatingSystemView : Gtk.Box { sponsor_list.add_css_class ("boxed-list"); sponsor_list.add_css_class (Granite.STYLE_CLASS_RICH_LIST); - sponsor_list.append (new SponsorUsRow ("https://github.com/sponsors/elementary")); + sponsor_list.append (sponsor_us_row); var thebasics_link = new LinkRow ( documentation_url, @@ -435,6 +438,14 @@ public class About.OperatingSystemView : Gtk.Box { } } + public void load_sponsors_goal (GLib.Cancellable cancellable) { + if (sponsor_us_row.was_loaded) { + return; + } + + sponsor_us_row.get_goal_progress (cancellable); + } + private async void get_upstream_release () { // Upstream distro version (for "Built on" text) // FIXME: Add distro specific field to /etc/os-release and use that instead @@ -735,6 +746,8 @@ public class About.OperatingSystemView : Gtk.Box { private class SponsorUsRow : Gtk.ListBoxRow { public string uri { get; construct; } + private Gtk.Label target_label; + private Gtk.LevelBar levelbar; private Gtk.Revealer details_revealer; public SponsorUsRow (string uri) { @@ -743,6 +756,12 @@ public class About.OperatingSystemView : Gtk.Box { ); } + public bool was_loaded { + get { + return details_revealer.reveal_child; + } + } + class construct { set_accessible_role (LINK); } @@ -757,19 +776,19 @@ public class About.OperatingSystemView : Gtk.Box { hexpand = true }; - var target_label = new Gtk.Label (null) { + target_label = new Gtk.Label (null) { halign = START }; target_label.add_css_class (Granite.STYLE_CLASS_DIM_LABEL); target_label.add_css_class (Granite.STYLE_CLASS_SMALL_LABEL); - var level_bar = new Gtk.LevelBar (); - level_bar.add_css_class (Granite.STYLE_CLASS_FLAT); - level_bar.add_css_class ("pink"); + levelbar = new Gtk.LevelBar (); + levelbar.add_css_class (Granite.STYLE_CLASS_FLAT); + levelbar.add_css_class ("pink"); var details_box = new Gtk.Box (VERTICAL, 0); details_box.append (target_label); - details_box.append (level_bar); + details_box.append (levelbar); details_revealer = new Gtk.Revealer () { child = details_box, @@ -788,13 +807,12 @@ public class About.OperatingSystemView : Gtk.Box { child = grid; add_css_class ("link"); - get_goal_progress (level_bar, target_label); } - private void get_goal_progress (Gtk.LevelBar levelbar, Gtk.Label target_label) { + public void get_goal_progress (GLib.Cancellable cancellable) { var message = new Soup.Message ("GET", "https://elementary.io/api/sponsors_goal"); var session = new Soup.Session (); - session.send_and_read_async.begin (message, GLib.Priority.DEFAULT, null , (obj, res) => { + session.send_and_read_async.begin (message, GLib.Priority.DEFAULT, cancellable , (obj, res) => { try { var bytes = session.send_and_read_async.end (res);