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.
67 lines
2.4 KiB
Dart
67 lines
2.4 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
import '../../focus/input_mode_tracker.dart';
|
|
import '../../i18n/strings.g.dart';
|
|
import '../../utils/app_logger.dart';
|
|
import '../../utils/dialogs.dart';
|
|
import '../../utils/snackbar_helper.dart';
|
|
|
|
/// Shared "connect this tracker" launcher.
|
|
///
|
|
/// Handles the busy/already-connected guard, shows the service's code dialog
|
|
/// once `connect` hands us a payload, auto-launches the browser on pointer
|
|
/// platforms, closes the dialog when the flow resolves, and surfaces a failure
|
|
/// snack. Service-specific pieces are supplied via [connect], [buildDialog],
|
|
/// and [urlFor] so every `TrackersProvider`-backed flow shares one code path.
|
|
Future<void> launchTrackerConnect<T>(
|
|
BuildContext context, {
|
|
required bool isBusyOrConnected,
|
|
required String serviceName,
|
|
required Future<bool> Function(void Function(T)) connect,
|
|
required VoidCallback onCancel,
|
|
required Widget Function(T payload, VoidCallback onCancel) buildDialog,
|
|
required String Function(T payload) urlFor,
|
|
}) async {
|
|
if (isBusyOrConnected) return;
|
|
|
|
final autoLaunchBrowser = !InputModeTracker.isKeyboardMode(context);
|
|
var dialogOpen = false;
|
|
|
|
final ok = await connect((payload) {
|
|
if (!context.mounted) return;
|
|
dialogOpen = true;
|
|
showScopedDialog<void>(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (_) => buildDialog(payload, () {
|
|
// Flip synchronously so the post-await guard below is a no-op —
|
|
// `whenComplete` fires a microtask later and loses the race otherwise.
|
|
dialogOpen = false;
|
|
onCancel();
|
|
}),
|
|
).whenComplete(() => dialogOpen = false);
|
|
if (autoLaunchBrowser) {
|
|
unawaited(
|
|
launchUrl(Uri.parse(urlFor(payload)), mode: LaunchMode.externalApplication).catchError((Object e) {
|
|
appLogger.d('$serviceName: failed to auto-launch browser', error: e);
|
|
return false;
|
|
}),
|
|
);
|
|
}
|
|
});
|
|
|
|
if (!context.mounted) return;
|
|
// Close the dialog iff we showed one and it's still up (not already closed by
|
|
// the Cancel button). This is the ONLY site that dismisses the dialog —
|
|
// popping here and having the dialog self-pop would pop the screen behind.
|
|
if (dialogOpen) {
|
|
Navigator.of(context).pop();
|
|
}
|
|
if (!ok) {
|
|
showAppSnackBar(context, t.services.connectFailed(service: serviceName));
|
|
}
|
|
}
|