From 4d29da84b2011d49f90c3c38885c1a99d83bde99 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Sun, 27 Oct 2024 22:21:03 +0800 Subject: [PATCH] Fix slideshow using the same shuffled index for multiple iterations --- app/lib/widget/slideshow_viewer/bloc.dart | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/lib/widget/slideshow_viewer/bloc.dart b/app/lib/widget/slideshow_viewer/bloc.dart index 643dda99..d9fc12f0 100644 --- a/app/lib/widget/slideshow_viewer/bloc.dart +++ b/app/lib/widget/slideshow_viewer/bloc.dart @@ -38,8 +38,18 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger { String get tag => _log.fullName; /// Convert the page index to the corresponding item index - int convertPageToFileIndex(int pageIndex) => - _shuffledIndex[pageIndex % files.length]; + int convertPageToFileIndex(int pageIndex) { + if (config.isShuffle) { + final i = pageIndex ~/ files.length; + if (!_shuffledIndex.containsKey(i)) { + final index = [for (var i = 0; i < files.length; ++i) i]; + _shuffledIndex[i] = index..shuffle(); + } + return _shuffledIndex[i]![pageIndex % files.length]; + } else { + return _shuffledIndex[0]![pageIndex % files.length]; + } + } FileDescriptor getFileByPageIndex(int pageIndex) => files[convertPageToFileIndex(pageIndex)]; @@ -51,7 +61,7 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger { startIndex: startIndex, config: config, ); - _shuffledIndex = parsedConfig.shuffled; + _shuffledIndex = {0: parsedConfig.shuffled}; initialPage = parsedConfig.initial; pageCount = parsedConfig.count; emit(state.copyWith( @@ -259,7 +269,7 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger { final int startIndex; final SlideshowConfig config; - late final List _shuffledIndex; + late final Map> _shuffledIndex; late final int initialPage; late final int? pageCount; Timer? _pageChangeTimer;