diff --git a/packages/trashy_road/lib/src/loading/cubit/preload/preload_cubit.dart b/packages/trashy_road/lib/src/loading/cubit/preload/preload_cubit.dart index 1374eaed..35366d72 100644 --- a/packages/trashy_road/lib/src/loading/cubit/preload/preload_cubit.dart +++ b/packages/trashy_road/lib/src/loading/cubit/preload/preload_cubit.dart @@ -1,5 +1,6 @@ import 'package:audioplayers/audioplayers.dart'; import 'package:bloc/bloc.dart'; +import 'package:collection/collection.dart'; import 'package:equatable/equatable.dart'; import 'package:flame/cache.dart'; import 'package:flutter/widgets.dart'; @@ -29,36 +30,31 @@ class PreloadCubit extends Cubit { /// Load items sequentially allows display of what is being loaded Future loadSequentially() async { + final displayImages = + Assets.images.display.values.whereType().toList(); + final spritePaths = + Assets.images.sprites.values.map((image) => image.path).toList(); + final phases = [ - PreloadPhase( - 'audio', - () => audio.loadAll(Assets.audio.values), + ...PreloadPhase.sliced( + name: 'audio', + items: Assets.audio.values, + start: audio.loadAll, ), - PreloadPhase( - 'images', - () => imageProviderCache.loadAll( - [ - Assets.images.display.pauseIcon, - Assets.images.display.paperBackground, - Assets.images.display.replayIcon, - Assets.images.display.playIcon, - Assets.images.display.menuIcon, - Assets.images.display.nextIcon, - ], - ), + ...PreloadPhase.sliced( + name: 'images', + items: displayImages, + start: imageProviderCache.loadAll, ), - PreloadPhase( - 'game images', - () => images.loadAll([]), + ...PreloadPhase.sliced( + name: 'sprites', + items: spritePaths, + start: images.loadAll, ), - PreloadPhase( - 'maps', - () => tiled.loadAll( - [ - Assets.tiles.map1, - Assets.tiles.map2, - ], - ), + ...PreloadPhase.sliced( + name: 'maps', + items: [Assets.tiles.map1, Assets.tiles.map2], + start: tiled.loadAll, ), ]; @@ -81,4 +77,24 @@ class PreloadPhase { final String label; final ValueGetter> start; + + static Iterable sliced({ + required String name, + required Iterable items, + required Future Function(List items) start, + }) { + const sliceSize = 15; + final slices = items.slices(sliceSize).toList(); + final phases = []; + for (var phaseIndex = 0; phaseIndex < slices.length; phaseIndex++) { + final phase = slices[phaseIndex]; + phases.add( + PreloadPhase( + '$name ${phaseIndex + 1} of ${slices.length}', + () => start(phase), + ), + ); + } + return phases; + } }