Skip to content

Commit

Permalink
Merge pull request #1737 from ubuntu/refactor/migrate-manage-snaps-pr…
Browse files Browse the repository at this point in the history
…oviders

refactor: Migrate manage snaps providers
  • Loading branch information
spydon authored Jul 17, 2024
2 parents e3dd562 + 48b4ce3 commit 7c0fee1
Show file tree
Hide file tree
Showing 29 changed files with 1,835 additions and 1,550 deletions.
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();

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

0 comments on commit 7c0fee1

Please sign in to comment.