A cross platform Audiobookshelf client for Android and iOS
Dart
Fix cast progress bars not updating live and app dying in background
Nathan Poulsonc990e6f93b3d
diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dartindex 69d93ad0..6f23bf7e 100644--- a/lib/providers/library_provider.dart+++ b/lib/providers/library_provider.dart@@ -508,6 +508,13 @@ class LibraryProvider extends ChangeNotifier {}});ChromecastService.setOnBookFinishedCallback(markFinishedLocally);+ ChromecastService.setOnPlaybackStateChangedCallback((playing) {+ if (playing) {+ onPlaybackStarted();+ } else {+ onPlaybackStopped();+ }+ });restoreOfflineMode().then((_) async {debugPrint(diff --git a/lib/services/chromecast_service.dart b/lib/services/chromecast_service.dartindex 3c37041f..f55e348f 100644--- a/lib/services/chromecast_service.dart+++ b/lib/services/chromecast_service.dart@@ -2,6 +2,7 @@ import 'dart:async';import 'dart:io';import 'package:flutter/material.dart';import 'package:flutter_chrome_cast/flutter_chrome_cast.dart';+import 'package:wakelock_plus/wakelock_plus.dart';import 'api_service.dart';import 'audio_player_service.dart';import 'progress_sync_service.dart';@@ -52,6 +53,13 @@ class ChromecastService extends ChangeNotifier {_onBookFinishedCallback = cb;}+ /// Called when cast playback state changes (playing/not playing).+ /// Used by LibraryProvider for idle timer / battery-saving lifecycle.+ static void Function(bool isPlaying)? _onPlaybackStateChangedCallback;+ static void setOnPlaybackStateChangedCallback(void Function(bool isPlaying)? cb) {+ _onPlaybackStateChangedCallback = cb;+ }+StreamSubscription? _sessionSub, _mediaStatusSub, _positionSub;Timer? _syncTimer;final _progressSync = ProgressSyncService();@@ -136,6 +144,8 @@ class ChromecastService extends ChangeNotifier {_mediaStatusSub?.cancel();_positionSub?.cancel();_syncTimer?.cancel();+ _updateWakeLock(false);+ _onPlaybackStateChangedCallback?.call(false);notifyListeners();}@@ -156,6 +166,14 @@ class ChromecastService extends ChangeNotifier {}}+ // Notify listeners & manage wake lock on playback state transitions+ final wasPlaying = prev == CastPlaybackState.playing || prev == CastPlaybackState.buffering;+ final nowPlaying = _playbackState == CastPlaybackState.playing || _playbackState == CastPlaybackState.buffering;+ if (wasPlaying != nowPlaying) {+ _onPlaybackStateChangedCallback?.call(nowPlaying);+ _updateWakeLock(nowPlaying);+ }+// Detect playback completion: was playing/buffering → now idleif (_playbackState == CastPlaybackState.idle &&(prev == CastPlaybackState.playing || prev == CastPlaybackState.buffering) &&@@ -601,6 +619,8 @@ class ChromecastService extends ChangeNotifier {_castingItemId = _castingEpisodeId = _castingTitle = _castingAuthor = _castingCoverUrl = null;_castingDuration = 0; _castingChapters = [];_fallbackTracks = null; _fallbackOffsets = null; _fallbackTrackIdx = -1;+ _updateWakeLock(false);+ _onPlaybackStateChangedCallback?.call(false);notifyListeners();}@@ -720,6 +740,18 @@ class ChromecastService extends ChangeNotifier {await seekTo(Duration.zero);}+ // ── Wake Lock ──++ void _updateWakeLock(bool active) {+ if (active) {+ WakelockPlus.enable();+ debugPrint('[Cast] Wake lock enabled');+ } else {+ WakelockPlus.disable();+ debugPrint('[Cast] Wake lock disabled');+ }+ }+@overridevoid dispose() { _sessionSub?.cancel(); _mediaStatusSub?.cancel(); _positionSub?.cancel(); _syncTimer?.cancel(); super.dispose(); }}diff --git a/lib/widgets/card_progress_bar.dart b/lib/widgets/card_progress_bar.dartindex 2e895786..30bcd9b6 100644--- a/lib/widgets/card_progress_bar.dart+++ b/lib/widgets/card_progress_bar.dart@@ -75,7 +75,7 @@ class _CardDualProgressBarState extends State<CardDualProgressBar> with TickerPr}void _syncTicker() {- if (_isPlaying && widget.isActive) {+ if (_isPlaying && (widget.isActive || _isCastMode)) {if (!_smoothTicker.isAnimating) _smoothTicker.repeat();} else {if (_smoothTicker.isAnimating) _smoothTicker.stop();diff --git a/pubspec.lock b/pubspec.lockindex 0f472be2..2ad6c5e4 100644--- a/pubspec.lock+++ b/pubspec.lock@@ -1113,6 +1113,22 @@ packages:url: "https://pub.dev"source: hostedversion: "15.0.2"+ wakelock_plus:+ dependency: "direct main"+ description:+ name: wakelock_plus+ sha256: "61713aa82b7f85c21c9f4cd0a148abd75f38a74ec645fcb1e446f882c82fd09b"+ url: "https://pub.dev"+ source: hosted+ version: "1.3.3"+ wakelock_plus_platform_interface:+ dependency: transitive+ description:+ name: wakelock_plus_platform_interface+ sha256: "24b84143787220a403491c2e5de0877fbbb87baf3f0b18a2a988973863db4b03"+ url: "https://pub.dev"+ source: hosted+ version: "1.4.0"web:dependency: transitivedescription:diff --git a/pubspec.yaml b/pubspec.yamlindex 5ce29a43..c8a734ae 100644--- a/pubspec.yaml+++ b/pubspec.yaml@@ -80,6 +80,7 @@ dependencies:vibration: ^3.1.8webview_flutter: ^4.13.1flutter_markdown_plus: ^1.0.7+ wakelock_plus: ^1.3.3dev_dependencies:flutter_test: