A cross platform Audiobookshelf client for Android and iOS
Dart
Fix local server URL not tried on startup when remote is unreachable
Nathan Poulson9f1cb7464db1
diff --git a/lib/providers/auth_provider.dart b/lib/providers/auth_provider.dartindex cb38fb99..8c162d5e 100644--- a/lib/providers/auth_provider.dart+++ b/lib/providers/auth_provider.dart@@ -102,15 +102,27 @@ class AuthProvider extends ChangeNotifier {// Check if server is actually reachabledebugPrint('[Auth] pinging server... (${sw.elapsedMilliseconds}ms)');- final reachable = await ApiService.pingServer(savedUrl, customHeaders: _customHeaders);- _serverReachable = reachable;+ var reachable = await ApiService.pingServer(savedUrl, customHeaders: _customHeaders);debugPrint('[Auth] ping result: reachable=$reachable (${sw.elapsedMilliseconds}ms)');+ // If remote URL failed and local server is enabled, try local URL+ if (!reachable && _localServerEnabled && _localServerUrl.isNotEmpty) {+ debugPrint('[Auth] Remote unreachable, trying local server... (${sw.elapsedMilliseconds}ms)');+ final localReachable = await ApiService.pingServer(_localServerUrl, customHeaders: _customHeaders)+ .timeout(const Duration(seconds: 3), onTimeout: () => false);+ if (localReachable) {+ debugPrint('[Auth] Local server reachable - switching (${sw.elapsedMilliseconds}ms)');+ _useLocalServer = true;+ reachable = true;+ }+ }+ _serverReachable = reachable;+// Fetch full user info (needed for isAdmin, permissions, etc.)if (reachable) {try {debugPrint('[Auth] fetching /me... (${sw.elapsedMilliseconds}ms)');- final api = ApiService(baseUrl: savedUrl, token: savedToken, customHeaders: _customHeaders);+ final api = ApiService(baseUrl: activeServerUrl!, token: savedToken, customHeaders: _customHeaders);final me = await api.getMe();if (me != null) {_userJson = me;@@ -295,6 +307,7 @@ class AuthProvider extends ChangeNotifier {final reachable = await ApiService.pingServer(_localServerUrl, customHeaders: _customHeaders).timeout(const Duration(seconds: 3), onTimeout: () => false);_useLocalServer = reachable;+ if (reachable) _serverReachable = true;} catch (_) {_useLocalServer = false;}diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dartindex 6f23bf7e..2f0e48e5 100644--- a/lib/providers/library_provider.dart+++ b/lib/providers/library_provider.dart@@ -596,8 +596,19 @@ class LibraryProvider extends ChangeNotifier {_stopServerPingTimer();setNetworkOffline(true);} else if (_networkOffline && !_manualOffline) {- // Device has connectivity but we're still offline — server was unreachable- _startServerPingTimer();+ // Device has connectivity but we're still offline — server was unreachable.+ // If on WiFi, try local server first before falling back to ping timer.+ if (result.contains(ConnectivityResult.wifi)) {+ _auth?.checkLocalServer().then((_) {+ if (_auth?.serverReachable == true || _auth?.useLocalServer == true) {+ setNetworkOffline(false);+ } else {+ _startServerPingTimer();+ }+ });+ } else {+ _startServerPingTimer();+ }}});// Then listen for changes@@ -639,6 +650,21 @@ class LibraryProvider extends ChangeNotifier {_stopServerPingTimer();return;}+ // Try local server first if enabled and on WiFi+ final auth = _auth;+ if (auth != null && auth.localServerEnabled && auth.localServerUrl.isNotEmpty) {+ final localReachable = await ApiService.pingServer(+ auth.localServerUrl,+ customHeaders: auth.customHeaders,+ ).timeout(const Duration(seconds: 3), onTimeout: () => false);+ if (localReachable) {+ debugPrint('[Library] Local server ping succeeded — going online');+ await auth.checkLocalServer();+ _stopServerPingTimer();+ setNetworkOffline(false);+ return;+ }+ }final reachable = await ApiService.pingServer(serverUrl,customHeaders: _auth?.customHeaders ?? {},