forked from lichess-org/mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard_preferences.dart
More file actions
489 lines (431 loc) · 16.1 KB
/
board_preferences.dart
File metadata and controls
489 lines (431 loc) · 16.1 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import 'package:chessground/chessground.dart';
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:freezed_annotation/freezed_annotation.dart';
import 'package:lichess_mobile/l10n/l10n.dart';
import 'package:lichess_mobile/src/model/common/chess.dart';
import 'package:lichess_mobile/src/model/settings/preferences_storage.dart';
import 'package:lichess_mobile/src/styles/styles.dart';
import 'package:lichess_mobile/src/utils/color_palette.dart';
import 'package:lichess_mobile/src/utils/system.dart';
part 'board_preferences.freezed.dart';
part 'board_preferences.g.dart';
const kBoardDefaultBrightnessFilter = 1.0;
const kBoardDefaultHueFilter = 0.0;
final boardPreferencesProvider = NotifierProvider<BoardPreferences, BoardPrefs>(
BoardPreferences.new,
name: 'BoardPreferencesProvider',
);
/// A provider that returns the effective piece animation duration, considering Android system settings.
final effectivePieceAnimationDurationProvider = Provider<Duration>((ref) {
final boardPrefs = ref.watch(boardPreferencesProvider);
final androidAnimationsAsync = ref.watch(androidAnimationsProvider);
final androidAnimationsEnabled = androidAnimationsAsync.maybeWhen(
data: (enabled) => enabled,
orElse: () => true, // Default to enabled if we can't determine
);
// If Android animations are disabled, always return Duration.zero regardless of app setting
if (!androidAnimationsEnabled) {
return Duration.zero;
}
return boardPrefs.pieceAnimationDuration;
});
class BoardPreferences extends Notifier<BoardPrefs> with PreferencesStorage<BoardPrefs> {
@override
@protected
PrefCategory get prefCategory => PrefCategory.board;
@override
@protected
BoardPrefs get defaults => BoardPrefs.defaults;
@override
BoardPrefs fromJson(Map<String, dynamic> json) => BoardPrefs.fromJson(json);
@override
BoardPrefs build() {
return fetch();
}
Future<void> setPieceSet(PieceSet pieceSet) {
return save(state.copyWith(pieceSet: pieceSet));
}
Future<void> setBoardTheme(BoardTheme boardTheme) async {
await save(state.copyWith(boardTheme: boardTheme));
}
Future<void> togglePremoves() async {
await save(state.copyWith(premoves: !state.premoves));
}
Future<void> toggleConfirmResignAndDraw() async {
await save(state.copyWith(confirmResignAndDraw: !state.confirmResignAndDraw));
}
Future<void> setPieceShiftMethod(PieceShiftMethod pieceShiftMethod) async {
await save(state.copyWith(pieceShiftMethod: pieceShiftMethod));
}
Future<void> setCastlingMethod(CastlingMethod castlingMethod) {
return save(state.copyWith(castlingMethod: castlingMethod));
}
Future<void> toggleHapticFeedback() {
return save(state.copyWith(hapticFeedback: !state.hapticFeedback));
}
Future<void> toggleImmersiveModeWhilePlaying() {
return save(
state.copyWith(immersiveModeWhilePlaying: !(state.immersiveModeWhilePlaying ?? false)),
);
}
Future<void> toggleShowLegalMoves() {
return save(state.copyWith(showLegalMoves: !state.showLegalMoves));
}
Future<void> toggleBoardHighlights() {
return save(state.copyWith(boardHighlights: !state.boardHighlights));
}
Future<void> toggleCoordinates() {
return save(state.copyWith(coordinates: !state.coordinates));
}
Future<void> toggleBorder() {
return save(state.copyWith(showBorder: !state.showBorder));
}
Future<void> togglePieceAnimation() {
return save(state.copyWith(pieceAnimation: !state.pieceAnimation));
}
Future<void> toggleMagnifyDraggedPiece() {
return save(state.copyWith(magnifyDraggedPiece: !state.magnifyDraggedPiece));
}
Future<void> setDragTargetKind(DragTargetKind dragTargetKind) {
return save(state.copyWith(dragTargetKind: dragTargetKind));
}
Future<void> setMaterialDifferenceFormat(MaterialDifferenceFormat materialDifferenceFormat) {
return save(state.copyWith(materialDifferenceFormat: materialDifferenceFormat));
}
Future<void> setClockPosition(ClockPosition clockPosition) {
return save(state.copyWith(clockPosition: clockPosition));
}
Future<void> toggleMoveListDisplay() {
return save(state.copyWith(moveListDisplay: !state.moveListDisplay));
}
Future<void> toggleEnableShapeDrawings() {
return save(state.copyWith(enableShapeDrawings: !state.enableShapeDrawings));
}
Future<void> setShapeColor(ShapeColor shapeColor) {
return save(state.copyWith(shapeColor: shapeColor));
}
Future<void> adjustColors({double? brightness, double? hue}) {
return save(state.copyWith(brightness: brightness ?? state.brightness, hue: hue ?? state.hue));
}
}
@Freezed(fromJson: true, toJson: true)
sealed class BoardPrefs with _$BoardPrefs implements Serializable {
const BoardPrefs._();
@Assert('brightness >= 0.2 && brightness <= 1.4, hue >= 0.0 && hue <= 360.0')
const factory BoardPrefs({
@JsonKey(defaultValue: PieceSet.staunty, unknownEnumValue: PieceSet.staunty)
required PieceSet pieceSet,
@JsonKey(defaultValue: BoardTheme.brown, unknownEnumValue: BoardTheme.brown)
required BoardTheme boardTheme,
bool? immersiveModeWhilePlaying,
required bool hapticFeedback,
required bool showLegalMoves,
required bool boardHighlights,
required bool coordinates,
required bool pieceAnimation,
@JsonKey(
defaultValue: MaterialDifferenceFormat.materialDifference,
unknownEnumValue: MaterialDifferenceFormat.materialDifference,
)
required MaterialDifferenceFormat materialDifferenceFormat,
@JsonKey(defaultValue: ClockPosition.right, unknownEnumValue: ClockPosition.right)
required ClockPosition clockPosition,
@JsonKey(defaultValue: PieceShiftMethod.either, unknownEnumValue: PieceShiftMethod.either)
required PieceShiftMethod pieceShiftMethod,
@JsonKey(
defaultValue: CastlingMethod.kingOverRook,
unknownEnumValue: CastlingMethod.kingOverRook,
)
required CastlingMethod castlingMethod,
@JsonKey(defaultValue: true) required bool moveListDisplay,
@JsonKey(defaultValue: true) required bool premoves,
@JsonKey(defaultValue: true) required bool confirmResignAndDraw,
/// Whether to enable shape drawings on the board for games and puzzles.
@JsonKey(defaultValue: true) required bool enableShapeDrawings,
@JsonKey(defaultValue: true) required bool magnifyDraggedPiece,
@JsonKey(defaultValue: DragTargetKind.circle, unknownEnumValue: DragTargetKind.circle)
required DragTargetKind dragTargetKind,
@JsonKey(defaultValue: ShapeColor.green, unknownEnumValue: ShapeColor.green)
required ShapeColor shapeColor,
@JsonKey(defaultValue: false) required bool showBorder,
@JsonKey(defaultValue: kBoardDefaultBrightnessFilter) required double brightness,
@JsonKey(defaultValue: kBoardDefaultHueFilter) required double hue,
}) = _BoardPrefs;
static const defaults = BoardPrefs(
pieceSet: PieceSet.staunty,
boardTheme: BoardTheme.brown,
immersiveModeWhilePlaying: false,
hapticFeedback: true,
showLegalMoves: true,
boardHighlights: true,
coordinates: true,
pieceAnimation: true,
materialDifferenceFormat: MaterialDifferenceFormat.materialDifference,
clockPosition: ClockPosition.right,
moveListDisplay: true,
premoves: true,
confirmResignAndDraw: true,
pieceShiftMethod: PieceShiftMethod.either,
castlingMethod: CastlingMethod.kingOverRook,
enableShapeDrawings: true,
magnifyDraggedPiece: true,
dragTargetKind: DragTargetKind.circle,
shapeColor: ShapeColor.green,
showBorder: false,
brightness: kBoardDefaultBrightnessFilter,
hue: kBoardDefaultHueFilter,
);
bool get hasColorAdjustments =>
brightness != kBoardDefaultBrightnessFilter || hue != kBoardDefaultHueFilter;
ChessboardSettings toBoardSettings() {
return ChessboardSettings(
pieceAssets: pieceSet.assets,
colorScheme: boardTheme.colors,
brightness: brightness,
hue: hue,
border: showBorder
? BoardBorder(color: darken(boardTheme.colors.darkSquare, 0.2), width: 16.0)
: null,
showValidMoves: showLegalMoves,
showLastMove: boardHighlights,
enableCoordinates: coordinates,
animationDuration: pieceAnimationDuration,
dragFeedbackScale: magnifyDraggedPiece ? 2.0 : 1.0,
dragFeedbackOffset: Offset(0.0, magnifyDraggedPiece ? -1.0 : 0.0),
dragTargetKind: dragTargetKind,
pieceShiftMethod: pieceShiftMethod,
drawShape: DrawShapeOptions(enable: enableShapeDrawings, newShapeColor: shapeColor.color),
);
}
GameData toGameData({
required Variant variant,
required Position position,
required PlayerSide playerSide,
required NormalMove? promotionMove,
required void Function(NormalMove, {bool? isDrop}) onMove,
required void Function(Role? role) onPromotionSelection,
Premovable? premovable,
}) {
return GameData(
playerSide: playerSide,
onMove: onMove,
onPromotionSelection: onPromotionSelection,
premovable: premoves ? premovable : null,
promotionMove: promotionMove,
sideToMove: position.turn,
validMoves: _makeLegalMoves(position, variant: variant, castlingMethod: castlingMethod),
isCheck: boardHighlights && position.isCheck,
);
}
factory BoardPrefs.fromJson(Map<String, dynamic> json) {
return _$BoardPrefsFromJson(json);
}
Duration get pieceAnimationDuration =>
pieceAnimation ? const Duration(milliseconds: 150) : Duration.zero;
}
IMap<Square, ISet<Square>> _makeLegalMoves(
Position pos, {
required CastlingMethod castlingMethod,
required Variant variant,
}) {
final Map<Square, ISet<Square>> result = {};
for (final entry in pos.legalMoves.entries) {
final dests = entry.value.squares;
if (dests.isNotEmpty) {
final from = entry.key;
final destSet = dests.toSet();
if (variant != Variant.chess960 &&
from == pos.board.kingOf(pos.turn) &&
entry.key.file == 4) {
if (dests.contains(Square.a1)) {
destSet.add(Square.c1);
} else if (dests.contains(Square.a8)) {
destSet.add(Square.c8);
}
if (dests.contains(Square.h1)) {
destSet.add(Square.g1);
} else if (dests.contains(Square.h8)) {
destSet.add(Square.g8);
}
if (castlingMethod == CastlingMethod.kingTwoSquares) {
destSet.removeAll([Square.a1, Square.h1, Square.a8, Square.h8]);
}
}
result[from] = ISet(destSet);
}
}
return IMap(result);
}
/// Colors taken from lila: https://github.com/lichess-org/chessground/blob/54a7e71bf88701c1109d3b9b8106b464012b94cf/src/state.ts#L178
enum ShapeColor {
green,
red,
blue,
yellow;
Color get color => Color(switch (this) {
ShapeColor.green => 0x15781B,
ShapeColor.red => 0x882020,
ShapeColor.blue => 0x003088,
ShapeColor.yellow => 0xe68f00,
}).withAlpha(0xAA);
}
/// The chessboard theme.
enum BoardTheme {
system('System', 'system'),
brown('Brown', 'brown'),
wood('Wood', 'wood'),
wood2('Wood 2', 'wood2'),
wood3('Wood 3', 'wood3'),
wood4('Wood 4', 'wood4'),
maple('Maple', 'maple'),
maple2('Maple 2', 'maple2'),
blue('Blue', 'blue'),
blue2('Blue 2', 'blue2'),
blue3('Blue 3', 'blue3'),
blueMarble('Blue Marble', 'blue-marble'),
canvas('Canvas', 'canvas'),
leather('Leather', 'leather'),
ic('IC', 'ic'),
green('Green', 'green'),
marble('Marble', 'marble'),
greenPlastic('Green Plastic', 'green-plastic'),
grey('Grey', 'grey'),
metal('Metal', 'metal'),
olive('Olive', 'olive'),
newspaper('Newspaper', 'newspaper'),
purple('Purple', 'purple'),
purpleDiag('Purple-Diag', 'purple-diag'),
pinkPyramid('Pink', 'pink'),
horsey('Horsey', 'horsey');
final String label;
final String gifApiName;
const BoardTheme(this.label, this.gifApiName);
ChessboardColorScheme get colors {
switch (this) {
case BoardTheme.system:
return getBoardColorScheme() ?? ChessboardColorScheme.brown;
case BoardTheme.blue:
return ChessboardColorScheme.blue;
case BoardTheme.blue2:
return ChessboardColorScheme.blue2;
case BoardTheme.blue3:
return ChessboardColorScheme.blue3;
case BoardTheme.blueMarble:
return ChessboardColorScheme.blueMarble;
case BoardTheme.canvas:
return ChessboardColorScheme.canvas;
case BoardTheme.wood:
return ChessboardColorScheme.wood;
case BoardTheme.wood2:
return ChessboardColorScheme.wood2;
case BoardTheme.wood3:
return ChessboardColorScheme.wood3;
case BoardTheme.wood4:
return ChessboardColorScheme.wood4;
case BoardTheme.maple:
return ChessboardColorScheme.maple;
case BoardTheme.maple2:
return ChessboardColorScheme.maple2;
case BoardTheme.brown:
return ChessboardColorScheme.brown;
case BoardTheme.leather:
return ChessboardColorScheme.leather;
case BoardTheme.ic:
return ChessboardColorScheme.ic;
case BoardTheme.green:
return ChessboardColorScheme.green;
case BoardTheme.marble:
return ChessboardColorScheme.marble;
case BoardTheme.greenPlastic:
return ChessboardColorScheme.greenPlastic;
case BoardTheme.grey:
return ChessboardColorScheme.grey;
case BoardTheme.metal:
return ChessboardColorScheme.metal;
case BoardTheme.olive:
return ChessboardColorScheme.olive;
case BoardTheme.newspaper:
return ChessboardColorScheme.newspaper;
case BoardTheme.purple:
return ChessboardColorScheme.purple;
case BoardTheme.purpleDiag:
return ChessboardColorScheme.purpleDiag;
case BoardTheme.pinkPyramid:
return ChessboardColorScheme.pinkPyramid;
case BoardTheme.horsey:
return ChessboardColorScheme.horsey;
}
}
Widget get thumbnail => switch (this) {
BoardTheme.system => SizedBox(
height: 44,
width: 44 * 6,
child: Row(
children: [
for (final c in const [1, 2, 3, 4, 5, 6])
Container(
width: 44,
color: c.isEven
? BoardTheme.system.colors.darkSquare
: BoardTheme.system.colors.lightSquare,
),
],
),
),
BoardTheme.ic => SizedBox(
height: 44,
width: 44 * 6,
child: Row(
children: [
for (final c in const [1, 2, 3, 4, 5, 6])
Container(
width: 44,
color: c.isEven ? BoardTheme.ic.colors.darkSquare : BoardTheme.ic.colors.lightSquare,
),
],
),
),
_ => Image.asset(
'assets/board-thumbnails/$name.jpg',
height: 44,
errorBuilder: (context, o, st) => const SizedBox.shrink(),
),
};
}
enum MaterialDifferenceFormat {
materialDifference,
capturedPieces,
hidden;
bool get visible => this != MaterialDifferenceFormat.hidden;
String l10n(AppLocalizations l10n) => switch (this) {
MaterialDifferenceFormat.materialDifference => l10n.preferencesMaterialDifference,
MaterialDifferenceFormat.capturedPieces => l10n.mobileSettingsMaterialDifferenceCapturedPieces,
MaterialDifferenceFormat.hidden => l10n.puzzleHidden, // should be good to reuse this
};
}
enum ClockPosition {
left,
right;
String label(AppLocalizations l10n) => switch (this) {
ClockPosition.left => l10n.mobilePositionLeft,
ClockPosition.right => l10n.mobilePositionRight,
};
}
enum CastlingMethod {
/// Allow castling by moving either the king over the rook or two squares (to match lichess website).
kingOverRook,
/// Allow castling only by moving the king two squares.
kingTwoSquares;
String l10n(AppLocalizations l10n) => switch (this) {
CastlingMethod.kingOverRook => l10n.preferencesCastleByMovingOntoTheRook,
CastlingMethod.kingTwoSquares => l10n.preferencesCastleByMovingTwoSquares,
};
}
String dragTargetKindLabel(AppLocalizations l10n, DragTargetKind kind) => switch (kind) {
DragTargetKind.circle => l10n.mobileSettingsDraggedTargetCircle,
DragTargetKind.square => l10n.mobileSettingsDraggedTargetSquare,
DragTargetKind.none => l10n.none,
};