Files
pipelines/app/lib/features/attention/ui/notification_permission_tile.dart
T
alan 7718604268 feat(app): add the notification capability behind a platform seam
An abstract NotificationBridge with a dart:js_interop web implementation and
an honest unsupported stub; a NotificationController that reports only the
platform's permission word, asks only from an explicit gesture, never
re-prompts a denied answer, and notifies once per run+kind+finished marker
under its own persisted ledger key. One reusable native
NotificationPermissionTile for Settings, an isolated browser probe, and
focused tests. No shell/settings wiring.
2026-10-22 01:23:24 +01:00

128 lines
4.4 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/host.dart';
import '../notify/notification_models.dart';
/// The permission tile's root, so a caller can find it whichever host draws it.
const notificationTileKey = ValueKey<String>('notification-permission-tile');
/// The explicit-gesture Enable action, present only when a request is possible.
const notificationEnableKey = ValueKey<String>(
'notification-permission-enable',
);
/// The honest state line, which says plainly when the in-app surface is the
/// only channel.
const notificationStateKey = ValueKey<String>('notification-permission-state');
/// The reusable Notifications permission tile for Settings.
///
/// It renders the platform's own [state] with the browser's own copy, shows the
/// Android rationale verbatim, and offers the [notificationRequestLabel] action
/// only while a request is still possible. Nothing here asks for permission on
/// its own — [onRequest] is the caller's explicit-gesture handler, and the tile
/// never guesses a grant.
///
/// The caller owns the surrounding section/card and passes the live state; the
/// tile reads no store, bridge or clock.
class NotificationPermissionTile extends StatelessWidget {
const NotificationPermissionTile({
super.key,
required this.state,
this.asked = false,
this.onRequest,
this.cupertino,
});
final NotificationPermissionState state;
/// Whether the operator has already been asked; only changes the wording.
final bool asked;
/// The explicit-gesture handler. Absent means no action is drawn even when a
/// request would be possible.
final VoidCallback? onRequest;
/// Forces the native host in tests and fixtures; otherwise the ambient
/// [usesCupertinoHost] decides.
final bool? cupertino;
@override
Widget build(BuildContext context) {
final view = notificationPermissionView(state, asked: asked);
final native = cupertino ?? usesCupertinoHost(context);
final action = view.canRequest && onRequest != null
? _action(native, view.actionLabel!, onRequest!)
: null;
final bodyStyle = native
? CupertinoTheme.of(context).textTheme.textStyle.copyWith(
color: CupertinoColors.secondaryLabel.resolveFrom(context),
)
: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
native
? CupertinoListTile(
key: notificationTileKey,
leading: const Icon(CupertinoIcons.bell),
title: const Text(notificationSectionTitle),
)
: ListTile(
key: notificationTileKey,
leading: const Icon(Icons.notifications_outlined),
title: const Text(notificationSectionTitle),
),
Padding(
padding: const EdgeInsetsDirectional.fromSTEB(16, 0, 16, 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(notificationRationale, style: bodyStyle),
const SizedBox(height: 8),
Semantics(
liveRegion: true,
child: Text(
view.body,
key: notificationStateKey,
style: bodyStyle,
),
),
// The action sits on its own row, as the browser's settings
// section does: a native button in a row's trailing slot would
// squeeze the section title to one character per line at large
// text, and its label is too long to share the row.
if (action != null) ...[
const SizedBox(height: 8),
Align(
alignment: AlignmentDirectional.centerStart,
child: action,
),
],
],
),
),
],
);
}
Widget _action(bool native, String label, VoidCallback onRequest) {
if (native) {
return CupertinoButton(
key: notificationEnableKey,
padding: EdgeInsets.zero,
onPressed: onRequest,
child: Text(label),
);
}
return TextButton(
key: notificationEnableKey,
onPressed: onRequest,
child: Text(label),
);
}
}