Files
doormile_milderapp/lib/helpers/poll_policy.dart
2026-08-11 13:16:33 +05:30

40 lines
2.4 KiB
Dart

/// ───────────────────────────────────────────────────────────────────────────
/// HOW OFTEN THE APP ASKS THE SERVER ANYTHING
///
/// One policy, because the two screens that poll had invented their own and
/// neither was tied to what the rider was actually doing.
///
/// • Bookings polled every **3 seconds, always** — on duty, off duty, phone in
/// pocket, screen off, nothing running. Twelve hundred requests an hour for
/// a queue that changes a few times a shift.
/// • Home polled every 5s on duty and 20s off, which at least noticed duty,
/// but not whether there was any live work to watch.
///
/// Neither stopped when the app went to the background. A rider on duty with
/// the phone in his pocket was running a request every three seconds, all
/// shift, on his own battery and his own data pack. That is not a technical
/// detail — it is the app quietly costing him money and charge for nothing.
///
/// The rule now: **poll fast only while something is actually happening.**
///
/// live stop in progress → 5s (the rider is working it; staleness shows)
/// on duty, nothing live → 30s (waiting for the hub to assign)
/// off duty → 60s (nothing can arrive; this is just a heartbeat)
/// app backgrounded → stop (see [PollLifecycle])
///
/// A stop the rider is working is still watched at the old speed. Everything
/// else drops by a factor of ten.
/// ───────────────────────────────────────────────────────────────────────────
Duration pollInterval({required bool hasLiveWork, required bool onDuty}) {
if (hasLiveWork) return const Duration(seconds: 5);
if (onDuty) return const Duration(seconds: 30);
return const Duration(seconds: 60);
}
/// Polling must also stop when the app is not in front of the rider: the work
/// it does then can never be seen, because nobody is looking at the screen.
/// Both polling screens implement that in their own
/// `didChangeAppLifecycleState` — cancel on pause, restart plus one immediate
/// catch-up fetch on resume, so the rider never comes back to a screen showing
/// what was true a minute ago.