fix(flutter): drop action buttons from error notifications

A failed download has no usable output file, so error notifications now
carry no buttons at all; only the done event offers open file / open
folder actions.
This commit is contained in:
Levi committed 2026-09-17 09:55:07 +08:00
1 parent 6f4635e2eb
commit 014221a1a7
2 files changed
+23 -34

No files matched your search

@@ -27,10 +27,10 @@ const _openFileAction = 'open_file';
const _openFolderAction = 'open_folder'; const _openFolderAction = 'open_folder';
// macOS resolves buttons through pre-registered notification categories, one // macOS resolves buttons through pre-registered notification categories, one
// per action combination a terminal task event can offer. // per action combination a done task event can offer. Error notifications
// carry no buttons at all.
const _categoryDoneSingleFile = 'taskDoneSingleFile'; const _categoryDoneSingleFile = 'taskDoneSingleFile';
const _categoryDoneFolder = 'taskDoneFolder'; const _categoryDoneFolder = 'taskDoneFolder';
const _categoryTaskError = 'taskTaskError';
/// A notification button resolved for one task event. /// A notification button resolved for one task event.
/// ///
@@ -105,7 +105,6 @@ class AppNotificationController extends AsyncNotifier<AppNotificationState> {
notificationCategories: [ notificationCategories: [
DarwinNotificationCategory(_categoryDoneSingleFile, actions: [openFileAction, openFolderAction]), DarwinNotificationCategory(_categoryDoneSingleFile, actions: [openFileAction, openFolderAction]),
DarwinNotificationCategory(_categoryDoneFolder, actions: [openFolderAction]), DarwinNotificationCategory(_categoryDoneFolder, actions: [openFolderAction]),
DarwinNotificationCategory(_categoryTaskError, actions: [openFolderAction]),
], ],
); );
final linux = LinuxInitializationSettings( final linux = LinuxInitializationSettings(
@@ -164,16 +163,16 @@ class AppNotificationController extends AsyncNotifier<AppNotificationState> {
title: locale.notificationTaskDone, title: locale.notificationTaskDone,
body: event.name, body: event.name,
taskId: event.taskId, taskId: event.taskId,
allowOpenFile: true, withActions: true,
); );
case TaskEventType.error: case TaskEventType.error:
// A failed download has no usable output to open, so no buttons.
await _showNotification( await _showNotification(
locale: locale, locale: locale,
title: locale.notificationTaskError, title: locale.notificationTaskError,
body: event.name, body: event.name,
taskId: event.taskId, taskId: event.taskId,
// A failed download rarely has a usable output file to open. withActions: false,
allowOpenFile: false,
); );
} }
}); });
@@ -184,17 +183,16 @@ class AppNotificationController extends AsyncNotifier<AppNotificationState> {
required String title, required String title,
required String body, required String body,
required String taskId, required String taskId,
required bool allowOpenFile, required bool withActions,
}) async { }) async {
final target = await resolveTaskTarget(taskId, allowOpenFile: allowOpenFile); final target = withActions ? await resolveTaskTarget(taskId) : null;
final payload = target == null ? '' : jsonEncode({'path': target.path}); final payload = target == null ? '' : jsonEncode({'path': target.path});
final details = NotificationDetails( final details = NotificationDetails(
macOS: DarwinNotificationDetails( macOS: DarwinNotificationDetails(
categoryIdentifier: switch (target) { categoryIdentifier: switch (target) {
null => null, null => null,
_ when target.canOpenFile => _categoryDoneSingleFile, _ when target.canOpenFile => _categoryDoneSingleFile,
_ when allowOpenFile => _categoryDoneFolder, _ => _categoryDoneFolder,
_ => _categoryTaskError,
}, },
), ),
linux: LinuxNotificationDetails( linux: LinuxNotificationDetails(
@@ -216,19 +214,16 @@ class AppNotificationController extends AsyncNotifier<AppNotificationState> {
await _plugin.show(id: _notificationId++, title: title, body: body, notificationDetails: details, payload: payload); await _plugin.show(id: _notificationId++, title: title, body: body, notificationDetails: details, payload: payload);
} }
/// Resolves the download target a terminal task event points at, following /// Resolves the download target a done task event points at, following the
/// the same rule as the task details page: only single-file tasks offer /// same rule as the task details page: only single-file tasks offer "open
/// "open file", folder tasks (e.g. multi-file torrents) can only reveal the /// file", folder tasks (e.g. multi-file torrents) can only reveal the
/// folder. Returns null when the task is already gone. /// folder. Returns null when the task is already gone.
@visibleForTesting @visibleForTesting
Future<({String path, bool canOpenFile})?> resolveTaskTarget( Future<({String path, bool canOpenFile})?> resolveTaskTarget(String taskId) async {
String taskId, {
required bool allowOpenFile,
}) async {
final apiTask = await findTask(taskId); final apiTask = await findTask(taskId);
if (apiTask == null) return null; if (apiTask == null) return null;
final record = TaskRecord.fromApi(apiTask); final record = TaskRecord.fromApi(apiTask);
return (path: record.storagePath, canOpenFile: allowOpenFile && !record.isFolder); return (path: record.storagePath, canOpenFile: !record.isFolder);
} }
@visibleForTesting @visibleForTesting
@@ -122,12 +122,12 @@ void main() {
expect(calls.where((call) => call.method == 'show'), hasLength(2)); expect(calls.where((call) => call.method == 'show'), hasLength(2));
}); });
test('macOS registers action categories for every terminal event combination', () async { test('macOS registers action categories for done task combinations', () async {
await start(enabled: true); await start(enabled: true);
final categories = initialization()['notificationCategories'] as List<dynamic>; final categories = initialization()['notificationCategories'] as List<dynamic>;
final identifiers = categories.map((category) => (category as Map)['identifier']).toList(); final identifiers = categories.map((category) => (category as Map)['identifier']).toList();
expect(identifiers, containsAll(['taskDoneSingleFile', 'taskDoneFolder', 'taskTaskError'])); expect(identifiers, ['taskDoneSingleFile', 'taskDoneFolder']);
final singleFile = categories.firstWhere((c) => c['identifier'] == 'taskDoneSingleFile') as Map; final singleFile = categories.firstWhere((c) => c['identifier'] == 'taskDoneSingleFile') as Map;
final singleActions = (singleFile['actions'] as List).cast<Map>(); final singleActions = (singleFile['actions'] as List).cast<Map>();
expect(singleActions.map((action) => action['identifier']), ['open_file', 'open_folder']); expect(singleActions.map((action) => action['identifier']), ['open_file', 'open_folder']);
@@ -138,7 +138,7 @@ void main() {
final container = await start(enabled: true); final container = await start(enabled: true);
final notifier = container.read(appNotificationControllerProvider.notifier) as _TestNotificationController; final notifier = container.read(appNotificationControllerProvider.notifier) as _TestNotificationController;
final target = await notifier.resolveTaskTarget('single', allowOpenFile: true); final target = await notifier.resolveTaskTarget('single');
expect(target?.path, '/downloads/archive.zip'); expect(target?.path, '/downloads/archive.zip');
expect(target?.canOpenFile, isTrue); expect(target?.canOpenFile, isTrue);
@@ -148,21 +148,12 @@ void main() {
final container = await start(enabled: true); final container = await start(enabled: true);
final notifier = container.read(appNotificationControllerProvider.notifier) as _TestNotificationController; final notifier = container.read(appNotificationControllerProvider.notifier) as _TestNotificationController;
final target = await notifier.resolveTaskTarget('folder', allowOpenFile: true); final target = await notifier.resolveTaskTarget('folder');
expect(target?.path, '/downloads/torrent-bundle'); expect(target?.path, '/downloads/torrent-bundle');
expect(target?.canOpenFile, isFalse); expect(target?.canOpenFile, isFalse);
}); });
test('an errored task never resolves an open-file target', () async {
final container = await start(enabled: true);
final notifier = container.read(appNotificationControllerProvider.notifier) as _TestNotificationController;
final target = await notifier.resolveTaskTarget('single', allowOpenFile: false);
expect(target?.canOpenFile, isFalse);
});
test('a done single-file task notification selects the single-file category and payload path', () async { test('a done single-file task notification selects the single-file category and payload path', () async {
await start(enabled: true); await start(enabled: true);
events.add(const TaskEvent(type: TaskEventType.done, taskId: 'single', name: 'archive.zip')); events.add(const TaskEvent(type: TaskEventType.done, taskId: 'single', name: 'archive.zip'));
@@ -184,13 +175,16 @@ void main() {
expect(platformSpecifics['categoryIdentifier'], 'taskDoneFolder'); expect(platformSpecifics['categoryIdentifier'], 'taskDoneFolder');
}); });
test('an errored task notification selects the error category', () async { test('an errored task notification carries no actions at all', () async {
await start(enabled: true); await start(enabled: true);
events.add(const TaskEvent(type: TaskEventType.error, taskId: 'single', name: 'archive.zip')); events.add(const TaskEvent(type: TaskEventType.error, taskId: 'single', name: 'archive.zip'));
await Future<void>.delayed(Duration.zero); await Future<void>.delayed(Duration.zero);
final platformSpecifics = shownNotifications()[0]['platformSpecifics'] as Map<dynamic, dynamic>; final notifications = shownNotifications();
expect(platformSpecifics['categoryIdentifier'], 'taskTaskError'); expect(notifications, hasLength(1));
final platformSpecifics = notifications[0]['platformSpecifics'] as Map<dynamic, dynamic>;
expect(platformSpecifics['categoryIdentifier'], isNull);
expect(notifications[0]['payload'], '');
}); });
test('action responses route to file and folder handlers by encoded spec', () async { test('action responses route to file and folder handlers by encoded spec', () async {