fix: apply code review

pull/206/head
arturplaczek 3 years ago
parent 111295d89e
commit c0ac11dc35

@ -14,10 +14,10 @@ enum GameBonus {
sparkyTurboCharge, sparkyTurboCharge,
/// Bonus achieved when the ball goes in the dino mouth. /// Bonus achieved when the ball goes in the dino mouth.
dino, dinoChomp,
/// Bonus achieved when a ball enters the android spaceship. /// Bonus achieved when a ball enters the android spaceship.
android, androidSpaceship,
} }
/// {@template game_state} /// {@template game_state}

@ -6,8 +6,8 @@ import 'package:pinball/game/game.dart';
import 'package:pinball/l10n/l10n.dart'; import 'package:pinball/l10n/l10n.dart';
import 'package:pinball/theme/theme.dart'; import 'package:pinball/theme/theme.dart';
class ScoreBalls extends StatelessWidget { class BallCountDisplay extends StatelessWidget {
const ScoreBalls({Key? key}) : super(key: key); const BallCountDisplay({Key? key}) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -25,9 +25,9 @@ class ScoreBalls extends StatelessWidget {
const SizedBox(width: 8), const SizedBox(width: 8),
Row( Row(
children: [ children: [
ScoreBall(isActive: balls >= 1), BallIndicator(isActive: balls >= 1),
ScoreBall(isActive: balls >= 2), BallIndicator(isActive: balls >= 2),
ScoreBall(isActive: balls >= 3), BallIndicator(isActive: balls >= 3),
], ],
), ),
], ],
@ -36,8 +36,8 @@ class ScoreBalls extends StatelessWidget {
} }
@visibleForTesting @visibleForTesting
class ScoreBall extends StatelessWidget { class BallIndicator extends StatelessWidget {
const ScoreBall({ const BallIndicator({
Key? key, Key? key,
required this.isActive, required this.isActive,
}) : super(key: key); }) : super(key: key);

@ -32,7 +32,7 @@ class BonusAnimation extends StatelessWidget {
key: key, key: key,
); );
BonusAnimation.dino({ BonusAnimation.dinoChomp({
Key? key, Key? key,
VoidCallback? onCompleted, VoidCallback? onCompleted,
}) : this._( }) : this._(
@ -41,7 +41,7 @@ class BonusAnimation extends StatelessWidget {
key: key, key: key,
); );
BonusAnimation.android({ BonusAnimation.androidSpaceship({
Key? key, Key? key,
VoidCallback? onCompleted, VoidCallback? onCompleted,
}) : this._( }) : this._(
@ -50,7 +50,7 @@ class BonusAnimation extends StatelessWidget {
key: key, key: key,
); );
BonusAnimation.google({ BonusAnimation.googleWord({
Key? key, Key? key,
VoidCallback? onCompleted, VoidCallback? onCompleted,
}) : this._( }) : this._(

@ -4,7 +4,7 @@ import 'package:pinball/game/game.dart';
import 'package:pinball/gen/gen.dart'; import 'package:pinball/gen/gen.dart';
/// {@template game_hud} /// {@template game_hud}
/// Overlay of a [PinballGame]. /// Overlay on the [PinballGame].
/// ///
/// Displays the current [GameState.score], [GameState.balls] and animates when /// Displays the current [GameState.score], [GameState.balls] and animates when
/// the player gets a [GameBonus]. /// the player gets a [GameBonus].
@ -33,7 +33,8 @@ class _GameHudState extends State<GameHud> {
height: _width / _ratio, height: _width / _ratio,
width: _width, width: _width,
child: BlocListener<GameBloc, GameState>( child: BlocListener<GameBloc, GameState>(
listenWhen: _listenWhenBonusChanges, listenWhen: (previous, current) =>
previous.bonusHistory.length != current.bonusHistory.length,
listener: (_, __) => setState(() => showAnimation = true), listener: (_, __) => setState(() => showAnimation = true),
child: AnimatedSwitcher( child: AnimatedSwitcher(
duration: kThemeAnimationDuration, duration: kThemeAnimationDuration,
@ -51,12 +52,6 @@ class _GameHudState extends State<GameHud> {
), ),
); );
} }
bool _listenWhenBonusChanges(GameState previous, GameState current) {
final previousCount = previous.bonusHistory.length;
final currentCount = current.bonusHistory.length;
return previousCount != currentCount;
}
} }
class _ScoreViewDecoration extends StatelessWidget { class _ScoreViewDecoration extends StatelessWidget {
@ -105,12 +100,12 @@ class _AnimationView extends StatelessWidget {
return BonusAnimation.dashNest(onCompleted: onComplete); return BonusAnimation.dashNest(onCompleted: onComplete);
case GameBonus.sparkyTurboCharge: case GameBonus.sparkyTurboCharge:
return BonusAnimation.sparkyTurboCharge(onCompleted: onComplete); return BonusAnimation.sparkyTurboCharge(onCompleted: onComplete);
case GameBonus.dino: case GameBonus.dinoChomp:
return BonusAnimation.dino(onCompleted: onComplete); return BonusAnimation.dinoChomp(onCompleted: onComplete);
case GameBonus.googleWord: case GameBonus.googleWord:
return BonusAnimation.google(onCompleted: onComplete); return BonusAnimation.googleWord(onCompleted: onComplete);
case GameBonus.android: case GameBonus.androidSpaceship:
return BonusAnimation.android(onCompleted: onComplete); return BonusAnimation.androidSpaceship(onCompleted: onComplete);
} }
} }
} }

@ -21,7 +21,7 @@ class ScoreView extends StatelessWidget {
), ),
child: AnimatedSwitcher( child: AnimatedSwitcher(
duration: kThemeAnimationDuration, duration: kThemeAnimationDuration,
child: isGameOver ? const _GameOver() : const _ScoreWidget(), child: isGameOver ? const _GameOver() : const _ScoreDisplay(),
), ),
); );
} }
@ -43,8 +43,8 @@ class _GameOver extends StatelessWidget {
} }
} }
class _ScoreWidget extends StatelessWidget { class _ScoreDisplay extends StatelessWidget {
const _ScoreWidget({Key? key}) : super(key: key); const _ScoreDisplay({Key? key}) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -52,6 +52,7 @@ class _ScoreWidget extends StatelessWidget {
return Column( return Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [ children: [
Text( Text(
l10n.score.toLowerCase(), l10n.score.toLowerCase(),
@ -60,7 +61,7 @@ class _ScoreWidget extends StatelessWidget {
), ),
), ),
const _ScoreText(), const _ScoreText(),
const ScoreBalls(), const BallCountDisplay(),
], ],
); );
} }

@ -1,5 +1,5 @@
export 'ball_count_display.dart';
export 'bonus_animation.dart'; export 'bonus_animation.dart';
export 'game_hud.dart'; export 'game_hud.dart';
export 'play_button_overlay.dart'; export 'play_button_overlay.dart';
export 'score_ball.dart';
export 'score_view.dart'; export 'score_view.dart';

@ -27,12 +27,12 @@ void main() {
testWidgets('three active balls', (tester) async { testWidgets('three active balls', (tester) async {
await tester.pumpApp( await tester.pumpApp(
const ScoreBalls(), const BallCountDisplay(),
gameBloc: gameBloc, gameBloc: gameBloc,
); );
await tester.pump(); await tester.pump();
expect(find.byType(ScoreBall), findsNWidgets(3)); expect(find.byType(BallIndicator), findsNWidgets(3));
}); });
testWidgets('two active balls', (tester) async { testWidgets('two active balls', (tester) async {
@ -46,31 +46,62 @@ void main() {
); );
await tester.pumpApp( await tester.pumpApp(
const ScoreBalls(), const BallCountDisplay(),
gameBloc: gameBloc, gameBloc: gameBloc,
); );
await tester.pump(); await tester.pump();
expect( expect(
find.byWidgetPredicate( find.byWidgetPredicate(
(widget) => widget is ScoreBall && widget.isActive, (widget) => widget is BallIndicator && widget.isActive,
), ),
findsNWidgets(2), findsNWidgets(2),
); );
expect( expect(
find.byWidgetPredicate( find.byWidgetPredicate(
(widget) => widget is ScoreBall && !widget.isActive, (widget) => widget is BallIndicator && !widget.isActive,
), ),
findsOneWidget, findsOneWidget,
); );
}); });
testWidgets('one active balls', (tester) async {
final state = initialState.copyWith(
balls: 1,
);
whenListen(
gameBloc,
Stream.value(state),
initialState: state,
);
await tester.pumpApp(
const BallCountDisplay(),
gameBloc: gameBloc,
);
await tester.pump();
expect(
find.byWidgetPredicate(
(widget) => widget is BallIndicator && widget.isActive,
),
findsOneWidget,
);
expect(
find.byWidgetPredicate(
(widget) => widget is BallIndicator && !widget.isActive,
),
findsNWidgets(2),
);
});
}); });
testWidgets('active score ball is displaying with proper color', testWidgets('active score ball is displaying with proper color',
(tester) async { (tester) async {
await tester.pumpApp( await tester.pumpApp(
const ScoreBall(isActive: true), const BallIndicator(isActive: true),
); );
await tester.pump(); await tester.pump();
@ -85,7 +116,7 @@ void main() {
testWidgets('inactive score ball is displaying with proper color', testWidgets('inactive score ball is displaying with proper color',
(tester) async { (tester) async {
await tester.pumpApp( await tester.pumpApp(
const ScoreBall(isActive: false), const BallIndicator(isActive: false),
); );
await tester.pump(); await tester.pump();

@ -34,7 +34,7 @@ void main() {
testWidgets('dino', (tester) async { testWidgets('dino', (tester) async {
await tester.pumpApp( await tester.pumpApp(
BonusAnimation.dino(), BonusAnimation.dinoChomp(),
); );
await tester.pump(); await tester.pump();
@ -52,7 +52,7 @@ void main() {
testWidgets('google', (tester) async { testWidgets('google', (tester) async {
await tester.pumpApp( await tester.pumpApp(
BonusAnimation.google(), BonusAnimation.googleWord(),
); );
await tester.pump(); await tester.pump();
@ -61,7 +61,7 @@ void main() {
testWidgets('android', (tester) async { testWidgets('android', (tester) async {
await tester.pumpApp( await tester.pumpApp(
BonusAnimation.android(), BonusAnimation.androidSpaceship(),
); );
await tester.pump(); await tester.pump();

@ -9,27 +9,26 @@ import 'package:flutter_localizations/flutter_localizations.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/l10n/l10n.dart'; import 'package:pinball/l10n/l10n.dart';
import 'package:pinball_components/pinball_components.dart';
import '../../../helpers/helpers.dart'; import '../../../helpers/helpers.dart';
void main() { void main() {
group('GameHud', () { group('GameHud', () {
late GameBloc gameBloc; late GameBloc gameBloc;
late StreamController<GameState> stateController;
const initialState = GameState( const initialState = GameState(
score: 10, score: 1000,
balls: 2, balls: 2,
bonusHistory: [], bonusHistory: [],
); );
setUp(() async { setUp(() async {
gameBloc = MockGameBloc(); gameBloc = MockGameBloc();
stateController = StreamController<GameState>()..add(initialState);
await BonusAnimation.loadAssets(); await BonusAnimation.loadAssets();
whenListen( whenListen(
gameBloc, gameBloc,
stateController.stream, Stream.value(initialState),
initialState: initialState, initialState: initialState,
); );
}); });
@ -63,7 +62,7 @@ void main() {
gameBloc: gameBloc, gameBloc: gameBloc,
); );
expect(find.text(initialState.score.toString()), findsOneWidget); expect(find.text(initialState.score.formatScore()), findsOneWidget);
}, },
); );
@ -72,15 +71,21 @@ void main() {
(tester) async { (tester) async {
final state = initialState.copyWith( final state = initialState.copyWith(
bonusHistory: [GameBonus.dashNest], bonusHistory: [GameBonus.dashNest],
balls: 0,
); );
stateController.add(state);
whenListen(
gameBloc,
Stream.value(state),
initialState: initialState,
);
await tester.pumpApp( await tester.pumpApp(
GameHud(), GameHud(),
gameBloc: gameBloc, gameBloc: gameBloc,
); );
expect(find.byType(ScoreView), findsOneWidget); expect(find.byType(ScoreView), findsOneWidget);
expect(find.byType(BonusAnimation), findsNothing);
}, },
); );
}); });
@ -106,7 +111,7 @@ void main() {
} }
testWidgets( testWidgets(
'is going back to ScoreView after the animation', 'goes back to ScoreView after the animation',
(tester) async { (tester) async {
await tester.runAsync(() async { await tester.runAsync(() async {
final state = initialState.copyWith( final state = initialState.copyWith(

Loading…
Cancel
Save