fix: apply code review

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

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save