Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Migrate manage snaps providers #1737

Merged
merged 20 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 41 additions & 14 deletions packages/app_center/lib/manage/local_snap_providers.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,49 @@
import 'package:app_center/manage/manage_model.dart';
import 'package:app_center/snapd/snapd.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:snapd/snapd.dart';

part 'local_snap_providers.g.dart';

final localSnapFilterProvider = StateProvider.autoDispose<String>((_) => '');
final showLocalSystemAppsProvider = StateProvider<bool>((_) => false);
final localSnapSortOrderProvider =
StateProvider<SnapSortOrder>((_) => SnapSortOrder.alphabeticalAsc);
final localSnapsProvider = Provider.autoDispose(
(ref) => ref
.watch(manageModelProvider.select((m) => m.nonRefreshableSnaps))
.where(
(snap) => snap.titleOrName
.toLowerCase()
.contains(ref.watch(localSnapFilterProvider).toLowerCase()),
)
.where(
(snap) =>
ref.watch(showLocalSystemAppsProvider) || snap.apps.isNotEmpty,
)
.sortedSnaps(ref.watch(localSnapSortOrderProvider)),
);

@riverpod
class FilteredLocalSnaps extends _$FilteredLocalSnaps {
@override
Future<Iterable<Snap>> build() async {
final nonRefreshableSnaps = await ref.watch(
manageModelProvider.future.select(
(m) => m.then((value) => value.nonRefreshableSnaps),
),
);
void refreshFunction(_, __) => _refreshWithFilters(nonRefreshableSnaps);
ref.listen(localSnapFilterProvider, refreshFunction);
ref.listen(showLocalSystemAppsProvider, refreshFunction);
ref.listen(localSnapSortOrderProvider, refreshFunction);
return _refreshWithFilters(nonRefreshableSnaps, updateState: false);
d-loose marked this conversation as resolved.
Show resolved Hide resolved
}

Iterable<Snap> _refreshWithFilters(
Iterable<Snap> nonRefreshableSnaps, {
bool updateState = true,
}) {
final filter = ref.read(localSnapFilterProvider).toLowerCase();
final showSystemApps = ref.read(showLocalSystemAppsProvider);
final sortOrder = ref.read(localSnapSortOrderProvider);
final filteredSnaps = nonRefreshableSnaps
.where(
(snap) =>
snap.titleOrName.toLowerCase().contains(filter) &&
(showSystemApps || snap.apps.isNotEmpty),
)
.sortedSnaps(sortOrder);
if (updateState) {
state = AsyncData(filteredSnaps);
}
return filteredSnaps;
}
}
27 changes: 27 additions & 0 deletions packages/app_center/lib/manage/local_snap_providers.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 14 additions & 55 deletions packages/app_center/lib/manage/manage_model.dart
Original file line number Diff line number Diff line change
@@ -1,63 +1,22 @@
import 'package:app_center/manage/manage_snaps_data.dart';
import 'package:app_center/snapd/snapd.dart';
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:snapd/snapd.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:ubuntu_service/ubuntu_service.dart';

final manageModelProvider = ChangeNotifierProvider.autoDispose(
(ref) => ManageModel(
snapd: getService<SnapdService>(),
updatesModel: ref.read(updatesModelProvider),
)..init(),
);
part 'manage_model.g.dart';

class ManageModel extends ChangeNotifier {
ManageModel({
required this.snapd,
required this.updatesModel,
}) : _state = const AsyncValue.loading();
final SnapdService snapd;
final UpdatesModel updatesModel;

AsyncValue<void> get state => _state;
AsyncValue<void> _state;

List<Snap>? _installedSnaps;
List<String>? _refreshableSnapNames;

bool _isRefreshable(Snap snap) => updatesModel.hasUpdate(snap.name);
Iterable<Snap> get refreshableSnaps =>
_installedSnaps?.where(_isRefreshable) ?? const Iterable.empty();
Iterable<Snap> get nonRefreshableSnaps =>
_installedSnaps?.whereNot(_isRefreshable) ?? const Iterable.empty();

void _getRefreshableSnapNames() {
final refreshableSnapNames = updatesModel.refreshableSnapNames.toList();
if (!listEquals(refreshableSnapNames, _refreshableSnapNames)) {
_refreshableSnapNames = refreshableSnapNames;
notifyListeners();
}
}

// TODO: cache local snaps
Future<void> init() async {
updatesModel.addListener(_getRefreshableSnapNames);
_state = await AsyncValue.guard(() async {
await _getInstalledSnaps();
notifyListeners();
});
}
@Riverpod(keepAlive: true)
class ManageModel extends _$ManageModel {
late final _snapd = getService<SnapdService>();

@override
void dispose() {
updatesModel.removeListener(_getRefreshableSnapNames);
super.dispose();
}

Future<void> _getInstalledSnaps() async {
_installedSnaps = await snapd.getSnaps().then(
(snaps) => snaps.sortedBy((snap) => snap.titleOrName.toLowerCase()),
);
Future<ManageSnapsData> build() async {
final installedSnaps = await _snapd.getSnaps();
final refreshableSnapNames =
(await ref.watch(updatesModelProvider.future)).snapNames;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it'd be better to disentangle the updates from the manage model entirely, or at least use select more often to avoid unnecessary rebuilds and loading states. For example:

Before:

Screencast.from.2024-07-12.11-54-33.webm

After:

Screencast.from.2024-07-12.11-54-55.webm

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I forgot about this, I had them separately first, but there was some problem with the slivers.
I'll try that again!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the latest changes the loading state behavior is still a bit different

Screencast.from.2024-07-15.17-01-24.webm

I'm wondering what's the best option here - do we need to hide the list of installed snaps during the loading state?
@anasereijo do you have an opinion on that?

return ManageSnapsData(
installedSnaps: installedSnaps,
refreshableSnapNames: refreshableSnapNames,
);
}
}
25 changes: 25 additions & 0 deletions packages/app_center/lib/manage/manage_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading