Merge lp:~donadigo/appcenter/refactor-package-class into lp:~elementary-apps/appcenter/appcenter

Proposed by Adam Bieńkowski
Status: Merged
Approved by: Corentin Noël
Approved revision: 355
Merged at revision: 363
Proposed branch: lp:~donadigo/appcenter/refactor-package-class
Merge into: lp:~elementary-apps/appcenter/appcenter
Diff against target: 189 lines (+55/-35)
1 file modified
src/Core/Package.vala (+55/-35)
To merge this branch: bzr merge lp:~donadigo/appcenter/refactor-package-class
Reviewer Review Type Date Requested Status
Danielle Foré Approve
Review via email: mp+313878@code.launchpad.net

Commit message

Package.vala:
* Use GObject-style construction
* Cache properties

Description of the change

This branch refactors package class, to improve performance and reduce unneded operations.

The class now uses GObject construction. In addition, instead of always getting the property of the class like name, summary and version, it has each property saved, so there is no need for getting the property again, the packagekit package is also cached.

To post a comment you must log in.
Revision history for this message
Danielle Foré (danrabbit) wrote :

Looks good. I haven't noticed any issues with this branch :)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Core/Package.vala'
2--- src/Core/Package.vala 2016-09-14 16:58:13 +0000
3+++ src/Core/Package.vala 2016-12-26 21:41:27 +0000
4@@ -33,7 +33,7 @@
5
6 public const string OS_UPDATES_ID = "xxx-os-updates";
7
8- public AppStream.Component component { public get; private set; }
9+ public AppStream.Component component { get; construct; }
10 public ChangeInformation change_information { public get; private set; }
11 public Gee.TreeSet<Pk.Package> installed_packages { public get; private set; }
12 public GLib.Cancellable action_cancellable { public get; private set; }
13@@ -72,20 +72,26 @@
14 public bool is_os_updates {
15 get {
16 return component.id == "xxx-os-updates";
17- }
18- }
19-
20- public Package (AppStream.Component component) {
21- this.component = component;
22+ }
23+ }
24+
25+ private string? name = null;
26+ private string? summary = null;
27+ private string? version = null;
28+ private Pk.Package? pk_package = null;
29+
30+ construct {
31 installed_packages = new Gee.TreeSet<Pk.Package> ();
32 change_information = new ChangeInformation ();
33- change_information.status_changed.connect (() => {
34- info_changed (change_information.status);
35- });
36+ change_information.status_changed.connect (() => info_changed (change_information.status));
37
38 action_cancellable = new GLib.Cancellable ();
39 }
40
41+ public Package (AppStream.Component component) {
42+ Object (component: component);
43+ }
44+
45 public void update_state () {
46 if (installed) {
47 if (change_information.has_changes ()) {
48@@ -102,6 +108,7 @@
49 if (state != State.UPDATE_AVAILABLE) {
50 return false;
51 }
52+
53 return yield perform_operation (State.UPDATING, State.INSTALLED, State.UPDATE_AVAILABLE);
54 }
55
56@@ -109,6 +116,7 @@
57 if (state != State.NOT_INSTALLED) {
58 return false;
59 }
60+
61 if (yield perform_operation (State.INSTALLING, State.INSTALLED, State.NOT_INSTALLED)) {
62 /* TODO: Move this to a higher level */
63 var application = (Gtk.Application)Application.get_default ();
64@@ -121,6 +129,7 @@
65
66 application.send_notification ("installed", notification);
67 }
68+
69 return true;
70 }
71
72@@ -131,6 +140,7 @@
73 if (state != State.INSTALLED) {
74 return false;
75 }
76+
77 return yield perform_operation (State.REMOVING, State.NOT_INSTALLED, State.INSTALLED);
78 }
79
80@@ -144,6 +154,7 @@
81 } finally {
82 clean_up_package_operation (exit_status, after_success, after_fail);
83 }
84+
85 return (exit_status == Pk.Exit.SUCCESS);
86 }
87
88@@ -184,31 +195,35 @@
89 }
90
91 public string? get_name () {
92- var _name = component.get_name ();
93- if (_name != null) {
94- return _name;
95- }
96-
97- var package = find_package ();
98- if (package != null) {
99- return package.get_name ();
100- }
101-
102- return null;
103+ if (name != null) {
104+ return name;
105+ }
106+
107+ name = component.get_name ();
108+ if (name == null) {
109+ var package = find_package ();
110+ if (package != null) {
111+ name = package.get_name ();
112+ }
113+ }
114+
115+ return name;
116 }
117
118 public string? get_summary () {
119- var summary = component.get_summary ();
120 if (summary != null) {
121 return summary;
122 }
123
124- var package = find_package ();
125- if (package != null) {
126- return package.get_summary ();
127+ summary = component.get_summary ();
128+ if (summary == null) {
129+ var package = find_package ();
130+ if (package != null) {
131+ summary = package.get_summary ();
132+ }
133 }
134
135- return null;
136+ return summary;
137 }
138
139 public string get_progress_description () {
140@@ -265,6 +280,10 @@
141 }
142
143 public string? get_version () {
144+ if (version != null) {
145+ return version;
146+ }
147+
148 var package = find_package ();
149 if (package != null) {
150 string returned = package.get_version ();
151@@ -275,27 +294,28 @@
152 returned = returned.split (":", 2)[1];
153 }
154
155- return returned;
156+ version = returned;
157 }
158
159- return null;
160+ return version;
161 }
162
163- private Pk.Package? find_package (bool installed = false) {
164+ private Pk.Package? find_package () {
165 if (component.id == OS_UPDATES_ID) {
166 return null;
167 }
168
169+ if (pk_package != null) {
170+ return pk_package;
171+ }
172+
173 try {
174- Pk.Bitfield filter = 0;
175- if (installed) {
176- filter = Pk.Bitfield.from_enums (Pk.Filter.INSTALLED);
177- }
178-
179- return AppCenterCore.Client.get_default ().get_app_package (component.get_pkgnames ()[0], filter);
180+ pk_package = AppCenterCore.Client.get_default ().get_app_package (component.get_pkgnames ()[0], 0);
181 } catch (Error e) {
182- critical (e.message);
183+ warning (e.message);
184 return null;
185 }
186+
187+ return pk_package;
188 }
189 }

Subscribers

People subscribed via source and target branches