Repository navigation
feat(shortcuts): launcher shortcuts for your services #271
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dcf6745
feat(shortcuts): choose which services get launcher shortcuts
retransmit c3d80bf
feat(shortcuts): keep launcher shortcuts in step with the active profile
retransmit c60e79c
feat(android): publish launcher shortcuts
retransmit c69ce77
feat(settings): pick which services get launcher shortcuts
retransmit 9d87c7f
fix(shortcuts): wait for profiles to load before publishing shortcuts
retransmit d048170
fix(android): publish launcher shortcuts off the main thread
retransmit f2fda33
fix(shortcuts): retry refused updates and keep quick ticks
retransmit 49972ff
fix(android): share one shortcut thread across the process
retransmit 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import 'dart:convert'; | ||
|
|
||
| import 'package:core_models/core_models.dart'; | ||
| import 'package:core_profile/core_profile.dart'; | ||
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
|
||
| import '../preferences.dart'; | ||
|
|
||
| /// How many services get a launcher shortcut. Most launchers show about four | ||
| /// on a long-press, so a longer list would mostly go unseen. | ||
| const int maxLauncherShortcuts = 4; | ||
|
|
||
| /// The `atrium://` link a shortcut opens: the instance's service screen, | ||
| /// picked by name, through the same path as any other Atrium link. | ||
| String shortcutLink(Instance instance) => Uri( | ||
| scheme: 'atrium', | ||
| host: instance.kind.name.toLowerCase(), | ||
| queryParameters: <String, String>{'instance': instance.name}, | ||
| ).toString(); | ||
|
|
||
| /// The instances that get a shortcut, in sidebar order. | ||
| /// | ||
| /// [chosenIds] is null until the user edits the list, and then the first | ||
| /// [maxLauncherShortcuts] services stand in, so shortcuts work with no setup. | ||
| /// Once chosen, an empty list means none, and IDs of deleted instances drop | ||
| /// out. | ||
| List<Instance> shortcutInstances( | ||
| List<Instance> instances, | ||
| List<String>? chosenIds, | ||
| ) { | ||
| final Iterable<Instance> picked = chosenIds == null | ||
| ? instances | ||
| : instances.where((Instance i) => chosenIds.contains(i.id)); | ||
| return picked.take(maxLauncherShortcuts).toList(growable: false); | ||
| } | ||
|
|
||
| /// The services a profile's user ticked for launcher shortcuts, or null when | ||
| /// they never edited the list. Kept per profile in the settings box. | ||
| class LauncherShortcutChoiceController extends Notifier<List<String>?> { | ||
| LauncherShortcutChoiceController(this.profileId); | ||
|
|
||
| final String profileId; | ||
|
|
||
| String get _key => 'launcherShortcuts.$profileId'; | ||
|
|
||
| @override | ||
| List<String>? build() { | ||
| final String? raw = ref.read(settingsBoxProvider).get(_key); | ||
| if (raw == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| final Object? decoded = jsonDecode(raw); | ||
| if (decoded is List) { | ||
| return decoded.whereType<String>().toList(growable: false); | ||
| } | ||
| } on FormatException { | ||
| // Falls through to the default below. | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /// Shows [ids] at once, so a second tap made while this one is still | ||
| /// saving starts from it, then saves. A failed save puts the earlier | ||
| /// choice back, unless a later one has replaced it. | ||
| Future<void> setChosen(List<String> ids) async { | ||
| final List<String>? previous = state; | ||
| final List<String> next = List<String>.unmodifiable(ids); | ||
| state = next; | ||
| try { | ||
| await ref.read(settingsBoxProvider).put(_key, jsonEncode(next)); | ||
| } catch (_) { | ||
| if (identical(state, next)) { | ||
| state = previous; | ||
| } | ||
| rethrow; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| final launcherShortcutChoiceProvider = NotifierProvider.family< | ||
| LauncherShortcutChoiceController, List<String>?, String>( | ||
| LauncherShortcutChoiceController.new, | ||
| ); | ||
|
|
||
| /// The active profile's services that get a launcher shortcut. | ||
| final Provider<List<Instance>> launcherShortcutInstancesProvider = | ||
| Provider<List<Instance>>((Ref ref) { | ||
| final Profile? profile = ref.watch(activeProfileProvider); | ||
| if (profile == null) { | ||
| return const <Instance>[]; | ||
| } | ||
| return shortcutInstances( | ||
| profile.instances, | ||
| ref.watch(launcherShortcutChoiceProvider(profile.id)), | ||
| ); | ||
| }); |
93 changes: 93 additions & 0 deletions
93
app/lib/src/launcher_shortcuts/launcher_shortcuts_publisher.dart
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import 'package:collection/collection.dart'; | ||
| import 'package:core_models/core_models.dart'; | ||
| import 'package:core_profile/core_profile.dart'; | ||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:flutter/services.dart'; | ||
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
|
||
| import 'launcher_shortcuts.dart'; | ||
|
|
||
| /// Hands the launcher shortcut list to Android, which shows it on a | ||
| /// long-press of Atrium's icon and in launcher search. | ||
| class LauncherShortcutsPublisher { | ||
| LauncherShortcutsPublisher([ | ||
| this._channel = const MethodChannel('app.atrium/launcher'), | ||
| ]); | ||
|
|
||
| final MethodChannel _channel; | ||
|
|
||
| /// What Android was last given. Android limits how often an app may change | ||
| /// its shortcuts, so an unchanged list is not sent again. | ||
| List<Map<String, String>>? _sent; | ||
|
|
||
| Future<void> publish(List<Instance> instances) async { | ||
| if (defaultTargetPlatform != TargetPlatform.android) { | ||
| return; | ||
| } | ||
| final List<Map<String, String>> shortcuts = <Map<String, String>>[ | ||
| for (final Instance i in instances) | ||
| <String, String>{ | ||
| 'id': i.id, | ||
| 'name': i.name, | ||
| 'link': shortcutLink(i), | ||
| 'icon': 'assets/service_icons/${i.kind.name}.png', | ||
| }, | ||
| ]; | ||
| if (const DeepCollectionEquality().equals(shortcuts, _sent)) { | ||
| return; | ||
| } | ||
| _sent = shortcuts; | ||
| try { | ||
| final bool? applied = await _channel.invokeMethod<bool>( | ||
| 'setShortcuts', | ||
| <String, Object>{'shortcuts': shortcuts}, | ||
| ); | ||
| // Android can refuse (rate limited while in the background) or fail. | ||
| // Forget the list so the same one is tried again, unless a newer | ||
| // publish has already replaced it. | ||
| if (applied != true && identical(_sent, shortcuts)) { | ||
| _sent = null; | ||
| } | ||
| } on MissingPluginException { | ||
| _sent = null; | ||
| } on PlatformException { | ||
| // Not sent, so the next change, or the next launch, tries again. | ||
| _sent = null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| final Provider<LauncherShortcutsPublisher> launcherShortcutsPublisherProvider = | ||
| Provider<LauncherShortcutsPublisher>( | ||
| (Ref ref) => LauncherShortcutsPublisher(), | ||
| ); | ||
|
|
||
| /// The shortcut list to publish, or null while the profiles are still | ||
| /// loading (at launch, while secure storage is read) or failed to load. | ||
| /// Publishing then would clear the shortcuts until the profiles arrive, or | ||
| /// for good if they never do. | ||
| final Provider<List<Instance>?> _publishableShortcutsProvider = | ||
| Provider<List<Instance>?>((Ref ref) { | ||
| if (!ref.watch(profileListProvider).hasValue) { | ||
| return null; | ||
| } | ||
| return ref.watch(launcherShortcutInstancesProvider); | ||
| }); | ||
|
|
||
| /// Publishes the active profile's shortcut list once the profiles have | ||
| /// loaded, and again whenever it changes: a profile switch, a service added, | ||
| /// renamed, reordered or deleted, or a new choice in Settings. Watched once | ||
| /// at the app root. | ||
| final Provider<void> launcherShortcutsSyncProvider = Provider<void>((Ref ref) { | ||
| final LauncherShortcutsPublisher publisher = | ||
| ref.watch(launcherShortcutsPublisherProvider); | ||
| ref.listen<List<Instance>?>( | ||
| _publishableShortcutsProvider, | ||
| (_, List<Instance>? next) { | ||
| if (next != null) { | ||
| publisher.publish(next); | ||
| } | ||
| }, | ||
| fireImmediately: true, | ||
| ); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.