Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Skip pre-play server call when AuthProvider already knows we're offline

Barnabas52eb08168af4
diff --git a/lib/providers/auth_provider.dart b/lib/providers/auth_provider.dart
index 6a594cd9..3601ba39 100644
--- a/lib/providers/auth_provider.dart
+++ b/lib/providers/auth_provider.dart
@@ -25,7 +25,17 @@ class AuthProvider extends ChangeNotifier {
Map<String, dynamic>? _userJson;
Map<String, dynamic>? _serverSettings;
String? _serverVersion;
- bool _serverReachable = true;
+ bool __serverReachable = true;
+ bool get _serverReachable => __serverReachable;
+ set _serverReachable(bool value) {
+ if (__serverReachable == value) return;
+ __serverReachable = value;
+ // Mirror the offline state into AudioPlayerService so it can short-
+ // circuit pre-play server calls (e.g. session creation) without
+ // waiting on a network timeout. Lets downloaded books start playing
+ // instantly when we already know we're offline.
+ AudioPlayerService().setKnownOffline(!value);
+ }
Map<String, String> _customHeaders = {};
// Local server auto-switch
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 0fa974a6..4d71e825 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -795,6 +795,17 @@ class AudioPlayerService extends ChangeNotifier {
List<String> get activeStreamUrls => _activeStreamUrls;
Map<String, String> get activeStreamHeaders => _activeStreamHeaders;
bool _isOfflineMode = false;
+
+ /// Externally-pushed signal that the server is currently unreachable.
+ /// AuthProvider mirrors `serverReachable` here on each ping result so we
+ /// can short-circuit pre-play server calls (e.g. session creation) for
+ /// instant offline playback - no need to wait for a network timeout to
+ /// discover what the auth layer already knows.
+ bool _knownOffline = false;
+ void setKnownOffline(bool offline) {
+ _knownOffline = offline;
+ }
+
bool _isBackgrounded = false;
bool get isBackgrounded => _isBackgrounded;
SharedPreferences? _prefs;
@@ -1663,14 +1674,12 @@ class AudioPlayerService extends ChangeNotifier {
final manualOffline = prefs.getBool('manual_offline_mode') ?? false;
debugPrint('[Player] manualOffline=$manualOffline, api=${_api != null}');
- // Try to start a server session for sync (unless manual offline).
- // Cap the call at 5s so a silently-dropping reverse proxy doesn't
- // hold up local playback for the full 20s api-level timeout. On a
- // healthy server the response comes back in under 1s; on truly dead
- // networks the kernel returns ECONNREFUSED in milliseconds; only
- // the "host accepts but never replies" case takes the full window
- // and that's the case we're shortening.
- if (_api != null && !manualOffline) {
+ // Try to start a server session for sync. Skip entirely if the user
+ // is in manual offline mode OR if AuthProvider has told us the server
+ // is unreachable (skip = instant local playback). Otherwise the call
+ // is capped at 5s so a silently-dropping reverse proxy doesn't hold
+ // up local playback for the full 20s api-level timeout.
+ if (_api != null && !manualOffline && !_knownOffline) {
try {
debugPrint('[Player] Starting server session for local playback...');
final sessionData = await (_currentEpisodeId != null