Skip to content

Commit

Permalink
UpdateManager: handle notifications (#2112)
Browse files Browse the repository at this point in the history
* UpdateManager: handle notifications

* Fix up mistakes

* code style

---------

Co-authored-by: Ryan Kornheisl <[email protected]>
  • Loading branch information
danirabbit and zeebok committed Feb 16, 2024
1 parent b47a914 commit 935fd14
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
32 changes: 1 addition & 31 deletions src/Core/Client.vala
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public class AppCenterCore.Client : Object {

private const int SECONDS_BETWEEN_REFRESHES = 60 * 60 * 24;

private AsyncMutex update_notification_mutex = new AsyncMutex ();

private Client () { }

construct {
Expand Down Expand Up @@ -66,35 +64,7 @@ public class AppCenterCore.Client : Object {
}

public async void refresh_updates () {
yield update_notification_mutex.lock ();

bool was_empty = updates_number == 0U;
updates_number = yield UpdateManager.get_default ().get_updates (null);

var application = Application.get_default ();
if (was_empty && updates_number != 0U) {
string title = ngettext ("Update Available", "Updates Available", updates_number);
string body = ngettext ("%u app update is available", "%u app updates are available", updates_number).printf (updates_number);

var notification = new Notification (title);
notification.set_body (body);
notification.set_icon (new ThemedIcon ("software-update-available"));
notification.set_default_action ("app.show-updates");

application.send_notification ("io.elementary.appcenter.updates", notification);
} else {
application.withdraw_notification ("io.elementary.appcenter.updates");
}

try {
yield Granite.Services.Application.set_badge (updates_number);
yield Granite.Services.Application.set_badge_visible (updates_number != 0);
} catch (Error e) {
warning ("Error setting updates badge: %s", e.message);
}

update_notification_mutex.unlock ();

yield UpdateManager.get_default ().get_updates (null);
installed_apps_changed ();
}

Expand Down
28 changes: 28 additions & 0 deletions src/Core/UpdateManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,34 @@ public class AppCenterCore.UpdateManager : Object {
count += 1;
}

if (!AppCenter.App.settings.get_boolean ("automatic-updates")) {
var application = Application.get_default ();
if (count > 0) {
var title = ngettext ("Update Available", "Updates Available", count);
var body = ngettext (
"%u app update is available",
"%u app updates are available",
count
).printf (count);

var notification = new Notification (title);
notification.set_body (body);
notification.set_icon (new ThemedIcon ("software-update-available"));
notification.set_default_action ("app.show-updates");

application.send_notification ("io.elementary.appcenter.updates", notification);
} else {
application.withdraw_notification ("io.elementary.appcenter.updates");
}

try {
yield Granite.Services.Application.set_badge (count);
yield Granite.Services.Application.set_badge_visible (count != 0);
} catch (Error e) {
warning ("Error setting updates badge: %s", e.message);
}
}

runtime_updates.update_state ();
return count;
}
Expand Down

0 comments on commit 935fd14

Please sign in to comment.