Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Add a sleep timer option to stop at the end of a podcast episode

Barnabasefab45aaa71c
diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb
index 15d19ed5..75f462a6 100644
--- a/lib/l10n/app_en.arb
+++ b/lib/l10n/app_en.arb
@@ -3490,5 +3490,7 @@
"type": "String"
}
}
- }
+ },
+ "endOfEpisode": "End of Episode",
+ "sleepTimerSheetEpisodeSleepStart": "Sleep at end of episode"
}
diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart
index 6e723620..abb6fa2a 100644
--- a/lib/l10n/app_localizations.dart
+++ b/lib/l10n/app_localizations.dart
@@ -10037,6 +10037,18 @@ abstract class AppLocalizations {
/// In en, this message translates to:
/// **'Revoke \"{name}\"? Apps using this key will lose access immediately.'**
String adminApiKeysDeleteContent(String name);
+
+ /// No description provided for @endOfEpisode.
+ ///
+ /// In en, this message translates to:
+ /// **'End of Episode'**
+ String get endOfEpisode;
+
+ /// No description provided for @sleepTimerSheetEpisodeSleepStart.
+ ///
+ /// In en, this message translates to:
+ /// **'Sleep at end of episode'**
+ String get sleepTimerSheetEpisodeSleepStart;
}
class _AppLocalizationsDelegate
diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart
index 4582534e..578ce384 100644
--- a/lib/l10n/app_localizations_de.dart
+++ b/lib/l10n/app_localizations_de.dart
@@ -5814,4 +5814,10 @@ class AppLocalizationsDe extends AppLocalizations {
String adminApiKeysDeleteContent(String name) {
return 'Revoke \"$name\"? Apps using this key will lose access immediately.';
}
+
+ @override
+ String get endOfEpisode => 'End of Episode';
+
+ @override
+ String get sleepTimerSheetEpisodeSleepStart => 'Sleep at end of episode';
}
diff --git a/lib/l10n/app_localizations_el.dart b/lib/l10n/app_localizations_el.dart
index 39f48b02..9254053d 100644
--- a/lib/l10n/app_localizations_el.dart
+++ b/lib/l10n/app_localizations_el.dart
@@ -5749,4 +5749,10 @@ class AppLocalizationsEl extends AppLocalizations {
String adminApiKeysDeleteContent(String name) {
return 'Revoke \"$name\"? Apps using this key will lose access immediately.';
}
+
+ @override
+ String get endOfEpisode => 'End of Episode';
+
+ @override
+ String get sleepTimerSheetEpisodeSleepStart => 'Sleep at end of episode';
}
diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart
index e7df4550..5317d2ea 100644
--- a/lib/l10n/app_localizations_en.dart
+++ b/lib/l10n/app_localizations_en.dart
@@ -5749,4 +5749,10 @@ class AppLocalizationsEn extends AppLocalizations {
String adminApiKeysDeleteContent(String name) {
return 'Revoke \"$name\"? Apps using this key will lose access immediately.';
}
+
+ @override
+ String get endOfEpisode => 'End of Episode';
+
+ @override
+ String get sleepTimerSheetEpisodeSleepStart => 'Sleep at end of episode';
}
diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart
index 897f0521..f0b9473c 100644
--- a/lib/l10n/app_localizations_es.dart
+++ b/lib/l10n/app_localizations_es.dart
@@ -5749,4 +5749,10 @@ class AppLocalizationsEs extends AppLocalizations {
String adminApiKeysDeleteContent(String name) {
return 'Revoke \"$name\"? Apps using this key will lose access immediately.';
}
+
+ @override
+ String get endOfEpisode => 'End of Episode';
+
+ @override
+ String get sleepTimerSheetEpisodeSleepStart => 'Sleep at end of episode';
}
diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart
index 77765d27..70d75c84 100644
--- a/lib/l10n/app_localizations_fr.dart
+++ b/lib/l10n/app_localizations_fr.dart
@@ -5751,4 +5751,10 @@ class AppLocalizationsFr extends AppLocalizations {
String adminApiKeysDeleteContent(String name) {
return 'Revoke \"$name\"? Apps using this key will lose access immediately.';
}
+
+ @override
+ String get endOfEpisode => 'End of Episode';
+
+ @override
+ String get sleepTimerSheetEpisodeSleepStart => 'Sleep at end of episode';
}
diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart
index 9d2f07ba..acd1b233 100644
--- a/lib/l10n/app_localizations_it.dart
+++ b/lib/l10n/app_localizations_it.dart
@@ -5749,4 +5749,10 @@ class AppLocalizationsIt extends AppLocalizations {
String adminApiKeysDeleteContent(String name) {
return 'Revoke \"$name\"? Apps using this key will lose access immediately.';
}
+
+ @override
+ String get endOfEpisode => 'End of Episode';
+
+ @override
+ String get sleepTimerSheetEpisodeSleepStart => 'Sleep at end of episode';
}
diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart
index 58d51c40..103ebe5f 100644
--- a/lib/l10n/app_localizations_ja.dart
+++ b/lib/l10n/app_localizations_ja.dart
@@ -5749,4 +5749,10 @@ class AppLocalizationsJa extends AppLocalizations {
String adminApiKeysDeleteContent(String name) {
return 'Revoke \"$name\"? Apps using this key will lose access immediately.';
}
+
+ @override
+ String get endOfEpisode => 'End of Episode';
+
+ @override
+ String get sleepTimerSheetEpisodeSleepStart => 'Sleep at end of episode';
}
diff --git a/lib/l10n/app_localizations_no.dart b/lib/l10n/app_localizations_no.dart
index 558866f0..45e1a680 100644
--- a/lib/l10n/app_localizations_no.dart
+++ b/lib/l10n/app_localizations_no.dart
@@ -5749,4 +5749,10 @@ class AppLocalizationsNo extends AppLocalizations {
String adminApiKeysDeleteContent(String name) {
return 'Revoke \"$name\"? Apps using this key will lose access immediately.';
}
+
+ @override
+ String get endOfEpisode => 'End of Episode';
+
+ @override
+ String get sleepTimerSheetEpisodeSleepStart => 'Sleep at end of episode';
}
diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart
index 8cb40205..4cfeb003 100644
--- a/lib/l10n/app_localizations_pt.dart
+++ b/lib/l10n/app_localizations_pt.dart
@@ -5749,4 +5749,10 @@ class AppLocalizationsPt extends AppLocalizations {
String adminApiKeysDeleteContent(String name) {
return 'Revoke \"$name\"? Apps using this key will lose access immediately.';
}
+
+ @override
+ String get endOfEpisode => 'End of Episode';
+
+ @override
+ String get sleepTimerSheetEpisodeSleepStart => 'Sleep at end of episode';
}
diff --git a/lib/l10n/app_localizations_ro.dart b/lib/l10n/app_localizations_ro.dart
index 821dec77..1245b56d 100644
--- a/lib/l10n/app_localizations_ro.dart
+++ b/lib/l10n/app_localizations_ro.dart
@@ -5749,4 +5749,10 @@ class AppLocalizationsRo extends AppLocalizations {
String adminApiKeysDeleteContent(String name) {
return 'Revoke \"$name\"? Apps using this key will lose access immediately.';
}
+
+ @override
+ String get endOfEpisode => 'End of Episode';
+
+ @override
+ String get sleepTimerSheetEpisodeSleepStart => 'Sleep at end of episode';
}
diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart
index bbc4ed71..c6e27c5e 100644
--- a/lib/l10n/app_localizations_ru.dart
+++ b/lib/l10n/app_localizations_ru.dart
@@ -5752,4 +5752,10 @@ class AppLocalizationsRu extends AppLocalizations {
String adminApiKeysDeleteContent(String name) {
return 'Revoke \"$name\"? Apps using this key will lose access immediately.';
}
+
+ @override
+ String get endOfEpisode => 'End of Episode';
+
+ @override
+ String get sleepTimerSheetEpisodeSleepStart => 'Sleep at end of episode';
}
diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart
index 3f641297..c2782a7c 100644
--- a/lib/l10n/app_localizations_zh.dart
+++ b/lib/l10n/app_localizations_zh.dart
@@ -5670,4 +5670,10 @@ class AppLocalizationsZh extends AppLocalizations {
String adminApiKeysDeleteContent(String name) {
return 'Revoke \"$name\"? Apps using this key will lose access immediately.';
}
+
+ @override
+ String get endOfEpisode => 'End of Episode';
+
+ @override
+ String get sleepTimerSheetEpisodeSleepStart => 'Sleep at end of episode';
}
diff --git a/lib/services/sleep_timer_service.dart b/lib/services/sleep_timer_service.dart
index 0cc5a164..cca1e47d 100644
--- a/lib/services/sleep_timer_service.dart
+++ b/lib/services/sleep_timer_service.dart
@@ -8,7 +8,7 @@ import 'scoped_prefs.dart';
import 'audio_player_service.dart';
import 'chromecast_service.dart';
-enum SleepTimerMode { off, time, chapters }
+enum SleepTimerMode { off, time, chapters, episodes }
/// Auto sleep timer settings — automatically start a sleep timer within a time window.
class AutoSleepSettings {
@@ -152,6 +152,8 @@ class SleepTimerService extends ChangeNotifier {
return '<1m';
} else if (_mode == SleepTimerMode.chapters) {
return '$_chaptersRemaining ch';
+ } else if (_mode == SleepTimerMode.episodes) {
+ return 'ep';
}
return '';
}
@@ -328,6 +330,40 @@ class SleepTimerService extends ChangeNotifier {
debugPrint('[SleepTimer] Added 1 chapter — now $_chaptersRemaining remaining');
}
+ // ── Episode-based sleep (podcasts) ──
+
+ /// Stop at the end of the current podcast episode. Implemented as the
+ /// end-of-chapter analog: we pause a few seconds before the episode's natural
+ /// end so the player never "completes". That keeps every queue auto-advance
+ /// path (manual / auto_next / playlist, gapless or not) from firing, so it
+ /// works the same in any queue mode. Rewind-on-sleep masks the small gap.
+ void setEpisodeSleep() {
+ cancel();
+ _mode = SleepTimerMode.episodes;
+ _startEpisodeMonitor();
+ notifyListeners();
+ debugPrint('[SleepTimer] Set episode sleep (stop at end of current episode)');
+ }
+
+ // Pause this many seconds before the episode end. Wide enough that a 1 Hz
+ // position tick reliably lands in the window before any auto-advance triggers.
+ static const _episodeEndGuardSec = 2.5;
+
+ void _startEpisodeMonitor() {
+ _positionSub?.cancel();
+ // Targets local playback; while casting totalDuration is 0 so this no-ops.
+ _positionSub = _player.positionStream.listen((_) {
+ if (_mode != SleepTimerMode.episodes) return;
+ if (!_isPlaybackActive) return;
+ final totalSec = _player.totalDuration;
+ if (totalSec <= 0) return;
+ final posSec = _player.position.inMilliseconds / 1000.0;
+ if (posSec >= totalSec - _episodeEndGuardSec) {
+ _triggerSleep();
+ }
+ });
+ }
+
// ── Common ──
int _getCurrentChapterIndex() {
diff --git a/lib/widgets/sleep_timer_sheet.dart b/lib/widgets/sleep_timer_sheet.dart
index 7f81cd7b..c94a4c8d 100644
--- a/lib/widgets/sleep_timer_sheet.dart
+++ b/lib/widgets/sleep_timer_sheet.dart
@@ -56,6 +56,11 @@ class _SleepTimerSheetState extends State<SleepTimerSheet> {
_customChapters = results[3] as int;
_sleepRewindSeconds = results[4] as int;
_tabIndex = results[5] as int;
+ // Podcast episodes only have Timer + End-of-Episode tabs; clamp a saved
+ // "specific chapter" tab index (2) so it doesn't fall off the end.
+ if (AudioPlayerService().currentEpisodeId != null && _tabIndex > 1) {
+ _tabIndex = 1;
+ }
final currentIdx = _getCurrentChapterIndexFromPlayer();
if (currentIdx >= 0) _selectedChapterIndex = currentIdx;
});
@@ -86,6 +91,7 @@ class _SleepTimerSheetState extends State<SleepTimerSheet> {
builder: (_, __) {
final sleep = SleepTimerService();
final isActive = sleep.isActive;
+ final isEpisode = AudioPlayerService().currentEpisodeId != null;
final navBarPad = MediaQuery.of(context).viewPadding.bottom;
@@ -125,9 +131,16 @@ class _SleepTimerSheetState extends State<SleepTimerSheet> {
_tab(l.timer, Icons.timer_outlined, 0, accent),
const SizedBox(width: 4),
_tab(
- l.endOfChapter, Icons.auto_stories_outlined, 1, accent),
- const SizedBox(width: 4),
- _tab(l.sleepTimerSheetTabSpecificChapter, Icons.bookmark_outlined, 2, accent),
+ isEpisode ? l.endOfEpisode : l.endOfChapter,
+ isEpisode
+ ? Icons.podcasts_outlined
+ : Icons.auto_stories_outlined,
+ 1,
+ accent),
+ if (!isEpisode) ...[
+ const SizedBox(width: 4),
+ _tab(l.sleepTimerSheetTabSpecificChapter, Icons.bookmark_outlined, 2, accent),
+ ],
]),
),
const SizedBox(height: 20),
@@ -136,7 +149,9 @@ class _SleepTimerSheetState extends State<SleepTimerSheet> {
if (_tabIndex == 0)
_buildTimerTab(accent, tt, l)
else if (_tabIndex == 1)
- _buildChapterTab(accent, tt, l)
+ (isEpisode
+ ? _buildEpisodeTab(accent, tt, l)
+ : _buildChapterTab(accent, tt, l))
else
_buildSpecificChapterTab(accent, tt, l),
const SizedBox(height: 16),
@@ -166,6 +181,7 @@ class _SleepTimerSheetState extends State<SleepTimerSheet> {
SleepTimerService sleep, Color accent, TextTheme tt, AppLocalizations l) {
final cs = Theme.of(context).colorScheme;
final isTime = sleep.mode == SleepTimerMode.time;
+ final isEpisode = sleep.mode == SleepTimerMode.episodes;
String countdownLabel;
if (isTime) {
@@ -174,6 +190,8 @@ class _SleepTimerSheetState extends State<SleepTimerSheet> {
final s = r.inSeconds % 60;
countdownLabel =
'${m.toString().padLeft(2, '0')}:${s.toString().padLeft(2, '0')}';
+ } else if (isEpisode) {
+ countdownLabel = l.endOfEpisode;
} else {
countdownLabel = l.sleepTimerSheetChaptersLeft(sleep.chaptersRemaining);
}
@@ -199,7 +217,7 @@ class _SleepTimerSheetState extends State<SleepTimerSheet> {
),
),
] else ...[
- Icon(Icons.auto_stories_outlined,
+ Icon(isEpisode ? Icons.podcasts_outlined : Icons.auto_stories_outlined,
size: 28, color: accent.withValues(alpha: 0.6)),
const SizedBox(height: 8),
Text(countdownLabel,
@@ -208,35 +226,37 @@ class _SleepTimerSheetState extends State<SleepTimerSheet> {
],
const SizedBox(height: 20),
- // Quick add buttons
- Text(l.addMoreTime,
- style: TextStyle(color: cs.onSurfaceVariant, fontSize: 12)),
- const SizedBox(height: 10),
- if (isTime)
- Wrap(
- spacing: 8,
- runSpacing: 8,
- alignment: WrapAlignment.center,
- children: [
- for (final mins in [5, 10, 15, 30])
- _presetChip(
- accent, l.sleepTimerSheetAddMinutesChip(mins), false, () {
- sleep.addTime(Duration(minutes: mins));
- }),
- ])
- else
- Wrap(
- spacing: 8,
- runSpacing: 8,
- alignment: WrapAlignment.center,
- children: [
- for (final ch in [1, 2, 3])
- _presetChip(accent, l.sleepTimerSheetAddChaptersChip(ch), false,
- () {
- for (int i = 0; i < ch; i++) sleep.addChapter();
- }),
- ]),
- const SizedBox(height: 20),
+ // Quick add buttons (not applicable to "end of episode")
+ if (!isEpisode) ...[
+ Text(l.addMoreTime,
+ style: TextStyle(color: cs.onSurfaceVariant, fontSize: 12)),
+ const SizedBox(height: 10),
+ if (isTime)
+ Wrap(
+ spacing: 8,
+ runSpacing: 8,
+ alignment: WrapAlignment.center,
+ children: [
+ for (final mins in [5, 10, 15, 30])
+ _presetChip(
+ accent, l.sleepTimerSheetAddMinutesChip(mins), false, () {
+ sleep.addTime(Duration(minutes: mins));
+ }),
+ ])
+ else
+ Wrap(
+ spacing: 8,
+ runSpacing: 8,
+ alignment: WrapAlignment.center,
+ children: [
+ for (final ch in [1, 2, 3])
+ _presetChip(accent, l.sleepTimerSheetAddChaptersChip(ch), false,
+ () {
+ for (int i = 0; i < ch; i++) sleep.addChapter();
+ }),
+ ]),
+ const SizedBox(height: 20),
+ ],
// Cancel button
SizedBox(
@@ -612,6 +632,34 @@ class _SleepTimerSheetState extends State<SleepTimerSheet> {
]);
}
+ Widget _buildEpisodeTab(Color accent, TextTheme tt, AppLocalizations l) {
+ final cs = Theme.of(context).colorScheme;
+ return Column(children: [
+ Icon(Icons.podcasts_outlined,
+ size: 40, color: accent.withValues(alpha: 0.5)),
+ const SizedBox(height: 16),
+ // Stops at the end of the current episode (no counter — episodes only roll
+ // forward when a queue mode is feeding the next one).
+ SizedBox(
+ width: double.infinity,
+ height: 44,
+ child: FilledButton(
+ style: FilledButton.styleFrom(
+ backgroundColor: accent,
+ foregroundColor: cs.surface,
+ shape: RoundedRectangleBorder(
+ borderRadius: BorderRadius.circular(12))),
+ onPressed: () {
+ SleepTimerService().setEpisodeSleep();
+ Navigator.pop(context);
+ },
+ child: Text(l.sleepTimerSheetEpisodeSleepStart,
+ style:
+ const TextStyle(fontWeight: FontWeight.w600, fontSize: 14)),
+ )),
+ ]);
+ }
+
/// Format seconds as a human-readable label (e.g. "30s", "5m", "1m 30s").
String _rewindLabel(int seconds, AppLocalizations l) {
if (seconds == 0) return l.off;