Files
plezy/lib/services/trackers/tracker_connect_runner.dart
edde746 4c8272d5b1 refactor(trackers): drive Trakt through the tracker coordinator
Trakt was the one service outside the tracker abstraction. TraktScrobbleService
re-implemented the whole playback lifecycle beside TrackerCoordinator, and
TraktSyncService pushed watched state from its own WatchStateNotifier
subscription, so the player called two objects at every lifecycle point and one
watch could be written twice. TraktTracker now implements RealtimeScrobbleTracker
like Simkl; the duplicated player call sites collapse to one each, and Trakt
shares the coordinator's ID resolver instead of re-fetching show ids every
episode.

Capabilities are split so a tracker declares what it is rather than being
special-cased: ScrobblePolicy carries each service's own resend/seek rules,
EpisodeHistoryTracker names the remote row a per-item history write targets, and
SeriesProgressTracker covers one-counter-per-series services. Writes from all
four trackers go through a shared TrackerWriteQueue, generalised from the
Trakt-only queue, with the legacy Trakt payload migrated on load. Trakt becomes
the fourth TrackersProvider slot and TraktAccountProvider is deleted, so one
object owns the active session per profile.

Two failure paths found while consolidating are fixed here too.

The queue's retries only ran on profile bind, connect and app foreground, so a
network blip mid-session left queued watches waiting for the next foreground.
OfflineModeProvider now notifies on connectivity changes, not just offline-state
or WiFi-flag changes, and main.dart flushes the queue when the network returns.

The queue also counted every failure toward the five attempts that permanently
drop an item, so a rate limit or a service having a bad hour could discard a
pending watch - the loss the queue exists to prevent. Only an answer about the
write itself now spends an attempt: 4xx counts, while rate limits, 5xx,
recoverable token-refresh failures and requests that never arrived do not. A
back-off answer also defers that service for the rest of the flush, so a queue
holding many rows does not fire all of them at a service that just asked for
quiet.
2026-07-30 14:51:32 +02:00

29 lines
967 B
Dart

import '../../utils/app_logger.dart';
/// Shared "authorize → enrich → save → assign" pipeline.
///
/// Callers manage the in-flight flag + `notifyListeners` around this call;
/// this helper only owns the inner steps so the `TrackersProvider` service
/// flows can't drift. Returns `true` only on a fully applied
/// session — null-from-authorize (cancel/denied/expired) and any exception
/// both surface as `false`.
Future<bool> runConnectPipeline<T>({
required String logLabel,
required Future<T?> Function() authorize,
required Future<T> Function(T raw) enrich,
required Future<void> Function(T enriched) save,
required void Function(T enriched) assign,
}) async {
try {
final raw = await authorize();
if (raw == null) return false;
final enriched = await enrich(raw);
await save(enriched);
assign(enriched);
return true;
} catch (e) {
appLogger.w('$logLabel connect failed', error: e);
return false;
}
}