-
Notifications
You must be signed in to change notification settings - Fork 126
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
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b5cb985
refactor: Modern style providers for manage page + fix local snap uni…
spydon 2c0f752
test: Update tests to register mock services
spydon 35014d3
fix: Invalidate provider after snap is removed
spydon e742b06
fix: Rename localSnapsProvider to filteredLocalSnapsProvider
spydon 45e1fcc
Remove debug print
spydon 5c4e9a9
fix: Skip loading on filter change
spydon 6ee5fb2
fix: Make manage page work offline
spydon 69ea316
Remove unnecessary error handler listener
spydon 8dabaa7
refactor: Split up ManageModel
spydon d8c52a8
test: Fix tests for split up manage provider
spydon 31def92
fix: Remove ManageSnapData
spydon c06b500
docs: Update comment about locally installed snap
spydon 7d76d01
Regenerate mocks
spydon 728a71b
fix: Show loading when local snap is not found
spydon aa9078c
Regenerate mocks
spydon 2de7dc6
fix: Ignore kind == null exceptions for the snap model
spydon d1e3b04
fix: Indicate to the user when there is no internet
spydon 88984e3
fix: Let update list load meanwhile local list loads
spydon 54bf7b6
fix: Remove unnecessary loading spinner
spydon 48b4ce3
fix: Update wording of error message
spydon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,35 @@ | ||
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), | ||
), | ||
); | ||
return nonRefreshableSnaps | ||
.where( | ||
(snap) => snap.titleOrName | ||
.toLowerCase() | ||
.contains(ref.watch(localSnapFilterProvider).toLowerCase()), | ||
) | ||
.where( | ||
(snap) => | ||
ref.watch(showLocalSystemAppsProvider) || snap.apps.isNotEmpty, | ||
) | ||
.sortedSnaps(ref.watch(localSnapSortOrderProvider)); | ||
} | ||
} |
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 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,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; | ||
return ManageSnapsData( | ||
installedSnaps: installedSnaps, | ||
refreshableSnapNames: refreshableSnapNames, | ||
); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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?