Skip to content

Commit

Permalink
Merge pull request #1536 from codinesh/managepage-cancel-updates
Browse files Browse the repository at this point in the history
fix: Allow cancelling updates in manage page
  • Loading branch information
BLKKKBVSIK authored Dec 30, 2023
2 parents f925bde + 1736eb6 commit 4a14e5a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/app_center/lib/src/manage/manage_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ class _ActionButtons extends ConsumerWidget {
error: (_, __) => ('', const SizedBox.shrink()),
);

final updatesInprogress = updatesModel.refreshableSnapNames.isNotEmpty &&
!updatesModel.state.isLoading &&
updatesModel.activeChangeId != null;
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Expand Down Expand Up @@ -304,6 +307,19 @@ class _ActionButtons extends ConsumerWidget {
],
),
),
const SizedBox(width: 8),
PushButton.outlined(
onPressed: updatesInprogress
? () => ref
.read(updatesModelProvider)
.cancelChange(updatesModel.activeChangeId!)
: null,
child: Text(
l10n.snapActionCancelLabel,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
],
);
}
Expand Down
22 changes: 22 additions & 0 deletions packages/app_center/lib/src/snapd/updates_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,26 @@ class UpdatesModel extends ChangeNotifier {
_activeChangeId = null;
await refresh();
}

Future<void> cancelChange(String changeId) async {
if (changeId.isEmpty) return;

try {
final changeDetails = await snapd.getChange(changeId);

// If the change is already completed, ignore silently.
// If it wouldn't be ignored, an error would be displayed to the user,
// which might be confusing.
if (changeDetails.ready) {
return;
}

final abortChange = await snapd.abortChange(changeId);
await snapd.waitChange(abortChange.id);
_activeChangeId = null;
notifyListeners();
} on SnapdException catch (e) {
_handleError(e);
}
}
}
28 changes: 28 additions & 0 deletions packages/app_center/test/manage_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,34 @@ void main() {
);
});

testWidgets('cancel refresh all', (tester) async {
final mockUpdatesModel = createMockUpdatesModel(
refreshableSnapNames: refreshableSnaps.map((snap) => snap.name),
isBusy: true,
);

await tester.pumpApp(
(_) => ProviderScope(
overrides: [
launchProvider.overrideWith((_, __) => createMockSnapLauncher()),
manageModelProvider.overrideWith(
(_) => createMockManageModel(
refreshableSnaps: refreshableSnaps,
),
),
snapModelProvider.overrideWith((_, __) => snapModel),
updatesModelProvider.overrideWith((_) => mockUpdatesModel),
],
child: const ManagePage(),
),
);
await tester.pump();

final cancelButton = find.buttonWithText(tester.l10n.snapActionCancelLabel);
expect(cancelButton, findsOneWidget);
expect(cancelButton, isEnabled);
});

testWidgets('error dialog', (tester) async {
await tester.pumpApp(
(_) => ProviderScope(
Expand Down
10 changes: 10 additions & 0 deletions packages/app_center/test/test_utils.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,16 @@ class MockUpdatesModel extends _i1.Mock implements _i6.UpdatesModel {
returnValueForMissingStub: _i14.Future<void>.value(),
) as _i14.Future<void>);

@override
_i14.Future<void> cancelChange(String? changeId) => (super.noSuchMethod(
Invocation.method(
#cancelChange,
[changeId],
),
returnValue: _i14.Future<void>.value(),
returnValueForMissingStub: _i14.Future<void>.value(),
) as _i14.Future<void>);

@override
void addListener(_i15.VoidCallback? listener) => super.noSuchMethod(
Invocation.method(
Expand Down

0 comments on commit 4a14e5a

Please sign in to comment.