Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Prevent infinite sheet stacking, add double-back-to-exit, auto-scroll series

Nathan Poulson4ba084dd251a
diff --git a/lib/screens/app_shell.dart b/lib/screens/app_shell.dart
index d2d1209f..a6ca9bdd 100644
--- a/lib/screens/app_shell.dart
+++ b/lib/screens/app_shell.dart
@@ -51,7 +51,7 @@ class _AppShellState extends State<AppShell> with WidgetsBindingObserver, Ticker
String? _lastItemId;
bool _expandedIsOpen = false;
bool _wasCasting = false;
-
+ DateTime? _lastBackPress;
// Lazily build tabs so startup on Absorbing does not initialize Home/Library
// work until the user actually visits those tabs.
@@ -268,9 +268,21 @@ class _AppShellState extends State<AppShell> with WidgetsBindingObserver, Ticker
return;
}
- // If already on Absorbing tab, move app to background (keep playback alive)
+ // If already on Absorbing tab, require double-back to exit
if (_currentIndex == 2) {
- SystemChannels.platform.invokeMethod('SystemNavigator.pop', true);
+ final now = DateTime.now();
+ if (_lastBackPress != null && now.difference(_lastBackPress!) < const Duration(seconds: 2)) {
+ SystemChannels.platform.invokeMethod('SystemNavigator.pop', true);
+ return;
+ }
+ _lastBackPress = now;
+ ScaffoldMessenger.of(context).clearSnackBars();
+ ScaffoldMessenger.of(context).showSnackBar(SnackBar(
+ content: const Text('Press back again to exit'),
+ duration: const Duration(seconds: 2),
+ behavior: SnackBarBehavior.floating,
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
+ ));
return;
}
diff --git a/lib/widgets/book_detail_sheet.dart b/lib/widgets/book_detail_sheet.dart
index 70bf5d5d..a1444f50 100644
--- a/lib/widgets/book_detail_sheet.dart
+++ b/lib/widgets/book_detail_sheet.dart
@@ -562,9 +562,11 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
Future<void> _openSeries(BuildContext context, String? seriesId, String seriesName) async {
if (seriesId == null) return;
final auth = context.read<AuthProvider>();
- // Show series sheet on top of the detail sheet (don't close detail)
+ // Close current sheet before opening series to prevent infinite stacking
+ final nav = Navigator.of(context);
+ nav.pop();
showSeriesBooksSheet(
- context,
+ nav.context,
seriesName: seriesName,
seriesId: seriesId,
serverUrl: auth.serverUrl,
diff --git a/lib/widgets/episode_list_sheet.dart b/lib/widgets/episode_list_sheet.dart
index 24a0ccc0..2872bd85 100644
--- a/lib/widgets/episode_list_sheet.dart
+++ b/lib/widgets/episode_list_sheet.dart
@@ -893,8 +893,9 @@ class _EpisodeDetailSheetState extends State<EpisodeDetailSheet> {
const SizedBox(height: 16),
GestureDetector(
onTap: () {
- Navigator.pop(context);
- EpisodeListSheet.show(context, widget.podcastItem);
+ final nav = Navigator.of(context);
+ nav.pop();
+ EpisodeListSheet.show(nav.context, widget.podcastItem);
},
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
@@ -1154,7 +1155,12 @@ class _EpisodeRowState extends State<_EpisodeRow> {
}
return InkWell(
- onTap: () => EpisodeDetailSheet.show(context, widget.podcastItem, ep),
+ 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(
diff --git a/lib/widgets/series_books_sheet.dart b/lib/widgets/series_books_sheet.dart
index 62f933cb..1bccaffc 100644
--- a/lib/widgets/series_books_sheet.dart
+++ b/lib/widgets/series_books_sheet.dart
@@ -67,18 +67,53 @@ class _SeriesBooksSheetState extends State<SeriesBooksSheet> {
bool _isMarkingAll = false;
bool _autoDownloadEnabled = false;
+ bool _didAutoScroll = false;
+
@override
void initState() {
super.initState();
// Use passed books as initial data
_books = _unwrapBooks(widget.books);
_sortBooks();
- if (_books.isNotEmpty) _isLoading = false;
+ if (_books.isNotEmpty) {
+ _isLoading = false;
+ _scrollToUpNext();
+ }
// Fetch full data from API for proper sequence info
_fetchFromApi();
_loadAutoDownloadState();
}
+ void _scrollToUpNext() {
+ if (_didAutoScroll || _books.isEmpty) return;
+ _didAutoScroll = true;
+ final lib = context.read<LibraryProvider>();
+ int firstUnfinished = -1;
+ for (int i = 0; i < _books.length; i++) {
+ final bookId = _books[i]['id'] as String? ?? '';
+ if (lib.getProgressData(bookId)?['isFinished'] != true) {
+ firstUnfinished = i;
+ break;
+ }
+ }
+ // If all finished, scroll to bottom; if first is unfinished, stay at top
+ final targetIndex = firstUnfinished == -1 ? _books.length - 1 : firstUnfinished;
+ if (targetIndex <= 0) return;
+ WidgetsBinding.instance.addPostFrameCallback((_) {
+ if (!mounted || !widget.scrollController.hasClients) return;
+ // Each book card is ~88px (80 height + 8 bottom padding)
+ final offset = (targetIndex * 88.0).clamp(
+ 0.0,
+ widget.scrollController.position.maxScrollExtent,
+ );
+ widget.scrollController.animateTo(
+ offset,
+ duration: const Duration(milliseconds: 400),
+ curve: Curves.easeOutCubic,
+ );
+ });
+ }
+
void _loadAutoDownloadState() {
final seriesId = widget.seriesId;
if (seriesId == null || seriesId.isEmpty) return;
@@ -179,6 +214,7 @@ class _SeriesBooksSheetState extends State<SeriesBooksSheet> {
_sortBooks();
_isLoading = false;
});
+ _scrollToUpNext();
return;
}
}
@@ -561,10 +597,13 @@ class _SeriesBooksSheetState extends State<SeriesBooksSheet> {
child: InkWell(
onTap: () {
if (bookId.isNotEmpty) {
+ // Close series sheet before opening book to prevent infinite stacking
+ final nav = Navigator.of(context);
+ nav.pop();
if (lib.isPodcastLibrary) {
- EpisodeListSheet.show(context, book);
+ EpisodeListSheet.show(nav.context, book);
} else {
- showBookDetailSheet(context, bookId);
+ showBookDetailSheet(nav.context, bookId);
}
}
},