Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Play the next downloaded episode of a podcast while offline, even when it was never put on the queue

Barnabas587ac5d40298
diff --git a/lib/providers/_lp_absorbing.dart b/lib/providers/_lp_absorbing.dart
index d33edfad..5541c801 100644
--- a/lib/providers/_lp_absorbing.dart
+++ b/lib/providers/_lp_absorbing.dart
@@ -1242,7 +1242,54 @@ mixin _AbsorbingMixin on ChangeNotifier, _StateMixin, _CoreMixin {
episodes[publishedAt] = entry;
}
}
- if (finishedTimestamp == null || episodes.isEmpty) return;
+ // The queue only knows episodes that were put on it. Offline, the
+ // show's other downloads are just as playable, and each carries its
+ // own publish date, so they join the pick in date order too.
+ for (final info in dl.downloadedItems) {
+ if (!info.itemId.startsWith('$showId-')) continue;
+ final epId = info.itemId.substring(37);
+ if (epId != finishedEpId &&
+ (episodes.values.any((e) => e.key == info.itemId) ||
+ _progressMap[info.itemId]?['isFinished'] == true)) {
+ continue;
+ }
+ Map<String, dynamic>? session;
+ try {
+ session = jsonDecode(info.sessionData ?? '') as Map<String, dynamic>;
+ } catch (_) {
+ continue;
+ }
+ final item = session['libraryItem'] as Map<String, dynamic>? ?? {};
+ final media = item['media'] as Map<String, dynamic>? ?? {};
+ final own = (media['episodes'] as List<dynamic>? ?? const [])
+ .whereType<Map<String, dynamic>>()
+ .firstWhere((e) => e['id'] == epId, orElse: () => const {});
+ final publishedAt = (own['publishedAt'] as num?)?.toInt();
+ if (publishedAt == null) continue;
+ if (epId == finishedEpId) {
+ finishedTimestamp ??= publishedAt;
+ continue;
+ }
+ final entity = <String, dynamic>{
+ 'id': showId,
+ 'libraryId': item['libraryId'] ?? info.libraryId,
+ 'media': {
+ 'metadata': media['metadata'] ??
+ {'title': info.title ?? '', 'authorName': info.author ?? ''},
+ },
+ 'recentEpisode': {
+ ...own,
+ 'duration': own['duration'] ?? session['duration'],
+ },
+ '_absorbingKey': info.itemId,
+ };
+ episodes[publishedAt] = MapEntry(info.itemId, entity);
+ }
+ if (finishedTimestamp == null || episodes.isEmpty) {
+ debugPrint('[AutoAdvance] offline podcast advance: nothing to play next '
+ '(finished date known=${finishedTimestamp != null}, candidates=${episodes.length})');
+ return;
+ }
final sorted = episodes.keys.toList()..sort();
final int? nextTimestamp;
@@ -1259,9 +1306,11 @@ mixin _AbsorbingMixin on ChangeNotifier, _StateMixin, _CoreMixin {
final ep = nextData['recentEpisode'] as Map<String, dynamic>;
final nextEpId = ep['id'] as String;
+ _absorbingItemCache[nextKey] ??= nextData;
_absorbingIdsAdd(nextKey, afterKey: finishedKey);
_saveManualAbsorbing();
notifyListeners();
+ debugPrint('[AutoAdvance] offline podcast advance -> $nextKey');
final media = nextData['media'] as Map<String, dynamic>? ?? {};
final metadata = media['metadata'] as Map<String, dynamic>? ?? {};
diff --git a/lib/services/download_service.dart b/lib/services/download_service.dart
index 5dea5173..e54b8f35 100644
--- a/lib/services/download_service.dart
+++ b/lib/services/download_service.dart
@@ -259,14 +259,31 @@ String? _stripLibraryItem(String? sessionJson) {
if (libItem == null) return sessionJson;
final media = libItem['media'] as Map<String, dynamic>?;
if (media != null) {
+ final own = ownEpisodeRecord(media, session['episodeId'] as String?);
media.remove('episodes');
media.remove('audioFiles');
+ if (own != null) media['episodes'] = [own];
}
return jsonEncode(session);
} catch (_) {}
return sessionJson;
}
+/// The small part of a podcast episode's record worth keeping with its
+/// download: enough to order the show's downloads by publish date offline,
+/// where the server's episode list is out of reach.
+Map<String, dynamic>? ownEpisodeRecord(Map<String, dynamic> media, String? episodeId) {
+ if (episodeId == null) return null;
+ for (final e in media['episodes'] as List<dynamic>? ?? const []) {
+ if (e is! Map<String, dynamic> || e['id'] != episodeId) continue;
+ return {
+ for (final k in const ['id', 'title', 'publishedAt', 'pubDate', 'duration', 'season', 'episode', 'index'])
+ if (e[k] != null) k: e[k],
+ };
+ }
+ return null;
+}
+
/// Sanitize a string for use as a filesystem directory/file name.
String _sanitizePath(String name) {
// Replace filesystem-illegal characters with underscore
@@ -1805,9 +1822,11 @@ class DownloadService extends ChangeNotifier {
final slimItem = Map<String, dynamic>.from(fullItem);
final media = slimItem['media'] as Map<String, dynamic>?;
if (media != null) {
+ final own = ownEpisodeRecord(media, episodeId);
slimItem['media'] = Map<String, dynamic>.from(media)
..remove('episodes')
..remove('audioFiles');
+ if (own != null) (slimItem['media'] as Map<String, dynamic>)['episodes'] = [own];
}
slimItem['libraryFiles'] = [if (ebookFile != null) ebookFile];
slimSession['libraryItem'] = slimItem;