Skip to content

Commit

Permalink
fix: Don't reload lists after each update in refreshAll
Browse files Browse the repository at this point in the history
  • Loading branch information
spydon committed Oct 3, 2024
1 parent 8212bf4 commit f39c8f6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/app_center/lib/manage/local_snap_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion packages/app_center/lib/manage/updates_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -164,6 +168,7 @@ class UpdatesModel extends _$UpdatesModel {
);
}
ref.invalidateSelf();
ref.invalidate(filteredLocalSnapsProvider);
}
}

Expand Down
19 changes: 9 additions & 10 deletions packages/app_center/lib/snapd/snap_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> refresh() async {
///
/// Returns `true` if the snap was updated, `false` otherwise.
Future<bool> refresh() async {
assert(
state.hasStoreSnap,
'The snap must be loaded from the store before updating it',
Expand All @@ -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.
Expand Down Expand Up @@ -173,18 +169,20 @@ class SnapModel extends _$SnapModel {
}
}

Future<void> _listenUntilDone(
Future<bool> _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();
}
});
Expand All @@ -195,6 +193,7 @@ class SnapModel extends _$SnapModel {
if (invalidate) {
ref.invalidateSelf();
}
return completedSuccessfully;
}
}

Expand Down

0 comments on commit f39c8f6

Please sign in to comment.