Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Extract library tab grids and episode detail/row widgets

Nathan Poulson8880cfec8af7
diff --git a/lib/screens/library_screen.dart b/lib/screens/library_screen.dart
index 5907578b..d3c3225a 100644
--- a/lib/screens/library_screen.dart
+++ b/lib/screens/library_screen.dart
@@ -10,10 +10,12 @@ import '../services/api_service.dart';
import '../services/download_service.dart';
import '../services/audio_player_service.dart';
import '../widgets/absorb_page_header.dart';
-import '../widgets/library_grid_tiles.dart';
import '../widgets/library_search_results.dart';
import '../main.dart' show oledNotifier;
import '../widgets/library_sort_filter_sheet.dart';
+import '../widgets/library_books_tab.dart';
+import '../widgets/library_series_tab.dart';
+import '../widgets/library_authors_tab.dart';
/// Responsive grid column count based on available width.
/// Returns 3 on phones, scales up on tablets/iPads.
@@ -1224,7 +1226,7 @@ class LibraryScreenState extends State<LibraryScreen> with TickerProviderStateMi
? _buildSearchResults(cs, tt)
: hasTabs
? _buildTabbedContent(cs, tt)
- : _buildGrid(cs, tt),
+ : _buildGrid(),
// Floating tab bar at bottom (book libraries only, hidden during search)
if (hasTabs)
Positioned(
@@ -1400,9 +1402,9 @@ class LibraryScreenState extends State<LibraryScreen> with TickerProviderStateMi
return IndexedStack(
index: _currentTab,
children: [
- _buildGrid(cs, tt),
- _buildSeriesGrid(cs, tt),
- _buildAuthorsGrid(cs, tt),
+ _buildGrid(),
+ _buildSeriesGrid(),
+ _buildAuthorsGrid(),
],
);
}
@@ -1444,194 +1446,46 @@ class LibraryScreenState extends State<LibraryScreen> with TickerProviderStateMi
// ═══════════════════════════════════════════════════════════════
// LIBRARY TAB - BROWSE GRID
// ═══════════════════════════════════════════════════════════════
- Widget _buildGrid(ColorScheme cs, TextTheme tt) {
- if (_items.isEmpty && _isLoadingPage) {
- return const Center(child: CircularProgressIndicator());
- }
- if (_items.isEmpty && !_isLoadingPage) {
- final filterMsg = switch (_filter) {
- LibraryFilter.inProgress => 'No books in progress',
- LibraryFilter.finished => 'No finished books',
- LibraryFilter.notStarted => 'All books have been started',
- LibraryFilter.downloaded => 'No downloaded books',
- LibraryFilter.inASeries => 'No series found',
- LibraryFilter.hasEbook => 'No books with eBooks',
- LibraryFilter.genre => 'No books in "${_genreFilter ?? 'genre'}"',
- LibraryFilter.none => 'No books found',
- };
- return RefreshIndicator(
- onRefresh: _refreshAll,
- child: CustomScrollView(
- physics: const AlwaysScrollableScrollPhysics(),
- slivers: [
- SliverFillRemaining(
- child: Center(
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Icon(Icons.library_books_outlined,
- size: 56, color: cs.onSurfaceVariant.withValues(alpha: 0.3)),
- const SizedBox(height: 12),
- Text(filterMsg,
- style: tt.bodyLarge?.copyWith(color: cs.onSurfaceVariant)),
- if (_filter != LibraryFilter.none) ...[
- const SizedBox(height: 8),
- GestureDetector(
- onTap: () => _changeFilter(LibraryFilter.none),
- child: Text('Clear filter',
- style: tt.bodySmall?.copyWith(color: cs.primary)),
- ),
- ],
- ],
- ),
- ),
- ),
- ],
- ),
- );
- }
-
- return RefreshIndicator(
+ Widget _buildGrid() {
+ return LibraryBooksTab(
+ items: _items,
+ isLoadingPage: _isLoadingPage,
+ hasMore: _hasMore,
+ scrollController: _scrollController,
+ filter: _filter,
+ genreFilter: _genreFilter,
+ rectangleCovers: _rectangleCovers,
+ coverAspectRatio: _coverAspectRatio,
onRefresh: _refreshAll,
- child: GridView.builder(
- controller: _scrollController,
- physics: const AlwaysScrollableScrollPhysics(),
- padding: const EdgeInsets.fromLTRB(16, 8, 16, 100),
- gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
- crossAxisCount: responsiveGridCount(context),
- childAspectRatio: _rectangleCovers ? 0.48 : 0.68,
- crossAxisSpacing: 10,
- mainAxisSpacing: 10,
- ),
- itemCount: _items.length + (_hasMore ? 1 : 0),
- itemBuilder: (context, index) {
- if (index >= _items.length) {
- return const Center(
- child: Padding(
- padding: EdgeInsets.all(16),
- child: CircularProgressIndicator(strokeWidth: 2),
- ),
- );
- }
- final item = _items[index];
- if (item.containsKey('collapsedSeries')) {
- return GridSeriesTile(item: item, coverAspectRatio: _coverAspectRatio);
- }
- return GridBookTile(item: item, coverAspectRatio: _coverAspectRatio);
- },
- ),
+ onClearFilter: () => _changeFilter(LibraryFilter.none),
);
}
// ═══════════════════════════════════════════════════════════════
// SERIES TAB - GRID
// ═══════════════════════════════════════════════════════════════
- Widget _buildSeriesGrid(ColorScheme cs, TextTheme tt) {
- if (_seriesItems.isEmpty && _isLoadingSeriesPage) {
- return const Center(child: CircularProgressIndicator());
- }
- if (_seriesItems.isEmpty && !_isLoadingSeriesPage) {
- return RefreshIndicator(
- onRefresh: _refreshSeries,
- child: CustomScrollView(
- physics: const AlwaysScrollableScrollPhysics(),
- slivers: [
- SliverFillRemaining(
- child: Center(
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Icon(Icons.collections_bookmark_outlined,
- size: 56, color: cs.onSurfaceVariant.withValues(alpha: 0.3)),
- const SizedBox(height: 12),
- Text('No series found',
- style: tt.bodyLarge?.copyWith(color: cs.onSurfaceVariant)),
- ],
- ),
- ),
- ),
- ],
- ),
- );
- }
-
- return RefreshIndicator(
+ Widget _buildSeriesGrid() {
+ return LibrarySeriesTab(
+ seriesItems: _seriesItems,
+ isLoadingSeriesPage: _isLoadingSeriesPage,
+ hasMoreSeries: _hasMoreSeries,
+ scrollController: _seriesScrollController,
+ rectangleCovers: _rectangleCovers,
+ coverAspectRatio: _coverAspectRatio,
onRefresh: _refreshSeries,
- child: GridView.builder(
- controller: _seriesScrollController,
- physics: const AlwaysScrollableScrollPhysics(),
- padding: const EdgeInsets.fromLTRB(16, 8, 16, 100),
- gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
- crossAxisCount: responsiveGridCount(context),
- childAspectRatio: _rectangleCovers ? 0.48 : 0.68,
- crossAxisSpacing: 10,
- mainAxisSpacing: 10,
- ),
- itemCount: _seriesItems.length + (_hasMoreSeries ? 1 : 0),
- itemBuilder: (context, index) {
- if (index >= _seriesItems.length) {
- return const Center(
- child: Padding(
- padding: EdgeInsets.all(16),
- child: CircularProgressIndicator(strokeWidth: 2),
- ),
- );
- }
- return GridSeriesTileDirect(series: _seriesItems[index], coverAspectRatio: _coverAspectRatio);
- },
- ),
);
}
// ═══════════════════════════════════════════════════════════════
// AUTHORS TAB - GRID
// ═══════════════════════════════════════════════════════════════
- Widget _buildAuthorsGrid(ColorScheme cs, TextTheme tt) {
- if (_isLoadingAuthors) {
- return const Center(child: CircularProgressIndicator());
- }
- if (_authors.isEmpty && _authorsLoaded) {
- return RefreshIndicator(
- onRefresh: _refreshAuthors,
- child: CustomScrollView(
- physics: const AlwaysScrollableScrollPhysics(),
- slivers: [
- SliverFillRemaining(
- child: Center(
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Icon(Icons.people_outline_rounded,
- size: 56, color: cs.onSurfaceVariant.withValues(alpha: 0.3)),
- const SizedBox(height: 12),
- Text('No authors found',
- style: tt.bodyLarge?.copyWith(color: cs.onSurfaceVariant)),
- ],
- ),
- ),
- ),
- ],
- ),
- );
- }
-
- return RefreshIndicator(
+ Widget _buildAuthorsGrid() {
+ return LibraryAuthorsTab(
+ authors: _authors,
+ isLoadingAuthors: _isLoadingAuthors,
+ authorsLoaded: _authorsLoaded,
+ scrollController: _authorsScrollController,
onRefresh: _refreshAuthors,
- child: GridView.builder(
- controller: _authorsScrollController,
- physics: const AlwaysScrollableScrollPhysics(),
- padding: const EdgeInsets.fromLTRB(16, 8, 16, 100),
- gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
- crossAxisCount: responsiveGridCount(context),
- childAspectRatio: 0.68,
- crossAxisSpacing: 10,
- mainAxisSpacing: 10,
- ),
- itemCount: _authors.length,
- itemBuilder: (context, index) {
- return GridAuthorTile(author: _authors[index]);
- },
- ),
);
}
diff --git a/lib/widgets/episode_detail_sheet.dart b/lib/widgets/episode_detail_sheet.dart
new file mode 100644
index 00000000..193095f7
--- /dev/null
+++ b/lib/widgets/episode_detail_sheet.dart
@@ -0,0 +1,656 @@
+import 'dart:ui';
+import 'package:flutter/material.dart';
+import 'package:provider/provider.dart';
+import 'package:cached_network_image/cached_network_image.dart';
+import '../providers/library_provider.dart';
+import '../services/audio_player_service.dart';
+import '../services/download_service.dart';
+import '../services/progress_sync_service.dart';
+import '../services/chromecast_service.dart';
+import '../providers/auth_provider.dart';
+import 'card_buttons.dart';
+import 'html_description.dart';
+import 'playlist_picker_sheet.dart';
+import 'stackable_sheet.dart';
+import 'episode_list_sheet.dart';
+
+class EpisodeDetailSheet extends StatefulWidget {
+ final Map<String, dynamic> podcastItem;
+ final Map<String, dynamic> episode;
+ final ScrollController? scrollController;
+
+ const EpisodeDetailSheet({super.key, required this.podcastItem, required this.episode})
+ : scrollController = null;
+
+ const EpisodeDetailSheet._({
+ required this.podcastItem,
+ required this.episode,
+ required this.scrollController,
+ }) : super(key: null);
+
+ static void show(BuildContext context, Map<String, dynamic> podcastItem, Map<String, dynamic> episode) {
+ showStackableSheet(
+ context: context,
+ useSafeArea: true,
+ initialChildSize: 0.75,
+ maxChildSize: 0.95,
+ builder: (_, scrollController) => EpisodeDetailSheet._(
+ podcastItem: podcastItem,
+ episode: episode,
+ scrollController: scrollController,
+ ),
+ );
+ }
+
+ @override
+ State<EpisodeDetailSheet> createState() => _EpisodeDetailSheetState();
+}
+
+class _EpisodeDetailSheetState extends State<EpisodeDetailSheet> {
+ bool _chaptersExpanded = false;
+
+ String get _itemId => widget.podcastItem['id'] as String? ?? '';
+
+ String get _showTitle {
+ final media = widget.podcastItem['media'] as Map<String, dynamic>? ?? {};
+ final meta = media['metadata'] as Map<String, dynamic>? ?? {};
+ return meta['title'] as String? ?? '';
+ }
+
+ String get _episodeTitle => widget.episode['title'] as String? ?? 'Episode';
+ String get _episodeId => widget.episode['id'] as String? ?? '';
+ double get _duration {
+ final d = (widget.episode['duration'] as num?)?.toDouble() ?? 0;
+ if (d > 0) return d;
+ // recentEpisode from ABS personalized sections often omits top-level duration
+ final af = widget.episode['audioFile'] as Map<String, dynamic>?;
+ return (af?['duration'] as num?)?.toDouble() ?? 0;
+ }
+ int get _publishedAt => (widget.episode['publishedAt'] as num?)?.toInt() ?? 0;
+ String? get _episodeNumber => widget.episode['episode'] as String?;
+ String? get _season => widget.episode['season'] as String?;
+ List<dynamic> get _chapters => widget.episode['chapters'] as List<dynamic>? ?? [];
+
+ String get _rawDescription => widget.episode['description'] as String? ?? '';
+
+ Future<void> _play() async {
+ final auth = context.read<AuthProvider>();
+ final api = auth.apiService;
+ if (api == null) return;
+
+ final cast = ChromecastService();
+ if (cast.isConnected) {
+ await cast.castItem(
+ api: api, itemId: _itemId, title: _episodeTitle, author: _showTitle,
+ coverUrl: api.getCoverUrl(_itemId), totalDuration: _duration, chapters: _chapters,
+ episodeId: _episodeId,
+ );
+ if (mounted) Navigator.of(context, rootNavigator: true).popUntil((route) => route.isFirst);
+ return;
+ }
+
+ final error = await AudioPlayerService().playItem(
+ api: api, itemId: _itemId, title: _episodeTitle, author: _showTitle,
+ coverUrl: api.getCoverUrl(_itemId), totalDuration: _duration, chapters: _chapters,
+ episodeId: _episodeId,
+ episodeTitle: _episodeTitle,
+ );
+ if (mounted) {
+ if (error != null) showErrorSnackBar(context, error);
+ Navigator.of(context, rootNavigator: true).popUntil((route) => route.isFirst);
+ }
+ }
+
+ Future<void> _download() async {
+ final auth = context.read<AuthProvider>();
+ final api = auth.apiService;
+ if (api == null) return;
+
+ final error = await DownloadService().downloadItem(
+ api: api,
+ itemId: '$_itemId-$_episodeId',
+ title: _episodeTitle,
+ author: _showTitle,
+ coverUrl: api.getCoverUrl(_itemId),
+ episodeId: _episodeId,
+ );
+ if (error != null && mounted) {
+ ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(error)));
+ }
+ }
+
+ Future<void> _toggleFinished() async {
+ final auth = context.read<AuthProvider>();
+ final api = auth.apiService;
+ if (api == null) return;
+ final lib = context.read<LibraryProvider>();
+ final key = '$_itemId-$_episodeId';
+ final progressData = lib.getEpisodeProgressData(_itemId, _episodeId);
+ final isFinished = progressData?['isFinished'] == true;
+ final currentTime = (progressData?['currentTime'] as num?)?.toDouble() ?? 0;
+
+ try {
+ if (isFinished) {
+ // Confirm before un-finishing
+ final confirmed = await showDialog<bool>(
+ context: context,
+ builder: (ctx) => AlertDialog(
+ title: const Text('Mark as Not Finished?'),
+ content: const Text('This will clear the finished status but keep your current position.'),
+ actions: [
+ TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('Cancel')),
+ FilledButton(onPressed: () => Navigator.pop(ctx, true), child: const Text('Unmark')),
+ ],
+ ),
+ );
+ if (confirmed != true) return;
+ // Un-finish — keep current position
+ await api.updateEpisodeProgress(
+ _itemId, _episodeId,
+ currentTime: currentTime,
+ duration: _duration,
+ isFinished: false,
+ );
+ await lib.refresh();
+ if (mounted) {
+ ScaffoldMessenger.of(context).showSnackBar(SnackBar(
+ content: const Text('Marked as not finished'),
+ behavior: SnackBarBehavior.floating,
+ duration: const Duration(seconds: 2),
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
+ ));
+ }
+ } else {
+ // Confirm before marking finished
+ final confirmed = await showDialog<bool>(
+ context: context,
+ builder: (ctx) => AlertDialog(
+ title: const Text('Mark as Fully Absorbed?'),
+ content: const Text('This will set your progress to 100% for this episode.'),
+ actions: [
+ TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('Cancel')),
+ FilledButton(onPressed: () => Navigator.pop(ctx, true), child: const Text('Fully Absorb')),
+ ],
+ ),
+ );
+ if (confirmed != true) return;
+ // Mark finished — update server then local state for instant UI
+ await api.updateEpisodeProgress(
+ _itemId, _episodeId,
+ currentTime: _duration,
+ duration: _duration,
+ isFinished: true,
+ );
+ lib.markFinishedLocally(key, skipAutoAdvance: true);
+ if (mounted) {
+ ScaffoldMessenger.of(context).showSnackBar(SnackBar(
+ content: const Text('Marked as finished — nice!'),
+ behavior: SnackBarBehavior.floating,
+ duration: const Duration(seconds: 2),
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
+ ));
+ }
+ }
+ } catch (_) {
+ if (mounted) {
+ ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
+ content: Text('Failed to update — check your connection')));
+ }
+ }
+ }
+
+ void _confirmDeleteDownload(BuildContext context, String dlKey) {
+ showDialog(context: context, builder: (ctx) => AlertDialog(
+ title: const Text('Remove download?'),
+ content: const Text('This will be removed from your device.'),
+ actions: [
+ TextButton(onPressed: () => Navigator.pop(ctx), child: const Text('Cancel')),
+ TextButton(onPressed: () {
+ DownloadService().deleteDownload(dlKey);
+ 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))),
+ ],
+ ));
+ }
+
+ String? get _coverUrl {
+ final auth = context.read<AuthProvider>();
+ return auth.apiService?.getCoverUrl(_itemId, width: 800);
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final cs = Theme.of(context).colorScheme;
+ final tt = Theme.of(context).textTheme;
+ final lib = context.watch<LibraryProvider>();
+ final coverUrl = _coverUrl;
+
+ final dlKey = '$_itemId-$_episodeId';
+
+ final progress = lib.getEpisodeProgress(_itemId, _episodeId);
+ final progressData = lib.getEpisodeProgressData(_itemId, _episodeId);
+ final isFinished = progressData?['isFinished'] == true;
+
+ String dateLabel = '';
+ if (_publishedAt > 0) {
+ final date = DateTime.fromMillisecondsSinceEpoch(_publishedAt);
+ final diff = DateTime.now().difference(date);
+ if (diff.inDays == 0) dateLabel = 'Today';
+ else if (diff.inDays == 1) dateLabel = 'Yesterday';
+ else if (diff.inDays < 7) dateLabel = '${diff.inDays}d ago';
+ else if (diff.inDays < 30) dateLabel = '${(diff.inDays / 7).floor()}w ago';
+ else dateLabel = '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
+ }
+
+ String durationLabel = '';
+ if (_duration > 0) {
+ final h = (_duration / 3600).floor();
+ final m = ((_duration % 3600) / 60).floor();
+ durationLabel = h > 0 ? '${h}h ${m}m' : '${m}m';
+ }
+
+ return Container(
+ clipBehavior: Clip.antiAlias,
+ decoration: BoxDecoration(
+ color: Theme.of(context).scaffoldBackgroundColor,
+ borderRadius: const BorderRadius.vertical(top: Radius.circular(24)),
+ ),
+ child: Stack(children: [
+ // Blurred cover background
+ if (coverUrl != null)
+ Positioned.fill(
+ child: RepaintBoundary(
+ child: CachedNetworkImage(
+ imageUrl: coverUrl, fit: BoxFit.cover,
+ httpHeaders: lib.mediaHeaders,
+ imageBuilder: (_, p) => ImageFiltered(
+ imageFilter: ImageFilter.blur(sigmaX: 50, sigmaY: 50, tileMode: TileMode.decal),
+ child: Image(image: p, fit: BoxFit.cover)),
+ placeholder: (_, __) => const SizedBox(),
+ errorWidget: (_, __, ___) => const SizedBox(),
+ ),
+ ),
+ ),
+ // Gradient overlay
+ Positioned.fill(child: DecoratedBox(decoration: BoxDecoration(gradient: LinearGradient(
+ begin: Alignment.topCenter, end: Alignment.bottomCenter,
+ colors: [
+ Theme.of(context).scaffoldBackgroundColor.withValues(alpha: 0.6),
+ Theme.of(context).scaffoldBackgroundColor.withValues(alpha: 0.85),
+ Theme.of(context).scaffoldBackgroundColor,
+ ],
+ )))),
+ // Content
+ ListView(
+ controller: widget.scrollController,
+ padding: EdgeInsets.fromLTRB(20, 8, 20, 32 + MediaQuery.of(context).viewPadding.bottom),
+ children: [
+ // Drag handle
+ Center(child: Container(width: 40, height: 4, margin: const EdgeInsets.only(bottom: 16),
+ decoration: BoxDecoration(color: cs.onSurface.withValues(alpha: 0.24), borderRadius: BorderRadius.circular(2)))),
+
+ // Episode title (centered)
+ Text(_episodeTitle, textAlign: TextAlign.center,
+ style: tt.headlineSmall?.copyWith(fontWeight: FontWeight.w700, color: cs.onSurface)),
+ const SizedBox(height: 4),
+
+ // Show title
+ if (_showTitle.isNotEmpty)
+ Text(_showTitle, textAlign: TextAlign.center,
+ style: tt.bodyMedium?.copyWith(color: cs.onSurface.withValues(alpha: 0.6))),
+
+ const SizedBox(height: 12),
+
+ // Progress bar
+ if (progress > 0) ...[
+ ClipRRect(borderRadius: BorderRadius.circular(3),
+ child: LinearProgressIndicator(
+ value: progress.clamp(0.0, 1.0), minHeight: 4,
+ backgroundColor: cs.onSurface.withValues(alpha: 0.1),
+ valueColor: AlwaysStoppedAnimation(
+ isFinished ? cs.primary.withValues(alpha: 0.4) : cs.primary),
+ )),
+ const SizedBox(height: 4),
+ Text('${(progress * 100).toStringAsFixed(1)}% complete', textAlign: TextAlign.center,
+ style: tt.labelSmall?.copyWith(color: cs.onSurfaceVariant)),
+ const SizedBox(height: 12),
+ ],
+
+ // Play button (full width, matching book detail)
+ SizedBox(height: 52, child: FilledButton.icon(
+ onPressed: _play,
+ icon: Icon(
+ progress > 0 && !isFinished ? Icons.play_arrow_rounded : Icons.podcasts_rounded,
+ size: 24,
+ ),
+ label: Text(
+ progress > 0 && !isFinished ? 'Resume' : 'Play Episode',
+ style: tt.titleMedium?.copyWith(fontWeight: FontWeight.w600, color: cs.onPrimary),
+ ),
+ style: FilledButton.styleFrom(
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
+ ),
+ )),
+ const SizedBox(height: 12),
+
+ // Download + Finished row
+ Row(children: [
+ Expanded(child: ListenableBuilder(
+ listenable: DownloadService(),
+ builder: (context, _) {
+ final dl = DownloadService();
+ final downloaded = dl.isDownloaded(dlKey);
+ final downloading = dl.isDownloading(dlKey);
+ final dlProgress = dl.downloadProgress(dlKey);
+
+ final IconData icon;
+ final String label;
+ final Color color;
+ if (downloaded) {
+ icon = Icons.download_done_rounded;
+ label = 'Saved';
+ color = (Theme.of(context).brightness == Brightness.dark ? Colors.greenAccent : Colors.green.shade700).withValues(alpha: 0.7);
+ } else if (downloading) {
+ icon = Icons.downloading_rounded;
+ label = '${(dlProgress * 100).toStringAsFixed(0)}%';
+ color = cs.primary;
+ } else {
+ icon = Icons.download_outlined;
+ label = 'Download';
+ color = cs.onSurfaceVariant;
+ }
+
+ return GestureDetector(
+ onTap: downloaded
+ ? () => _confirmDeleteDownload(context, dlKey)
+ : downloading
+ ? () => DownloadService().cancelDownload(dlKey)
+ : _download,
+ child: Container(
+ height: 36,
+ clipBehavior: Clip.antiAlias,
+ decoration: BoxDecoration(
+ color: downloaded ? (Theme.of(context).brightness == Brightness.dark ? Colors.greenAccent : Colors.green.shade700).withValues(alpha: 0.06) : cs.onSurface.withValues(alpha: 0.06),
+ borderRadius: BorderRadius.circular(14),
+ border: Border.all(color: downloaded ? (Theme.of(context).brightness == Brightness.dark ? Colors.greenAccent : Colors.green.shade700).withValues(alpha: 0.15) : cs.onSurface.withValues(alpha: 0.08)),
+ ),
+ child: Stack(children: [
+ if (downloading)
+ FractionallySizedBox(
+ widthFactor: dlProgress.clamp(0.0, 1.0),
+ child: Container(
+ decoration: BoxDecoration(
+ color: cs.primary.withValues(alpha: 0.15),
+ borderRadius: BorderRadius.circular(13),
+ ),
+ ),
+ ),
+ Center(child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
+ Icon(icon, size: 16, color: color),
+ const SizedBox(width: 6),
+ Text(label, style: TextStyle(color: color, fontSize: 12, fontWeight: FontWeight.w500)),
+ ])),
+ ]),
+ ),
+ );
+ },
+ )),
+ const SizedBox(width: 8),
+ Expanded(child: GestureDetector(
+ onTap: _toggleFinished,
+ child: Container(
+ height: 36,
+ decoration: BoxDecoration(
+ color: isFinished ? Colors.green.withValues(alpha: 0.06) : cs.onSurface.withValues(alpha: 0.06),
+ borderRadius: BorderRadius.circular(14),
+ border: Border.all(color: isFinished ? Colors.green.withValues(alpha: 0.15) : cs.onSurface.withValues(alpha: 0.08)),
+ ),
+ child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
+ Icon(
+ isFinished ? Icons.check_circle_rounded : Icons.check_circle_outline_rounded,
+ size: 16,
+ color: isFinished ? Colors.green : cs.onSurfaceVariant,
+ ),
+ const SizedBox(width: 6),
+ Text(
+ isFinished ? 'Fully Absorbed' : 'Fully Absorb',
+ style: TextStyle(
+ color: isFinished ? Colors.green : cs.onSurfaceVariant,
+ fontSize: 12, fontWeight: FontWeight.w500,
+ ),
+ ),
+ ]),
+ ),
+ )),
+ const SizedBox(width: 8),
+ GestureDetector(
+ onTap: () => _showMoreSheet(context, lib, dlKey, progress, isFinished),
+ child: Container(
+ height: 36, width: 44,
+ decoration: BoxDecoration(
+ color: cs.onSurface.withValues(alpha: 0.06),
+ borderRadius: BorderRadius.circular(14),
+ border: Border.all(color: cs.onSurface.withValues(alpha: 0.1)),
+ ),
+ child: Icon(Icons.more_horiz_rounded, size: 18, color: cs.onSurfaceVariant),
+ ),
+ ),
+ ]),
+
+ // Metadata chips
+ const SizedBox(height: 16),
+ Wrap(spacing: 8, runSpacing: 8, children: [
+ if (dateLabel.isNotEmpty) _chip(Icons.calendar_today_rounded, dateLabel),
+ if (durationLabel.isNotEmpty) _chip(Icons.schedule_rounded, durationLabel),
+ if (_episodeNumber != null) _chip(Icons.tag_rounded, 'Episode $_episodeNumber'),
+ if (_season != null) _chip(Icons.layers_rounded, 'Season $_season'),
+ ]),
+
+ // All Episodes button (series-style)
+ const SizedBox(height: 16),
+ GestureDetector(
+ onTap: () {
+ final nav = Navigator.of(context);
+ nav.pop();
+ EpisodeListSheet.show(nav.context, widget.podcastItem);
+ },
+ child: Container(
+ padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
+ decoration: BoxDecoration(
+ color: cs.primary.withValues(alpha: 0.08),
+ borderRadius: BorderRadius.circular(12),
+ border: Border.all(color: cs.primary.withValues(alpha: 0.15)),
+ ),
+ child: Row(children: [
+ Icon(Icons.podcasts_rounded, size: 16, color: cs.primary.withValues(alpha: 0.7)),
+ const SizedBox(width: 8),
+ Expanded(child: Text('All Episodes',
+ style: tt.bodySmall?.copyWith(color: cs.primary.withValues(alpha: 0.9), fontWeight: FontWeight.w500))),
+ Icon(Icons.chevron_right_rounded, size: 18, color: cs.primary.withValues(alpha: 0.5)),
+ ]),
+ ),
+ ),
+
+ // Chapters
+ if (_chapters.isNotEmpty) ...[const SizedBox(height: 16),
+ GestureDetector(onTap: () => setState(() => _chaptersExpanded = !_chaptersExpanded),
+ child: Row(children: [
+ Text('Chapters (${_chapters.length})', style: tt.titleSmall?.copyWith(color: cs.onSurfaceVariant, fontWeight: FontWeight.w600)),
+ const Spacer(), Icon(_chaptersExpanded ? Icons.expand_less : Icons.expand_more, color: cs.onSurface.withValues(alpha: 0.3), size: 20)])),
+ if (_chaptersExpanded) ...[const SizedBox(height: 8),
+ ..._chapters.asMap().entries.map((e) {
+ final ch = e.value as Map<String, dynamic>;
+ return Padding(padding: const EdgeInsets.symmetric(vertical: 3),
+ child: Row(children: [
+ SizedBox(width: 28, child: Text('${e.key + 1}', style: tt.labelSmall?.copyWith(color: cs.onSurface.withValues(alpha: 0.3)))),
+ Expanded(child: Text(ch['title'] as String? ?? 'Chapter ${e.key + 1}', maxLines: 1, overflow: TextOverflow.ellipsis, style: tt.bodySmall?.copyWith(color: cs.onSurface.withValues(alpha: 0.6)))),
+ Text(_fmtDur(((ch['end'] as num?)?.toDouble() ?? 0) - ((ch['start'] as num?)?.toDouble() ?? 0)), style: tt.labelSmall?.copyWith(color: cs.onSurface.withValues(alpha: 0.3))),
+ ]));
+ })]],
+
+ // Description
+ if (_rawDescription.isNotEmpty) ...[
+ const SizedBox(height: 16),
+ Text('About This Episode', style: tt.titleSmall?.copyWith(color: cs.onSurfaceVariant, fontWeight: FontWeight.w600)),
+ const SizedBox(height: 6),
+ HtmlDescription(
+ html: _rawDescription,
+ maxLines: 4,
+ style: tt.bodySmall?.copyWith(color: cs.onSurface.withValues(alpha: 0.7), height: 1.5),
+ linkColor: cs.primary,
+ ),
+ ],
+ ],
+ ),
+ ]),
+ );
+ }
+
+ void _showMoreSheet(BuildContext context, LibraryProvider lib, String dlKey, double progress, bool isFinished) {
+ final cs = Theme.of(context).colorScheme;
+ showModalBottomSheet(
+ context: context,
+ backgroundColor: Theme.of(context).bottomSheetTheme.backgroundColor,
+ shape: const RoundedRectangleBorder(
+ borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
+ ),
+ builder: (ctx) {
+ return SafeArea(
+ child: Padding(
+ padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
+ child: Column(mainAxisSize: MainAxisSize.min, children: [
+ Center(child: Container(width: 40, height: 4, margin: const EdgeInsets.only(bottom: 16),
+ decoration: BoxDecoration(color: cs.onSurface.withValues(alpha: 0.24), borderRadius: BorderRadius.circular(2)))),
+ _moreItem(cs, lib.isOnAbsorbingList(dlKey)
+ ? Icons.remove_circle_outline_rounded : Icons.add_circle_outline_rounded,
+ lib.isOnAbsorbingList(dlKey) ? 'Remove from Absorbing' : 'Add to Absorbing',
+ onTap: () async {
+ Navigator.pop(ctx);
+ if (lib.isOnAbsorbingList(dlKey)) {
+ await lib.removeFromAbsorbing(dlKey);
+ if (context.mounted) {
+ ScaffoldMessenger.of(context).showSnackBar(SnackBar(
+ duration: const Duration(seconds: 3),
+ content: const Text('Removed from Absorbing'),
+ behavior: SnackBarBehavior.floating,
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12))));
+ }
+ } else {
+ await lib.addToAbsorbingQueue(dlKey);
+ final cached = Map<String, dynamic>.from(widget.podcastItem);
+ cached['recentEpisode'] = Map<String, dynamic>.from(widget.episode);
+ cached['_absorbingKey'] = dlKey;
+ lib.absorbingItemCache[dlKey] = cached;
+ if (context.mounted) {
+ ScaffoldMessenger.of(context).showSnackBar(SnackBar(
+ duration: const Duration(seconds: 3),
+ content: const Text('Added to Absorbing'),
+ behavior: SnackBarBehavior.floating,
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12))));
+ }
+ }
+ }),
+ if (!lib.isOffline)
+ _moreItem(cs, Icons.playlist_add_rounded, 'Add to Playlist',
+ onTap: () {
+ Navigator.pop(ctx);
+ PlaylistPickerSheet.show(
+ context,
+ widget.podcastItem['id'] as String,
+ episodeId: widget.episode['id'] as String?,
+ );
+ }),
+ if (progress > 0 || isFinished)
+ _moreItem(cs, Icons.restart_alt_rounded, 'Reset Progress',
+ onTap: () { Navigator.pop(ctx); _resetProgress(context); }),
+ ]),
+ ),
+ );
+ },
+ );
+ }
+
+ Widget _moreItem(ColorScheme cs, IconData icon, String label, {required VoidCallback onTap}) {
+ return Padding(
+ padding: const EdgeInsets.only(bottom: 6),
+ child: GestureDetector(onTap: onTap, child: Container(height: 44,
+ decoration: BoxDecoration(color: cs.onSurface.withValues(alpha: 0.06), borderRadius: BorderRadius.circular(14),
+ border: Border.all(color: cs.onSurface.withValues(alpha: 0.1))),
+ child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
+ Icon(icon, size: 16, color: cs.onSurfaceVariant), const SizedBox(width: 8),
+ Text(label, style: TextStyle(color: cs.onSurfaceVariant, fontSize: 13, fontWeight: FontWeight.w500))]))),
+ );
+ }
+
+ String _fmtDur(double s) {
+ final h = (s / 3600).floor(); final m = ((s % 3600) / 60).floor();
+ if (h > 0) return '${h}h ${m}m';
+ return '${m}m';
+ }
+
+ Future<void> _resetProgress(BuildContext context) async {
+ final confirmed = await showDialog<bool>(
+ context: context,
+ builder: (ctx) => AlertDialog(
+ title: const Text('Reset Progress?'),
+ content: const Text('This will erase all progress for this episode and set it back to the beginning. This can\'t be undone.'),
+ actions: [
+ TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('Cancel')),
+ FilledButton(onPressed: () => Navigator.pop(ctx, true),
+ style: FilledButton.styleFrom(backgroundColor: Theme.of(ctx).colorScheme.error),
+ child: const Text('Reset')),
+ ],
+ ),
+ );
+ if (confirmed != true) return;
+ final auth = context.read<AuthProvider>();
+ final api = auth.apiService;
+ if (api == null) return;
+ final player = AudioPlayerService();
+
+ if (player.currentItemId == _itemId && player.currentEpisodeId == _episodeId) {
+ await player.stopWithoutSaving();
+ }
+
+ final compoundKey = '$_itemId-$_episodeId';
+ await ProgressSyncService().deleteLocal(compoundKey);
+ final ok = await api.deleteEpisodeProgress(_itemId, _episodeId);
+ // Mark as unfinished with zero progress on the server
+ await api.updateEpisodeProgress(
+ _itemId, _episodeId,
+ currentTime: 0,
+ duration: _duration,
+ isFinished: false,
+ );
+
+ if (context.mounted) {
+ context.read<LibraryProvider>().resetProgressFor(compoundKey);
+ ScaffoldMessenger.of(context).showSnackBar(SnackBar(
+ duration: const Duration(seconds: 3),
+ content: Text(ok ? 'Progress reset — fresh start!' : 'Reset may not have synced — check your server'),
+ behavior: SnackBarBehavior.floating,
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12))));
+ }
+ }
+
+ Widget _chip(IconData icon, String text) {
+ final cs = Theme.of(context).colorScheme;
+ return Container(
+ constraints: const BoxConstraints(maxWidth: 200),
+ padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
+ decoration: BoxDecoration(color: cs.onSurface.withValues(alpha: 0.06), borderRadius: BorderRadius.circular(20),
+ border: Border.all(color: cs.onSurface.withValues(alpha: 0.08))),
+ child: Row(mainAxisSize: MainAxisSize.min, children: [
+ Icon(icon, size: 12, color: cs.onSurfaceVariant), const SizedBox(width: 4),
+ Flexible(child: Text(text, overflow: TextOverflow.ellipsis, maxLines: 1,
+ style: TextStyle(color: cs.onSurfaceVariant, fontSize: 11)))]));
+ }
+}
diff --git a/lib/widgets/episode_list_sheet.dart b/lib/widgets/episode_list_sheet.dart
index 31c7c598..0038236f 100644
--- a/lib/widgets/episode_list_sheet.dart
+++ b/lib/widgets/episode_list_sheet.dart
@@ -8,13 +8,13 @@ import 'package:cached_network_image/cached_network_image.dart';
import '../providers/library_provider.dart';
import '../services/audio_player_service.dart';
import '../services/download_service.dart';
-import '../services/progress_sync_service.dart';
import '../services/chromecast_service.dart';
import '../providers/auth_provider.dart';
import 'card_buttons.dart';
import 'html_description.dart';
-import 'playlist_picker_sheet.dart';
import 'stackable_sheet.dart';
+import 'episode_row.dart';
+export 'episode_detail_sheet.dart';
/// Bottom sheet that shows a podcast's episode list.
/// Mirrors the UX of [BookDetailSheet] but adapted for podcast shows.
@@ -735,7 +735,7 @@ class _EpisodeListSheetState extends State<EpisodeListSheet> {
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
const SizedBox(width: 8),
- Expanded(child: _SelectableEpisodeRow(
+ Expanded(child: SelectableEpisodeRow(
episode: ep,
itemId: _itemId,
)),
@@ -770,7 +770,7 @@ class _EpisodeListSheetState extends State<EpisodeListSheet> {
),
child: Icon(Icons.add_circle_outline_rounded, color: Theme.of(context).colorScheme.primary),
),
- child: _EpisodeRow(
+ child: EpisodeRow(
episode: ep,
podcastItem: widget.podcastItem,
itemId: _itemId,
@@ -835,941 +835,3 @@ class _EpisodeListSheetState extends State<EpisodeListSheet> {
style: TextStyle(color: highlight ? cs.primary : cs.onSurfaceVariant, fontSize: 11)))]));
}
}
-
-// ── Episode Detail Sheet ──
-
-class EpisodeDetailSheet extends StatefulWidget {
- final Map<String, dynamic> podcastItem;
- final Map<String, dynamic> episode;
- final ScrollController? scrollController;
-
- const EpisodeDetailSheet({super.key, required this.podcastItem, required this.episode})
- : scrollController = null;
-
- const EpisodeDetailSheet._({
- required this.podcastItem,
- required this.episode,
- required this.scrollController,
- }) : super(key: null);
-
- static void show(BuildContext context, Map<String, dynamic> podcastItem, Map<String, dynamic> episode) {
- showStackableSheet(
- context: context,
- useSafeArea: true,
- initialChildSize: 0.75,
- maxChildSize: 0.95,
- builder: (_, scrollController) => EpisodeDetailSheet._(
- podcastItem: podcastItem,
- episode: episode,
- scrollController: scrollController,
- ),
- );
- }
-
- @override
- State<EpisodeDetailSheet> createState() => _EpisodeDetailSheetState();
-}
-
-class _EpisodeDetailSheetState extends State<EpisodeDetailSheet> {
- bool _chaptersExpanded = false;
-
- String get _itemId => widget.podcastItem['id'] as String? ?? '';
-
- String get _showTitle {
- final media = widget.podcastItem['media'] as Map<String, dynamic>? ?? {};
- final meta = media['metadata'] as Map<String, dynamic>? ?? {};
- return meta['title'] as String? ?? '';
- }
-
- String get _episodeTitle => widget.episode['title'] as String? ?? 'Episode';
- String get _episodeId => widget.episode['id'] as String? ?? '';
- double get _duration {
- final d = (widget.episode['duration'] as num?)?.toDouble() ?? 0;
- if (d > 0) return d;
- // recentEpisode from ABS personalized sections often omits top-level duration
- final af = widget.episode['audioFile'] as Map<String, dynamic>?;
- return (af?['duration'] as num?)?.toDouble() ?? 0;
- }
- int get _publishedAt => (widget.episode['publishedAt'] as num?)?.toInt() ?? 0;
- String? get _episodeNumber => widget.episode['episode'] as String?;
- String? get _season => widget.episode['season'] as String?;
- List<dynamic> get _chapters => widget.episode['chapters'] as List<dynamic>? ?? [];
-
- String get _rawDescription => widget.episode['description'] as String? ?? '';
-
- Future<void> _play() async {
- final auth = context.read<AuthProvider>();
- final api = auth.apiService;
- if (api == null) return;
-
- final cast = ChromecastService();
- if (cast.isConnected) {
- await cast.castItem(
- api: api, itemId: _itemId, title: _episodeTitle, author: _showTitle,
- coverUrl: api.getCoverUrl(_itemId), totalDuration: _duration, chapters: _chapters,
- episodeId: _episodeId,
- );
- if (mounted) Navigator.of(context, rootNavigator: true).popUntil((route) => route.isFirst);
- return;
- }
-
- final error = await AudioPlayerService().playItem(
- api: api, itemId: _itemId, title: _episodeTitle, author: _showTitle,
- coverUrl: api.getCoverUrl(_itemId), totalDuration: _duration, chapters: _chapters,
- episodeId: _episodeId,
- episodeTitle: _episodeTitle,
- );
- if (mounted) {
- if (error != null) showErrorSnackBar(context, error);
- Navigator.of(context, rootNavigator: true).popUntil((route) => route.isFirst);
- }
- }
-
- Future<void> _download() async {
- final auth = context.read<AuthProvider>();
- final api = auth.apiService;
- if (api == null) return;
-
- final error = await DownloadService().downloadItem(
- api: api,
- itemId: '$_itemId-$_episodeId',
- title: _episodeTitle,
- author: _showTitle,
- coverUrl: api.getCoverUrl(_itemId),
- episodeId: _episodeId,
- );
- if (error != null && mounted) {
- ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(error)));
- }
- }
-
- Future<void> _toggleFinished() async {
- final auth = context.read<AuthProvider>();
- final api = auth.apiService;
- if (api == null) return;
- final lib = context.read<LibraryProvider>();
- final key = '$_itemId-$_episodeId';
- final progressData = lib.getEpisodeProgressData(_itemId, _episodeId);
- final isFinished = progressData?['isFinished'] == true;
- final currentTime = (progressData?['currentTime'] as num?)?.toDouble() ?? 0;
-
- try {
- if (isFinished) {
- // Confirm before un-finishing
- final confirmed = await showDialog<bool>(
- context: context,
- builder: (ctx) => AlertDialog(
- title: const Text('Mark as Not Finished?'),
- content: const Text('This will clear the finished status but keep your current position.'),
- actions: [
- TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('Cancel')),
- FilledButton(onPressed: () => Navigator.pop(ctx, true), child: const Text('Unmark')),
- ],
- ),
- );
- if (confirmed != true) return;
- // Un-finish — keep current position
- await api.updateEpisodeProgress(
- _itemId, _episodeId,
- currentTime: currentTime,
- duration: _duration,
- isFinished: false,
- );
- await lib.refresh();
- if (mounted) {
- ScaffoldMessenger.of(context).showSnackBar(SnackBar(
- content: const Text('Marked as not finished'),
- behavior: SnackBarBehavior.floating,
- duration: const Duration(seconds: 2),
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
- ));
- }
- } else {
- // Confirm before marking finished
- final confirmed = await showDialog<bool>(
- context: context,
- builder: (ctx) => AlertDialog(
- title: const Text('Mark as Fully Absorbed?'),
- content: const Text('This will set your progress to 100% for this episode.'),
- actions: [
- TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('Cancel')),
- FilledButton(onPressed: () => Navigator.pop(ctx, true), child: const Text('Fully Absorb')),
- ],
- ),
- );
- if (confirmed != true) return;
- // Mark finished — update server then local state for instant UI
- await api.updateEpisodeProgress(
- _itemId, _episodeId,
- currentTime: _duration,
- duration: _duration,
- isFinished: true,
- );
- lib.markFinishedLocally(key, skipAutoAdvance: true);
- if (mounted) {
- ScaffoldMessenger.of(context).showSnackBar(SnackBar(
- content: const Text('Marked as finished — nice!'),
- behavior: SnackBarBehavior.floating,
- duration: const Duration(seconds: 2),
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
- ));
- }
- }
- } catch (_) {
- if (mounted) {
- ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
- content: Text('Failed to update — check your connection')));
- }
- }
- }
-
- void _confirmDeleteDownload(BuildContext context, String dlKey) {
- showDialog(context: context, builder: (ctx) => AlertDialog(
- title: const Text('Remove download?'),
- content: const Text('This will be removed from your device.'),
- actions: [
- TextButton(onPressed: () => Navigator.pop(ctx), child: const Text('Cancel')),
- TextButton(onPressed: () {
- DownloadService().deleteDownload(dlKey);
- 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))),
- ],
- ));
- }
-
- String? get _coverUrl {
- final auth = context.read<AuthProvider>();
- return auth.apiService?.getCoverUrl(_itemId, width: 800);
- }
-
- @override
- Widget build(BuildContext context) {
- final cs = Theme.of(context).colorScheme;
- final tt = Theme.of(context).textTheme;
- final lib = context.watch<LibraryProvider>();
- final coverUrl = _coverUrl;
-
- final dlKey = '$_itemId-$_episodeId';
-
- final progress = lib.getEpisodeProgress(_itemId, _episodeId);
- final progressData = lib.getEpisodeProgressData(_itemId, _episodeId);
- final isFinished = progressData?['isFinished'] == true;
-
- String dateLabel = '';
- if (_publishedAt > 0) {
- final date = DateTime.fromMillisecondsSinceEpoch(_publishedAt);
- final diff = DateTime.now().difference(date);
- if (diff.inDays == 0) dateLabel = 'Today';
- else if (diff.inDays == 1) dateLabel = 'Yesterday';
- else if (diff.inDays < 7) dateLabel = '${diff.inDays}d ago';
- else if (diff.inDays < 30) dateLabel = '${(diff.inDays / 7).floor()}w ago';
- else dateLabel = '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
- }
-
- String durationLabel = '';
- if (_duration > 0) {
- final h = (_duration / 3600).floor();
- final m = ((_duration % 3600) / 60).floor();
- durationLabel = h > 0 ? '${h}h ${m}m' : '${m}m';
- }
-
- return Container(
- clipBehavior: Clip.antiAlias,
- decoration: BoxDecoration(
- color: Theme.of(context).scaffoldBackgroundColor,
- borderRadius: const BorderRadius.vertical(top: Radius.circular(24)),
- ),
- child: Stack(children: [
- // Blurred cover background
- if (coverUrl != null)
- Positioned.fill(
- child: RepaintBoundary(
- child: CachedNetworkImage(
- imageUrl: coverUrl, fit: BoxFit.cover,
- httpHeaders: lib.mediaHeaders,
- imageBuilder: (_, p) => ImageFiltered(
- imageFilter: ImageFilter.blur(sigmaX: 50, sigmaY: 50, tileMode: TileMode.decal),
- child: Image(image: p, fit: BoxFit.cover)),
- placeholder: (_, __) => const SizedBox(),
- errorWidget: (_, __, ___) => const SizedBox(),
- ),
- ),
- ),
- // Gradient overlay
- Positioned.fill(child: DecoratedBox(decoration: BoxDecoration(gradient: LinearGradient(
- begin: Alignment.topCenter, end: Alignment.bottomCenter,
- colors: [
- Theme.of(context).scaffoldBackgroundColor.withValues(alpha: 0.6),
- Theme.of(context).scaffoldBackgroundColor.withValues(alpha: 0.85),
- Theme.of(context).scaffoldBackgroundColor,
- ],
- )))),
- // Content
- ListView(
- controller: widget.scrollController,
- padding: EdgeInsets.fromLTRB(20, 8, 20, 32 + MediaQuery.of(context).viewPadding.bottom),
- children: [
- // Drag handle
- Center(child: Container(width: 40, height: 4, margin: const EdgeInsets.only(bottom: 16),
- decoration: BoxDecoration(color: cs.onSurface.withValues(alpha: 0.24), borderRadius: BorderRadius.circular(2)))),
-
- // Episode title (centered)
- Text(_episodeTitle, textAlign: TextAlign.center,
- style: tt.headlineSmall?.copyWith(fontWeight: FontWeight.w700, color: cs.onSurface)),
- const SizedBox(height: 4),
-
- // Show title
- if (_showTitle.isNotEmpty)
- Text(_showTitle, textAlign: TextAlign.center,
- style: tt.bodyMedium?.copyWith(color: cs.onSurface.withValues(alpha: 0.6))),
-
- const SizedBox(height: 12),
-
- // Progress bar
- if (progress > 0) ...[
- ClipRRect(borderRadius: BorderRadius.circular(3),
- child: LinearProgressIndicator(
- value: progress.clamp(0.0, 1.0), minHeight: 4,
- backgroundColor: cs.onSurface.withValues(alpha: 0.1),
- valueColor: AlwaysStoppedAnimation(
- isFinished ? cs.primary.withValues(alpha: 0.4) : cs.primary),
- )),
- const SizedBox(height: 4),
- Text('${(progress * 100).toStringAsFixed(1)}% complete', textAlign: TextAlign.center,
- style: tt.labelSmall?.copyWith(color: cs.onSurfaceVariant)),
- const SizedBox(height: 12),
- ],
-
- // Play button (full width, matching book detail)
- SizedBox(height: 52, child: FilledButton.icon(
- onPressed: _play,
- icon: Icon(
- progress > 0 && !isFinished ? Icons.play_arrow_rounded : Icons.podcasts_rounded,
- size: 24,
- ),
- label: Text(
- progress > 0 && !isFinished ? 'Resume' : 'Play Episode',
- style: tt.titleMedium?.copyWith(fontWeight: FontWeight.w600, color: cs.onPrimary),
- ),
- style: FilledButton.styleFrom(
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
- ),
- )),
- const SizedBox(height: 12),
-
- // Download + Finished row
- Row(children: [
- Expanded(child: ListenableBuilder(
- listenable: DownloadService(),
- builder: (context, _) {
- final dl = DownloadService();
- final downloaded = dl.isDownloaded(dlKey);
- final downloading = dl.isDownloading(dlKey);
- final dlProgress = dl.downloadProgress(dlKey);
-
- final IconData icon;
- final String label;
- final Color color;
- if (downloaded) {
- icon = Icons.download_done_rounded;
- label = 'Saved';
- color = (Theme.of(context).brightness == Brightness.dark ? Colors.greenAccent : Colors.green.shade700).withValues(alpha: 0.7);
- } else if (downloading) {
- icon = Icons.downloading_rounded;
- label = '${(dlProgress * 100).toStringAsFixed(0)}%';
- color = cs.primary;
- } else {
- icon = Icons.download_outlined;
- label = 'Download';
- color = cs.onSurfaceVariant;
- }
-
- return GestureDetector(
- onTap: downloaded
- ? () => _confirmDeleteDownload(context, dlKey)
- : downloading
- ? () => DownloadService().cancelDownload(dlKey)
- : _download,
- child: Container(
- height: 36,
- clipBehavior: Clip.antiAlias,
- decoration: BoxDecoration(
- color: downloaded ? (Theme.of(context).brightness == Brightness.dark ? Colors.greenAccent : Colors.green.shade700).withValues(alpha: 0.06) : cs.onSurface.withValues(alpha: 0.06),
- borderRadius: BorderRadius.circular(14),
- border: Border.all(color: downloaded ? (Theme.of(context).brightness == Brightness.dark ? Colors.greenAccent : Colors.green.shade700).withValues(alpha: 0.15) : cs.onSurface.withValues(alpha: 0.08)),
- ),
- child: Stack(children: [
- if (downloading)
- FractionallySizedBox(
- widthFactor: dlProgress.clamp(0.0, 1.0),
- child: Container(
- decoration: BoxDecoration(
- color: cs.primary.withValues(alpha: 0.15),
- borderRadius: BorderRadius.circular(13),
- ),
- ),
- ),
- Center(child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
- Icon(icon, size: 16, color: color),
- const SizedBox(width: 6),
- Text(label, style: TextStyle(color: color, fontSize: 12, fontWeight: FontWeight.w500)),
- ])),
- ]),
- ),
- );
- },
- )),
- const SizedBox(width: 8),
- Expanded(child: GestureDetector(
- onTap: _toggleFinished,
- child: Container(
- height: 36,
- decoration: BoxDecoration(
- color: isFinished ? Colors.green.withValues(alpha: 0.06) : cs.onSurface.withValues(alpha: 0.06),
- borderRadius: BorderRadius.circular(14),
- border: Border.all(color: isFinished ? Colors.green.withValues(alpha: 0.15) : cs.onSurface.withValues(alpha: 0.08)),
- ),
- child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
- Icon(
- isFinished ? Icons.check_circle_rounded : Icons.check_circle_outline_rounded,
- size: 16,
- color: isFinished ? Colors.green : cs.onSurfaceVariant,
- ),
- const SizedBox(width: 6),
- Text(
- isFinished ? 'Fully Absorbed' : 'Fully Absorb',
- style: TextStyle(
- color: isFinished ? Colors.green : cs.onSurfaceVariant,
- fontSize: 12, fontWeight: FontWeight.w500,
- ),
- ),
- ]),
- ),
- )),
- const SizedBox(width: 8),
- GestureDetector(
- onTap: () => _showMoreSheet(context, lib, dlKey, progress, isFinished),
- child: Container(
- height: 36, width: 44,
- decoration: BoxDecoration(
- color: cs.onSurface.withValues(alpha: 0.06),
- borderRadius: BorderRadius.circular(14),
- border: Border.all(color: cs.onSurface.withValues(alpha: 0.1)),
- ),
- child: Icon(Icons.more_horiz_rounded, size: 18, color: cs.onSurfaceVariant),
- ),
- ),
- ]),
-
- // Metadata chips
- const SizedBox(height: 16),
- Wrap(spacing: 8, runSpacing: 8, children: [
- if (dateLabel.isNotEmpty) _chip(Icons.calendar_today_rounded, dateLabel),
- if (durationLabel.isNotEmpty) _chip(Icons.schedule_rounded, durationLabel),
- if (_episodeNumber != null) _chip(Icons.tag_rounded, 'Episode $_episodeNumber'),
- if (_season != null) _chip(Icons.layers_rounded, 'Season $_season'),
- ]),
-
- // All Episodes button (series-style)
- const SizedBox(height: 16),
- GestureDetector(
- onTap: () {
- final nav = Navigator.of(context);
- nav.pop();
- EpisodeListSheet.show(nav.context, widget.podcastItem);
- },
- child: Container(
- padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
- decoration: BoxDecoration(
- color: cs.primary.withValues(alpha: 0.08),
- borderRadius: BorderRadius.circular(12),
- border: Border.all(color: cs.primary.withValues(alpha: 0.15)),
- ),
- child: Row(children: [
- Icon(Icons.podcasts_rounded, size: 16, color: cs.primary.withValues(alpha: 0.7)),
- const SizedBox(width: 8),
- Expanded(child: Text('All Episodes',
- style: tt.bodySmall?.copyWith(color: cs.primary.withValues(alpha: 0.9), fontWeight: FontWeight.w500))),
- Icon(Icons.chevron_right_rounded, size: 18, color: cs.primary.withValues(alpha: 0.5)),
- ]),
- ),
- ),
-
- // Chapters
- if (_chapters.isNotEmpty) ...[const SizedBox(height: 16),
- GestureDetector(onTap: () => setState(() => _chaptersExpanded = !_chaptersExpanded),
- child: Row(children: [
- Text('Chapters (${_chapters.length})', style: tt.titleSmall?.copyWith(color: cs.onSurfaceVariant, fontWeight: FontWeight.w600)),
- const Spacer(), Icon(_chaptersExpanded ? Icons.expand_less : Icons.expand_more, color: cs.onSurface.withValues(alpha: 0.3), size: 20)])),
- if (_chaptersExpanded) ...[const SizedBox(height: 8),
- ..._chapters.asMap().entries.map((e) {
- final ch = e.value as Map<String, dynamic>;
- return Padding(padding: const EdgeInsets.symmetric(vertical: 3),
- child: Row(children: [
- SizedBox(width: 28, child: Text('${e.key + 1}', style: tt.labelSmall?.copyWith(color: cs.onSurface.withValues(alpha: 0.3)))),
- Expanded(child: Text(ch['title'] as String? ?? 'Chapter ${e.key + 1}', maxLines: 1, overflow: TextOverflow.ellipsis, style: tt.bodySmall?.copyWith(color: cs.onSurface.withValues(alpha: 0.6)))),
- Text(_fmtDur(((ch['end'] as num?)?.toDouble() ?? 0) - ((ch['start'] as num?)?.toDouble() ?? 0)), style: tt.labelSmall?.copyWith(color: cs.onSurface.withValues(alpha: 0.3))),
- ]));
- })]],
-
- // Description
- if (_rawDescription.isNotEmpty) ...[
- const SizedBox(height: 16),
- Text('About This Episode', style: tt.titleSmall?.copyWith(color: cs.onSurfaceVariant, fontWeight: FontWeight.w600)),
- const SizedBox(height: 6),
- HtmlDescription(
- html: _rawDescription,
- maxLines: 4,
- style: tt.bodySmall?.copyWith(color: cs.onSurface.withValues(alpha: 0.7), height: 1.5),
- linkColor: cs.primary,
- ),
- ],
- ],
- ),
- ]),
- );
- }
-
- void _showMoreSheet(BuildContext context, LibraryProvider lib, String dlKey, double progress, bool isFinished) {
- final cs = Theme.of(context).colorScheme;
- showModalBottomSheet(
- context: context,
- backgroundColor: Theme.of(context).bottomSheetTheme.backgroundColor,
- shape: const RoundedRectangleBorder(
- borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
- ),
- builder: (ctx) {
- return SafeArea(
- child: Padding(
- padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
- child: Column(mainAxisSize: MainAxisSize.min, children: [
- Center(child: Container(width: 40, height: 4, margin: const EdgeInsets.only(bottom: 16),
- decoration: BoxDecoration(color: cs.onSurface.withValues(alpha: 0.24), borderRadius: BorderRadius.circular(2)))),
- _moreItem(cs, lib.isOnAbsorbingList(dlKey)
- ? Icons.remove_circle_outline_rounded : Icons.add_circle_outline_rounded,
- lib.isOnAbsorbingList(dlKey) ? 'Remove from Absorbing' : 'Add to Absorbing',
- onTap: () async {
- Navigator.pop(ctx);
- if (lib.isOnAbsorbingList(dlKey)) {
- await lib.removeFromAbsorbing(dlKey);
- if (context.mounted) {
- ScaffoldMessenger.of(context).showSnackBar(SnackBar(
- duration: const Duration(seconds: 3),
- content: const Text('Removed from Absorbing'),
- behavior: SnackBarBehavior.floating,
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12))));
- }
- } else {
- await lib.addToAbsorbingQueue(dlKey);
- final cached = Map<String, dynamic>.from(widget.podcastItem);
- cached['recentEpisode'] = Map<String, dynamic>.from(widget.episode);
- cached['_absorbingKey'] = dlKey;
- lib.absorbingItemCache[dlKey] = cached;
- if (context.mounted) {
- ScaffoldMessenger.of(context).showSnackBar(SnackBar(
- duration: const Duration(seconds: 3),
- content: const Text('Added to Absorbing'),
- behavior: SnackBarBehavior.floating,
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12))));
- }
- }
- }),
- if (!lib.isOffline)
- _moreItem(cs, Icons.playlist_add_rounded, 'Add to Playlist',
- onTap: () {
- Navigator.pop(ctx);
- PlaylistPickerSheet.show(
- context,
- widget.podcastItem['id'] as String,
- episodeId: widget.episode['id'] as String?,
- );
- }),
- if (progress > 0 || isFinished)
- _moreItem(cs, Icons.restart_alt_rounded, 'Reset Progress',
- onTap: () { Navigator.pop(ctx); _resetProgress(context); }),
- ]),
- ),
- );
- },
- );
- }
-
- Widget _moreItem(ColorScheme cs, IconData icon, String label, {required VoidCallback onTap}) {
- return Padding(
- padding: const EdgeInsets.only(bottom: 6),
- child: GestureDetector(onTap: onTap, child: Container(height: 44,
- decoration: BoxDecoration(color: cs.onSurface.withValues(alpha: 0.06), borderRadius: BorderRadius.circular(14),
- border: Border.all(color: cs.onSurface.withValues(alpha: 0.1))),
- child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
- Icon(icon, size: 16, color: cs.onSurfaceVariant), const SizedBox(width: 8),
- Text(label, style: TextStyle(color: cs.onSurfaceVariant, fontSize: 13, fontWeight: FontWeight.w500))]))),
- );
- }
-
- String _fmtDur(double s) {
- final h = (s / 3600).floor(); final m = ((s % 3600) / 60).floor();
- if (h > 0) return '${h}h ${m}m';
- return '${m}m';
- }
-
- Future<void> _resetProgress(BuildContext context) async {
- final confirmed = await showDialog<bool>(
- context: context,
- builder: (ctx) => AlertDialog(
- title: const Text('Reset Progress?'),
- content: const Text('This will erase all progress for this episode and set it back to the beginning. This can\'t be undone.'),
- actions: [
- TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('Cancel')),
- FilledButton(onPressed: () => Navigator.pop(ctx, true),
- style: FilledButton.styleFrom(backgroundColor: Theme.of(ctx).colorScheme.error),
- child: const Text('Reset')),
- ],
- ),
- );
- if (confirmed != true) return;
- final auth = context.read<AuthProvider>();
- final api = auth.apiService;
- if (api == null) return;
- final player = AudioPlayerService();
-
- if (player.currentItemId == _itemId && player.currentEpisodeId == _episodeId) {
- await player.stopWithoutSaving();
- }
-
- final compoundKey = '$_itemId-$_episodeId';
- await ProgressSyncService().deleteLocal(compoundKey);
- final ok = await api.deleteEpisodeProgress(_itemId, _episodeId);
- // Mark as unfinished with zero progress on the server
- await api.updateEpisodeProgress(
- _itemId, _episodeId,
- currentTime: 0,
- duration: _duration,
- isFinished: false,
- );
-
- if (context.mounted) {
- context.read<LibraryProvider>().resetProgressFor(compoundKey);
- ScaffoldMessenger.of(context).showSnackBar(SnackBar(
- duration: const Duration(seconds: 3),
- content: Text(ok ? 'Progress reset — fresh start!' : 'Reset may not have synced — check your server'),
- behavior: SnackBarBehavior.floating,
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12))));
- }
- }
-
- Widget _chip(IconData icon, String text) {
- final cs = Theme.of(context).colorScheme;
- return Container(
- constraints: const BoxConstraints(maxWidth: 200),
- padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
- decoration: BoxDecoration(color: cs.onSurface.withValues(alpha: 0.06), borderRadius: BorderRadius.circular(20),
- border: Border.all(color: cs.onSurface.withValues(alpha: 0.08))),
- child: Row(mainAxisSize: MainAxisSize.min, children: [
- Icon(icon, size: 12, color: cs.onSurfaceVariant), const SizedBox(width: 4),
- Flexible(child: Text(text, overflow: TextOverflow.ellipsis, maxLines: 1,
- style: TextStyle(color: cs.onSurfaceVariant, fontSize: 11)))]));
- }
-}
-
-// ── Episode Row ──
-
-class _EpisodeRow extends StatefulWidget {
- final Map<String, dynamic> episode;
- final Map<String, dynamic> podcastItem;
- final String itemId;
- final String podcastTitle;
- final VoidCallback onPlay;
- final VoidCallback onDownload;
-
- const _EpisodeRow({
- required this.episode,
- required this.podcastItem,
- required this.itemId,
- required this.podcastTitle,
- required this.onPlay,
- required this.onDownload,
- });
-
- @override
- State<_EpisodeRow> createState() => _EpisodeRowState();
-}
-
-class _EpisodeRowState extends State<_EpisodeRow> {
- @override
- Widget build(BuildContext context) {
- final cs = Theme.of(context).colorScheme;
- final lib = context.watch<LibraryProvider>();
- final ep = widget.episode;
-
- final title = ep['title'] as String? ?? 'Episode';
- final episodeId = ep['id'] as String? ?? '';
- final duration = (ep['duration'] as num?)?.toDouble() ?? 0;
- final publishedAt = (ep['publishedAt'] as num?)?.toInt() ?? 0;
- final episodeNumber = ep['episode'] as String?;
- final season = ep['season'] as String?;
-
- // Progress
- final progress = lib.getEpisodeProgress(widget.itemId, episodeId);
- final progressData = lib.getEpisodeProgressData(widget.itemId, episodeId);
- final isFinished = progressData?['isFinished'] == true;
-
- // Download key for reactive lookups
- final dlKey = '${widget.itemId}-$episodeId';
-
- // Format publish date
- String dateLabel = '';
- if (publishedAt > 0) {
- final date = DateTime.fromMillisecondsSinceEpoch(publishedAt);
- final now = DateTime.now();
- final diff = now.difference(date);
- if (diff.inDays == 0) {
- dateLabel = 'Today';
- } else if (diff.inDays == 1) {
- dateLabel = 'Yesterday';
- } else if (diff.inDays < 7) {
- dateLabel = '${diff.inDays}d ago';
- } else if (diff.inDays < 30) {
- dateLabel = '${(diff.inDays / 7).floor()}w ago';
- } else {
- dateLabel = '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
- }
- }
-
- // Format duration
- String durationLabel = '';
- if (duration > 0) {
- final h = (duration / 3600).floor();
- final m = ((duration % 3600) / 60).floor();
- if (h > 0) {
- durationLabel = '${h}h ${m}m';
- } else {
- durationLabel = '${m}m';
- }
- }
-
- return InkWell(
- onTap: () {
- // Close episode list before opening detail to prevent infinite stacking
- final nav = Navigator.of(context);
- nav.pop();
- EpisodeDetailSheet.show(nav.context, widget.podcastItem, ep);
- },
- child: Padding(
- padding: const EdgeInsets.fromLTRB(20, 10, 12, 10),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Row(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- // Play/status indicator
- Padding(
- padding: const EdgeInsets.only(top: 2),
- child: isFinished
- ? Icon(Icons.check_circle_rounded, size: 18, color: cs.primary.withValues(alpha: 0.6))
- : progress > 0
- ? SizedBox(
- width: 18, height: 18,
- child: CircularProgressIndicator(
- value: progress,
- strokeWidth: 2.5,
- backgroundColor: cs.surfaceContainerHighest,
- color: cs.primary,
- ),
- )
- : Icon(Icons.circle_outlined, size: 18,
- color: cs.onSurfaceVariant.withValues(alpha: 0.3)),
- ),
- const SizedBox(width: 12),
-
- // Title + metadata
- Expanded(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(title,
- style: TextStyle(
- fontSize: 14,
- fontWeight: FontWeight.w500,
- color: isFinished
- ? cs.onSurfaceVariant.withValues(alpha: 0.5)
- : cs.onSurface,
- ),
- maxLines: 2, overflow: TextOverflow.ellipsis,
- ),
- if (episodeNumber != null || season != null) ...[
- const SizedBox(height: 2),
- Text(
- [
- if (season != null) 'S$season',
- if (episodeNumber != null) 'E$episodeNumber',
- ].join(' '),
- style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.5)),
- ),
- ],
- const SizedBox(height: 4),
- Row(
- children: [
- if (dateLabel.isNotEmpty)
- Text(dateLabel,
- style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.6)),
- ),
- if (dateLabel.isNotEmpty && durationLabel.isNotEmpty)
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 6),
- child: Text('·',
- style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.4)),
- ),
- ),
- if (durationLabel.isNotEmpty)
- Text(durationLabel,
- style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.6)),
- ),
- ListenableBuilder(
- listenable: DownloadService(),
- builder: (_, __) {
- final downloaded = DownloadService().isDownloaded(dlKey);
- if (!downloaded) return const SizedBox.shrink();
- return Row(mainAxisSize: MainAxisSize.min, children: [
- const SizedBox(width: 6),
- Icon(Icons.download_done_rounded, size: 12, color: cs.primary.withValues(alpha: 0.6)),
- ]);
- },
- ),
- ],
- ),
- ],
- ),
- ),
-
- // Action buttons
- Row(
- mainAxisSize: MainAxisSize.min,
- children: [
- // Download button (reactive)
- ListenableBuilder(
- listenable: DownloadService(),
- builder: (_, __) {
- final dl = DownloadService();
- final downloaded = dl.isDownloaded(dlKey);
- final downloading = dl.isDownloading(dlKey);
- final dlProgress = dl.downloadProgress(dlKey);
-
- if (downloaded) {
- return Padding(
- padding: const EdgeInsets.all(8),
- child: Icon(Icons.download_done_rounded, size: 20,
- color: (Theme.of(context).brightness == Brightness.dark ? Colors.greenAccent : Colors.green.shade700).withValues(alpha: 0.7)),
- );
- }
- if (downloading) {
- return Padding(
- padding: const EdgeInsets.all(8),
- child: SizedBox(width: 20, height: 20,
- child: Stack(alignment: Alignment.center, children: [
- CircularProgressIndicator(
- value: dlProgress > 0 ? dlProgress : null,
- strokeWidth: 2, color: cs.primary),
- Text((dlProgress * 100).toStringAsFixed(0),
- style: TextStyle(fontSize: 7, color: cs.primary, fontWeight: FontWeight.w600)),
- ])),
- );
- }
- return IconButton(
- onPressed: widget.onDownload,
- icon: Icon(Icons.download_rounded, size: 20,
- color: cs.onSurfaceVariant.withValues(alpha: 0.5)),
- visualDensity: VisualDensity.compact,
- constraints: const BoxConstraints(minWidth: 36, minHeight: 36),
- );
- },
- ),
-
- // Play button
- IconButton(
- onPressed: widget.onPlay,
- icon: Icon(
- progress > 0 && !isFinished
- ? Icons.play_circle_filled_rounded
- : Icons.play_circle_outline_rounded,
- size: 28,
- color: cs.primary,
- ),
- visualDensity: VisualDensity.compact,
- constraints: const BoxConstraints(minWidth: 40, minHeight: 40),
- ),
- ],
- ),
- ],
- ),
-
- ],
- ),
- ),
- );
- }
-}
-
-// ── Compact episode row for selection mode ──
-
-class _SelectableEpisodeRow extends StatelessWidget {
- final Map<String, dynamic> episode;
- final String itemId;
-
- const _SelectableEpisodeRow({
- required this.episode,
- required this.itemId,
- });
-
- @override
- Widget build(BuildContext context) {
- final cs = Theme.of(context).colorScheme;
- final lib = context.watch<LibraryProvider>();
-
- final title = episode['title'] as String? ?? 'Episode';
- final episodeId = episode['id'] as String? ?? '';
- final duration = (episode['duration'] as num?)?.toDouble() ?? 0;
-
- final progressData = lib.getEpisodeProgressData(itemId, episodeId);
- final isFinished = progressData?['isFinished'] == true;
-
- String durationLabel = '';
- if (duration > 0) {
- final h = (duration / 3600).floor();
- final m = ((duration % 3600) / 60).floor();
- durationLabel = h > 0 ? '${h}h ${m}m' : '${m}m';
- }
-
- return Row(children: [
- if (isFinished)
- Padding(
- padding: const EdgeInsets.only(right: 6),
- child: Icon(Icons.check_circle_rounded, size: 14, color: cs.primary.withValues(alpha: 0.6)),
- ),
- Expanded(
- child: Text(title,
- style: TextStyle(
- fontSize: 13,
- fontWeight: FontWeight.w500,
- color: isFinished ? cs.onSurfaceVariant.withValues(alpha: 0.5) : cs.onSurface,
- ),
- maxLines: 1, overflow: TextOverflow.ellipsis,
- ),
- ),
- if (durationLabel.isNotEmpty)
- Padding(
- padding: const EdgeInsets.only(left: 8),
- child: Text(durationLabel,
- style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.5))),
- ),
- ]);
- }
-}
diff --git a/lib/widgets/episode_row.dart b/lib/widgets/episode_row.dart
new file mode 100644
index 00000000..9ad24981
--- /dev/null
+++ b/lib/widgets/episode_row.dart
@@ -0,0 +1,298 @@
+import 'package:flutter/material.dart';
+import 'package:provider/provider.dart';
+import '../providers/library_provider.dart';
+import '../services/download_service.dart';
+import 'episode_detail_sheet.dart';
+
+class EpisodeRow extends StatefulWidget {
+ final Map<String, dynamic> episode;
+ final Map<String, dynamic> podcastItem;
+ final String itemId;
+ final String podcastTitle;
+ final VoidCallback onPlay;
+ final VoidCallback onDownload;
+
+ const EpisodeRow({
+ super.key,
+ required this.episode,
+ required this.podcastItem,
+ required this.itemId,
+ required this.podcastTitle,
+ required this.onPlay,
+ required this.onDownload,
+ });
+
+ @override
+ State<EpisodeRow> createState() => _EpisodeRowState();
+}
+
+class _EpisodeRowState extends State<EpisodeRow> {
+ @override
+ Widget build(BuildContext context) {
+ final cs = Theme.of(context).colorScheme;
+ final lib = context.watch<LibraryProvider>();
+ final ep = widget.episode;
+
+ final title = ep['title'] as String? ?? 'Episode';
+ final episodeId = ep['id'] as String? ?? '';
+ final duration = (ep['duration'] as num?)?.toDouble() ?? 0;
+ final publishedAt = (ep['publishedAt'] as num?)?.toInt() ?? 0;
+ final episodeNumber = ep['episode'] as String?;
+ final season = ep['season'] as String?;
+
+ // Progress
+ final progress = lib.getEpisodeProgress(widget.itemId, episodeId);
+ final progressData = lib.getEpisodeProgressData(widget.itemId, episodeId);
+ final isFinished = progressData?['isFinished'] == true;
+
+ // Download key for reactive lookups
+ final dlKey = '${widget.itemId}-$episodeId';
+
+ // Format publish date
+ String dateLabel = '';
+ if (publishedAt > 0) {
+ final date = DateTime.fromMillisecondsSinceEpoch(publishedAt);
+ final now = DateTime.now();
+ final diff = now.difference(date);
+ if (diff.inDays == 0) {
+ dateLabel = 'Today';
+ } else if (diff.inDays == 1) {
+ dateLabel = 'Yesterday';
+ } else if (diff.inDays < 7) {
+ dateLabel = '${diff.inDays}d ago';
+ } else if (diff.inDays < 30) {
+ dateLabel = '${(diff.inDays / 7).floor()}w ago';
+ } else {
+ dateLabel = '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
+ }
+ }
+
+ // Format duration
+ String durationLabel = '';
+ if (duration > 0) {
+ final h = (duration / 3600).floor();
+ final m = ((duration % 3600) / 60).floor();
+ if (h > 0) {
+ durationLabel = '${h}h ${m}m';
+ } else {
+ durationLabel = '${m}m';
+ }
+ }
+
+ return InkWell(
+ onTap: () {
+ // Close episode list before opening detail to prevent infinite stacking
+ final nav = Navigator.of(context);
+ nav.pop();
+ EpisodeDetailSheet.show(nav.context, widget.podcastItem, ep);
+ },
+ child: Padding(
+ padding: const EdgeInsets.fromLTRB(20, 10, 12, 10),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Row(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ // Play/status indicator
+ Padding(
+ padding: const EdgeInsets.only(top: 2),
+ child: isFinished
+ ? Icon(Icons.check_circle_rounded, size: 18, color: cs.primary.withValues(alpha: 0.6))
+ : progress > 0
+ ? SizedBox(
+ width: 18, height: 18,
+ child: CircularProgressIndicator(
+ value: progress,
+ strokeWidth: 2.5,
+ backgroundColor: cs.surfaceContainerHighest,
+ color: cs.primary,
+ ),
+ )
+ : Icon(Icons.circle_outlined, size: 18,
+ color: cs.onSurfaceVariant.withValues(alpha: 0.3)),
+ ),
+ const SizedBox(width: 12),
+
+ // Title + metadata
+ Expanded(
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Text(title,
+ style: TextStyle(
+ fontSize: 14,
+ fontWeight: FontWeight.w500,
+ color: isFinished
+ ? cs.onSurfaceVariant.withValues(alpha: 0.5)
+ : cs.onSurface,
+ ),
+ maxLines: 2, overflow: TextOverflow.ellipsis,
+ ),
+ if (episodeNumber != null || season != null) ...[
+ const SizedBox(height: 2),
+ Text(
+ [
+ if (season != null) 'S$season',
+ if (episodeNumber != null) 'E$episodeNumber',
+ ].join(' '),
+ style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.5)),
+ ),
+ ],
+ const SizedBox(height: 4),
+ Row(
+ children: [
+ if (dateLabel.isNotEmpty)
+ Text(dateLabel,
+ style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.6)),
+ ),
+ if (dateLabel.isNotEmpty && durationLabel.isNotEmpty)
+ Padding(
+ padding: const EdgeInsets.symmetric(horizontal: 6),
+ child: Text('·',
+ style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.4)),
+ ),
+ ),
+ if (durationLabel.isNotEmpty)
+ Text(durationLabel,
+ style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.6)),
+ ),
+ ListenableBuilder(
+ listenable: DownloadService(),
+ builder: (_, __) {
+ final downloaded = DownloadService().isDownloaded(dlKey);
+ if (!downloaded) return const SizedBox.shrink();
+ return Row(mainAxisSize: MainAxisSize.min, children: [
+ const SizedBox(width: 6),
+ Icon(Icons.download_done_rounded, size: 12, color: cs.primary.withValues(alpha: 0.6)),
+ ]);
+ },
+ ),
+ ],
+ ),
+ ],
+ ),
+ ),
+
+ // Action buttons
+ Row(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ // Download button (reactive)
+ ListenableBuilder(
+ listenable: DownloadService(),
+ builder: (_, __) {
+ final dl = DownloadService();
+ final downloaded = dl.isDownloaded(dlKey);
+ final downloading = dl.isDownloading(dlKey);
+ final dlProgress = dl.downloadProgress(dlKey);
+
+ if (downloaded) {
+ return Padding(
+ padding: const EdgeInsets.all(8),
+ child: Icon(Icons.download_done_rounded, size: 20,
+ color: (Theme.of(context).brightness == Brightness.dark ? Colors.greenAccent : Colors.green.shade700).withValues(alpha: 0.7)),
+ );
+ }
+ if (downloading) {
+ return Padding(
+ padding: const EdgeInsets.all(8),
+ child: SizedBox(width: 20, height: 20,
+ child: Stack(alignment: Alignment.center, children: [
+ CircularProgressIndicator(
+ value: dlProgress > 0 ? dlProgress : null,
+ strokeWidth: 2, color: cs.primary),
+ Text((dlProgress * 100).toStringAsFixed(0),
+ style: TextStyle(fontSize: 7, color: cs.primary, fontWeight: FontWeight.w600)),
+ ])),
+ );
+ }
+ return IconButton(
+ onPressed: widget.onDownload,
+ icon: Icon(Icons.download_rounded, size: 20,
+ color: cs.onSurfaceVariant.withValues(alpha: 0.5)),
+ visualDensity: VisualDensity.compact,
+ constraints: const BoxConstraints(minWidth: 36, minHeight: 36),
+ );
+ },
+ ),
+
+ // Play button
+ IconButton(
+ onPressed: widget.onPlay,
+ icon: Icon(
+ progress > 0 && !isFinished
+ ? Icons.play_circle_filled_rounded
+ : Icons.play_circle_outline_rounded,
+ size: 28,
+ color: cs.primary,
+ ),
+ visualDensity: VisualDensity.compact,
+ constraints: const BoxConstraints(minWidth: 40, minHeight: 40),
+ ),
+ ],
+ ),
+ ],
+ ),
+
+ ],
+ ),
+ ),
+ );
+ }
+}
+
+class SelectableEpisodeRow extends StatelessWidget {
+ final Map<String, dynamic> episode;
+ final String itemId;
+
+ const SelectableEpisodeRow({
+ super.key,
+ required this.episode,
+ required this.itemId,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ final cs = Theme.of(context).colorScheme;
+ final lib = context.watch<LibraryProvider>();
+
+ final title = episode['title'] as String? ?? 'Episode';
+ final episodeId = episode['id'] as String? ?? '';
+ final duration = (episode['duration'] as num?)?.toDouble() ?? 0;
+
+ final progressData = lib.getEpisodeProgressData(itemId, episodeId);
+ final isFinished = progressData?['isFinished'] == true;
+
+ String durationLabel = '';
+ if (duration > 0) {
+ final h = (duration / 3600).floor();
+ final m = ((duration % 3600) / 60).floor();
+ durationLabel = h > 0 ? '${h}h ${m}m' : '${m}m';
+ }
+
+ return Row(children: [
+ if (isFinished)
+ Padding(
+ padding: const EdgeInsets.only(right: 6),
+ child: Icon(Icons.check_circle_rounded, size: 14, color: cs.primary.withValues(alpha: 0.6)),
+ ),
+ Expanded(
+ child: Text(title,
+ style: TextStyle(
+ fontSize: 13,
+ fontWeight: FontWeight.w500,
+ color: isFinished ? cs.onSurfaceVariant.withValues(alpha: 0.5) : cs.onSurface,
+ ),
+ maxLines: 1, overflow: TextOverflow.ellipsis,
+ ),
+ ),
+ if (durationLabel.isNotEmpty)
+ Padding(
+ padding: const EdgeInsets.only(left: 8),
+ child: Text(durationLabel,
+ style: TextStyle(fontSize: 11, color: cs.onSurfaceVariant.withValues(alpha: 0.5))),
+ ),
+ ]);
+ }
+}
diff --git a/lib/widgets/library_authors_tab.dart b/lib/widgets/library_authors_tab.dart
new file mode 100644
index 00000000..ff869b9e
--- /dev/null
+++ b/lib/widgets/library_authors_tab.dart
@@ -0,0 +1,73 @@
+import 'package:flutter/material.dart';
+import '../screens/library_screen.dart';
+import 'library_grid_tiles.dart';
+
+class LibraryAuthorsTab extends StatelessWidget {
+ final List<Map<String, dynamic>> authors;
+ final bool isLoadingAuthors;
+ final bool authorsLoaded;
+ final ScrollController scrollController;
+ final Future<void> Function() onRefresh;
+
+ const LibraryAuthorsTab({
+ super.key,
+ required this.authors,
+ required this.isLoadingAuthors,
+ required this.authorsLoaded,
+ required this.scrollController,
+ required this.onRefresh,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ final cs = Theme.of(context).colorScheme;
+ final tt = Theme.of(context).textTheme;
+
+ if (isLoadingAuthors) {
+ return const Center(child: CircularProgressIndicator());
+ }
+ if (authors.isEmpty && authorsLoaded) {
+ return RefreshIndicator(
+ onRefresh: onRefresh,
+ child: CustomScrollView(
+ physics: const AlwaysScrollableScrollPhysics(),
+ slivers: [
+ SliverFillRemaining(
+ child: Center(
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Icon(Icons.people_outline_rounded,
+ size: 56, color: cs.onSurfaceVariant.withValues(alpha: 0.3)),
+ const SizedBox(height: 12),
+ Text('No authors found',
+ style: tt.bodyLarge?.copyWith(color: cs.onSurfaceVariant)),
+ ],
+ ),
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+
+ return RefreshIndicator(
+ onRefresh: onRefresh,
+ child: GridView.builder(
+ controller: scrollController,
+ physics: const AlwaysScrollableScrollPhysics(),
+ padding: const EdgeInsets.fromLTRB(16, 8, 16, 100),
+ gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
+ crossAxisCount: responsiveGridCount(context),
+ childAspectRatio: 0.68,
+ crossAxisSpacing: 10,
+ mainAxisSpacing: 10,
+ ),
+ itemCount: authors.length,
+ itemBuilder: (context, index) {
+ return GridAuthorTile(author: authors[index]);
+ },
+ ),
+ );
+ }
+}
diff --git a/lib/widgets/library_books_tab.dart b/lib/widgets/library_books_tab.dart
new file mode 100644
index 00000000..2734b660
--- /dev/null
+++ b/lib/widgets/library_books_tab.dart
@@ -0,0 +1,113 @@
+import 'package:flutter/material.dart';
+import '../screens/library_screen.dart';
+import 'library_grid_tiles.dart';
+
+class LibraryBooksTab extends StatelessWidget {
+ final List<Map<String, dynamic>> items;
+ final bool isLoadingPage;
+ final bool hasMore;
+ final ScrollController scrollController;
+ final LibraryFilter filter;
+ final String? genreFilter;
+ final bool rectangleCovers;
+ final double coverAspectRatio;
+ final Future<void> Function() onRefresh;
+ final VoidCallback onClearFilter;
+
+ const LibraryBooksTab({
+ super.key,
+ required this.items,
+ required this.isLoadingPage,
+ required this.hasMore,
+ required this.scrollController,
+ required this.filter,
+ this.genreFilter,
+ required this.rectangleCovers,
+ required this.coverAspectRatio,
+ required this.onRefresh,
+ required this.onClearFilter,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ final cs = Theme.of(context).colorScheme;
+ final tt = Theme.of(context).textTheme;
+
+ if (items.isEmpty && isLoadingPage) {
+ return const Center(child: CircularProgressIndicator());
+ }
+ if (items.isEmpty && !isLoadingPage) {
+ final filterMsg = switch (filter) {
+ LibraryFilter.inProgress => 'No books in progress',
+ LibraryFilter.finished => 'No finished books',
+ LibraryFilter.notStarted => 'All books have been started',
+ LibraryFilter.downloaded => 'No downloaded books',
+ LibraryFilter.inASeries => 'No series found',
+ LibraryFilter.hasEbook => 'No books with eBooks',
+ LibraryFilter.genre => 'No books in "${genreFilter ?? 'genre'}"',
+ LibraryFilter.none => 'No books found',
+ };
+ return RefreshIndicator(
+ onRefresh: onRefresh,
+ child: CustomScrollView(
+ physics: const AlwaysScrollableScrollPhysics(),
+ slivers: [
+ SliverFillRemaining(
+ child: Center(
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Icon(Icons.library_books_outlined,
+ size: 56, color: cs.onSurfaceVariant.withValues(alpha: 0.3)),
+ const SizedBox(height: 12),
+ Text(filterMsg,
+ style: tt.bodyLarge?.copyWith(color: cs.onSurfaceVariant)),
+ if (filter != LibraryFilter.none) ...[
+ const SizedBox(height: 8),
+ GestureDetector(
+ onTap: onClearFilter,
+ child: Text('Clear filter',
+ style: tt.bodySmall?.copyWith(color: cs.primary)),
+ ),
+ ],
+ ],
+ ),
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+
+ return RefreshIndicator(
+ onRefresh: onRefresh,
+ child: GridView.builder(
+ controller: scrollController,
+ physics: const AlwaysScrollableScrollPhysics(),
+ padding: const EdgeInsets.fromLTRB(16, 8, 16, 100),
+ gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
+ crossAxisCount: responsiveGridCount(context),
+ childAspectRatio: rectangleCovers ? 0.48 : 0.68,
+ crossAxisSpacing: 10,
+ mainAxisSpacing: 10,
+ ),
+ itemCount: items.length + (hasMore ? 1 : 0),
+ itemBuilder: (context, index) {
+ if (index >= items.length) {
+ return const Center(
+ child: Padding(
+ padding: EdgeInsets.all(16),
+ child: CircularProgressIndicator(strokeWidth: 2),
+ ),
+ );
+ }
+ final item = items[index];
+ if (item.containsKey('collapsedSeries')) {
+ return GridSeriesTile(item: item, coverAspectRatio: coverAspectRatio);
+ }
+ return GridBookTile(item: item, coverAspectRatio: coverAspectRatio);
+ },
+ ),
+ );
+ }
+}
diff --git a/lib/widgets/library_series_tab.dart b/lib/widgets/library_series_tab.dart
new file mode 100644
index 00000000..2c6e4690
--- /dev/null
+++ b/lib/widgets/library_series_tab.dart
@@ -0,0 +1,85 @@
+import 'package:flutter/material.dart';
+import '../screens/library_screen.dart';
+import 'library_grid_tiles.dart';
+
+class LibrarySeriesTab extends StatelessWidget {
+ final List<Map<String, dynamic>> seriesItems;
+ final bool isLoadingSeriesPage;
+ final bool hasMoreSeries;
+ final ScrollController scrollController;
+ final bool rectangleCovers;
+ final double coverAspectRatio;
+ final Future<void> Function() onRefresh;
+
+ const LibrarySeriesTab({
+ super.key,
+ required this.seriesItems,
+ required this.isLoadingSeriesPage,
+ required this.hasMoreSeries,
+ required this.scrollController,
+ required this.rectangleCovers,
+ required this.coverAspectRatio,
+ required this.onRefresh,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ final cs = Theme.of(context).colorScheme;
+ final tt = Theme.of(context).textTheme;
+
+ if (seriesItems.isEmpty && isLoadingSeriesPage) {
+ return const Center(child: CircularProgressIndicator());
+ }
+ if (seriesItems.isEmpty && !isLoadingSeriesPage) {
+ return RefreshIndicator(
+ onRefresh: onRefresh,
+ child: CustomScrollView(
+ physics: const AlwaysScrollableScrollPhysics(),
+ slivers: [
+ SliverFillRemaining(
+ child: Center(
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Icon(Icons.collections_bookmark_outlined,
+ size: 56, color: cs.onSurfaceVariant.withValues(alpha: 0.3)),
+ const SizedBox(height: 12),
+ Text('No series found',
+ style: tt.bodyLarge?.copyWith(color: cs.onSurfaceVariant)),
+ ],
+ ),
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+
+ return RefreshIndicator(
+ onRefresh: onRefresh,
+ child: GridView.builder(
+ controller: scrollController,
+ physics: const AlwaysScrollableScrollPhysics(),
+ padding: const EdgeInsets.fromLTRB(16, 8, 16, 100),
+ gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
+ crossAxisCount: responsiveGridCount(context),
+ childAspectRatio: rectangleCovers ? 0.48 : 0.68,
+ crossAxisSpacing: 10,
+ mainAxisSpacing: 10,
+ ),
+ itemCount: seriesItems.length + (hasMoreSeries ? 1 : 0),
+ itemBuilder: (context, index) {
+ if (index >= seriesItems.length) {
+ return const Center(
+ child: Padding(
+ padding: EdgeInsets.all(16),
+ child: CircularProgressIndicator(strokeWidth: 2),
+ ),
+ );
+ }
+ return GridSeriesTileDirect(series: seriesItems[index], coverAspectRatio: coverAspectRatio);
+ },
+ ),
+ );
+ }
+}