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_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<void>(
builder: (context) {
final audio = context.read<PinballAudio>();
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),
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<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
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(
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(

@ -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<ThemeState>.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<void>.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<void>.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<void>(
PinballGamePage.route(
theme: theme,
isDebugMode: isDebugMode,
),
);
@ -95,6 +111,7 @@ void main() {
},
),
),
themeCubit: themeCubit,
);
await tester.tap(find.text('Tap me'));

Loading…
Cancel
Save