Fix slideshow using the same shuffled index for multiple iterations

This commit is contained in:
Ming Ming 2024-10-27 22:21:03 +08:00
parent c20f2441ef
commit 4d29da84b2

View file

@ -38,8 +38,18 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger {
String get tag => _log.fullName; String get tag => _log.fullName;
/// Convert the page index to the corresponding item index /// Convert the page index to the corresponding item index
int convertPageToFileIndex(int pageIndex) => int convertPageToFileIndex(int pageIndex) {
_shuffledIndex[pageIndex % files.length]; 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) => FileDescriptor getFileByPageIndex(int pageIndex) =>
files[convertPageToFileIndex(pageIndex)]; files[convertPageToFileIndex(pageIndex)];
@ -51,7 +61,7 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger {
startIndex: startIndex, startIndex: startIndex,
config: config, config: config,
); );
_shuffledIndex = parsedConfig.shuffled; _shuffledIndex = {0: parsedConfig.shuffled};
initialPage = parsedConfig.initial; initialPage = parsedConfig.initial;
pageCount = parsedConfig.count; pageCount = parsedConfig.count;
emit(state.copyWith( emit(state.copyWith(
@ -259,7 +269,7 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger {
final int startIndex; final int startIndex;
final SlideshowConfig config; final SlideshowConfig config;
late final List<int> _shuffledIndex; late final Map<int, List<int>> _shuffledIndex;
late final int initialPage; late final int initialPage;
late final int? pageCount; late final int? pageCount;
Timer? _pageChangeTimer; Timer? _pageChangeTimer;