feat: update PinballGamePage

pull/195/head
arturplaczek 3 years ago
parent 446f782abb
commit edad16ff81

@ -5,45 +5,25 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pinball/game/game.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_audio/pinball_audio.dart';
import 'package:pinball_theme/pinball_theme.dart';
class PinballGamePage extends StatelessWidget { class PinballGamePage extends StatelessWidget {
const PinballGamePage({ const PinballGamePage({
Key? key, Key? key,
required this.theme, this.isDebugMode = kDebugMode,
required this.game,
}) : super(key: key); }) : super(key: key);
final PinballTheme theme; final bool isDebugMode;
final PinballGame game;
static Route route({ static Route route({
required PinballTheme theme,
bool isDebugMode = kDebugMode, bool isDebugMode = kDebugMode,
}) { }) {
return MaterialPageRoute<void>( return MaterialPageRoute<void>(
builder: (context) { builder: (context) {
final audio = context.read<PinballAudio>(); return PinballGamePage(
isDebugMode: isDebugMode,
final game = isDebugMode
? DebugPinballGame(theme: theme, audio: audio)
: PinballGame(theme: theme, audio: audio);
final pinballAudio = context.read<PinballAudio>();
final loadables = [
...game.preLoadAssets(),
pinballAudio.load(),
];
return MultiBlocProvider(
providers: [
BlocProvider(create: (_) => GameBloc()),
BlocProvider(
create: (_) => AssetsManagerCubit(loadables)..load(),
),
],
child: PinballGamePage(theme: theme, game: game),
); );
}, },
); );
@ -51,7 +31,31 @@ class PinballGamePage extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return PinballGameView(game: game); final theme = context.read<ThemeCubit>().state.theme;
final audio = context.read<PinballAudio>();
final pinballAudio = context.read<PinballAudio>();
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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final loadingProgress = context.watch<AssetsManagerCubit>().state.progress; final isLoading = context.select(
(AssetsManagerCubit bloc) => bloc.state.progress != 1,
);
if (loadingProgress != 1) { return Scaffold(
return Scaffold( backgroundColor: Colors.blue,
body: Center( body: isLoading
child: Text( ? _PinballGameLoadingView()
loadingProgress.toString(), : 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( return Stack(
children: [ children: [
Positioned.fill( Positioned.fill(

@ -5,40 +5,46 @@ import 'package:flame/game.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball_theme/pinball_theme.dart'; import 'package:pinball/theme/theme.dart';
import '../../helpers/helpers.dart'; import '../../helpers/helpers.dart';
void main() { void main() {
const theme = PinballTheme(characterTheme: DashTheme());
final game = PinballTestGame(); final game = PinballTestGame();
group('PinballGamePage', () { group('PinballGamePage', () {
testWidgets('renders PinballGameView', (tester) async { late ThemeCubit themeCubit;
final gameBloc = MockGameBloc(); late GameBloc gameBloc;
setUp(() {
themeCubit = MockThemeCubit();
gameBloc = MockGameBloc();
whenListen(
themeCubit,
const Stream<ThemeState>.empty(),
initialState: const ThemeState.initial(),
);
whenListen( whenListen(
gameBloc, gameBloc,
Stream.value(const GameState.initial()), Stream.value(const GameState.initial()),
initialState: const GameState.initial(), initialState: const GameState.initial(),
); );
});
testWidgets('renders PinballGameView', (tester) async {
await tester.pumpApp( await tester.pumpApp(
PinballGamePage(theme: theme, game: game), PinballGamePage(),
gameBloc: gameBloc, themeCubit: themeCubit,
); );
expect(find.byType(PinballGameView), findsOneWidget); expect(find.byType(PinballGameView), findsOneWidget);
}); });
testWidgets( testWidgets(
'renders the loading indicator while the assets load', 'renders the loading indicator while the assets load',
(tester) async { (tester) async {
final gameBloc = MockGameBloc();
whenListen(
gameBloc,
Stream.value(const GameState.initial()),
initialState: const GameState.initial(),
);
final assetsManagerCubit = MockAssetsManagerCubit(); final assetsManagerCubit = MockAssetsManagerCubit();
final initialAssetsState = AssetsManagerState( final initialAssetsState = AssetsManagerState(
loadables: [Future<void>.value()], loadables: [Future<void>.value()],
@ -51,11 +57,21 @@ void main() {
); );
await tester.pumpApp( await tester.pumpApp(
PinballGamePage(theme: theme, game: game), PinballGameView(
gameBloc: gameBloc, game: game,
),
assetsManagerCubit: assetsManagerCubit, 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( final loadedAssetsState = AssetsManagerState(
loadables: [Future<void>.value()], loadables: [Future<void>.value()],
@ -68,7 +84,8 @@ void main() {
); );
await tester.pump(); await tester.pump();
expect(find.byType(PinballGameView), findsOneWidget);
expect(find.byType(PinballGameBody), findsOneWidget);
}, },
); );
@ -85,7 +102,6 @@ void main() {
onPressed: () { onPressed: () {
Navigator.of(context).push<void>( Navigator.of(context).push<void>(
PinballGamePage.route( PinballGamePage.route(
theme: theme,
isDebugMode: isDebugMode, isDebugMode: isDebugMode,
), ),
); );
@ -95,6 +111,7 @@ void main() {
}, },
), ),
), ),
themeCubit: themeCubit,
); );
await tester.tap(find.text('Tap me')); await tester.tap(find.text('Tap me'));

Loading…
Cancel
Save