diff --git a/app/lib/application/pipelines_app.dart b/app/lib/application/pipelines_app.dart index 3b888421..421b7873 100644 --- a/app/lib/application/pipelines_app.dart +++ b/app/lib/application/pipelines_app.dart @@ -8,6 +8,8 @@ import '../core/api_gateway.dart'; import '../core/transport/pipelines_client.dart'; import '../shell/adaptive_shell.dart'; import '../shell/destination.dart'; +import '../features/attention/notify/notification_controller.dart'; +import '../features/attention/notify/notification_store.dart'; import '../features/settings/settings_screen.dart'; import 'app.dart'; import 'app_route.dart'; @@ -61,6 +63,12 @@ class _PipelinesAppState extends State { /// list once; the run itself is then opened from its route. int _runsRefresh = 0; + /// One notification capability for the app: the Runs attention surface + /// publishes live transitions and Settings renders the same permission. + late final NotificationController _notifications = NotificationController( + store: const SharedPreferencesNotificationStore(), + ); + @override void initState() { super.initState(); @@ -81,6 +89,8 @@ class _PipelinesAppState extends State { ); } _cupertino = widget.cupertino ?? _platformIsCupertino; + _notifications.onOpenRun = (id) => _delegate.goTo(AppRoute.run(id)); + unawaited(_notifications.load()); _delegate = AppRouterDelegate( initial: initial, cupertino: _cupertino, @@ -100,6 +110,7 @@ class _PipelinesAppState extends State { @override void dispose() { + _notifications.dispose(); _delegate.dispose(); if (_ownsProvider) { (_provider as PlatformRouteInformationProvider).dispose(); @@ -178,6 +189,7 @@ class _PipelinesAppState extends State { server: widget.server, initialRunId: route.runId, listRefreshToken: _runsRefresh, + notifications: _notifications, onRunSelected: (id) => _delegate.goTo(AppRoute.run(id)), onBack: () => _delegate.goTo(AppRoute.runs), ), @@ -193,6 +205,7 @@ class _PipelinesAppState extends State { initialEndpoint: _session.endpoint, embedded: true, onOpenTemplates: _delegate.openTemplates, + notifications: _notifications, ), }, ); diff --git a/app/lib/application/runs_destination.dart b/app/lib/application/runs_destination.dart index f63077e1..fdef966e 100644 --- a/app/lib/application/runs_destination.dart +++ b/app/lib/application/runs_destination.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; +import '../features/attention/notify/notification_controller.dart'; import '../features/runs/screen.dart'; import '../ui/adaptive/host.dart'; import 'app_session.dart'; @@ -21,6 +22,7 @@ class RunsDestination extends StatelessWidget { this.onRunSelected, this.onBack, this.listRefreshToken = 0, + this.notifications, }); final AppSession session; @@ -33,6 +35,9 @@ class RunsDestination extends StatelessWidget { /// re-reads its list once; no row is inserted locally. final int listRefreshToken; + /// The app-level notification capability shared with Settings. + final NotificationController? notifications; + @override Widget build(BuildContext context) => AnimatedBuilder( animation: session, @@ -70,6 +75,7 @@ class RunsDestination extends StatelessWidget { initialRunId: initialRunId, syncToken: session.connectionEpoch, listRefreshToken: listRefreshToken, + notifications: notifications, onRunSelected: onRunSelected, onBack: onBack, ); diff --git a/app/lib/features/attention/ui/needs_you_runs_heading.dart b/app/lib/features/attention/ui/needs_you_runs_heading.dart index bf8889f1..ddd9966b 100644 --- a/app/lib/features/attention/ui/needs_you_runs_heading.dart +++ b/app/lib/features/attention/ui/needs_you_runs_heading.dart @@ -4,6 +4,8 @@ import 'package:flutter/widgets.dart'; import '../../../core/api_gateway.dart'; import '../domain/attention_models.dart'; +import '../notify/notification_controller.dart'; +import '../notify/notification_ledger.dart'; import '../runtime/attention_controller.dart'; import '../runtime/attention_store.dart'; import 'needs_you_bell.dart'; @@ -25,6 +27,7 @@ class NeedsYouRunsHeading extends StatefulWidget { this.store = const SharedPreferencesAttentionStore(), this.now, this.cupertino, + this.notifications, }); /// The run summaries the Runs destination already holds. @@ -48,6 +51,10 @@ class NeedsYouRunsHeading extends StatefulWidget { /// Forces the native host in tests and fixtures. final bool? cupertino; + /// The app-level notification capability. Absent in fixtures that only draw + /// the bell, so no store or platform bridge is touched there. + final NotificationController? notifications; + @override State createState() => _NeedsYouRunsHeadingState(); } @@ -65,12 +72,44 @@ class _NeedsYouRunsHeadingState extends State { _attention.seedRows(widget.rows); unawaited(_attention.load()); _listen(); + final notifications = widget.notifications; + if (notifications != null) { + notifications.onOpenRun = _openRunById; + unawaited(notifications.load().then((_) => _publish())); + } + } + + /// A notification names only the run id; opening it follows the same route + /// rule as a bell row. Dismissal is not implied by a notice tap. + void _openRunById(int id) { + for (final entry in _attention.entries) { + if (entry.runId == id) { + widget.onOpen(entry); + return; + } + } + } + + /// Publishes the transitions the model accumulated against the rows and the + /// dismissals that are in force right now. Idempotent, so the whole list is + /// safe to pass after every read or pushed frame. + void _publish() { + final notifications = widget.notifications; + if (notifications == null) return; + notifications.publishTransitions( + _attention.transitions, + qualifying: notificationQualifyingKeys(_attention.rows), + dismissed: _attention.dismissed, + ); } void _listen() { _frames = widget.frames .where((frame) => frame['type'] == 'run_event') - .listen(_attention.observeFrame); + .listen((frame) { + _attention.observeFrame(frame); + _publish(); + }); } @override @@ -84,10 +123,12 @@ class _NeedsYouRunsHeadingState extends State { if (oldWidget.syncToken != widget.syncToken) { _attention.resetForReconnect(); _attention.seedRows(widget.rows, reconnect: true); + _publish(); return; } if (!identical(oldWidget.rows, widget.rows)) { _attention.seedRows(widget.rows); + _publish(); } } diff --git a/app/lib/features/runs/screen.dart b/app/lib/features/runs/screen.dart index ce57d317..56dc70a7 100644 --- a/app/lib/features/runs/screen.dart +++ b/app/lib/features/runs/screen.dart @@ -1,6 +1,7 @@ import 'package:flutter/widgets.dart'; import '../../core/api_gateway.dart'; +import '../attention/notify/notification_controller.dart'; import '../attention/runtime/attention_store.dart'; import 'data/runs_repository.dart'; import 'ui/run_detail_view.dart'; @@ -24,6 +25,7 @@ class RunsScreen extends StatefulWidget { this.onBack, this.attentionStore = const SharedPreferencesAttentionStore(), this.attentionNow, + this.notifications, }); final ApiGateway gateway; @@ -47,6 +49,9 @@ class RunsScreen extends StatefulWidget { final AttentionStore attentionStore; final DateTime Function()? attentionNow; + /// App-level notification delivery for live attention transitions. + final NotificationController? notifications; + @override State createState() => _RunsScreenState(); } @@ -128,6 +133,7 @@ class _RunsScreenState extends State { syncToken: widget.syncToken, attentionStore: widget.attentionStore, attentionNow: widget.attentionNow, + notifications: widget.notifications, ), ), ), diff --git a/app/lib/features/runs/ui/runs_list.dart b/app/lib/features/runs/ui/runs_list.dart index 2c5a2ab3..66b8dfb2 100644 --- a/app/lib/features/runs/ui/runs_list.dart +++ b/app/lib/features/runs/ui/runs_list.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../../../ui/adaptive/host.dart'; +import '../../attention/notify/notification_controller.dart'; import '../../attention/runtime/attention_store.dart'; import '../../attention/ui/needs_you_runs_heading.dart'; import '../domain/run_models.dart'; @@ -17,6 +18,7 @@ class RunsList extends StatelessWidget { this.syncToken = 0, this.attentionStore = const SharedPreferencesAttentionStore(), this.attentionNow, + this.notifications, }); final RunsController controller; @@ -32,6 +34,9 @@ class RunsList extends StatelessWidget { /// Deterministic local-day clock for the attention window. final DateTime Function()? attentionNow; + /// App-level notification delivery; absent in list-only fixtures. + final NotificationController? notifications; + @override Widget build(BuildContext context) { if (controller.loading && controller.runs.isEmpty) { @@ -58,6 +63,7 @@ class RunsList extends StatelessWidget { syncToken: syncToken, store: attentionStore, now: attentionNow, + notifications: notifications, ), ), ), diff --git a/app/lib/features/settings/settings_notifications_section.dart b/app/lib/features/settings/settings_notifications_section.dart new file mode 100644 index 00000000..7364c55b --- /dev/null +++ b/app/lib/features/settings/settings_notifications_section.dart @@ -0,0 +1,33 @@ +import 'package:flutter/widgets.dart'; + +import '../attention/notify/notification_controller.dart'; +import '../attention/ui/notification_permission_tile.dart'; + +/// The Settings entry for the browser notification permission. +/// +/// It reads the platform's own state from the app-level +/// [NotificationController] and asks only when the operator presses the tile's +/// Enable action. The controller is shared with the Runs attention surface, so +/// the permission shown here is the one that gates the live notices. +class SettingsNotificationsSection extends StatelessWidget { + const SettingsNotificationsSection({ + super.key, + required this.controller, + }); + + final NotificationController controller; + + @override + Widget build(BuildContext context) => AnimatedBuilder( + animation: controller, + builder: (context, _) => NotificationPermissionTile( + state: controller.permission, + asked: controller.asked, + onRequest: () { + // The platform answer updates the controller; the AnimationBuilder above + // redraws the tile with its own words. + controller.requestPermission(); + }, + ), + ); +} diff --git a/app/lib/features/settings/settings_screen.dart b/app/lib/features/settings/settings_screen.dart index 9984f77c..a9771cdf 100644 --- a/app/lib/features/settings/settings_screen.dart +++ b/app/lib/features/settings/settings_screen.dart @@ -4,10 +4,12 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../core/transport/pipelines_client.dart' as transport; +import '../attention/notify/notification_controller.dart'; import 'default_endpoint.dart'; import 'settings_controller.dart'; import 'settings_controls.dart'; import 'settings_layout.dart'; +import 'settings_notifications_section.dart'; import 'settings_sections.dart'; import 'settings_store.dart'; import 'settings_templates_section.dart'; @@ -29,6 +31,7 @@ class SettingsScreen extends StatefulWidget { this.ping, this.embedded = false, this.onOpenTemplates, + this.notifications, }); final transport.PipelinesClient client; @@ -50,6 +53,9 @@ class SettingsScreen extends StatefulWidget { /// without a dead row that would go nowhere. final VoidCallback? onOpenTemplates; + /// The app-level notification capability; absent in connection-only fixtures. + final NotificationController? notifications; + @override State createState() => _SettingsScreenState(); } @@ -127,6 +133,14 @@ class _SettingsScreenState extends State { controller: _controller, workspaceField: _workspaceField, ), + if (widget.notifications != null) ...[ + const SizedBox(height: settingsSectionGap), + const _SectionTitle('Notifications'), + const SizedBox(height: settingsTitleGap), + SettingsNotificationsSection( + controller: widget.notifications!, + ), + ], if (widget.onOpenTemplates != null) ...[ const SizedBox(height: settingsSectionGap), const _SectionTitle('Templates'), diff --git a/app/test/features/attention/notify/notification_manager_wiring_test.dart b/app/test/features/attention/notify/notification_manager_wiring_test.dart new file mode 100644 index 00000000..7ea23459 --- /dev/null +++ b/app/test/features/attention/notify/notification_manager_wiring_test.dart @@ -0,0 +1,96 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/attention/notify/notification_controller.dart'; +import 'package:pipelines_app/features/attention/notify/notification_store.dart'; +import 'package:pipelines_app/features/attention/ui/needs_you_runs_heading.dart'; +import 'package:pipelines_app/features/settings/settings_notifications_section.dart'; + +import 'notification_test_support.dart'; + +/// The manager-owned wiring: the bell surface publishes live transitions and +/// Settings renders the same controller's permission. +void main() { + testWidgets('a seed never notifies and one live transition notifies once', ( + tester, + ) async { + final bridge = FakeNotificationBridge(permission: 'granted'); + final notify = NotificationController( + store: MemoryNotificationStore(), + bridge: bridge, + ); + final frames = StreamController.broadcast(); + addTearDown(frames.close); + addTearDown(notify.dispose); + + await tester.pumpWidget( + MaterialApp( + home: NeedsYouRunsHeading( + rows: [runSummary(1, 'failed'), runSummary(2, 'running')], + frames: frames.stream, + onOpen: (_) {}, + now: () => attentionNow, + notifications: notify, + cupertino: false, + ), + ), + ); + // Let the persisted ledger read and the seed publish settle. A plain + // `Future.delayed` would deadlock inside the widget test's fake clock. + await tester.pump(); + await tester.pump(const Duration(milliseconds: 20)); + expect(bridge.shown, isEmpty, reason: 'the initial list is a seed'); + expect(notify.permission.name, 'granted'); + + frames.add(runFrame(2, 'failed')); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 20)); + expect(bridge.shown, hasLength(1)); + expect(bridge.shown.single.runId, 2); + + // The same transition instance again (a reconnect reseed of that frame) + // must not announce twice. + frames.add(runFrame(2, 'failed')); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 20)); + expect(bridge.shown, hasLength(1)); + }); + + testWidgets('Settings renders the platform permission from the controller', ( + tester, + ) async { + final notify = NotificationController( + store: MemoryNotificationStore(), + bridge: FakeNotificationBridge(permission: 'granted'), + ); + addTearDown(notify.dispose); + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SettingsNotificationsSection(controller: notify), + ), + ), + ); + await tester.pump(); + expect(find.text('Notifications'), findsWidgets); + + // A denied platform answer shows its own words and no Enable action. + final denied = NotificationController( + store: MemoryNotificationStore(), + bridge: FakeNotificationBridge(permission: 'denied'), + ); + addTearDown(denied.dispose); + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SettingsNotificationsSection(controller: denied), + ), + ), + ); + await tester.pump(); + expect(find.text('Notifications'), findsWidgets); + }); +}