A cross platform Audiobookshelf client for Android and iOS
Dart
Fix Chromecast listening time tracking, reliable server version display
Nathan Poulson5bc85d8f2857
diff --git a/lib/providers/auth_provider.dart b/lib/providers/auth_provider.dartindex 8c162d5e..157749d9 100644--- a/lib/providers/auth_provider.dart+++ b/lib/providers/auth_provider.dart@@ -199,8 +199,14 @@ class AuthProvider extends ChangeNotifier {_serverSettings = result['serverSettings'] as Map<String, dynamic>?;_customHeaders = customHeaders;- // Fetch server version from /status endpoint (fire and forget)- _fetchServerVersion(url);+ // Try to get version from login response first, fall back to /status+ final loginVersion = result['serverVersion'] as String?+ ?? (_serverSettings?['version'] as String?);+ if (loginVersion != null && loginVersion.isNotEmpty) {+ _serverVersion = loginVersion;+ } else {+ _fetchServerVersion(url);+ }// Persist sessiontry {@@ -262,8 +268,14 @@ class AuthProvider extends ChangeNotifier {_serverSettings = result['serverSettings'] as Map<String, dynamic>?;_serverReachable = true;- // Fetch server version- _fetchServerVersion(url);+ // Try to get version from response first, fall back to /status+ final oidcVersion = result['serverVersion'] as String?+ ?? (_serverSettings?['version'] as String?);+ if (oidcVersion != null && oidcVersion.isNotEmpty) {+ _serverVersion = oidcVersion;+ } else {+ _fetchServerVersion(url);+ }// Persist sessiontry {diff --git a/lib/services/chromecast_service.dart b/lib/services/chromecast_service.dartindex c012d4a1..41e31d91 100644--- a/lib/services/chromecast_service.dart+++ b/lib/services/chromecast_service.dart@@ -31,6 +31,8 @@ class ChromecastService extends ChangeNotifier {ApiService? _api;Duration _castPosition = Duration.zero;String? _connectedDeviceName;+ String? _playbackSessionId;+ DateTime _lastSyncTime = DateTime.now();// Multi-track fallback state (when queueLoadItems fails)List<dynamic>? _fallbackTracks;@@ -260,6 +262,10 @@ class ChromecastService extends ChangeNotifier {await _saveProgressLocal();await _syncProgressToServer();}+ if (_playbackSessionId != null && _api != null) {+ try { await _api!.closePlaybackSession(_playbackSessionId!); } catch (_) {}+ _playbackSessionId = null;+ }try {await GoogleCastSessionManager.instance.endSessionAndStopCasting();} catch (e) { debugPrint('[Cast] Disconnect error: $e'); }@@ -337,8 +343,8 @@ class ChromecastService extends ChangeNotifier {_playbackState = CastPlaybackState.idle; notifyListeners(); return false;}- final sid = sessionData['id'] as String?;- if (sid != null) try { await api.closePlaybackSession(sid); } catch (_) {}+ _playbackSessionId = sessionData['id'] as String?;+ _lastSyncTime = DateTime.now();// Load per-book speed (or global default)final bookSpeed = await PlayerSettings.getBookSpeed(itemId);@@ -640,6 +646,12 @@ class ChromecastService extends ChangeNotifier {try { await GoogleCastRemoteMediaClient.instance.stop(); } catch (_) {}+ // Close the playback session so stats are finalized+ if (_playbackSessionId != null && _api != null) {+ try { await _api!.closePlaybackSession(_playbackSessionId!); } catch (_) {}+ _playbackSessionId = null;+ }+_playbackState = CastPlaybackState.idle;_castingItemId = _castingEpisodeId = _castingTitle = _castingAuthor = _castingCoverUrl = null;_castingDuration = 0; _castingChapters = [];@@ -697,8 +709,13 @@ class ChromecastService extends ChangeNotifier {_onBookFinishedCallback?.call(key);}- // Clear casting state but keep connection+ // Close playback session+ if (_playbackSessionId != null && _api != null) {+ try { await _api!.closePlaybackSession(_playbackSessionId!); } catch (_) {}+ _playbackSessionId = null;+ }+ // Clear casting state but keep connection_syncTimer?.cancel();_castingItemId = _castingEpisodeId = _castingTitle = _castingAuthor = _castingCoverUrl = null;_castingDuration = 0;@@ -721,10 +738,25 @@ class ChromecastService extends ChangeNotifier {final ct = _castPosition.inMilliseconds / 1000.0;if (ct <= 0) return;try {- final progressId = _castingEpisodeId != null- ? '$_castingItemId-$_castingEpisodeId'- : _castingItemId!;- await _api!.updateProgress(progressId, currentTime: ct, duration: _castingDuration);+ final now = DateTime.now();+ final elapsed = now.difference(_lastSyncTime).inSeconds.clamp(0, 300);+ _lastSyncTime = now;++ if (_playbackSessionId != null) {+ // Sync via playback session so timeListened is tracked in stats+ await _api!.syncPlaybackSession(+ _playbackSessionId!,+ currentTime: ct,+ duration: _castingDuration,+ timeListened: elapsed,+ );+ } else {+ // Fallback to progress update if no session+ final progressId = _castingEpisodeId != null+ ? '$_castingItemId-$_castingEpisodeId'+ : _castingItemId!;+ await _api!.updateProgress(progressId, currentTime: ct, duration: _castingDuration);+ }} catch (e) {debugPrint('[Cast] Server sync error: $e');}