Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/features/earthquake/data/eew_replay_source.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:dpip/core/error/failure.dart';
import 'package:dpip/core/error/result.dart';
import 'package:dpip/core/network/api_exception.dart';
import 'package:dpip/core/realtime/realtime_source.dart';
Expand Down Expand Up @@ -35,7 +36,19 @@ class EewReplaySource extends RealtimeSource<List<Eew>> {
for (final e in all)
if (!e.isJma && (!onlyCwa || e.isCwa)) e,
];
});
}, shouldLog: (failure) => !isIgnorableFailure(failure));

/// A 404 is the archive saying "no record at that instant", which per the
/// endpoint's own contract covers both ends: a replay that has walked past
/// the retained range, and an event older than the first row the database
/// holds. Neither is a fault — the replay is running, there is just nothing
/// recorded there — and counting it as one would turn the whole tail of an
/// old event into a wall of retries. Inside the range the endpoint answers
/// 200 with `[]` for a second that simply carried no bulletin, so the two
/// cases stay distinguishable in the payload; for the card they mean the same
/// thing (nothing to show), and [_stickyAlerts] keeps the last real set.
@override
bool isIgnorableFailure(Failure failure) => failure is NotFoundFailure;

/// Null: same fetch-freshness rationale as the live [EewRealtimeSource] —
/// here doubly so, since the payload's own time is deliberately historical.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'package:dpip/core/settings/region_store.dart';
import 'package:dpip/core/settings/setting_keys.dart';
import 'package:dpip/core/settings/settings_store.dart';
import 'package:dpip/features/earthquake/domain/earthquake_report.dart';
import 'package:dpip/features/earthquake/replay_eew_probe.dart';
import 'package:dpip/shared/seismic/intensity.dart';
import 'package:dpip/features/earthquake/domain/report_repository.dart';
import 'package:dpip/shared/seismic/intensity_icon_renderer.dart';
Expand Down Expand Up @@ -356,7 +357,9 @@ class _ReportMapDetailState extends State<_ReportMapDetail> {
/// the event was never replayable.
Future<void> _probeReplay() async {
final repository = context.read<ReportRepository>();
final timestamp = widget.report.originTimeUtc.millisecondsSinceEpoch - 2000;
final timestamp =
widget.report.originTimeUtc.millisecondsSinceEpoch -
replayLeadIn.inMilliseconds;
final result = await repository.hasReplayAt(timestamp);
if (!mounted) return;
setState(() {
Expand Down Expand Up @@ -1096,8 +1099,11 @@ class _ReportHeader extends StatelessWidget {
}

void _openReplay(BuildContext context) {
// 2s lead-in before the report's origin time, matching the legacy replay.
final replayTimestamp = report.originTimeUtc.millisecondsSinceEpoch - 2000;
// [replayLeadIn] before the report's origin time — the replay page needs
// the same constant to recover the origin for its availability probe.
final replayTimestamp =
report.originTimeUtc.millisecondsSinceEpoch -
replayLeadIn.inMilliseconds;
context.pushNamed(
AppRoutes.earthquakeReplay,
pathParameters: {'id': report.id},
Expand Down
58 changes: 54 additions & 4 deletions lib/features/earthquake/presentation/pages/report_replay_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import 'package:dpip/core/realtime/realtime_service.dart';
import 'package:dpip/core/realtime/realtime_state.dart';
import 'package:dpip/core/realtime/replay_clock.dart';
import 'package:dpip/features/earthquake/domain/eew.dart';
import 'package:dpip/features/earthquake/replay_eew_probe.dart';
import 'package:dpip/features/earthquake/domain/eew_town_levels.dart';
import 'package:dpip/shared/seismic/spoken_intensity.dart';
import 'package:dpip/features/earthquake/domain/monitor_eew_announcement_controller.dart';
Expand All @@ -49,6 +50,7 @@ import 'package:dpip/features/earthquake/presentation/rts_realtime_controller.da
import 'package:dpip/features/earthquake/presentation/pages/report_list_page.dart'
show ReportListPage;
import 'package:dpip/features/earthquake/presentation/widgets/eew_card.dart';
import 'package:dpip/features/earthquake/presentation/widgets/replay_eew_notice.dart';
import 'package:dpip/shared/navigation/refresh_on_appear.dart';
import 'package:dpip/features/earthquake/replay_session.dart';
import 'package:dpip/l10n/gen/app_localizations.dart';
Expand Down Expand Up @@ -120,6 +122,21 @@ class _ReportReplayPageState extends State<ReportReplayPage> {
/// advances it through the alert set (modulo the count in the builder).
int _eewIndex = 0;

/// The last non-empty alert set the feed published.
///
/// `/api/v2/eq/eew/{sec}` answers with the bulletins *still in force* at that
/// second, not the ones issued that second: for the 2026-11-01 M5.6 event one
/// serial stayed in the set for about 90 seconds and the endpoint then went
/// back to `[]`. Driving the card straight off the poll therefore made it
/// vanish while the alert was still current, so the card holds the newest set
/// it has seen — a later empty answer means "nothing new", not "nothing".
List<Eew> _stickyAlerts = const [];

/// What the three samples said about EEW for this event, once they answer.
/// `null` until then, and `undecided` when a sample failed — a dead request
/// must never be read as "no data".
final ValueNotifier<ReplayEewVerdict?> _eewVerdict = ValueNotifier(null);

/// The app-wide immersive toggle, borrowed from the map tab for the length
/// of this route. The shell draws the bottom nav bar for every branch,
/// including this nested one, so a page-local flag could hide the replay's
Expand Down Expand Up @@ -167,6 +184,23 @@ class _ReportReplayPageState extends State<ReportReplayPage> {
cwaOnly: () => cwaOnly.enabled,
)..start();
_session.eew.addListener(_syncAnnouncement);
_session.eew.addListener(_captureAlerts);
// Started once, in parallel with the replay, and cached for the session:
// three instants of the event are enough to say "not detected", and asking
// again on every revisit would cost three requests for the same answer.
final origin = DateTime.fromMillisecondsSinceEpoch(
widget.replayTimestamp,
isUtc: true,
).add(replayLeadIn);
unawaited(
ReplayEewProbe(
context.read<ApiClient>(),
origin: origin,
cwaOnly: () => cwaOnly.enabled,
).run().then((verdict) {
if (mounted) _eewVerdict.value = verdict;
}),
);
_immersive = context.read<MapImmersiveController>()
..addListener(_onImmersiveChanged);
// A replay opens with its controls, whatever the map tab was left in.
Expand Down Expand Up @@ -274,6 +308,12 @@ class _ReportReplayPageState extends State<ReportReplayPage> {
/// The listener's last cached fix, for choosing which alert to speak.
geo.LatLng? _rankObserver;

/// Keeps the newest non-empty alert set — see [_stickyAlerts].
void _captureAlerts() {
final alerts = _session.eew.alerts;
if (alerts.isNotEmpty) _stickyAlerts = alerts;
}

void _syncAnnouncement() {
final controller = _announcement;
final l10n = _l10n;
Expand All @@ -296,6 +336,8 @@ class _ReportReplayPageState extends State<ReportReplayPage> {
_ticker?.cancel();
_tick.dispose();
_session.eew.removeListener(_syncAnnouncement);
_session.eew.removeListener(_captureAlerts);
_eewVerdict.dispose();
_speechSettings?.removeListener(_syncAnnouncement);
_announcement?.dispose();
_clockSecond.dispose();
Expand Down Expand Up @@ -444,11 +486,19 @@ class _ReportReplayPageState extends State<ReportReplayPage> {
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ListenableBuilder(
listenable: _session.eew,
listenable: Listenable.merge([
_session.eew,
_eewVerdict,
]),
builder: (context, _) {
final alerts = _session.eew.alerts;
if (alerts.isEmpty) {
return const SizedBox.shrink();
final alerts = _stickyAlerts;
final slot = replayEewSlot(
hasAlerts: alerts.isNotEmpty,
feed: _session.eew.state,
verdict: _eewVerdict.value,
);
if (slot != ReplayEewSlot.card) {
return ReplayEewNotice(slot: slot);
}
// One card at a time — tapping cycles through the
// active alerts (parallel earthquakes, overlapping
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import 'package:dpip/app/theme/app_radius.dart';
import 'package:dpip/app/theme/app_spacing.dart';
import 'package:dpip/features/earthquake/replay_eew_probe.dart';
import 'package:dpip/l10n/gen/app_localizations.dart';
import 'package:flutter/material.dart';

/// The line the replay shows on the EEW slot while there is no card to show.
///
/// Four different things can be true at that moment and they must not look
/// alike: nothing has answered yet, the feed is failing, the feed is down, or
/// the event was sampled and nothing turned up. Only the last one is a verdict;
/// the other three are states of the connection, so the wording never borrows
/// "there is no EEW" for them.
class ReplayEewNotice extends StatelessWidget {
const ReplayEewNotice({super.key, required this.slot});

/// The slot to draw. [ReplayEewSlot.card] and [ReplayEewSlot.pending] draw
/// nothing, so the caller can pass whatever it decided without branching.
final ReplayEewSlot slot;

@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
final (IconData icon, String message, String? detail) = switch (slot) {
ReplayEewSlot.waiting => (
Icons.hourglass_empty,
l10n.replayEewWaiting,
null,
),
ReplayEewSlot.retrying => (
Icons.sync_problem,
l10n.replayEewRetrying,
null,
),
ReplayEewSlot.unavailable => (
Icons.cloud_off,
l10n.replayEewUnavailable,
null,
),
ReplayEewSlot.none => (
Icons.search_off,
l10n.replayEewNone,
l10n.replayEewNoneDetail,
),
ReplayEewSlot.card ||
ReplayEewSlot.pending => (Icons.hourglass_empty, '', null),
};
if (message.isEmpty) return const SizedBox.shrink();

final theme = Theme.of(context);
final colors = theme.colorScheme;
final muted = colors.onSurfaceVariant;
return Container(
padding: const EdgeInsets.symmetric(
horizontal: AppSpacing.md,
vertical: AppSpacing.sm,
),
decoration: BoxDecoration(
color: colors.surface.withValues(alpha: 0.94),
borderRadius: AppRadius.medium,
boxShadow: [
BoxShadow(
color: Colors.black.withValues(alpha: 0.12),
blurRadius: 12,
),
],
),
child: Row(
children: [
Icon(icon, size: 16, color: muted),
const SizedBox(width: AppSpacing.sm),
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
message,
style: theme.textTheme.labelLarge?.copyWith(color: muted),
),
if (detail != null)
Text(
detail,
style: theme.textTheme.labelSmall?.copyWith(
color: muted.withValues(alpha: 0.8),
),
),
],
),
),
],
),
);
}
}
Loading
Loading