From 2b170b2832db368957564aca0a415b88b97ea582 Mon Sep 17 00:00:00 2001 From: Leonhard Kargl Date: Sat, 28 Feb 2026 12:10:09 +0100 Subject: [PATCH] ListModels: Use n items instead of custom properties All models in GTK and GLib have an n-items property. So bind to that instead of having to manage custom properties. Also drop up_to_date since that is not used anywhere anymore. --- src/Core/FlatpakBackend.vala | 21 +-------------------- src/Core/UpdateManager.vala | 11 ++++++----- src/Views/AppListUpdateView.vala | 10 +++++----- src/Views/Homepage.vala | 6 +++--- 4 files changed, 15 insertions(+), 33 deletions(-) diff --git a/src/Core/FlatpakBackend.vala b/src/Core/FlatpakBackend.vala index 3a2ca09d4..7b94ff773 100644 --- a/src/Core/FlatpakBackend.vala +++ b/src/Core/FlatpakBackend.vala @@ -85,8 +85,6 @@ public class AppCenterCore.FlatpakBackend : Object, Backend { private Gtk.FilterListModel _updated_packages; public ListModel updated_packages { get { return _updated_packages; } } - public bool has_updated_packages { get { return _updated_packages.n_items > 0; } } - // Right now only for runtime updates private GLib.ListStore additional_updates; @@ -95,9 +93,6 @@ public class AppCenterCore.FlatpakBackend : Object, Backend { public ListModel working_packages { get; private set; } - public bool has_working_packages { get { return working_packages.get_n_items () > 0; } } - public bool has_updatable_packages { get { return _updatable_packages.n_items > 0; } } - public uint n_updatable_packages { get { return _updatable_packages.n_items; } } public uint n_unpaid_updatable_packages { get { uint n = 0; @@ -121,13 +116,6 @@ public class AppCenterCore.FlatpakBackend : Object, Backend { } } - public bool up_to_date { - get { - return !has_updatable_packages && (!working || job_type != GET_UPDATES && job_type != REFRESH_CACHE - && job_type != GET_DOWNLOAD_SIZE); - } - } - private Package runtime_updates; private string user_metadata_path; @@ -219,8 +207,6 @@ public class AppCenterCore.FlatpakBackend : Object, Backend { } construct { - notify["working"].connect (() => Idle.add_once (() => notify_property ("up-to-date"))); - // Our listmodel structure including the updates: // addtional updates => flatten the two models => filter updatable packages => sort updating packages to the top // /\ @@ -267,7 +253,6 @@ public class AppCenterCore.FlatpakBackend : Object, Backend { updated_every_filter.append (not_updating_filter); _updated_packages = new Gtk.FilterListModel (installed_packages, updated_every_filter); - _updated_packages.items_changed.connect (() => notify_property ("has-updated-packages")); var updates_models = new GLib.ListStore (typeof (ListModel)); updates_models.append (additional_updates); @@ -279,18 +264,14 @@ public class AppCenterCore.FlatpakBackend : Object, Backend { _updatable_packages = new Gtk.FilterListModel (flatten_model, updatable_filter); _updatable_packages.items_changed.connect (() => { - notify_property ("has-updatable-packages"); - notify_property ("n-updatable-packages"); notify_property ("n-unpaid-updatable-packages"); notify_property ("updates-size"); - notify_property ("up-to-date"); }); var working_expression = new Gtk.PropertyExpression (typeof (Package), null, "working"); var working_filter = new Gtk.BoolFilter (working_expression); working_packages = new Gtk.FilterListModel (_sorted_packages, working_filter); - working_packages.items_changed.connect (() => notify_property ("has-working-packages")); worker_thread = new Thread ("flatpak-worker", worker_func); user_appstream_pool = new AppStream.Pool (); @@ -2023,7 +2004,7 @@ public class AppCenterCore.FlatpakBackend : Object, Backend { job_args.cancellable = cancellable; // Clear any packages previously marked as updatable - for (int i = (int) n_updatable_packages - 1; i >= 0; i--) { + for (int i = (int) updatable_packages.get_n_items () - 1; i >= 0; i--) { var package = (Package) updatable_packages.get_item (i); package.change_information.clear_update_info (); package.update_state (); diff --git a/src/Core/UpdateManager.vala b/src/Core/UpdateManager.vala index fb62dd935..6841cc494 100644 --- a/src/Core/UpdateManager.vala +++ b/src/Core/UpdateManager.vala @@ -27,7 +27,8 @@ public class AppCenterCore.UpdateManager : Object { public bool can_update_all { get { unowned var fp_client = FlatpakBackend.get_default (); - return !updating_all && fp_client.n_updatable_packages - fp_client.n_unpaid_updatable_packages > 0; + return !updating_all && + fp_client.updatable_packages.get_n_items () - fp_client.n_unpaid_updatable_packages > 0; } } @@ -48,7 +49,7 @@ public class AppCenterCore.UpdateManager : Object { last_refresh_time = new DateTime.from_unix_utc (AppCenter.App.settings.get_int64 ("last-refresh-time")); unowned var fp_client = FlatpakBackend.get_default (); - fp_client.notify["n-updatable-packages"].connect (on_n_updatable_packages_changed); + fp_client.updatable_packages.items_changed.connect (on_updatable_packages_changed); fp_client.notify["n-unpaid-updatable-packages"].connect (() => notify_property ("can-update-all")); AppCenter.App.settings.changed["automatic-updates"].connect (on_automatic_updates_changed); @@ -64,13 +65,13 @@ public class AppCenterCore.UpdateManager : Object { } } - private void on_n_updatable_packages_changed () { + private void on_updatable_packages_changed () { notify_property ("can-update-all"); update_badge.begin (); } private async void update_badge () { - var n_updatable_packages = FlatpakBackend.get_default ().n_updatable_packages; + var n_updatable_packages = FlatpakBackend.get_default ().updatable_packages.get_n_items (); try { yield Granite.Services.Application.set_badge (n_updatable_packages); @@ -167,7 +168,7 @@ public class AppCenterCore.UpdateManager : Object { yield update_all (); } else { var application = Application.get_default (); - var n_updatable_packages = fp_client.n_updatable_packages; + var n_updatable_packages = fp_client.updatable_packages.get_n_items (); if (n_updatable_packages > 0) { var title = ngettext ("Update Available", "Updates Available", n_updatable_packages); var body = ngettext ( diff --git a/src/Views/AppListUpdateView.vala b/src/Views/AppListUpdateView.vala index 72ac4cab8..c6cf92144 100644 --- a/src/Views/AppListUpdateView.vala +++ b/src/Views/AppListUpdateView.vala @@ -23,8 +23,8 @@ public class AppCenter.Views.AppListUpdateView : Adw.NavigationPage { var updatable_header_label = new Granite.HeaderLabel (_("Available Updates")) { hexpand = true }; - flatpak_backend.bind_property ( - "n-updatable-packages", updatable_header_label, "label", SYNC_CREATE, + flatpak_backend.updatable_packages.bind_property ( + "n-items", updatable_header_label, "label", SYNC_CREATE, (binding, from_value, ref to_value) => { var n_updatable_packages = from_value.get_uint (); @@ -70,7 +70,7 @@ public class AppCenter.Views.AppListUpdateView : Adw.NavigationPage { var updatable_section = new Granite.Box (VERTICAL, HALF); updatable_section.append (updatable_header); updatable_section.append (list_box); - flatpak_backend.bind_property ("has-updatable-packages", updatable_section, "visible", SYNC_CREATE); + flatpak_backend.updatable_packages.bind_property ("n-items", updatable_section, "visible", SYNC_CREATE); var in_progress_header = new Granite.HeaderLabel (_("In Progress")) { margin_end = 12, @@ -86,7 +86,7 @@ public class AppCenter.Views.AppListUpdateView : Adw.NavigationPage { var in_progress_section = new Granite.Box (VERTICAL, HALF); in_progress_section.append (in_progress_header); in_progress_section.append (in_progress_list); - flatpak_backend.bind_property ("has-working-packages", in_progress_section, "visible", SYNC_CREATE); + flatpak_backend.working_packages.bind_property ("n-items", in_progress_section, "visible", SYNC_CREATE); installed_header = new Granite.HeaderLabel (_("Up to Date")) { margin_end = 12, @@ -108,7 +108,7 @@ public class AppCenter.Views.AppListUpdateView : Adw.NavigationPage { var installed_section = new Granite.Box (VERTICAL, HALF); installed_section.append (installed_header); installed_section.append (installed_flowbox); - flatpak_backend.bind_property ("has-updated-packages", installed_section, "visible", SYNC_CREATE); + flatpak_backend.updated_packages.bind_property ("n-items", installed_section, "visible", SYNC_CREATE); var box = new Granite.Box (VERTICAL, DOUBLE); box.append (updatable_section); diff --git a/src/Views/Homepage.vala b/src/Views/Homepage.vala index 0ee1eae0b..2b0a3b2c0 100644 --- a/src/Views/Homepage.vala +++ b/src/Views/Homepage.vala @@ -140,8 +140,8 @@ public class AppCenter.Homepage : Adw.NavigationPage { updates_badge = new Gtk.Label ("!"); updates_badge.add_css_class (Granite.STYLE_CLASS_BADGE); - fp_client.bind_property ( - "n-updatable-packages", updates_badge, "label", SYNC_CREATE, + fp_client.updatable_packages.bind_property ( + "n-items", updates_badge, "label", SYNC_CREATE, (binding, from_value, ref to_value) => { to_value.set_string (from_value.get_uint ().to_string ()); return true; @@ -155,7 +155,7 @@ public class AppCenter.Homepage : Adw.NavigationPage { valign = Gtk.Align.START, transition_type = Gtk.RevealerTransitionType.CROSSFADE }; - fp_client.bind_property ("has-updatable-packages", updates_badge_revealer, "reveal-child", SYNC_CREATE); + fp_client.updatable_packages.bind_property ("n-items", updates_badge_revealer, "reveal-child", SYNC_CREATE); var updates_overlay = new Gtk.Overlay () { child = updates_button