Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Fix finished status reset on sync, auto-delete on finish, cloud icon, crash fixes

Nathan Poulsona035adeeea45
diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dart
index 4a2ba2df..adb9402a 100644
--- a/lib/providers/library_provider.dart
+++ b/lib/providers/library_provider.dart
@@ -2200,60 +2200,12 @@ class LibraryProvider extends ChangeNotifier {
// Slide the rolling download window forward for the finished item's series
_checkRollingDownloads(itemId);
- // Auto-delete finished download if this item's series/show is opted in
- if (_rollingDownloadSeries.isNotEmpty &&
- DownloadService().isDownloaded(itemId)) {
+ // Auto-delete finished download if the setting is enabled
+ if (DownloadService().isDownloaded(itemId)) {
PlayerSettings.getRollingDownloadDeleteFinished().then((delete) {
if (!delete) return;
- bool optedIn = false;
- if (itemId.length > 36) {
- optedIn = _rollingDownloadSeries.contains(itemId.substring(0, 36));
- } else {
- final data = _itemDataWithSeries(itemId);
- if (data != null) {
- final (seriesId, _) = _extractSeries(data);
- optedIn =
- seriesId != null && _rollingDownloadSeries.contains(seriesId);
- }
- }
- if (optedIn) {
- DownloadService().deleteDownload(itemId, skipStopCheck: true);
- _showRollingSnackBar('Deleted finished download');
- }
- });
- }
-
- // Auto-delete for queue-based downloads (manual queue + queueAutoDownload)
- if (DownloadService().isDownloaded(itemId)) {
- final isPodcastItem = itemId.length > 36;
- Future.wait([
- isPodcastItem ? PlayerSettings.getPodcastQueueMode() : PlayerSettings.getBookQueueMode(),
- PlayerSettings.getQueueAutoDownload(),
- PlayerSettings.getRollingDownloadDeleteFinished(),
- ]).then((results) {
- final qMode = results[0] as String;
- final qAutoDl = results[1] as bool;
- final deleteFin = results[2] as bool;
- if (qMode != 'manual' || !qAutoDl || !deleteFin) return;
- // Skip if already handled by series-based block above
- bool handledBySeries = false;
- if (_rollingDownloadSeries.isNotEmpty) {
- if (itemId.length > 36) {
- handledBySeries =
- _rollingDownloadSeries.contains(itemId.substring(0, 36));
- } else {
- final data = _itemDataWithSeries(itemId);
- if (data != null) {
- final (seriesId, _) = _extractSeries(data);
- handledBySeries =
- seriesId != null && _rollingDownloadSeries.contains(seriesId);
- }
- }
- }
- if (!handledBySeries) {
- DownloadService().deleteDownload(itemId, skipStopCheck: true);
- _showRollingSnackBar('Deleted finished download');
- }
+ DownloadService().deleteDownload(itemId, skipStopCheck: true);
+ _showRollingSnackBar('Deleted finished download');
});
}
diff --git a/lib/screens/absorbing_screen.dart b/lib/screens/absorbing_screen.dart
index 0128e4d0..443c5e52 100644
--- a/lib/screens/absorbing_screen.dart
+++ b/lib/screens/absorbing_screen.dart
@@ -452,54 +452,28 @@ class _AbsorbingScreenState extends State<AbsorbingScreen> {
AbsorbPageHeader(
title: 'Absorbing',
padding: const EdgeInsets.fromLTRB(20, 12, 12, 0),
- actions: [
- // Offline mode toggle
- GestureDetector(
- onTap: () {
- final newVal = !lib.isManualOffline;
- lib.setManualOffline(newVal);
- if (newVal) {
- // Only stop playback if the current item isn't downloaded
- final dl = DownloadService();
- final itemId = _player.currentItemId;
- final epId = _player.currentEpisodeId;
- final dlKey = epId != null && itemId != null
- ? '$itemId-$epId'
- : itemId;
- if (dlKey == null || !dl.isDownloaded(dlKey)) {
- _stopAndRefresh(lib);
- }
+ trailing: GestureDetector(
+ onTap: () {
+ final newVal = !lib.isManualOffline;
+ lib.setManualOffline(newVal);
+ if (newVal) {
+ final dl = DownloadService();
+ final itemId = _player.currentItemId;
+ final epId = _player.currentEpisodeId;
+ final dlKey = epId != null && itemId != null
+ ? '$itemId-$epId'
+ : itemId;
+ if (dlKey == null || !dl.isDownloaded(dlKey)) {
+ _stopAndRefresh(lib);
}
- },
- child: AnimatedContainer(
- duration: const Duration(milliseconds: 300),
- curve: Curves.easeOutCubic,
- padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
- decoration: BoxDecoration(
- color: effectiveOffline ? Colors.orange.withValues(alpha: 0.15) : subtleBg,
- borderRadius: BorderRadius.circular(20),
- border: Border.all(color: effectiveOffline ? Colors.orange.withValues(alpha: 0.3) : subtleBorder),
- ),
- child: Row(
- mainAxisSize: MainAxisSize.min,
- children: [
- Icon(
- effectiveOffline ? Icons.airplanemode_active_rounded : Icons.airplanemode_inactive_rounded,
- size: 14, color: effectiveOffline ? Colors.orange : muted,
- ),
- const SizedBox(width: 4),
- Text(
- effectiveOffline ? 'Offline' : 'Online',
- style: TextStyle(
- color: effectiveOffline ? Colors.orange : muted,
- fontSize: 11, fontWeight: FontWeight.w500,
- ),
- ),
- ],
- ),
- ),
+ }
+ },
+ child: Icon(
+ effectiveOffline ? Icons.cloud_off_rounded : Icons.cloud_done_rounded,
+ size: 16, color: effectiveOffline ? Colors.orange : Colors.green,
),
- const SizedBox(width: 8),
+ ),
+ actions: [
// Stop button (visible when playing)
if (_player.hasBook)
GestureDetector(
diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart
index ef063e31..986db9a1 100644
--- a/lib/screens/home_screen.dart
+++ b/lib/screens/home_screen.dart
@@ -314,62 +314,29 @@ class _HomeScreenState extends State<HomeScreen> {
SliverToBoxAdapter(
child: AbsorbPageHeader(
title: 'Home',
- actions: [
- GestureDetector(
- onTap: () {
- final newVal = !lib.isManualOffline;
- lib.setManualOffline(newVal);
- if (newVal) {
- final dl = DownloadService();
- final player = AudioPlayerService();
- final itemId = player.currentItemId;
- final epId = player.currentEpisodeId;
- final dlKey = epId != null && itemId != null
- ? '$itemId-$epId'
- : itemId;
- if (dlKey == null || !dl.isDownloaded(dlKey)) {
- player.stop();
- }
+ trailing: GestureDetector(
+ onTap: () {
+ final newVal = !lib.isManualOffline;
+ lib.setManualOffline(newVal);
+ if (newVal) {
+ final dl = DownloadService();
+ final player = AudioPlayerService();
+ final itemId = player.currentItemId;
+ final epId = player.currentEpisodeId;
+ final dlKey = epId != null && itemId != null
+ ? '$itemId-$epId'
+ : itemId;
+ if (dlKey == null || !dl.isDownloaded(dlKey)) {
+ player.stop();
}
- },
- child: AnimatedContainer(
- duration: const Duration(milliseconds: 300),
- curve: Curves.easeOutCubic,
- padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
- decoration: BoxDecoration(
- color: lib.isOffline
- ? Colors.orange.withValues(alpha: 0.15)
- : cs.onSurface.withValues(alpha: 0.06),
- borderRadius: BorderRadius.circular(20),
- border: Border.all(
- color: lib.isOffline
- ? Colors.orange.withValues(alpha: 0.3)
- : cs.onSurface.withValues(alpha: 0.08),
- ),
- ),
- child: Row(
- mainAxisSize: MainAxisSize.min,
- children: [
- Icon(
- lib.isOffline
- ? Icons.airplanemode_active_rounded
- : Icons.airplanemode_inactive_rounded,
- size: 14,
- color: lib.isOffline ? Colors.orange : cs.onSurfaceVariant,
- ),
- const SizedBox(width: 4),
- Text(
- lib.isOffline ? 'Offline' : 'Online',
- style: TextStyle(
- color: lib.isOffline ? Colors.orange : cs.onSurfaceVariant,
- fontSize: 11,
- fontWeight: FontWeight.w500,
- ),
- ),
- ],
- ),
- ),
+ }
+ },
+ child: Icon(
+ lib.isOffline ? Icons.cloud_off_rounded : Icons.cloud_done_rounded,
+ size: 16, color: lib.isOffline ? Colors.orange : Colors.green,
),
+ ),
+ actions: [
if (allLibraries.length > 1)
Material(
color: cs.onSurface.withValues(alpha: 0.06),
diff --git a/lib/screens/library_screen.dart b/lib/screens/library_screen.dart
index cd39582c..006576ac 100644
--- a/lib/screens/library_screen.dart
+++ b/lib/screens/library_screen.dart
@@ -140,6 +140,7 @@ class LibraryScreenState extends State<LibraryScreen> with TickerProviderStateMi
}
void _onLibraryProviderChanged() {
+ if (!mounted) return;
final lib = context.read<LibraryProvider>();
if (lib.selectedLibraryId != _lastLibraryId && lib.selectedLibraryId != null) {
_lastLibraryId = lib.selectedLibraryId;
diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart
index a379b0e3..0607fb44 100644
--- a/lib/screens/settings_screen.dart
+++ b/lib/screens/settings_screen.dart
@@ -91,22 +91,24 @@ class _SettingsScreenState extends State<SettingsScreen> {
GlobalKey _keyFor(String section) => _sectionKeys.putIfAbsent(section, () => GlobalKey());
void _onSectionExpanded(String section, bool expanded) {
- setState(() {
+ WidgetsBinding.instance.addPostFrameCallback((_) {
+ if (!mounted) return;
+ setState(() {
+ if (expanded) {
+ _expandedSection = section;
+ } else if (_expandedSection == section) {
+ _expandedSection = null;
+ }
+ });
if (expanded) {
- _expandedSection = section;
- } else if (_expandedSection == section) {
- _expandedSection = null;
+ Future.delayed(const Duration(milliseconds: 350), () {
+ final ctx = _keyFor(section).currentContext;
+ if (ctx != null && mounted) {
+ Scrollable.ensureVisible(ctx, duration: const Duration(milliseconds: 250), curve: Curves.easeOut, alignment: 0.3);
+ }
+ });
}
});
- if (expanded) {
- // Wait for the collapse/expand animations to finish before scrolling
- Future.delayed(const Duration(milliseconds: 350), () {
- final ctx = _keyFor(section).currentContext;
- if (ctx != null && mounted) {
- Scrollable.ensureVisible(ctx, duration: const Duration(milliseconds: 250), curve: Curves.easeOut, alignment: 0.3);
- }
- });
- }
}
@override
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 3f52c7a0..a1e25270 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -2293,6 +2293,7 @@ class AudioPlayerService extends ChangeNotifier {
currentTime: _totalDuration,
duration: _totalDuration,
speed: speed,
+ isFinished: true,
);
}
diff --git a/lib/services/chromecast_service.dart b/lib/services/chromecast_service.dart
index f55e348f..c59018b3 100644
--- a/lib/services/chromecast_service.dart
+++ b/lib/services/chromecast_service.dart
@@ -662,6 +662,7 @@ class ChromecastService extends ChangeNotifier {
currentTime: duration,
duration: duration,
speed: 1.0,
+ isFinished: true,
);
}
diff --git a/lib/services/progress_sync_service.dart b/lib/services/progress_sync_service.dart
index ab24ddab..8897972c 100644
--- a/lib/services/progress_sync_service.dart
+++ b/lib/services/progress_sync_service.dart
@@ -41,12 +41,19 @@ class ProgressSyncService {
required double currentTime,
required double duration,
required double speed,
+ bool? isFinished,
}) async {
+ // Preserve existing isFinished flag if not explicitly provided
+ if (isFinished == null) {
+ final existing = await getLocal(itemId);
+ isFinished = existing?['isFinished'] as bool? ?? false;
+ }
final data = {
'itemId': itemId,
'currentTime': currentTime,
'duration': duration,
'speed': speed,
+ 'isFinished': isFinished,
'timestamp': DateTime.now().millisecondsSinceEpoch,
};
await ScopedPrefs.setString('progress_$itemId', jsonEncode(data));
@@ -63,12 +70,14 @@ class ProgressSyncService {
required String itemId,
required double currentTime,
required double duration,
+ bool isFinished = false,
}) async {
final data = {
'itemId': itemId,
'currentTime': currentTime,
'duration': duration,
'speed': 1.0,
+ 'isFinished': isFinished,
'timestamp': 0,
};
await ScopedPrefs.setString('progress_$itemId', jsonEncode(data));
@@ -124,7 +133,8 @@ class ProgressSyncService {
final currentTime = (data['currentTime'] as num?)?.toDouble() ?? 0;
final duration = (data['duration'] as num?)?.toDouble() ?? 0;
- debugPrint('[Sync] Syncing $itemId: currentTime=$currentTime, duration=$duration, sessionId=$sessionId');
+ final isFinished = data['isFinished'] as bool? ?? false;
+ debugPrint('[Sync] Syncing $itemId: currentTime=$currentTime, duration=$duration, isFinished=$isFinished, sessionId=$sessionId');
try {
if (sessionId != null) {
@@ -138,6 +148,7 @@ class ProgressSyncService {
itemId,
currentTime: currentTime,
duration: duration,
+ isFinished: isFinished,
);
}
@@ -204,6 +215,7 @@ class ProgressSyncService {
itemId: itemId,
currentTime: serverTime,
duration: localDuration,
+ isFinished: serverProgress['isFinished'] as bool? ?? false,
);
final updated = await ScopedPrefs.getStringList('pending_syncs');
updated.remove(itemId);
@@ -221,17 +233,20 @@ class ProgressSyncService {
final apiItemId = isCompound ? itemId.substring(0, 36) : itemId;
final episodeId = isCompound ? itemId.substring(37) : null;
+ final localFinished = data['isFinished'] as bool? ?? false;
if (episodeId != null) {
await api.updateEpisodeProgress(
apiItemId, episodeId,
currentTime: localTime,
duration: localDuration,
+ isFinished: localFinished,
);
} else {
await api.updateProgress(
apiItemId,
currentTime: localTime,
duration: localDuration,
+ isFinished: localFinished,
);
}
debugPrint('[Sync] Flushed $itemId via progress update: ${localTime}s');
diff --git a/lib/widgets/absorb_page_header.dart b/lib/widgets/absorb_page_header.dart
index 6d15dbe6..6c263d2a 100644
--- a/lib/widgets/absorb_page_header.dart
+++ b/lib/widgets/absorb_page_header.dart
@@ -11,6 +11,7 @@ class AbsorbPageHeader extends StatelessWidget {
final Color? brandingColor;
final Color? titleColor;
final List<Widget>? actions;
+ final Widget? trailing;
final EdgeInsetsGeometry padding;
const AbsorbPageHeader({
@@ -19,6 +20,7 @@ class AbsorbPageHeader extends StatelessWidget {
this.brandingColor,
this.titleColor,
this.actions,
+ this.trailing,
this.padding = const EdgeInsets.fromLTRB(20, 12, 20, 0),
});
@@ -49,6 +51,10 @@ class AbsorbPageHeader extends StatelessWidget {
fontWeight: FontWeight.w300,
),
),
+ if (trailing != null) ...[
+ const SizedBox(width: 8),
+ trailing!,
+ ],
const Spacer(),
if (actions != null)
ConstrainedBox(