Files
pipelines/app/lib/features/attention/ui/needs_you_runs_heading.dart
T

178 lines
5.5 KiB
Dart

import 'dart:async';
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';
/// The `Needs you` bell as it appears in the Runs list heading.
///
/// It owns the live attention state for this destination: [rows] are the
/// already-loaded `list_runs` summaries and [frames] is the shared gateway's
/// unsolicited stream, so it never polls or issues an attention request. The
/// bell has no built-in host choice; the ambient app host (or [cupertino] in a
/// fixture) selects the native Material or Cupertino action and sheet.
class NeedsYouRunsHeading extends StatefulWidget {
const NeedsYouRunsHeading({
super.key,
required this.rows,
required this.frames,
required this.onOpen,
this.syncToken = 0,
this.store = const SharedPreferencesAttentionStore(),
this.now,
this.cupertino,
this.notifications,
this.attention,
});
/// The run summaries the Runs destination already holds.
final List<JsonMap> rows;
/// The shared gateway's frames; only `run_event` envelopes are folded.
final Stream<JsonMap> frames;
/// Opens an entry's run. A Failed entry is dismissed by the bell first.
final ValueChanged<AttentionEntry> onOpen;
/// Advances when the shared socket is replaced. The detector rebaselines
/// without replaying a transition that happened during the gap.
final int syncToken;
final AttentionStore store;
/// Injected clock for deterministic local-day windows.
final DateTime Function()? now;
/// 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;
/// The app's shared attention controller, so the shell can mirror the same
/// count on the Runs tab. The heading seeds it with [rows]; its owner folds
/// [frames] and resets it on reconnect. Absent, the heading owns a private
/// controller fed from [frames], which fixtures and the list tests rely on.
final AttentionController? attention;
@override
State<NeedsYouRunsHeading> createState() => _NeedsYouRunsHeadingState();
}
class _NeedsYouRunsHeadingState extends State<NeedsYouRunsHeading> {
late final AttentionController _attention =
widget.attention ??
AttentionController(store: widget.store, now: widget.now);
StreamSubscription<JsonMap>? _frames;
bool get _ownsController => widget.attention == null;
@override
void initState() {
super.initState();
_attention.seedRows(widget.rows);
if (_ownsController) {
unawaited(_attention.load());
_listen();
} else {
// The owner folds frames; publishing after every change it makes is the
// same idempotent call the private path makes after each frame.
_attention.addListener(_publish);
}
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((frame) {
_attention.observeFrame(frame);
_publish();
});
}
@override
void didUpdateWidget(covariant NeedsYouRunsHeading oldWidget) {
super.didUpdateWidget(oldWidget);
if (_ownsController && !identical(oldWidget.frames, widget.frames)) {
unawaited(_frames?.cancel());
_frames = null;
_listen();
}
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();
}
}
@override
void dispose() {
unawaited(_frames?.cancel());
if (_ownsController) {
_attention.dispose();
} else {
_attention.removeListener(_publish);
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _attention,
builder: (context, _) => NeedsYouBell(
entries: _attention.entries,
cupertino: widget.cupertino,
onOpen: widget.onOpen,
onDismiss: (entry) {
unawaited(_attention.dismiss(entry));
},
onClearAll: (snapshot) {
unawaited(_attention.clearAll(snapshot));
},
),
);
}
}