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 all 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
6 changes: 3 additions & 3 deletions packages/app_center/lib/error/error_l10n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sealed class ErrorMessage {
const ErrorMessage();

factory ErrorMessage.fromObject(Object? e) {
if (e is! SnapdException) return ErrorMessageUnkown();
if (e is! SnapdException) return ErrorMessageUnknown();
spydon marked this conversation as resolved.
Show resolved Hide resolved

switch (e.kind) {
case 'network-timeout':
Expand All @@ -22,7 +22,7 @@ sealed class ErrorMessage {
return patternMap.message(match);
}
}
return ErrorMessageUnkown();
return ErrorMessageUnknown();
}

static final _patternMaps =
Expand Down Expand Up @@ -79,4 +79,4 @@ class ErrorMessageRunningApps extends ErrorMessage {
final String snap;
}

class ErrorMessageUnkown extends ErrorMessage {}
class ErrorMessageUnknown extends ErrorMessage {}
68 changes: 53 additions & 15 deletions packages/app_center/lib/manage/local_snap_providers.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,60 @@
import 'package:app_center/manage/manage_model.dart';
import 'package:app_center/manage/updates_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';
import 'package:ubuntu_service/ubuntu_service.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 {
late final _snapd = getService<SnapdService>();

@override
Future<SnapListState> build() async {
final snapListState = await connectionCheck(_snapd.getSnaps, ref);
final snaps = snapListState.snaps;
final refreshableSnaps =
(await ref.watch(updatesModelProvider.future)).snaps.map((s) => s.name);
final nonRefreshableSnaps =
snaps.where((s) => !refreshableSnaps.contains(s.name));
void refreshFunction(_, __) => _refreshWithFilters(nonRefreshableSnaps);
ref.listen(localSnapFilterProvider, refreshFunction);
ref.listen(showLocalSystemAppsProvider, refreshFunction);
ref.listen(localSnapSortOrderProvider, refreshFunction);
return snapListState.copyWith(
snaps: _refreshWithFilters(nonRefreshableSnaps, updateState: false),
);
}

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(
SnapListState(
snaps: filteredSnaps,
hasInternet: state.value?.hasInternet ?? true,
),
);
}
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.

63 changes: 0 additions & 63 deletions packages/app_center/lib/manage/manage_model.dart

This file was deleted.

Loading
Loading