Files
plezy/lib/models/trackers/tracker_context.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

104 lines
3.4 KiB
Dart

import '../../utils/external_ids.dart';
import 'anime_ids.dart';
/// Immutable per-playback context passed from the coordinator to each
/// tracker. Built once at `startPlayback`.
///
/// Carries both Plex external IDs (tvdb/tmdb/imdb, always present when the
/// item has any GUIDs) and Fribb-derived anime IDs (null when the item isn't
/// in the Fribb mapping). General-purpose trackers (Simkl) prefer Plex IDs;
/// anime-only trackers (MAL, AniList) no-op when [anime] is null.
class TrackerContext {
final ExternalIds external;
final AnimeIds? anime;
final bool isMovie;
final int? season;
final int? episodeNumber;
final int? animeProgress;
/// Plex ratingKey of the item being played. Used only for logging — not
/// sent to any tracker.
final String ratingKey;
/// Library globalKey the item belongs to, or null when the metadata didn't
/// carry library info.
final String? libraryGlobalKey;
const TrackerContext._({
required this.external,
required this.anime,
required this.isMovie,
required this.ratingKey,
required this.libraryGlobalKey,
this.season,
this.episodeNumber,
this.animeProgress,
});
factory TrackerContext.movie({
required ExternalIds external,
required AnimeIds? anime,
required String ratingKey,
required String? libraryGlobalKey,
}) {
return TrackerContext._(
external: external,
anime: anime,
isMovie: true,
ratingKey: ratingKey,
libraryGlobalKey: libraryGlobalKey,
);
}
factory TrackerContext.episode({
required ExternalIds external,
required AnimeIds? anime,
required String ratingKey,
required String? libraryGlobalKey,
required int season,
required int episodeNumber,
int? animeProgress,
}) {
return TrackerContext._(
external: external,
anime: anime,
isMovie: false,
ratingKey: ratingKey,
libraryGlobalKey: libraryGlobalKey,
season: season,
episodeNumber: episodeNumber,
animeProgress: animeProgress,
);
}
/// Serialized into the persisted tracker write queue so a failed watched
/// write replays against exactly the item it was built for — no second
/// metadata fetch, no re-resolution against a library that may have changed.
Map<String, Object?> toJson() => {
'external': external.toJson(),
if (anime != null) 'anime': anime!.toJson(),
'isMovie': isMovie,
'ratingKey': ratingKey,
if (libraryGlobalKey != null) 'libraryGlobalKey': libraryGlobalKey,
if (season != null) 'season': season,
if (episodeNumber != null) 'episodeNumber': episodeNumber,
if (animeProgress != null) 'animeProgress': animeProgress,
};
/// Throws on a malformed row; the queue archives and discards the batch.
factory TrackerContext.fromJson(Map<String, Object?> json) {
final anime = json['anime'];
return TrackerContext._(
external: ExternalIds.fromJson((json['external'] as Map).cast<String, Object?>()),
anime: anime == null ? null : AnimeIds.fromJson((anime as Map).cast<String, Object?>()),
isMovie: json['isMovie'] as bool,
ratingKey: json['ratingKey'] as String,
libraryGlobalKey: json['libraryGlobalKey'] as String?,
season: (json['season'] as num?)?.toInt(),
episodeNumber: (json['episodeNumber'] as num?)?.toInt(),
animeProgress: (json['animeProgress'] as num?)?.toInt(),
);
}
}