From 47db691e5104ebdefa3b23dfc5dd5b1a335ce2f2 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Mon, 7 Oct 2024 10:38:23 +0200 Subject: [PATCH] fix: Don't reload lists after each update in `refreshAll` (#1826) Fixes #1813 --- .../lib/manage/local_snap_providers.dart | 2 +- .../app_center/lib/manage/updates_model.dart | 7 ++++++- packages/app_center/lib/snapd/snap_model.dart | 19 +++++++++---------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/app_center/lib/manage/local_snap_providers.dart b/packages/app_center/lib/manage/local_snap_providers.dart index 3a87885de..44869f732 100644 --- a/packages/app_center/lib/manage/local_snap_providers.dart +++ b/packages/app_center/lib/manage/local_snap_providers.dart @@ -21,7 +21,7 @@ class FilteredLocalSnaps extends _$FilteredLocalSnaps { final snapListState = await connectionCheck(_snapd.getSnaps, ref); final snaps = snapListState.snaps; final refreshableSnaps = - (await ref.watch(updatesModelProvider.future)).snaps.map((s) => s.name); + (await ref.read(updatesModelProvider.future)).snaps.map((s) => s.name); final nonRefreshableSnaps = snaps.where((s) => !refreshableSnaps.contains(s.name)); void refreshFunction(_, __) => _refreshWithFilters(nonRefreshableSnaps); diff --git a/packages/app_center/lib/manage/updates_model.dart b/packages/app_center/lib/manage/updates_model.dart index e27b883d2..a8bfc033e 100644 --- a/packages/app_center/lib/manage/updates_model.dart +++ b/packages/app_center/lib/manage/updates_model.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:app_center/constants.dart'; import 'package:app_center/error/error.dart'; +import 'package:app_center/manage/local_snap_providers.dart'; import 'package:app_center/providers/error_stream_provider.dart'; import 'package:app_center/snapd/snapd.dart'; import 'package:collection/collection.dart'; @@ -135,7 +136,10 @@ class UpdatesModel extends _$UpdatesModel { final refreshFuture = ref.read(SnapModelProvider(snapName).notifier).refresh(); try { - await refreshFuture; + final completedSuccessfully = await refreshFuture; + if (completedSuccessfully) { + ref.read(updatesModelProvider.notifier).removeFromList(snapName); + } } on Exception catch (e) { if (e is SnapdException && e.kind == 'auth-cancelled') { rethrow; @@ -164,6 +168,7 @@ class UpdatesModel extends _$UpdatesModel { ); } ref.invalidateSelf(); + ref.invalidate(filteredLocalSnapsProvider); } } diff --git a/packages/app_center/lib/snapd/snap_model.dart b/packages/app_center/lib/snapd/snap_model.dart index 0d726b52d..d3a0d5993 100644 --- a/packages/app_center/lib/snapd/snap_model.dart +++ b/packages/app_center/lib/snapd/snap_model.dart @@ -110,11 +110,13 @@ class SnapModel extends _$SnapModel { } final changeId = (await _snapd.abortChange(changeIdToAbort)).id; _updateChangeId(changeId); - return _listenUntilDone(changeId, ref, invalidate: false); + await _listenUntilDone(changeId, ref, invalidate: false); } /// Updates the version of the snap. - Future refresh() async { + /// + /// Returns `true` if the snap was updated, `false` otherwise. + Future refresh() async { assert( state.hasStoreSnap, 'The snap must be loaded from the store before updating it', @@ -130,13 +132,7 @@ class SnapModel extends _$SnapModel { SnapConfinement.classic, ); _updateChangeId(changeId); - await _listenUntilDone( - changeId, - ref, - onSuccess: () { - ref.read(updatesModelProvider.notifier).removeFromList(snapData.name); - }, - ); + return _listenUntilDone(changeId, ref); } /// Uninstalls the snap. @@ -173,18 +169,20 @@ class SnapModel extends _$SnapModel { } } - Future _listenUntilDone( + Future _listenUntilDone( String changeId, Ref ref, { bool invalidate = true, void Function()? onSuccess, }) async { + var completedSuccessfully = false; final completer = Completer(); final subscription = _snapd.watchChange(changeId).listen((event) { if (event.err != null) { completer.completeError(event.err!); } else if (event.ready) { completer.complete(); + completedSuccessfully = true; onSuccess?.call(); } }); @@ -195,6 +193,7 @@ class SnapModel extends _$SnapModel { if (invalidate) { ref.invalidateSelf(); } + return completedSuccessfully; } }