mirror of https://github.com/flutter/pinball.git
refactor: improve performance of loading screen and load I/O Pinball asset (#394)
parent
2284f57ac2
commit
28a3c15fe2
After Width: | Height: | Size: 7.8 KiB |
@ -1,27 +1,39 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball/select_character/select_character.dart';
|
||||
import 'package:pinball_audio/pinball_audio.dart';
|
||||
|
||||
part 'assets_manager_state.dart';
|
||||
|
||||
/// {@template assets_manager_cubit}
|
||||
/// Cubit responsable for pre loading any game assets
|
||||
/// {@endtemplate}
|
||||
class AssetsManagerCubit extends Cubit<AssetsManagerState> {
|
||||
/// {@macro assets_manager_cubit}
|
||||
AssetsManagerCubit(List<Future> loadables)
|
||||
: super(
|
||||
AssetsManagerState.initial(
|
||||
loadables: loadables,
|
||||
),
|
||||
);
|
||||
AssetsManagerCubit(this._game, this._player)
|
||||
: super(const AssetsManagerState.initial());
|
||||
|
||||
final PinballGame _game;
|
||||
final PinballPlayer _player;
|
||||
|
||||
/// Loads the assets
|
||||
Future<void> load() async {
|
||||
/// Assigning loadables is a very expensive operation. With this purposeful
|
||||
/// delay here, which is a bit random in duration but enough to let the UI
|
||||
/// do its job without adding too much delay for the user, we are letting
|
||||
/// the UI paint first, and then we start loading the assets.
|
||||
await Future<void>.delayed(const Duration(milliseconds: 300));
|
||||
emit(
|
||||
state.copyWith(
|
||||
loadables: [
|
||||
_game.preFetchLeaderboard(),
|
||||
..._game.preLoadAssets(),
|
||||
..._player.load(),
|
||||
...BonusAnimation.loadAssets(),
|
||||
...SelectedCharacter.loadAssets(),
|
||||
],
|
||||
),
|
||||
);
|
||||
final all = state.loadables.map((loadable) async {
|
||||
await loadable;
|
||||
emit(state.copyWith(loaded: [...state.loaded, loadable]));
|
||||
}).toList();
|
||||
|
||||
await Future.wait(all);
|
||||
}
|
||||
}
|
||||
|
@ -1,35 +0,0 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball/assets_manager/assets_manager.dart';
|
||||
|
||||
void main() {
|
||||
group('AssetsManagerCubit', () {
|
||||
final completer1 = Completer<void>();
|
||||
final completer2 = Completer<void>();
|
||||
|
||||
final future1 = completer1.future;
|
||||
final future2 = completer2.future;
|
||||
|
||||
blocTest<AssetsManagerCubit, AssetsManagerState>(
|
||||
'emits the loaded on the order that they load',
|
||||
build: () => AssetsManagerCubit([future1, future2]),
|
||||
act: (cubit) {
|
||||
cubit.load();
|
||||
completer2.complete();
|
||||
completer1.complete();
|
||||
},
|
||||
expect: () => [
|
||||
AssetsManagerState(
|
||||
loadables: [future1, future2],
|
||||
loaded: [future2],
|
||||
),
|
||||
AssetsManagerState(
|
||||
loadables: [future1, future2],
|
||||
loaded: [future2, future1],
|
||||
),
|
||||
],
|
||||
);
|
||||
});
|
||||
}
|
Loading…
Reference in new issue