Skip to content

Commit b6e5723

Browse files
committed
Improve life counter UI and fix timer/game state management issues
1 parent 1fb8fab commit b6e5723

8 files changed

Lines changed: 152 additions & 122 deletions

File tree

lib/app/view/app.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:magic_yeti/app/utils/device_info_provider.dart';
99
import 'package:magic_yeti/game/bloc/game_bloc.dart';
1010
import 'package:magic_yeti/home/match_history_bloc/match_history_bloc.dart';
1111
import 'package:magic_yeti/l10n/arb/app_localizations.dart';
12+
import 'package:magic_yeti/timer/bloc/timer_bloc.dart';
1213
import 'package:player_repository/player_repository.dart';
1314
import 'package:scryfall_repository/scryfall_repository.dart';
1415
import 'package:user_repository/user_repository.dart';
@@ -69,6 +70,9 @@ class App extends StatelessWidget {
6970
LoadMatchHistory(userId: context.read<AppBloc>().state.user.id),
7071
),
7172
),
73+
BlocProvider(
74+
create: (context) => TimerBloc(),
75+
),
7276
],
7377
child: const AppView(),
7478
),

lib/game/bloc/game_bloc.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class GameBloc extends Bloc<GameEvent, GameState> {
193193
GameFinishEvent event,
194194
Emitter<GameState> emit,
195195
) async {
196-
emit(state.copyWith(status: GameStatus.loading));
196+
emit(state.copyWith(status: GameStatus.finished));
197197

198198
final updateWinner = event.winner.copyWith(
199199
placement: const Value(1),
@@ -229,7 +229,6 @@ class GameBloc extends Bloc<GameEvent, GameState> {
229229
final alivePlayers =
230230
event.players.where((p) => p.state == PlayerModelState.active).toList();
231231
if (alivePlayers.length <= 1 && state.status == GameStatus.running) {
232-
emit(state.copyWith(status: GameStatus.finished));
233232
add(GameFinishEvent(winner: alivePlayers.first));
234233
}
235234
}

lib/home/home_page.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import 'package:magic_yeti/match_details/view/match_details_page.dart';
1818
import 'package:magic_yeti/profile/view/profile_page.dart';
1919
import 'package:magic_yeti/sign_up/sign_up.dart';
2020
import 'package:magic_yeti/stats_overview/stats_overview.dart';
21+
import 'package:magic_yeti/timer/bloc/timer_bloc.dart';
2122
import 'package:wakelock_plus/wakelock_plus.dart';
2223

2324
class HomePage extends StatefulWidget {
@@ -393,6 +394,8 @@ class GameModeButtons extends StatelessWidget {
393394
startingLifePoints: lifePoints,
394395
),
395396
);
397+
398+
context.read<TimerBloc>().add(const TimerStartEvent());
396399
context.go(GamePage.routePath);
397400
}
398401
}

lib/life_counter/view/game_over_page.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:magic_yeti/home/home_page.dart';
1111
import 'package:magic_yeti/l10n/l10n.dart';
1212
import 'package:magic_yeti/life_counter/bloc/game_over_bloc.dart';
1313
import 'package:magic_yeti/life_counter/view/game_page.dart';
14+
import 'package:magic_yeti/timer/bloc/timer_bloc.dart';
1415
import 'package:player_repository/models/player.dart';
1516
import 'package:player_repository/player_repository.dart';
1617

@@ -51,6 +52,14 @@ class GameOverView extends StatelessWidget {
5152
return Scaffold(
5253
appBar: AppBar(
5354
title: Text(l10n.gameOverTitle),
55+
leading: IconButton(
56+
icon: const Icon(Icons.arrow_back),
57+
onPressed: () {
58+
context.go(GamePage.routePath);
59+
context.read<TimerBloc>().add(const TimerStartEvent());
60+
context.read<GameBloc>().add(const GameResumeEvent());
61+
},
62+
),
5463
),
5564
body: BlocBuilder<GameOverBloc, GameOverState>(
5665
builder: (context, state) {
@@ -160,6 +169,7 @@ class ButtonsWidget extends StatelessWidget {
160169
);
161170

162171
context.read<GameBloc>().add(const GameResetEvent());
172+
context.read<TimerBloc>().add(const TimerStartEvent());
163173
context.go(GamePage.routePath);
164174
},
165175
child: Text(

lib/life_counter/view/game_page.dart

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,19 @@ class GamePage extends StatelessWidget {
2626
return const Center(child: CircularProgressIndicator());
2727
}
2828

29-
return BlocProvider(
30-
create: (context) => TimerBloc()..add(const TimerStartEvent()),
31-
child: BlocListener<GameBloc, GameState>(
32-
listener: (context, state) {
33-
if (state.status == GameStatus.finished) {
34-
context.read<TimerBloc>().add(const TimerPauseEvent());
35-
final gameLength = context.read<TimerBloc>().state.elapsedSeconds;
36-
context
37-
.read<GameBloc>()
38-
.add(GameUpdateTimerEvent(gameLength: gameLength));
39-
context.read<TimerBloc>().add(const TimerResetEvent());
40-
context.read<TimerBloc>().add(const TimerPauseEvent());
41-
context.go(GameOverPage.routePath);
42-
}
43-
},
44-
child:
45-
playerCount == 2 ? const TwoPlayerGame() : const FourPlayerGame(),
46-
),
29+
return BlocListener<GameBloc, GameState>(
30+
listener: (context, state) {
31+
if (state.status == GameStatus.finished) {
32+
context.read<TimerBloc>().add(const TimerPauseEvent());
33+
final gameLength = context.read<TimerBloc>().state.elapsedSeconds;
34+
context
35+
.read<GameBloc>()
36+
.add(GameUpdateTimerEvent(gameLength: gameLength));
37+
38+
context.go(GameOverPage.routePath);
39+
}
40+
},
41+
child: playerCount == 2 ? const TwoPlayerGame() : const FourPlayerGame(),
4742
);
4843
}
4944
}

0 commit comments

Comments
 (0)