Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Use less battery while you are reading an ebook

Barnabasbe7e50379fc4
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dart
index bf863820..9fce5c89 100644
--- a/lib/providers/_lp_core.dart
+++ b/lib/providers/_lp_core.dart
@@ -1112,7 +1112,7 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
void _startServerPingTimer() {
_serverPingTimer?.cancel();
- if (_isBackgrounded) return;
+ if (_isBackgrounded || _readerQuiet) return;
final serverUrl = _auth?.serverUrl;
if (serverUrl == null) return;
debugPrint('[Library] Starting server ping timer');
@@ -1172,8 +1172,9 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
_stopLocalProbeTimer();
return;
}
- // Don't run when truly idle in the background (battery).
- if (_isBackgrounded && !AudioPlayerService().isPlaying) {
+ // Don't run when truly idle in the background, or while reading (battery).
+ // Playback still needs it either way.
+ if ((_isBackgrounded || _readerQuiet) && !AudioPlayerService().isPlaying) {
_stopLocalProbeTimer();
return;
}
@@ -1251,7 +1252,7 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
void _startHealthCheckTimer() {
_healthCheckTimer?.cancel();
- if (_isBackgrounded) return;
+ if (_isBackgrounded || _readerQuiet) return;
debugPrint('[Library] Health check timer started (60s ping while online)');
_healthCheckTimer = Timer.periodic(const Duration(seconds: 60), (_) async {
if (_networkOffline || _manualOffline || !_deviceHasConnectivity) return;
@@ -1296,6 +1297,43 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
// still tears the socket down.
void onAppForegrounded() {
_isBackgrounded = false;
+ // Coming back to a book that's still open: stay quiet, the reader will say
+ // when it closes.
+ if (_readerQuiet) {
+ debugPrint('[Library] Foregrounded into the reader - staying quiet');
+ return;
+ }
+ _resumeLiveWork(quietSince: _backgroundedAt);
+ }
+
+ /// The ebook reader keeps the app foregrounded with nothing on screen that
+ /// needs live data, so while it's open the app drops to its background
+ /// behavior: no socket, no polling. Page turns still push progress over
+ /// HTTP, and playback keeps its own work running.
+ void setReaderQuiet(bool quiet) {
+ if (_readerQuiet == quiet) return;
+ _readerQuiet = quiet;
+ if (quiet) {
+ _readerQuietAt = DateTime.now();
+ debugPrint('[Library] Reader open - quieting live work');
+ _stopServerPingTimer();
+ _stopHealthCheckTimer();
+ if (!AudioPlayerService().isPlaying) _stopLocalProbeTimer();
+ _softDisconnectSocket();
+ return;
+ }
+ final since = _readerQuietAt;
+ _readerQuietAt = null;
+ debugPrint('[Library] Reader closed - waking live work');
+ // Backgrounded while reading: the background handler owns the wake-up.
+ if (_isBackgrounded) return;
+ _resumeLiveWork(quietSince: since);
+ }
+
+ /// Bring back the socket and the polling this app does while it's visible
+ /// and awake. [quietSince] is when live work stopped, so a long gap can
+ /// replay what the socket would have delivered.
+ void _resumeLiveWork({DateTime? quietSince}) {
_softReconnectSocket();
if (_networkOffline && _deviceHasConnectivity && !_manualOffline) {
_startServerPingTimer();
@@ -1305,13 +1343,12 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
_startLocalProbeTimer();
if (!isOffline && !_manualOffline) {
(this as LibraryProvider).checkSubscribedPodcasts();
- // Item events emitted while backgrounded were missed (the socket was
- // down) and are never replayed, so anything edited from another device
- // would stay stale forever. Skip quick app switches - a fresh socket
- // has nothing to have missed.
- final away = _backgroundedAt == null
+ // Item events emitted while the socket was down are never replayed, so
+ // anything edited from another device would stay stale forever. Skip
+ // quick gaps - a fresh socket has nothing to have missed.
+ final away = quietSince == null
? null
- : DateTime.now().difference(_backgroundedAt!);
+ : DateTime.now().difference(quietSince);
if (away != null && away > const Duration(seconds: 30)) {
_catchUpAfterBackground();
}
@@ -1351,14 +1388,14 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
}
void onPlaybackStarted() {
- if (!_isBackgrounded) {
+ if (!_isBackgrounded && !_readerQuiet) {
_softReconnectSocket();
}
_startLocalProbeTimer();
}
void onPlaybackStopped() {
- if (_isBackgrounded) {
+ if (_isBackgrounded || _readerQuiet) {
_softDisconnectSocket();
_stopLocalProbeTimer();
}
diff --git a/lib/providers/_lp_state.dart b/lib/providers/_lp_state.dart
index c449b949..4ce3ab0e 100644
--- a/lib/providers/_lp_state.dart
+++ b/lib/providers/_lp_state.dart
@@ -89,6 +89,11 @@ mixin _StateMixin on ChangeNotifier {
DateTime? _localLastReachableAt;
bool _isBackgrounded = false;
+ // The ebook reader holds the app in the foreground for long stretches with
+ // nothing on screen that needs live data, so it asks for the same quiet the
+ // app keeps while backgrounded.
+ bool _readerQuiet = false;
+ DateTime? _readerQuietAt;
bool _socketSoftDisconnected = false;
DateTime? _backgroundedAt;
diff --git a/lib/widgets/ebook_reader_view.dart b/lib/widgets/ebook_reader_view.dart
index a623fb71..3b26a071 100644
--- a/lib/widgets/ebook_reader_view.dart
+++ b/lib/widgets/ebook_reader_view.dart
@@ -167,12 +167,16 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
String _audioAuthor = '';
String? _audioCoverUrl;
bool _startingAudio = false;
+ // Held from initState so dispose can lift the quiet without a context read.
+ late final LibraryProvider _quietLib;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_epubController = EpubController();
+ _quietLib = context.read<LibraryProvider>();
+ _quietLib.setReaderQuiet(true);
_loadInitialLocation();
// A settings read failure must not stop the book from opening - fall back
// to the field defaults and open anyway.
@@ -468,6 +472,7 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
@override
void dispose() {
+ _quietLib.setReaderQuiet(false);
WidgetsBinding.instance.removeObserver(this);
_volumeNav.detach();
_routeAnim?.removeStatusListener(_onRouteAnim);
diff --git a/lib/widgets/foliate_reader_view.dart b/lib/widgets/foliate_reader_view.dart
index c4f9b711..b7ce2cca 100644
--- a/lib/widgets/foliate_reader_view.dart
+++ b/lib/widgets/foliate_reader_view.dart
@@ -89,6 +89,7 @@ class _FoliateReaderViewState extends State<FoliateReaderView> with WidgetsBindi
_setFullscreen(true);
_api = context.read<AuthProvider>().apiService;
_lib = context.read<LibraryProvider>();
+ _lib?.setReaderQuiet(true);
_loadInitialCfi();
_prepare();
_volumeNav.attach();
@@ -107,6 +108,7 @@ class _FoliateReaderViewState extends State<FoliateReaderView> with WidgetsBindi
@override
void dispose() {
+ _lib?.setReaderQuiet(false);
WidgetsBinding.instance.removeObserver(this);
_volumeNav.detach();
_setFullscreen(false);
diff --git a/lib/widgets/pdf_reader_view.dart b/lib/widgets/pdf_reader_view.dart
index c752a43b..44f756aa 100644
--- a/lib/widgets/pdf_reader_view.dart
+++ b/lib/widgets/pdf_reader_view.dart
@@ -57,6 +57,7 @@ class _PdfReaderViewState extends State<PdfReaderView> with WidgetsBindingObserv
_setFullscreen(true);
_api = context.read<AuthProvider>().apiService;
_lib = context.read<LibraryProvider>();
+ _lib?.setReaderQuiet(true);
_loadInitialPage();
_open();
_volumeNav.attach();
@@ -81,6 +82,7 @@ class _PdfReaderViewState extends State<PdfReaderView> with WidgetsBindingObserv
@override
void dispose() {
+ _lib?.setReaderQuiet(false);
WidgetsBinding.instance.removeObserver(this);
_volumeNav.detach();
_setFullscreen(false);