mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2025-12-23 11:29:30 +00:00
cancel timer when paused to reduce resource utilisation
This commit is contained in:
parent
ec8304fdc3
commit
c20f187265
1 changed files with 13 additions and 6 deletions
|
|
@ -18,9 +18,11 @@ class PlaybackReporter {
|
||||||
final AudiobookshelfApi authenticatedApi;
|
final AudiobookshelfApi authenticatedApi;
|
||||||
|
|
||||||
/// The stopwatch to keep track of the time since the last report
|
/// The stopwatch to keep track of the time since the last report
|
||||||
|
///
|
||||||
|
/// this should only run when media is playing
|
||||||
final _stopwatch = Stopwatch();
|
final _stopwatch = Stopwatch();
|
||||||
|
|
||||||
/// subscriptions
|
/// subscriptions to listen and then cancel when disposing
|
||||||
final List<StreamSubscription> _subscriptions = [];
|
final List<StreamSubscription> _subscriptions = [];
|
||||||
|
|
||||||
Duration _reportingInterval;
|
Duration _reportingInterval;
|
||||||
|
|
@ -30,7 +32,7 @@ class PlaybackReporter {
|
||||||
set reportingInterval(Duration value) {
|
set reportingInterval(Duration value) {
|
||||||
_reportingInterval = value;
|
_reportingInterval = value;
|
||||||
_cancelReportTimer();
|
_cancelReportTimer();
|
||||||
_setReportTimer();
|
_setReportTimerIfNotAlready();
|
||||||
_logger.info('set interval: $value');
|
_logger.info('set interval: $value');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,7 +65,7 @@ class PlaybackReporter {
|
||||||
// initial conditions
|
// initial conditions
|
||||||
if (player.playing) {
|
if (player.playing) {
|
||||||
_stopwatch.start();
|
_stopwatch.start();
|
||||||
_setReportTimer();
|
_setReportTimerIfNotAlready();
|
||||||
_logger.fine('starting stopwatch');
|
_logger.fine('starting stopwatch');
|
||||||
} else {
|
} else {
|
||||||
_logger.fine('not starting stopwatch');
|
_logger.fine('not starting stopwatch');
|
||||||
|
|
@ -72,8 +74,12 @@ class PlaybackReporter {
|
||||||
_subscriptions.add(
|
_subscriptions.add(
|
||||||
player.playerStateStream.listen((state) async {
|
player.playerStateStream.listen((state) async {
|
||||||
// set timer if any book is playing and cancel if not
|
// set timer if any book is playing and cancel if not
|
||||||
if (player.book != null && _reportTimer == null) {
|
if (player.book != null) {
|
||||||
_setReportTimer();
|
if (state.playing) {
|
||||||
|
_setReportTimerIfNotAlready();
|
||||||
|
} else {
|
||||||
|
_cancelReportTimer();
|
||||||
|
}
|
||||||
} else if (player.book == null && _reportTimer != null) {
|
} else if (player.book == null && _reportTimer != null) {
|
||||||
_logger.info('book is null, closing session');
|
_logger.info('book is null, closing session');
|
||||||
await closeSession();
|
await closeSession();
|
||||||
|
|
@ -195,7 +201,8 @@ class PlaybackReporter {
|
||||||
_logger.info('Closed session');
|
_logger.info('Closed session');
|
||||||
}
|
}
|
||||||
|
|
||||||
void _setReportTimer() {
|
void _setReportTimerIfNotAlready() {
|
||||||
|
if (_reportTimer != null) return;
|
||||||
_reportTimer = Timer.periodic(_reportingInterval, tryReportPlayback);
|
_reportTimer = Timer.periodic(_reportingInterval, tryReportPlayback);
|
||||||
_logger.fine('set timer with interval: $_reportingInterval');
|
_logger.fine('set timer with interval: $_reportingInterval');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue