Skip to content

Commit 19e1525

Browse files
committed
(Re)introduce a backend interface
This will allow us to unit test essential operations on package.
1 parent c49a7f3 commit 19e1525

4 files changed

Lines changed: 26 additions & 12 deletions

File tree

src/Core/Backend.vala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 elementary, Inc. (https://elementary.io)
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*
5+
* Authored by: Leonhard Kargl <leo.kargl@proton.me>
6+
*/
7+
8+
public interface AppCenterCore.Backend : Object {
9+
public signal void operation_finished (Package package, Package.State operation, Error? error);
10+
11+
public abstract void notify_package_changed (Package package);
12+
public abstract async void install_package (Package package, ChangeInformation change_info) throws Error;
13+
public abstract async void remove_package (Package package, ChangeInformation change_info) throws Error;
14+
public abstract async void update_package (Package package, ChangeInformation change_info) throws Error;
15+
}

src/Core/FlatpakBackend.vala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class AppCenterCore.FlatpakPackage : Package {
2222

2323
public FlatpakPackage (string uid, Flatpak.Installation installation, AppStream.Component component) {
2424
Object (
25+
backend: FlatpakBackend.get_default (),
2526
uid: uid,
2627
installation: installation,
2728
component: component
@@ -49,8 +50,7 @@ public class AppCenterCore.FlatpakPackage : Package {
4950
}
5051
}
5152

52-
public class AppCenterCore.FlatpakBackend : Object {
53-
public signal void operation_finished (Package package, Package.State operation, Error? error);
53+
public class AppCenterCore.FlatpakBackend : Object, Backend {
5454
public signal void on_metadata_remote_preprocessed (string remote_title);
5555
public signal void package_list_changed ();
5656

@@ -231,7 +231,7 @@ public class AppCenterCore.FlatpakBackend : Object {
231231
runtime_updates_component.summary = _("Updates to app runtimes");
232232
runtime_updates_component.add_icon (runtime_icon);
233233

234-
runtime_updates = new AppCenterCore.Package ("runtime-updates", runtime_updates_component);
234+
runtime_updates = new AppCenterCore.Package (this, "runtime-updates", runtime_updates_component);
235235

236236
additional_updates = new GLib.ListStore (typeof (Package));
237237
additional_updates.append (runtime_updates);

src/Core/Package.vala

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public class AppCenterCore.Package : Object {
9696
public const string LOCAL_ID_SUFFIX = ".appcenter-local";
9797
public const string DEFAULT_PRICE_DOLLARS = "1";
9898

99+
public unowned Backend backend { get; construct; }
99100
public string uid { get; construct; }
100101

101102
public AppStream.Component component { get; protected set; }
@@ -418,8 +419,8 @@ public class AppCenterCore.Package : Object {
418419
change_information = new ChangeInformation ();
419420
}
420421

421-
public Package (string uid, AppStream.Component component) {
422-
Object (uid: uid, component: component);
422+
public Package (Backend backend, string uid, AppStream.Component component) {
423+
Object (backend: backend, uid: uid, component: component);
423424
}
424425

425426
public void replace_component (AppStream.Component component) {
@@ -455,7 +456,7 @@ public class AppCenterCore.Package : Object {
455456
// Only trigger a notify if the state has changed, quite a lot of things listen to this
456457
if (state != new_state) {
457458
state = new_state;
458-
FlatpakBackend.get_default ().notify_package_changed (this);
459+
backend.notify_package_changed (this);
459460
}
460461
}
461462

@@ -502,15 +503,14 @@ public class AppCenterCore.Package : Object {
502503
change_information.start ();
503504
state = performing;
504505

505-
unowned var flatpak_backend = AppCenterCore.FlatpakBackend.get_default ();
506-
flatpak_backend.notify_package_changed (this);
506+
backend.notify_package_changed (this);
507507

508508
try {
509509
yield perform_package_operation ();
510-
flatpak_backend.operation_finished (this, performing, null);
510+
backend.operation_finished (this, performing, null);
511511
} catch (GLib.Error e) {
512512
warning ("Operation failed for package %s - %s", name, e.message);
513-
flatpak_backend.operation_finished (this, performing, e);
513+
backend.operation_finished (this, performing, e);
514514
throw e;
515515
} finally {
516516
change_information.complete ();
@@ -519,8 +519,6 @@ public class AppCenterCore.Package : Object {
519519
}
520520

521521
private async void perform_package_operation () throws GLib.Error {
522-
unowned var backend = AppCenterCore.FlatpakBackend.get_default ();
523-
524522
switch (state) {
525523
case State.UPDATING:
526524
yield backend.update_package (this, change_information);

src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ appcenter_files = files(
55
'SuspendControl.vala',
66
'Utils.vala',
77
'Core' / 'AddonFilter.vala',
8+
'Core' / 'Backend.vala',
89
'Core/CardUtils.vala',
910
'Core' / 'CategoryManager.vala',
1011
'Core/ChangeInformation.vala',

0 commit comments

Comments
 (0)