From 8a184f6ae6238e5a2e78b3839b1cf080fd16aae2 Mon Sep 17 00:00:00 2001 From: edde746 <86283021+edde746@users.noreply.github.com> Date: Sun, 14 Dec 2025 01:37:07 +0100 Subject: [PATCH] fix: playlist issue --- lib/services/play_queue_launcher.dart | 52 +++++++++++++++++++++------ 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/services/play_queue_launcher.dart b/lib/services/play_queue_launcher.dart index 9e7ae35b..29094b03 100644 --- a/lib/services/play_queue_launcher.dart +++ b/lib/services/play_queue_launcher.dart @@ -66,7 +66,7 @@ class PlayQueueLauncher { return _executeWithLoading( showLoading: showLoadingIndicator, action: t.common.shuffle, - execute: () async { + execute: (dismissLoading) async { final String ratingKey = item.ratingKey; final String? itemServerId = item.serverId ?? serverId; final String? itemServerName = item.serverName ?? serverName; @@ -110,6 +110,9 @@ class PlayQueueLauncher { } } + // Close loading dialog before navigating to the player + await dismissLoading(); + return _launchFromQueue( playQueue: playQueue, ratingKey: ratingKey, @@ -129,13 +132,16 @@ class PlayQueueLauncher { return _executeWithLoading( showLoading: showLoadingIndicator, action: t.discover.play, - execute: () async { + execute: (dismissLoading) async { final playQueue = await client.createPlayQueue( playlistID: int.parse(playlist.ratingKey), type: 'video', key: selectedItem.key, ); + // Close loading dialog before navigating to the player + await dismissLoading(); + return _launchFromQueue( playQueue: playQueue, ratingKey: playlist.ratingKey, @@ -163,7 +169,7 @@ class PlayQueueLauncher { return _executeWithLoading( showLoading: showLoadingIndicator, action: t.common.shuffle, - execute: () async { + execute: (dismissLoading) async { // Determine the rating key for the play queue String showRatingKey; if (mediaType == PlexMediaType.show) { @@ -181,6 +187,9 @@ class PlayQueueLauncher { shuffle: 1, ); + // Close loading dialog before navigating to the player + await dismissLoading(); + return _launchFromQueue( playQueue: playQueue, ratingKey: showRatingKey, @@ -242,19 +251,43 @@ class PlayQueueLauncher { Future _executeWithLoading({ required bool showLoading, required String action, - required Future Function() execute, + required Future Function( + Future Function() dismissLoading, + ) execute, }) async { + BuildContext? loadingDialogContext; + var loadingVisible = false; + // Show loading indicator if (showLoading && context.mounted) { + loadingVisible = true; showDialog( context: context, barrierDismissible: false, - builder: (context) => const Center(child: CircularProgressIndicator()), + builder: (dialogContext) { + loadingDialogContext = dialogContext; + return const Center(child: CircularProgressIndicator()); + }, ); } + Future dismissLoading() async { + if (!showLoading || !loadingVisible) return; + final dialogContext = loadingDialogContext; + if (dialogContext == null) return; + + // Only dismiss if the dialog is still the current route to avoid + // accidentally popping the player after navigation. + final route = ModalRoute.of(dialogContext); + if (route?.isCurrent ?? false) { + Navigator.of(dialogContext).pop(); + } + + loadingVisible = false; + } + try { - final result = await execute(); + final result = await execute(dismissLoading); // Handle empty queue result if (result is PlayQueueEmpty && context.mounted) { @@ -263,6 +296,7 @@ class PlayQueueLauncher { ); } + await dismissLoading(); return result; } catch (e) { appLogger.e('Failed to $action', error: e); @@ -277,12 +311,10 @@ class PlayQueueLauncher { ); } + await dismissLoading(); return PlayQueueError(e); } finally { - // Close loading indicator (guaranteed cleanup) - if (showLoading && context.mounted && Navigator.canPop(context)) { - Navigator.pop(context); - } + await dismissLoading(); } } }