Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Add sleep timer fade-out, tab scroll-to-first, and download removal feedback

Nathan Poulson7695aa1e071a
diff --git a/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt b/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt
index a9c702d5..d9d0cc75 100644
--- a/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt
+++ b/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt
@@ -55,6 +55,7 @@ class MainActivity : AudioServiceActivity() {
}
}
Log.d(TAG, "EQ method channel registered")
+
}
private fun handleInit(result: MethodChannel.Result) {
diff --git a/lib/screens/absorbing_screen.dart b/lib/screens/absorbing_screen.dart
index dbbd56e3..c6b0661e 100644
--- a/lib/screens/absorbing_screen.dart
+++ b/lib/screens/absorbing_screen.dart
@@ -20,6 +20,16 @@ class AbsorbingScreen extends StatefulWidget {
globalKey.currentState?._scrollToActiveCard();
}
+ /// Scroll to the first card (used when re-tapping the Absorbing tab)
+ static void scrollToFirst() {
+ final state = globalKey.currentState;
+ if (state != null && state._pageController.hasClients) {
+ state._pageController.animateToPage(0,
+ duration: const Duration(milliseconds: 350),
+ curve: Curves.easeOutCubic);
+ }
+ }
+
@override
State<AbsorbingScreen> createState() => _AbsorbingScreenState();
}
diff --git a/lib/screens/app_shell.dart b/lib/screens/app_shell.dart
index ce1c54e6..dec2949b 100644
--- a/lib/screens/app_shell.dart
+++ b/lib/screens/app_shell.dart
@@ -68,7 +68,14 @@ class _AppShellState extends State<AppShell> with WidgetsBindingObserver {
}
void _navigateTo(int index) {
- if (index == _currentIndex) return;
+ if (index == _currentIndex) {
+ // Already on this tab — handle re-tap actions
+ if (index == 2) {
+ // Absorbing tab: scroll to first card
+ AbsorbingScreen.scrollToFirst();
+ }
+ return;
+ }
_ensurePageBuilt(index);
setState(() {
_currentIndex = index;
diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart
index 0004ebbf..9c0ea145 100644
--- a/lib/screens/settings_screen.dart
+++ b/lib/screens/settings_screen.dart
@@ -36,6 +36,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
int _backSkip = 10;
bool _shakeToResetSleep = true;
bool _resetSleepOnPause = false;
+ bool _sleepFadeOut = true;
int _shakeAddMinutes = 5;
bool _autoContinueSeries = true;
bool _hideEbookOnly = false;
@@ -65,6 +66,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
final bk = await PlayerSettings.getBackSkip();
final shake = await PlayerSettings.getShakeToResetSleep();
final resetOnPause = await PlayerSettings.getResetSleepOnPause();
+ final sleepFade = await PlayerSettings.getSleepFadeOut();
final shakeMins = await PlayerSettings.getShakeAddMinutes();
final autoSeries = await PlayerSettings.getAutoContinueSeries();
final hideEbook = await PlayerSettings.getHideEbookOnly();
@@ -87,6 +89,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
_backSkip = bk;
_shakeToResetSleep = shake;
_resetSleepOnPause = resetOnPause;
+ _sleepFadeOut = sleepFade;
_shakeAddMinutes = shakeMins;
_autoContinueSeries = autoSeries;
_hideEbookOnly = hideEbook;
@@ -738,6 +741,20 @@ class _SettingsScreenState extends State<SettingsScreen> {
} : null,
),
const Divider(height: 1, indent: 16, endIndent: 16),
+ SwitchListTile(
+ title: const Text('Fade volume before sleep'),
+ subtitle: Text(
+ _sleepFadeOut
+ ? 'Gradually lowers volume during the last 30 seconds'
+ : 'Playback stops immediately when timer ends',
+ style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
+ value: _sleepFadeOut,
+ onChanged: _loaded ? (v) {
+ setState(() => _sleepFadeOut = v);
+ PlayerSettings.setSleepFadeOut(v);
+ } : null,
+ ),
+ const Divider(height: 1, indent: 16, endIndent: 16),
// ── Auto Sleep Timer ──
SwitchListTile(
title: const Text('Auto sleep timer'),
@@ -1569,6 +1586,13 @@ class _SettingsScreenState extends State<SettingsScreen> {
onPressed: () {
DownloadService().deleteDownload(info.itemId);
Navigator.pop(d);
+ ScaffoldMessenger.of(ctx)
+ ..clearSnackBars()
+ ..showSnackBar(SnackBar(
+ content: Text('"${info.title}" removed'),
+ duration: const Duration(seconds: 2),
+ behavior: SnackBarBehavior.floating,
+ ));
},
child: const Text('Remove')),
],
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 21b74c2d..3ee69e06 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -117,6 +117,9 @@ class PlayerSettings {
static Future<bool> getResetSleepOnPause() => _get('resetSleepOnPause', false);
static Future<void> setResetSleepOnPause(bool value) => _set('resetSleepOnPause', value);
+ static Future<bool> getSleepFadeOut() => _get('sleepFadeOut', true);
+ static Future<void> setSleepFadeOut(bool value) => _set('sleepFadeOut', value);
+
static Future<bool> getHideEbookOnly() => _get('hideEbookOnly', false);
static Future<void> setHideEbookOnly(bool value) => _set('hideEbookOnly', value, notify: true);
@@ -768,6 +771,8 @@ class AudioPlayerService extends ChangeNotifier {
bool get hasBook => _currentItemId != null;
bool get isPlaying => _player?.playing ?? false;
bool get isOfflineMode => _isOfflineMode;
+ double get volume => _player?.volume ?? 1.0;
+ Future<void> setVolume(double v) async => _player?.setVolume(v);
Stream<Duration> get positionStream =>
_player?.positionStream ?? const Stream.empty();
diff --git a/lib/services/sleep_timer_service.dart b/lib/services/sleep_timer_service.dart
index 14147d16..ad1eda03 100644
--- a/lib/services/sleep_timer_service.dart
+++ b/lib/services/sleep_timer_service.dart
@@ -106,9 +106,10 @@ class SleepTimerService extends ChangeNotifier {
static const _shakeThreshold = 25.0; // m/s² — raised from 15 to require a deliberate shake
static const _shakeCooldown = Duration(seconds: 3);
- // Wind-down warning
+ // Wind-down warning & fade
bool _warningSent = false;
static const _warningThreshold = Duration(seconds: 30);
+ double _fadeStartVolume = 1.0; // volume when fade begins
// Reset on pause/play
bool _wasPlaying = false; // tracks play state transitions
@@ -165,6 +166,10 @@ class SleepTimerService extends ChangeNotifier {
if (resetOnPause) {
_timeRemaining = _initialDuration;
_warningSent = false;
+ if (_isFadingOut) {
+ _isFadingOut = false;
+ _player.setVolume(_fadeStartVolume);
+ }
debugPrint('[SleepTimer] Reset to ${_initialDuration.inMinutes}m on resume');
onToast?.call('Sleep timer reset: ${_initialDuration.inMinutes}m');
}
@@ -175,12 +180,24 @@ class SleepTimerService extends ChangeNotifier {
if (isPlaying) {
_timeRemaining -= const Duration(seconds: 1);
- // Wind-down warning vibration at 30 seconds
+ // Wind-down warning at 30 seconds: vibration + optional fade
if (!_warningSent && _timeRemaining <= _warningThreshold && _timeRemaining.inSeconds > 0) {
_warningSent = true;
- _vibrateWarning();
onToast?.call('Sleep timer ending soon…');
- debugPrint('[SleepTimer] Warning: ${_timeRemaining.inSeconds}s remaining');
+ final fadeEnabled = await PlayerSettings.getSleepFadeOut();
+ if (fadeEnabled && !_cast.isCasting) {
+ _isFadingOut = true;
+ _fadeStartVolume = _player.volume;
+ debugPrint('[SleepTimer] Warning: ${_timeRemaining.inSeconds}s remaining — starting fade');
+ } else {
+ debugPrint('[SleepTimer] Warning: ${_timeRemaining.inSeconds}s remaining');
+ }
+ }
+
+ // Gradually lower volume during the last 30 seconds
+ if (_isFadingOut && _timeRemaining.inSeconds > 0 && !_cast.isCasting) {
+ final fraction = _timeRemaining.inSeconds / _warningThreshold.inSeconds;
+ _player.setVolume((_fadeStartVolume * fraction).clamp(0.0, 1.0));
}
notifyListeners();
@@ -192,9 +209,13 @@ class SleepTimerService extends ChangeNotifier {
void addTime(Duration extra) {
if (_mode != SleepTimerMode.time) return;
_timeRemaining += extra;
- // Reset warning if we're above threshold again
+ // Reset warning and restore volume if we're above threshold again
if (_timeRemaining > _warningThreshold) {
_warningSent = false;
+ if (_isFadingOut) {
+ _isFadingOut = false;
+ _player.setVolume(_fadeStartVolume);
+ }
}
notifyListeners();
debugPrint('[SleepTimer] Added ${extra.inMinutes}m — now ${_timeRemaining.inMinutes}m');
@@ -276,18 +297,25 @@ class SleepTimerService extends ChangeNotifier {
return -1;
}
+ bool _isFadingOut = false;
+ bool get isFadingOut => _isFadingOut;
+
void _triggerSleep() {
debugPrint('[SleepTimer] Triggering sleep — pausing playback');
- _vibrateSleep();
if (_cast.isCasting) {
_cast.pause();
} else {
_player.pause();
+ // Restore volume so next playback starts at normal level
+ _player.setVolume(_fadeStartVolume);
}
+ _isFadingOut = false;
cancel();
}
void cancel() {
+ final wasFading = _isFadingOut;
+ _isFadingOut = false;
_timer?.cancel();
_timer = null;
_positionSub?.cancel();
@@ -299,6 +327,10 @@ class SleepTimerService extends ChangeNotifier {
_chaptersRemaining = 0;
_targetChapterIndex = -1;
_warningSent = false;
+ // Restore volume if cancelled during fade-out
+ if (wasFading) {
+ _player.setVolume(_fadeStartVolume);
+ }
notifyListeners();
debugPrint('[SleepTimer] Cancelled');
}
@@ -310,24 +342,6 @@ class SleepTimerService extends ChangeNotifier {
HapticFeedback.mediumImpact();
}
- /// Double heavy buzz when timer is almost done (30s left)
- void _vibrateWarning() {
- HapticFeedback.heavyImpact();
- Future.delayed(const Duration(milliseconds: 200), () {
- HapticFeedback.heavyImpact();
- });
- }
-
- /// Triple heavy buzz when sleep actually triggers
- void _vibrateSleep() {
- HapticFeedback.heavyImpact();
- Future.delayed(const Duration(milliseconds: 150), () {
- HapticFeedback.heavyImpact();
- });
- Future.delayed(const Duration(milliseconds: 300), () {
- HapticFeedback.heavyImpact();
- });
- }
// ── Shake detection ──
diff --git a/lib/widgets/absorbing_card.dart b/lib/widgets/absorbing_card.dart
index 46da327f..ee6444fa 100644
--- a/lib/widgets/absorbing_card.dart
+++ b/lib/widgets/absorbing_card.dart
@@ -110,6 +110,7 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
_fetchChaptersIfNeeded();
_startChapterTracking();
ChromecastService().addListener(_onCastChanged);
+ DownloadService().addListener(_onDownloadChanged);
PlayerSettings.settingsChanged.addListener(_reloadButtonOrder);
_reloadButtonOrder();
}
@@ -120,6 +121,8 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
});
}
+ void _onDownloadChanged() { if (mounted) setState(() {}); }
+
void _onCastChanged() {
_startChapterTracking();
if (mounted) setState(() {});
@@ -237,6 +240,7 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
void dispose() {
PlayerSettings.settingsChanged.removeListener(_reloadButtonOrder);
ChromecastService().removeListener(_onCastChanged);
+ DownloadService().removeListener(_onDownloadChanged);
_chapterTrackSub?.cancel();
_blurredCover?.dispose();
super.dispose();
@@ -863,6 +867,15 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
void _deleteDownload(String dlKey) {
DownloadService().deleteDownload(dlKey);
+ if (mounted) {
+ ScaffoldMessenger.of(context)
+ ..clearSnackBars()
+ ..showSnackBar(const SnackBar(
+ content: Text('Download removed'),
+ duration: Duration(seconds: 2),
+ behavior: SnackBarBehavior.floating,
+ ));
+ }
}
Future<void> _removeFromAbsorbing() async {
diff --git a/lib/widgets/absorbing_shared.dart b/lib/widgets/absorbing_shared.dart
index 1a96817c..1fa73b93 100644
--- a/lib/widgets/absorbing_shared.dart
+++ b/lib/widgets/absorbing_shared.dart
@@ -91,7 +91,17 @@ class _DownloadWideButtonState extends State<DownloadWideButton> {
content: const Text('This will be removed from your device.'),
actions: [
TextButton(onPressed: () => Navigator.pop(ctx), child: const Text('Cancel')),
- TextButton(onPressed: () { _dl.deleteDownload(widget.itemId); Navigator.pop(ctx); },
+ TextButton(onPressed: () {
+ _dl.deleteDownload(widget.itemId);
+ Navigator.pop(ctx);
+ ScaffoldMessenger.of(context)
+ ..clearSnackBars()
+ ..showSnackBar(const SnackBar(
+ content: Text('Download removed'),
+ duration: Duration(seconds: 2),
+ behavior: SnackBarBehavior.floating,
+ ));
+ },
child: const Text('Remove', style: TextStyle(color: Colors.redAccent))),
],
));
diff --git a/pubspec.yaml b/pubspec.yaml
index d69adef2..5b11a221 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,7 +1,7 @@
name: absorb
description: A Material 3 Audiobookshelf client for Android.
publish_to: 'none'
-version: 1.7.23+34
+version: 1.7.26+34
environment:
sdk: '>=3.1.0 <4.0.0'