Skip to content

Commit

Permalink
add cancellable
Browse files Browse the repository at this point in the history
  • Loading branch information
alainm23 committed Dec 9, 2024
1 parent 9804483 commit 696b7a7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/Plug.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
36 changes: 27 additions & 9 deletions src/Views/OperatingSystemView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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,
Expand All @@ -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);

Expand Down

0 comments on commit 696b7a7

Please sign in to comment.