-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1737 from ubuntu/refactor/migrate-manage-snaps-pr…
…oviders refactor: Migrate manage snaps providers
- Loading branch information
Showing
29 changed files
with
1,835 additions
and
1,550 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
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.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.