Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Undo the sleep timer rewind if you start playing again within five minutes

Barnabas9bdf9fd9edd9
diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb
index 56aabc80..dd26983f 100644
--- a/lib/l10n/app_en.arb
+++ b/lib/l10n/app_en.arb
@@ -3700,6 +3700,14 @@
"episodeListPositionEnd": "End of queue",
"episodeListPositionNone": "Don't add to queue",
"episodeListPositionNoneDesc": "Still notified and downloaded",
+ "sleepRewindUndoNote": "Hit play within {minutes} minutes and the rewind is undone",
+ "@sleepRewindUndoNote": {
+ "placeholders": {
+ "minutes": {
+ "type": "int"
+ }
+ }
+ },
"lpQueueDownloadingItems": "{count, plural, =1{Queue: downloading 1 item} other{Queue: downloading {count} items}}",
"@lpQueueDownloadingItems": {
"placeholders": {
diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart
index 721e74aa..8449b2a4 100644
--- a/lib/l10n/app_localizations.dart
+++ b/lib/l10n/app_localizations.dart
@@ -10815,6 +10815,12 @@ abstract class AppLocalizations {
/// **'Still notified and downloaded'**
String get episodeListPositionNoneDesc;
+ /// No description provided for @sleepRewindUndoNote.
+ ///
+ /// In en, this message translates to:
+ /// **'Hit play within {minutes} minutes and the rewind is undone'**
+ String sleepRewindUndoNote(int minutes);
+
/// No description provided for @lpQueueDownloadingItems.
///
/// In en, this message translates to:
diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart
index 88023591..c824e8f3 100644
--- a/lib/l10n/app_localizations_de.dart
+++ b/lib/l10n/app_localizations_de.dart
@@ -6283,6 +6283,11 @@ class AppLocalizationsDe extends AppLocalizations {
@override
String get episodeListPositionNoneDesc => 'Still notified and downloaded';
+ @override
+ String sleepRewindUndoNote(int minutes) {
+ return 'Hit play within $minutes minutes and the rewind is undone';
+ }
+
@override
String lpQueueDownloadingItems(int count) {
String _temp0 = intl.Intl.pluralLogic(
diff --git a/lib/l10n/app_localizations_el.dart b/lib/l10n/app_localizations_el.dart
index 4528a1d9..cb16aca6 100644
--- a/lib/l10n/app_localizations_el.dart
+++ b/lib/l10n/app_localizations_el.dart
@@ -6201,6 +6201,11 @@ class AppLocalizationsEl extends AppLocalizations {
@override
String get episodeListPositionNoneDesc => 'Still notified and downloaded';
+ @override
+ String sleepRewindUndoNote(int minutes) {
+ return 'Hit play within $minutes minutes and the rewind is undone';
+ }
+
@override
String lpQueueDownloadingItems(int count) {
String _temp0 = intl.Intl.pluralLogic(
diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart
index 7221c237..47254460 100644
--- a/lib/l10n/app_localizations_en.dart
+++ b/lib/l10n/app_localizations_en.dart
@@ -6201,6 +6201,11 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get episodeListPositionNoneDesc => 'Still notified and downloaded';
+ @override
+ String sleepRewindUndoNote(int minutes) {
+ return 'Hit play within $minutes minutes and the rewind is undone';
+ }
+
@override
String lpQueueDownloadingItems(int count) {
String _temp0 = intl.Intl.pluralLogic(
diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart
index 60cc12d1..d3b7a890 100644
--- a/lib/l10n/app_localizations_es.dart
+++ b/lib/l10n/app_localizations_es.dart
@@ -6203,6 +6203,11 @@ class AppLocalizationsEs extends AppLocalizations {
@override
String get episodeListPositionNoneDesc => 'Still notified and downloaded';
+ @override
+ String sleepRewindUndoNote(int minutes) {
+ return 'Hit play within $minutes minutes and the rewind is undone';
+ }
+
@override
String lpQueueDownloadingItems(int count) {
String _temp0 = intl.Intl.pluralLogic(
diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart
index 55d0d365..749a744f 100644
--- a/lib/l10n/app_localizations_fr.dart
+++ b/lib/l10n/app_localizations_fr.dart
@@ -6221,6 +6221,11 @@ class AppLocalizationsFr extends AppLocalizations {
@override
String get episodeListPositionNoneDesc => 'Still notified and downloaded';
+ @override
+ String sleepRewindUndoNote(int minutes) {
+ return 'Hit play within $minutes minutes and the rewind is undone';
+ }
+
@override
String lpQueueDownloadingItems(int count) {
String _temp0 = intl.Intl.pluralLogic(
diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart
index 7c6f2f5a..fab2573a 100644
--- a/lib/l10n/app_localizations_it.dart
+++ b/lib/l10n/app_localizations_it.dart
@@ -6201,6 +6201,11 @@ class AppLocalizationsIt extends AppLocalizations {
@override
String get episodeListPositionNoneDesc => 'Still notified and downloaded';
+ @override
+ String sleepRewindUndoNote(int minutes) {
+ return 'Hit play within $minutes minutes and the rewind is undone';
+ }
+
@override
String lpQueueDownloadingItems(int count) {
String _temp0 = intl.Intl.pluralLogic(
diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart
index e6aa8e7b..4b8403b5 100644
--- a/lib/l10n/app_localizations_ja.dart
+++ b/lib/l10n/app_localizations_ja.dart
@@ -6201,6 +6201,11 @@ class AppLocalizationsJa extends AppLocalizations {
@override
String get episodeListPositionNoneDesc => 'Still notified and downloaded';
+ @override
+ String sleepRewindUndoNote(int minutes) {
+ return 'Hit play within $minutes minutes and the rewind is undone';
+ }
+
@override
String lpQueueDownloadingItems(int count) {
String _temp0 = intl.Intl.pluralLogic(
diff --git a/lib/l10n/app_localizations_no.dart b/lib/l10n/app_localizations_no.dart
index 5f20ad66..98c761aa 100644
--- a/lib/l10n/app_localizations_no.dart
+++ b/lib/l10n/app_localizations_no.dart
@@ -6201,6 +6201,11 @@ class AppLocalizationsNo extends AppLocalizations {
@override
String get episodeListPositionNoneDesc => 'Still notified and downloaded';
+ @override
+ String sleepRewindUndoNote(int minutes) {
+ return 'Hit play within $minutes minutes and the rewind is undone';
+ }
+
@override
String lpQueueDownloadingItems(int count) {
String _temp0 = intl.Intl.pluralLogic(
diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart
index 5efa3f1d..022d6784 100644
--- a/lib/l10n/app_localizations_pt.dart
+++ b/lib/l10n/app_localizations_pt.dart
@@ -6201,6 +6201,11 @@ class AppLocalizationsPt extends AppLocalizations {
@override
String get episodeListPositionNoneDesc => 'Still notified and downloaded';
+ @override
+ String sleepRewindUndoNote(int minutes) {
+ return 'Hit play within $minutes minutes and the rewind is undone';
+ }
+
@override
String lpQueueDownloadingItems(int count) {
String _temp0 = intl.Intl.pluralLogic(
diff --git a/lib/l10n/app_localizations_ro.dart b/lib/l10n/app_localizations_ro.dart
index 6f95be89..34550bf0 100644
--- a/lib/l10n/app_localizations_ro.dart
+++ b/lib/l10n/app_localizations_ro.dart
@@ -6201,6 +6201,11 @@ class AppLocalizationsRo extends AppLocalizations {
@override
String get episodeListPositionNoneDesc => 'Still notified and downloaded';
+ @override
+ String sleepRewindUndoNote(int minutes) {
+ return 'Hit play within $minutes minutes and the rewind is undone';
+ }
+
@override
String lpQueueDownloadingItems(int count) {
String _temp0 = intl.Intl.pluralLogic(
diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart
index c02b9059..ddc2f050 100644
--- a/lib/l10n/app_localizations_ru.dart
+++ b/lib/l10n/app_localizations_ru.dart
@@ -6205,6 +6205,11 @@ class AppLocalizationsRu extends AppLocalizations {
@override
String get episodeListPositionNoneDesc => 'Still notified and downloaded';
+ @override
+ String sleepRewindUndoNote(int minutes) {
+ return 'Hit play within $minutes minutes and the rewind is undone';
+ }
+
@override
String lpQueueDownloadingItems(int count) {
String _temp0 = intl.Intl.pluralLogic(
diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart
index 8f70288f..8fe2fc7f 100644
--- a/lib/l10n/app_localizations_zh.dart
+++ b/lib/l10n/app_localizations_zh.dart
@@ -6112,6 +6112,11 @@ class AppLocalizationsZh extends AppLocalizations {
@override
String get episodeListPositionNoneDesc => 'Still notified and downloaded';
+ @override
+ String sleepRewindUndoNote(int minutes) {
+ return 'Hit play within $minutes minutes and the rewind is undone';
+ }
+
@override
String lpQueueDownloadingItems(int count) {
String _temp0 = intl.Intl.pluralLogic(
diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart
index e7d799e5..14ae4991 100644
--- a/lib/screens/settings_screen.dart
+++ b/lib/screens/settings_screen.dart
@@ -2784,6 +2784,15 @@ class _SettingsScreenState extends State<SettingsScreen> {
],
),
),
+ if (_sleepRewindSeconds > 0)
+ Padding(
+ padding: const EdgeInsets.fromLTRB(28, 0, 28, 10),
+ child: Text(
+ l.sleepRewindUndoNote(
+ SleepTimerService.sleepRewindUndoWindow.inMinutes),
+ style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant),
+ ),
+ ),
const Divider(height: 1, indent: 16, endIndent: 16),
SwitchListTile(
title: Text(l.fadeVolumeBeforeSleep),
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index a81599e4..d52db5b4 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -5552,6 +5552,16 @@ class AudioPlayerService extends ChangeNotifier {
false; // User explicitly resumed — allow interrupt-resume again
_handler?._noisyPauseAt = null; // Clear noisy suppression window
_lastAutoRewindAmount = 0;
+ // Coming back within a few minutes of the sleep timer firing means you were
+ // awake for it, so put back what its rewind took. Done before the ordinary
+ // auto-rewind below so that still applies from the restored position.
+ if (_player != null) {
+ final undoTo = SleepTimerService()
+ .takeSleepRewindUndo(_currentItemId, position);
+ if (undoTo != null) {
+ await seekTo(undoTo, logDetail: 'sleep rewind undone');
+ }
+ }
// Auto-rewind on resume if enabled
if (_lastPauseTime != null && _player != null) {
final settings = await AutoRewindSettings.load();
diff --git a/lib/services/sleep_timer_service.dart b/lib/services/sleep_timer_service.dart
index 1bffd27b..f7f5c66d 100644
--- a/lib/services/sleep_timer_service.dart
+++ b/lib/services/sleep_timer_service.dart
@@ -118,6 +118,15 @@ class SleepTimerService extends ChangeNotifier {
int _targetChapterIndex = -1;
double _targetEndSeconds = -1;
StreamSubscription? _positionSub;
+
+ // Where a sleep rewind put us, so hitting play again shortly after can undo
+ // it. Falling asleep is what the rewind is for; coming straight back means
+ // you were awake for it and shouldn't have to listen through it again.
+ static const sleepRewindUndoWindow = Duration(minutes: 5);
+ DateTime? _sleepRewoundAt;
+ String? _sleepRewoundItemId;
+ Duration? _sleepRewoundFrom; // position the timer fired at
+ Duration? _sleepRewoundTo; // position after the rewind
// Shake detection
String _shakeMode = 'addTime'; // 'off', 'addTime', 'resetTimer'
@@ -492,7 +501,12 @@ class SleepTimerService extends ChangeNotifier {
// Auto-rewind so the user resumes from a few seconds back
final rewindSeconds = await PlayerSettings.getEffectiveSleepRewindSeconds(_player.currentItemId);
if (rewindSeconds > 0) {
+ final from = _player.position;
await _player.sleepTimerRewind(rewindSeconds);
+ _sleepRewoundAt = DateTime.now();
+ _sleepRewoundItemId = _player.currentItemId;
+ _sleepRewoundFrom = from;
+ _sleepRewoundTo = _player.position;
debugPrint('[SleepTimer] Rewound ${rewindSeconds}s');
}
}
@@ -503,6 +517,48 @@ class SleepTimerService extends ChangeNotifier {
}
}
+ /// Position to jump back to when playback resumes soon after a sleep rewind,
+ /// or null if there's nothing to undo. Consumed on read, so it only ever
+ /// applies once.
+ ///
+ /// Deliberately separate from the ordinary auto-rewind-on-pause: this only
+ /// reverses the extra jump the sleep timer made. Whatever auto-rewind wants
+ /// to do afterwards still applies from the restored position.
+ Duration? takeSleepRewindUndo(String? itemId, Duration currentPosition) {
+ final at = _sleepRewoundAt;
+ final from = _sleepRewoundFrom;
+ final to = _sleepRewoundTo;
+ if (at == null || from == null || to == null) return null;
+
+ void forget() {
+ _sleepRewoundAt = null;
+ _sleepRewoundItemId = null;
+ _sleepRewoundFrom = null;
+ _sleepRewoundTo = null;
+ }
+
+ if (DateTime.now().difference(at) > sleepRewindUndoWindow) {
+ debugPrint('[SleepTimer] Sleep rewind too old to undo');
+ forget();
+ return null;
+ }
+ if (itemId == null || itemId != _sleepRewoundItemId) {
+ debugPrint('[SleepTimer] Different item since the sleep rewind - not undoing');
+ forget();
+ return null;
+ }
+ // Moved since we rewound (a manual seek, or resuming somewhere else), so
+ // the rewind is no longer the thing that put us here.
+ if ((currentPosition - to).abs() > const Duration(seconds: 2)) {
+ debugPrint('[SleepTimer] Position moved since the sleep rewind - not undoing');
+ forget();
+ return null;
+ }
+ forget();
+ debugPrint('[SleepTimer] Undoing sleep rewind - back to ${from.inSeconds}s');
+ return from;
+ }
+
void cancel() {
final wasFading = _isFadingOut;
_isFadingOut = false;
diff --git a/lib/widgets/sleep_timer_sheet.dart b/lib/widgets/sleep_timer_sheet.dart
index c94a4c8d..eb8ea5d7 100644
--- a/lib/widgets/sleep_timer_sheet.dart
+++ b/lib/widgets/sleep_timer_sheet.dart
@@ -736,6 +736,16 @@ class _SleepTimerSheetState extends State<SleepTimerSheet> {
color: cs.onSurface.withValues(alpha: 0.3), fontSize: 11)),
]),
),
+ if (isEnabled)
+ Padding(
+ padding: const EdgeInsets.fromLTRB(28, 2, 12, 0),
+ child: Text(
+ l.sleepRewindUndoNote(
+ SleepTimerService.sleepRewindUndoWindow.inMinutes),
+ style: TextStyle(
+ color: cs.onSurface.withValues(alpha: 0.45), fontSize: 11),
+ ),
+ ),
]);
}
diff --git a/pubspec.yaml b/pubspec.yaml
index 15cbc0a4..461b1c62 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,7 +1,7 @@
name: absorb
description: An Audiobookshelf Client
publish_to: 'none'
-version: 1.9.3+231
+version: 1.9.3+232
environment:
sdk: '>=3.10.0 <4.0.0'