forked from lichess-org/mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroadcast_boards_tab.dart
More file actions
422 lines (392 loc) · 14.6 KB
/
broadcast_boards_tab.dart
File metadata and controls
422 lines (392 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import 'package:dartchess/dartchess.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lichess_mobile/src/model/broadcast/broadcast.dart';
import 'package:lichess_mobile/src/model/broadcast/broadcast_preferences.dart';
import 'package:lichess_mobile/src/model/broadcast/broadcast_round_controller.dart';
import 'package:lichess_mobile/src/model/common/eval.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/styles/styles.dart';
import 'package:lichess_mobile/src/utils/duration.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/utils/screen.dart';
import 'package:lichess_mobile/src/view/broadcast/broadcast_game_screen.dart';
import 'package:lichess_mobile/src/view/broadcast/broadcast_player_widget.dart';
import 'package:lichess_mobile/src/widgets/board_thumbnail.dart';
import 'package:lichess_mobile/src/widgets/clock.dart';
import 'package:lichess_mobile/src/widgets/platform_search_bar.dart';
import 'package:visibility_detector/visibility_detector.dart';
// height of 1.0 is important because we need to determine the height of the text
// to calculate the height of the header and footer of the board
const _kPlayerWidgetTextStyle = TextStyle(fontSize: 13, height: 1.0);
const _kPlayerWidgetPadding = EdgeInsets.symmetric(vertical: 5.0);
/// A tab that displays the live games of a broadcast round.
class BroadcastBoardsTab extends ConsumerWidget {
const BroadcastBoardsTab({
required this.tournamentId,
required this.roundId,
required this.tournamentSlug,
required this.showOnlyOngoingGames,
});
final BroadcastTournamentId tournamentId;
final BroadcastRoundId roundId;
final String tournamentSlug;
final bool showOnlyOngoingGames;
@override
Widget build(BuildContext context, WidgetRef ref) {
final round = ref.watch(broadcastRoundControllerProvider(roundId));
return switch (round) {
AsyncData(:final value) =>
value.games.isEmpty
? Padding(
padding: Styles.bodyPadding,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(Icons.info, size: 30),
const SizedBox(height: 8.0),
Text(context.l10n.broadcastNoBoardsYet, textAlign: TextAlign.center),
],
),
)
: BroadcastPreview(
games: showOnlyOngoingGames
? value.games.values.where((game) => game.isOngoing).toIList()
: value.games.values.toIList(),
tournamentId: tournamentId,
roundId: roundId,
title: value.round.name,
tournamentSlug: tournamentSlug,
roundSlug: value.round.slug,
customScoring: value.round.customScoring,
),
AsyncError(:final error) => Center(
child: Center(child: Text('Could not load broadcast: $error')),
),
_ => const Center(child: CircularProgressIndicator.adaptive()),
};
}
}
class BroadcastPreview extends ConsumerStatefulWidget {
const BroadcastPreview({
required this.tournamentId,
required this.roundId,
required this.games,
required this.title,
required this.tournamentSlug,
required this.roundSlug,
required this.customScoring,
});
// A circular progress indicator is used instead of shimmers currently
const BroadcastPreview.loading()
: tournamentId = const BroadcastTournamentId(''),
roundId = const BroadcastRoundId(''),
games = null,
title = '',
tournamentSlug = '',
roundSlug = '',
customScoring = null;
final BroadcastTournamentId tournamentId;
final BroadcastRoundId roundId;
final IList<BroadcastGame>? games;
final String title;
final String tournamentSlug;
final String roundSlug;
final BroadcastCustomScoring? customScoring;
@override
ConsumerState<BroadcastPreview> createState() => _BroadcastPreviewState();
}
class _BroadcastPreviewState extends ConsumerState<BroadcastPreview> {
String _searchQuery = '';
late final TextEditingController _searchController;
@override
void initState() {
super.initState();
_searchController = TextEditingController();
}
@override
void dispose() {
_searchController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final showEvaluationGauges = ref.watch(
broadcastPreferencesProvider.select((value) => value.showRoundEvaluationGauges),
);
const numberLoadingBoards = 12;
const boardSpacing = 10.0;
// height of the text based on the font size
// since the TextStyle is defined with an height at 1.0, this is the real height
// see: https://api.flutter.dev/flutter/painting/TextStyle/height.html
final textHeight = _kPlayerWidgetTextStyle.fontSize!;
final headerAndFooterHeight = textHeight + _kPlayerWidgetPadding.vertical;
final numberOfBoardsByRow = isTabletOrLarger(context) ? 3 : 2;
final screenWidth = MediaQuery.sizeOf(context).width;
final boardWithMaybeEvalBarWidth =
(screenWidth -
Styles.horizontalBodyPadding.horizontal -
(numberOfBoardsByRow - 1) * boardSpacing) /
numberOfBoardsByRow;
final IList<BroadcastGame>? games = widget.games == null
? null
: _searchQuery.isEmpty
? widget.games
: widget.games!.where((game) => _containsPlayer(game, _searchQuery)).toIList();
final showSearchBar = widget.games != null && widget.games!.length > 6;
final mediaQueryPadding = MediaQuery.paddingOf(context);
return CustomScrollView(
slivers: [
if (showSearchBar)
SliverSafeArea(
bottom: false,
sliver: SliverPadding(
padding: Styles.bodyPadding.copyWith(bottom: 0.0),
sliver: SliverToBoxAdapter(
child: PlatformSearchBar(
controller: _searchController,
onChanged: (value) {
setState(() {
_searchQuery = value;
});
},
onClear: () {
_searchController.clear();
setState(() {
_searchQuery = '';
});
},
),
),
),
),
SliverPadding(
padding: Styles.bodyPadding.add(
EdgeInsetsGeometry.only(
// top media query padding is already included in the SliverSafeArea above
top: showSearchBar ? 0.0 : mediaQueryPadding.top,
bottom: mediaQueryPadding.bottom,
),
),
sliver: SliverGrid(
delegate: SliverChildBuilderDelegate((context, index) {
final boardSize =
boardWithMaybeEvalBarWidth -
(showEvaluationGauges
? boardThumbnailEvalGaugeAspectRatio * boardWithMaybeEvalBarWidth
: 0);
if (games == null) {
return BoardThumbnail.loading(
size: boardSize,
header: _PlayerWidgetLoading(width: boardWithMaybeEvalBarWidth),
footer: _PlayerWidgetLoading(width: boardWithMaybeEvalBarWidth),
);
}
final game = games[index];
final playingSide = Setup.parseFen(game.fen).turn;
return ObservedBoardThumbnail(
roundId: widget.roundId,
game: game,
title: widget.title,
tournamentId: widget.tournamentId,
tournamentSlug: widget.tournamentSlug,
roundSlug: widget.roundSlug,
showEvaluationGauge: showEvaluationGauges,
boardSize: boardSize,
boardWithMaybeEvalBarWidth: boardWithMaybeEvalBarWidth,
playingSide: playingSide,
customScoring: widget.customScoring,
);
}, childCount: games == null ? numberLoadingBoards : games.length),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: numberOfBoardsByRow,
crossAxisSpacing: boardSpacing,
mainAxisSpacing: boardSpacing,
mainAxisExtent: boardWithMaybeEvalBarWidth + 2 * headerAndFooterHeight,
childAspectRatio: 1 + boardThumbnailEvalGaugeAspectRatio,
),
),
),
],
);
}
}
class ObservedBoardThumbnail extends ConsumerStatefulWidget {
const ObservedBoardThumbnail({
required this.roundId,
required this.game,
required this.title,
required this.tournamentId,
required this.tournamentSlug,
required this.roundSlug,
required this.showEvaluationGauge,
required this.boardSize,
required this.boardWithMaybeEvalBarWidth,
required this.playingSide,
required this.customScoring,
});
final BroadcastRoundId roundId;
final BroadcastGame game;
final String title;
final BroadcastTournamentId tournamentId;
final String tournamentSlug;
final String roundSlug;
final bool showEvaluationGauge;
final double boardSize;
final double boardWithMaybeEvalBarWidth;
final Side playingSide;
final BroadcastCustomScoring? customScoring;
@override
ConsumerState<ObservedBoardThumbnail> createState() => _ObservedBoardThumbnailState();
}
class _ObservedBoardThumbnailState extends ConsumerState<ObservedBoardThumbnail> {
bool isBoardVisible = false;
@override
Widget build(BuildContext context) {
return VisibilityDetector(
key: ValueKey(widget.game.id),
onVisibilityChanged: (visibilityInfo) {
if (visibilityInfo.visibleFraction > 0.3) {
if (!isBoardVisible && context.mounted) {
ref
.read(broadcastRoundControllerProvider(widget.roundId).notifier)
.addObservedGame(widget.game.id);
setState(() {
isBoardVisible = true;
});
}
} else {
if (isBoardVisible && context.mounted) {
ref
.read(broadcastRoundControllerProvider(widget.roundId).notifier)
.removeObservedGame(widget.game.id);
setState(() {
isBoardVisible = false;
});
}
}
},
child: BoardThumbnail(
animationDuration: const Duration(milliseconds: 150),
onTap: () {
Navigator.of(context).push(
BroadcastGameScreen.buildRoute(
context,
tournamentId: widget.tournamentId,
roundId: widget.roundId,
gameId: widget.game.id,
tournamentSlug: widget.tournamentSlug,
roundSlug: widget.roundSlug,
title: widget.title,
),
);
},
orientation: Side.white,
fen: widget.game.fen,
showEvaluationGauge: widget.showEvaluationGauge,
whiteWinningChances: (widget.game.cp != null || widget.game.mate != null)
? ExternalEval(cp: widget.game.cp, mate: widget.game.mate).winningChances(Side.white)
: null,
lastMove: widget.game.lastMove,
size: widget.boardSize,
header: _PlayerWidget(
width: widget.boardWithMaybeEvalBarWidth,
game: widget.game,
side: Side.black,
playingSide: widget.playingSide,
customScoring: widget.customScoring,
),
footer: _PlayerWidget(
width: widget.boardWithMaybeEvalBarWidth,
game: widget.game,
side: Side.white,
playingSide: widget.playingSide,
customScoring: widget.customScoring,
),
),
);
}
}
class _PlayerWidgetLoading extends StatelessWidget {
const _PlayerWidgetLoading({required this.width});
final double width;
@override
Widget build(BuildContext context) {
return SizedBox(
width: width,
child: Padding(
padding: _kPlayerWidgetPadding,
child: Container(
height: _kPlayerWidgetTextStyle.fontSize,
decoration: BoxDecoration(color: Colors.black, borderRadius: BorderRadius.circular(5)),
),
),
);
}
}
class _PlayerWidget extends StatelessWidget {
const _PlayerWidget({
required this.width,
required this.game,
required this.side,
required this.playingSide,
required this.customScoring,
});
final BroadcastGame game;
final Side side;
final Side playingSide;
final double width;
final BroadcastCustomScoring? customScoring;
@override
Widget build(BuildContext context) {
final playerWithClock = game.players[side]!;
final player = playerWithClock.player;
final clock = playerWithClock.clock;
final isClockActive = game.isOngoing && side == playingSide;
return SizedBox(
width: width,
child: Padding(
padding: _kPlayerWidgetPadding,
child: DefaultTextStyle.merge(
style: _kPlayerWidgetTextStyle,
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(child: BroadcastPlayerWidget(player: player, showRating: false)),
const SizedBox(width: 5),
if (game.isOver)
Text(
resultString(customScoring, side, game.status),
style: Theme.of(context).textTheme.bodySmall?.copyWith(
fontWeight: .bold,
color: game.status.colorFor(side, context),
),
)
else if (clock != null)
CountdownClockBuilder(
timeLeft: clock,
active: isClockActive,
builder: (context, timeLeft) => Text(
timeLeft.toHoursMinutesSeconds(),
style: TextStyle(
color: isClockActive ? Colors.orange[900] : null,
fontFeatures: const [FontFeature.tabularFigures()],
),
),
tickInterval: const Duration(seconds: 1),
clockUpdatedAt: game.updatedClockAt,
),
],
),
),
),
);
}
}
bool _containsPlayer(BroadcastGame game, String query) {
final q = query.toLowerCase();
return game.players.values.any((pwc) => pwc.player.name?.toLowerCase().contains(q) ?? false);
}