From edad16ff817641c324b1a1ddf27934895b2e9cf1 Mon Sep 17 00:00:00 2001 From: arturplaczek Date: Fri, 15 Apr 2022 14:08:20 +0200 Subject: [PATCH] feat: update PinballGamePage --- lib/game/view/pinball_game_page.dart | 109 ++++++++++++++------- test/game/view/pinball_game_page_test.dart | 53 ++++++---- 2 files changed, 108 insertions(+), 54 deletions(-) diff --git a/lib/game/view/pinball_game_page.dart b/lib/game/view/pinball_game_page.dart index f6b7ee81..39cd8265 100644 --- a/lib/game/view/pinball_game_page.dart +++ b/lib/game/view/pinball_game_page.dart @@ -5,45 +5,25 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball/start_game/bloc/start_game_bloc.dart'; +import 'package:pinball/theme/theme.dart'; import 'package:pinball_audio/pinball_audio.dart'; -import 'package:pinball_theme/pinball_theme.dart'; class PinballGamePage extends StatelessWidget { const PinballGamePage({ Key? key, - required this.theme, - required this.game, + this.isDebugMode = kDebugMode, }) : super(key: key); - final PinballTheme theme; - final PinballGame game; + final bool isDebugMode; static Route route({ - required PinballTheme theme, bool isDebugMode = kDebugMode, }) { return MaterialPageRoute( builder: (context) { - final audio = context.read(); - - final game = isDebugMode - ? DebugPinballGame(theme: theme, audio: audio) - : PinballGame(theme: theme, audio: audio); - - final pinballAudio = context.read(); - final loadables = [ - ...game.preLoadAssets(), - pinballAudio.load(), - ]; - - return MultiBlocProvider( - providers: [ - BlocProvider(create: (_) => GameBloc()), - BlocProvider( - create: (_) => AssetsManagerCubit(loadables)..load(), - ), - ], - child: PinballGamePage(theme: theme, game: game), + return PinballGamePage( + isDebugMode: isDebugMode, ); }, ); @@ -51,7 +31,31 @@ class PinballGamePage extends StatelessWidget { @override Widget build(BuildContext context) { - return PinballGameView(game: game); + final theme = context.read().state.theme; + final audio = context.read(); + final pinballAudio = context.read(); + + final game = isDebugMode + ? DebugPinballGame(theme: theme, audio: audio) + : PinballGame(theme: theme, audio: audio); + + final loadables = [ + ...game.preLoadAssets(), + pinballAudio.load(), + ]; + + return MultiBlocProvider( + providers: [ + BlocProvider(create: (_) => StartGameBloc(game: game)), + BlocProvider(create: (_) => GameBloc()), + BlocProvider( + create: (_) => AssetsManagerCubit(loadables)..load(), + ), + ], + child: PinballGameView( + game: game, + ), + ); } } @@ -65,18 +69,51 @@ class PinballGameView extends StatelessWidget { @override Widget build(BuildContext context) { - final loadingProgress = context.watch().state.progress; + final isLoading = context.select( + (AssetsManagerCubit bloc) => bloc.state.progress != 1, + ); - if (loadingProgress != 1) { - return Scaffold( - body: Center( - child: Text( - loadingProgress.toString(), - ), + return Scaffold( + backgroundColor: Colors.blue, + body: isLoading + ? _PinballGameLoadingView() + : PinballGameBody( + game: game, + ), + ); + } +} + +class _PinballGameLoadingView extends StatelessWidget { + @override + Widget build(BuildContext context) { + final loadingProgress = context.select( + (AssetsManagerCubit bloc) => bloc.state.progress, + ); + + return Padding( + padding: const EdgeInsets.all(24), + child: Center( + child: LinearProgressIndicator( + color: Colors.white, + value: loadingProgress, ), - ); - } + ), + ); + } +} +@visibleForTesting +class PinballGameBody extends StatelessWidget { + const PinballGameBody({ + Key? key, + required this.game, + }) : super(key: key); + + final PinballGame game; + + @override + Widget build(BuildContext context) { return Stack( children: [ Positioned.fill( diff --git a/test/game/view/pinball_game_page_test.dart b/test/game/view/pinball_game_page_test.dart index 85f9cfc3..8a907cc1 100644 --- a/test/game/view/pinball_game_page_test.dart +++ b/test/game/view/pinball_game_page_test.dart @@ -5,40 +5,46 @@ import 'package:flame/game.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pinball/game/game.dart'; -import 'package:pinball_theme/pinball_theme.dart'; +import 'package:pinball/theme/theme.dart'; import '../../helpers/helpers.dart'; void main() { - const theme = PinballTheme(characterTheme: DashTheme()); final game = PinballTestGame(); group('PinballGamePage', () { - testWidgets('renders PinballGameView', (tester) async { - final gameBloc = MockGameBloc(); + late ThemeCubit themeCubit; + late GameBloc gameBloc; + + setUp(() { + themeCubit = MockThemeCubit(); + gameBloc = MockGameBloc(); + + whenListen( + themeCubit, + const Stream.empty(), + initialState: const ThemeState.initial(), + ); + whenListen( gameBloc, Stream.value(const GameState.initial()), initialState: const GameState.initial(), ); + }); + testWidgets('renders PinballGameView', (tester) async { await tester.pumpApp( - PinballGamePage(theme: theme, game: game), - gameBloc: gameBloc, + PinballGamePage(), + themeCubit: themeCubit, ); + expect(find.byType(PinballGameView), findsOneWidget); }); testWidgets( 'renders the loading indicator while the assets load', (tester) async { - final gameBloc = MockGameBloc(); - whenListen( - gameBloc, - Stream.value(const GameState.initial()), - initialState: const GameState.initial(), - ); - final assetsManagerCubit = MockAssetsManagerCubit(); final initialAssetsState = AssetsManagerState( loadables: [Future.value()], @@ -51,11 +57,21 @@ void main() { ); await tester.pumpApp( - PinballGamePage(theme: theme, game: game), - gameBloc: gameBloc, + PinballGameView( + game: game, + ), assetsManagerCubit: assetsManagerCubit, + themeCubit: themeCubit, + gameBloc: gameBloc, + ); + + expect( + find.byWidgetPredicate( + (widget) => + widget is LinearProgressIndicator && widget.value == 0.0, + ), + findsOneWidget, ); - expect(find.text('0.0'), findsOneWidget); final loadedAssetsState = AssetsManagerState( loadables: [Future.value()], @@ -68,7 +84,8 @@ void main() { ); await tester.pump(); - expect(find.byType(PinballGameView), findsOneWidget); + + expect(find.byType(PinballGameBody), findsOneWidget); }, ); @@ -85,7 +102,6 @@ void main() { onPressed: () { Navigator.of(context).push( PinballGamePage.route( - theme: theme, isDebugMode: isDebugMode, ), ); @@ -95,6 +111,7 @@ void main() { }, ), ), + themeCubit: themeCubit, ); await tester.tap(find.text('Tap me'));