Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
fix: avoid pausing when not playing (#132)
Browse files Browse the repository at this point in the history
* fix: avoid pausing when not playing

* feat: display no PauseButton when not playing
  • Loading branch information
alestiago authored Feb 17, 2024
1 parent 479bb2c commit 58dfd34
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/trashy_road/lib/src/game/bloc/game_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class GameBloc extends Bloc<GameEvent, GameState> {

emit(
state.copyWith(
status: GameStatus.playing,
status: state.startedAt == null ? GameStatus.ready : GameStatus.playing,
pausedDuration: pausedDuration,
pausedAt: () => null,
),
Expand Down
25 changes: 18 additions & 7 deletions packages/trashy_road/lib/src/game/view/game_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,25 @@ class _GameView extends StatelessWidget {
alignment: Alignment.topRight,
child: Padding(
padding: const EdgeInsets.all(8),
child: PauseButton(
onPause: () {
gameBloc.add(const GamePausedEvent());
return true;
child: BlocBuilder<GameBloc, GameState>(
buildWhen: (previous, current) {
return current.status == GameStatus.playing;
},
onResume: () {
gameBloc.add(const GameResumedEvent());
return true;
builder: (context, state) {
if (state.status != GameStatus.playing) {
return const SizedBox.shrink();
}

return PauseButton(
onPause: () {
gameBloc.add(const GamePausedEvent());
return true;
},
onResume: () {
gameBloc.add(const GameResumedEvent());
return true;
},
);
},
),
),
Expand Down
32 changes: 32 additions & 0 deletions packages/trashy_road/test/src/game/bloc/game_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,38 @@ void main() {
),
],
);

blocTest<GameBloc, GameState>(
'resumes the game when the user was previously paused and not started',
build: () {
return withClock<GameBloc>(
_IncremetalClock(
initialTime: DateTime(0),
increment: const Duration(seconds: 1),
),
() => GameBloc(identifier: identifier, map: map),
);
},
act: (bloc) => bloc
..add(const GamePausedEvent())
..add(const GameResumedEvent()),
expect: () => [
GameState(
identifier: identifier,
map: map,
status: GameStatus.paused,
inventory: Inventory.empty(),
pausedAt: DateTime(0),
),
GameState(
identifier: identifier,
map: map,
status: GameStatus.ready,
inventory: Inventory.empty(),
pausedDuration: const Duration(seconds: 1),
),
],
);
});

group('$GameResetEvent', () {
Expand Down

0 comments on commit 58dfd34

Please sign in to comment.