diff --git a/packages/trashy_road/lib/src/game/bloc/game_bloc.dart b/packages/trashy_road/lib/src/game/bloc/game_bloc.dart index ae07ff42..bf129bad 100644 --- a/packages/trashy_road/lib/src/game/bloc/game_bloc.dart +++ b/packages/trashy_road/lib/src/game/bloc/game_bloc.dart @@ -127,7 +127,7 @@ class GameBloc extends Bloc { emit( state.copyWith( - status: GameStatus.playing, + status: state.startedAt == null ? GameStatus.ready : GameStatus.playing, pausedDuration: pausedDuration, pausedAt: () => null, ), diff --git a/packages/trashy_road/lib/src/game/view/game_page.dart b/packages/trashy_road/lib/src/game/view/game_page.dart index 5ec558af..26882860 100644 --- a/packages/trashy_road/lib/src/game/view/game_page.dart +++ b/packages/trashy_road/lib/src/game/view/game_page.dart @@ -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( + 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; + }, + ); }, ), ), diff --git a/packages/trashy_road/test/src/game/bloc/game_bloc_test.dart b/packages/trashy_road/test/src/game/bloc/game_bloc_test.dart index d29e97da..47fa6c82 100644 --- a/packages/trashy_road/test/src/game/bloc/game_bloc_test.dart +++ b/packages/trashy_road/test/src/game/bloc/game_bloc_test.dart @@ -424,6 +424,38 @@ void main() { ), ], ); + + blocTest( + 'resumes the game when the user was previously paused and not started', + build: () { + return withClock( + _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', () {