From 98e92213d11072ed3c4fc95aa9522d8e2b037bf8 Mon Sep 17 00:00:00 2001 From: Jimima Date: Wed, 8 Jan 2025 13:01:59 +0000 Subject: [PATCH] Formatting --- lib/src/model/game/game_controller.dart | 19 +-- lib/src/model/settings/board_preferences.dart | 136 +++++++--------- lib/src/view/game/game_body.dart | 5 +- .../view/settings/board_settings_screen.dart | 151 +++++++----------- 4 files changed, 123 insertions(+), 188 deletions(-) diff --git a/lib/src/model/game/game_controller.dart b/lib/src/model/game/game_controller.dart index 0acecf7883..91ad88195e 100644 --- a/lib/src/model/game/game_controller.dart +++ b/lib/src/model/game/game_controller.dart @@ -954,14 +954,9 @@ class GameState with _$GameState { }) = _GameState; /// The [Position] and its legal moves at the current cursor. - (Position, IMap>) getCurrentPosition( - CastlingMethod castlingMethod, - ) { + (Position, IMap>) getCurrentPosition(CastlingMethod castlingMethod) { final position = game.positionAt(stepCursor); - final legalMoves = makeLegalMoves( - position, - isChess960: game.meta.variant == Variant.chess960, - ); + final legalMoves = makeLegalMoves(position, isChess960: game.meta.variant == Variant.chess960); final Map castlingMap = { Square.a1: Square.c1, @@ -978,12 +973,10 @@ class GameState with _$GameState { moves, //king can castle ) ? (switch (castlingMethod) { - CastlingMethod.kingOverRook => - moves.removeAll(castlingMap.values), - CastlingMethod.kingTwoSquares => - moves.removeAll(castlingMap.keys), - _ => moves - }) + CastlingMethod.kingOverRook => moves.removeAll(castlingMap.values), + CastlingMethod.kingTwoSquares => moves.removeAll(castlingMap.keys), + _ => moves, + }) : moves, ); } diff --git a/lib/src/model/settings/board_preferences.dart b/lib/src/model/settings/board_preferences.dart index 225c2e182f..fd8c1d86ff 100644 --- a/lib/src/model/settings/board_preferences.dart +++ b/lib/src/model/settings/board_preferences.dart @@ -15,8 +15,7 @@ const kBoardDefaultBrightnessFilter = 1.0; const kBoardDefaultHueFilter = 0.0; @riverpod -class BoardPreferences extends _$BoardPreferences - with PreferencesStorage { +class BoardPreferences extends _$BoardPreferences with PreferencesStorage { // ignore: avoid_public_notifier_properties @override PrefCategory get prefCategory => PrefCategory.board; @@ -46,9 +45,7 @@ class BoardPreferences extends _$BoardPreferences } Future setCastlingMethod(CastlingMethod castlingMethod) { - return save( - state.copyWith(castlingMethod: castlingMethod), - ); + return save(state.copyWith(castlingMethod: castlingMethod)); } Future toggleHapticFeedback() { @@ -57,9 +54,7 @@ class BoardPreferences extends _$BoardPreferences Future toggleImmersiveModeWhilePlaying() { return save( - state.copyWith( - immersiveModeWhilePlaying: - !(state.immersiveModeWhilePlaying ?? false)), + state.copyWith(immersiveModeWhilePlaying: !(state.immersiveModeWhilePlaying ?? false)), ); } @@ -84,18 +79,15 @@ class BoardPreferences extends _$BoardPreferences } Future toggleMagnifyDraggedPiece() { - return save( - state.copyWith(magnifyDraggedPiece: !state.magnifyDraggedPiece)); + return save(state.copyWith(magnifyDraggedPiece: !state.magnifyDraggedPiece)); } Future setDragTargetKind(DragTargetKind dragTargetKind) { return save(state.copyWith(dragTargetKind: dragTargetKind)); } - Future setMaterialDifferenceFormat( - MaterialDifferenceFormat materialDifferenceFormat) { - return save( - state.copyWith(materialDifferenceFormat: materialDifferenceFormat)); + Future setMaterialDifferenceFormat(MaterialDifferenceFormat materialDifferenceFormat) { + return save(state.copyWith(materialDifferenceFormat: materialDifferenceFormat)); } Future setClockPosition(ClockPosition clockPosition) { @@ -103,8 +95,7 @@ class BoardPreferences extends _$BoardPreferences } Future toggleEnableShapeDrawings() { - return save( - state.copyWith(enableShapeDrawings: !state.enableShapeDrawings)); + return save(state.copyWith(enableShapeDrawings: !state.enableShapeDrawings)); } Future setShapeColor(ShapeColor shapeColor) { @@ -112,8 +103,7 @@ class BoardPreferences extends _$BoardPreferences } Future adjustColors({double? brightness, double? hue}) { - return save(state.copyWith( - brightness: brightness ?? state.brightness, hue: hue ?? state.hue)); + return save(state.copyWith(brightness: brightness ?? state.brightness, hue: hue ?? state.hue)); } } @@ -137,24 +127,19 @@ class BoardPrefs with _$BoardPrefs implements Serializable { ) required MaterialDifferenceFormat materialDifferenceFormat, required ClockPosition clockPosition, - @JsonKey( - defaultValue: PieceShiftMethod.either, - unknownEnumValue: PieceShiftMethod.either) + @JsonKey(defaultValue: PieceShiftMethod.either, unknownEnumValue: PieceShiftMethod.either) required PieceShiftMethod pieceShiftMethod, required CastlingMethod castlingMethod, /// 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) + @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: kBoardDefaultBrightnessFilter) required double brightness, @JsonKey(defaultValue: kBoardDefaultHueFilter) required double hue, }) = _BoardPrefs; @@ -181,8 +166,7 @@ class BoardPrefs with _$BoardPrefs implements Serializable { ); bool get hasColorAdjustments => - brightness != kBoardDefaultBrightnessFilter || - hue != kBoardDefaultHueFilter; + brightness != kBoardDefaultBrightnessFilter || hue != kBoardDefaultHueFilter; ChessboardSettings toBoardSettings() { return ChessboardSettings( @@ -190,10 +174,10 @@ class BoardPrefs with _$BoardPrefs implements Serializable { colorScheme: boardTheme.colors, brightness: brightness, hue: hue, - border: showBorder - ? BoardBorder( - color: darken(boardTheme.colors.darkSquare, 0.2), width: 16.0) - : null, + border: + showBorder + ? BoardBorder(color: darken(boardTheme.colors.darkSquare, 0.2), width: 16.0) + : null, showValidMoves: showLegalMoves, showLastMove: boardHighlights, enableCoordinates: coordinates, @@ -202,8 +186,7 @@ class BoardPrefs with _$BoardPrefs implements Serializable { dragFeedbackOffset: Offset(0.0, magnifyDraggedPiece ? -1.0 : 0.0), dragTargetKind: dragTargetKind, pieceShiftMethod: pieceShiftMethod, - drawShape: DrawShapeOptions( - enable: enableShapeDrawings, newShapeColor: shapeColor.color), + drawShape: DrawShapeOptions(enable: enableShapeDrawings, newShapeColor: shapeColor.color), ); } @@ -223,12 +206,11 @@ enum ShapeColor { yellow; Color get color => Color(switch (this) { - ShapeColor.green => 0x15781B, - ShapeColor.red => 0x882020, - ShapeColor.blue => 0x003088, - ShapeColor.yellow => 0xe68f00, - }) - .withAlpha(0xAA); + ShapeColor.green => 0x15781B, + ShapeColor.red => 0x882020, + ShapeColor.blue => 0x003088, + ShapeColor.yellow => 0xe68f00, + }).withAlpha(0xAA); } /// The chessboard theme. @@ -316,27 +298,29 @@ enum BoardTheme { } } - Widget get thumbnail => 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, - ), - ], - ), - ) - : Image.asset( - 'assets/board-thumbnails/$name.jpg', - height: 44, - errorBuilder: (context, o, st) => const SizedBox.shrink(), - ); + Widget get thumbnail => + 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, + ), + ], + ), + ) + : Image.asset( + 'assets/board-thumbnails/$name.jpg', + height: 44, + errorBuilder: (context, o, st) => const SizedBox.shrink(), + ); } enum MaterialDifferenceFormat { @@ -351,11 +335,11 @@ enum MaterialDifferenceFormat { bool get visible => this != MaterialDifferenceFormat.hidden; String l10n(AppLocalizations l10n) => switch (this) { - //TODO: Add l10n - MaterialDifferenceFormat.materialDifference => materialDifference.label, - MaterialDifferenceFormat.capturedPieces => capturedPieces.label, - MaterialDifferenceFormat.hidden => hidden.label, - }; + //TODO: Add l10n + MaterialDifferenceFormat.materialDifference => materialDifference.label, + MaterialDifferenceFormat.capturedPieces => capturedPieces.label, + MaterialDifferenceFormat.hidden => hidden.label, + }; } enum ClockPosition { @@ -364,19 +348,15 @@ enum ClockPosition { // TODO: l10n String get label => switch (this) { - ClockPosition.left => 'Left', - ClockPosition.right => 'Right', - }; + ClockPosition.left => 'Left', + ClockPosition.right => 'Right', + }; } -enum CastlingMethod { - kingOverRook, - kingTwoSquares, - either; -} +enum CastlingMethod { kingOverRook, kingTwoSquares, either } String dragTargetKindLabel(DragTargetKind kind) => switch (kind) { - DragTargetKind.circle => 'Circle', - DragTargetKind.square => 'Square', - DragTargetKind.none => 'None', - }; + DragTargetKind.circle => 'Circle', + DragTargetKind.square => 'Square', + DragTargetKind.none => 'None', +}; diff --git a/lib/src/view/game/game_body.dart b/lib/src/view/game/game_body.dart index 3e9fe18046..ce1c1e6d1b 100644 --- a/lib/src/view/game/game_body.dart +++ b/lib/src/view/game/game_body.dart @@ -103,8 +103,9 @@ class GameBody extends ConsumerWidget { return gameStateAsync.when( data: (gameState) { - final (position, legalMoves) = - gameState.getCurrentPosition(boardPreferences.castlingMethod); + final (position, legalMoves) = gameState.getCurrentPosition( + boardPreferences.castlingMethod, + ); final youAre = gameState.game.youAre ?? Side.white; final archivedBlackClock = gameState.game.archivedBlackClockAt(gameState.stepCursor); final archivedWhiteClock = gameState.game.archivedWhiteClockAt(gameState.stepCursor); diff --git a/lib/src/view/settings/board_settings_screen.dart b/lib/src/view/settings/board_settings_screen.dart index 8d05c98703..4628da9fc3 100644 --- a/lib/src/view/settings/board_settings_screen.dart +++ b/lib/src/view/settings/board_settings_screen.dart @@ -19,8 +19,7 @@ class BoardSettingsScreen extends StatelessWidget { @override Widget build(BuildContext context) { - return PlatformWidget( - androidBuilder: _androidBuilder, iosBuilder: _iosBuilder); + return PlatformWidget(androidBuilder: _androidBuilder, iosBuilder: _iosBuilder); } Widget _androidBuilder(BuildContext context) { @@ -31,8 +30,7 @@ class BoardSettingsScreen extends StatelessWidget { } Widget _iosBuilder(BuildContext context) { - return const CupertinoPageScaffold( - navigationBar: CupertinoNavigationBar(), child: _Body()); + return const CupertinoPageScaffold(navigationBar: CupertinoNavigationBar(), child: _Body()); } } @@ -53,8 +51,7 @@ class _Body extends ConsumerWidget { children: [ SettingsListTile( settingsLabel: Text(context.l10n.preferencesHowDoYouMovePieces), - settingsValue: - pieceShiftMethodl10n(context, boardPrefs.pieceShiftMethod), + settingsValue: pieceShiftMethodl10n(context, boardPrefs.pieceShiftMethod), showCupertinoTrailingValue: false, onTap: () { if (Theme.of(context).platform == TargetPlatform.android) { @@ -66,24 +63,21 @@ class _Body extends ConsumerWidget { onSelectedItemChanged: (PieceShiftMethod? value) { ref .read(boardPreferencesProvider.notifier) - .setPieceShiftMethod( - value ?? PieceShiftMethod.either); + .setPieceShiftMethod(value ?? PieceShiftMethod.either); }, ); } else { pushPlatformRoute( context, title: context.l10n.preferencesHowDoYouMovePieces, - builder: (context) => - const PieceShiftMethodSettingsScreen(), + builder: (context) => const PieceShiftMethodSettingsScreen(), ); } }, ), SettingsListTile( settingsLabel: Text( - context.l10n - .preferencesCastleByMovingTheKingTwoSquaresOrOntoTheRook, + context.l10n.preferencesCastleByMovingTheKingTwoSquaresOrOntoTheRook, ), settingsValue: boardPrefs.castlingMethod.name, showCupertinoTrailingValue: false, @@ -97,16 +91,13 @@ class _Body extends ConsumerWidget { onSelectedItemChanged: (CastlingMethod? value) { ref .read(boardPreferencesProvider.notifier) - .setCastlingMethod( - value ?? CastlingMethod.either, - ); + .setCastlingMethod(value ?? CastlingMethod.either); }, ); } else { pushPlatformRoute( context, - title: context.l10n - .preferencesCastleByMovingTheKingTwoSquaresOrOntoTheRook, + title: context.l10n.preferencesCastleByMovingTheKingTwoSquaresOrOntoTheRook, builder: (context) => const CastlingMethodSettingsScreen(), ); } @@ -116,9 +107,7 @@ class _Body extends ConsumerWidget { title: Text(context.l10n.mobilePrefMagnifyDraggedPiece), value: boardPrefs.magnifyDraggedPiece, onChanged: (value) { - ref - .read(boardPreferencesProvider.notifier) - .toggleMagnifyDraggedPiece(); + ref.read(boardPreferencesProvider.notifier).toggleMagnifyDraggedPiece(); }, ), SettingsListTile( @@ -160,38 +149,35 @@ class _Body extends ConsumerWidget { maxLines: 5, ), onChanged: (value) { - ref - .read(boardPreferencesProvider.notifier) - .toggleHapticFeedback(); + ref.read(boardPreferencesProvider.notifier).toggleHapticFeedback(); }, ), SwitchSettingTile( title: Text(context.l10n.preferencesPieceAnimation), value: boardPrefs.pieceAnimation, onChanged: (value) { - ref - .read(boardPreferencesProvider.notifier) - .togglePieceAnimation(); + ref.read(boardPreferencesProvider.notifier).togglePieceAnimation(); }, ), - if (Theme.of(context).platform == TargetPlatform.android && - !isTabletOrLarger(context)) + if (Theme.of(context).platform == TargetPlatform.android && !isTabletOrLarger(context)) androidVersionAsync.maybeWhen( - data: (version) => version != null && version.sdkInt >= 29 - ? SwitchSettingTile( - title: Text(context.l10n.mobileSettingsImmersiveMode), - subtitle: Text( - context.l10n.mobileSettingsImmersiveModeSubtitle, - maxLines: 5, - ), - value: boardPrefs.immersiveModeWhilePlaying ?? false, - onChanged: (value) { - ref - .read(boardPreferencesProvider.notifier) - .toggleImmersiveModeWhilePlaying(); - }, - ) - : const SizedBox.shrink(), + data: + (version) => + version != null && version.sdkInt >= 29 + ? SwitchSettingTile( + title: Text(context.l10n.mobileSettingsImmersiveMode), + subtitle: Text( + context.l10n.mobileSettingsImmersiveModeSubtitle, + maxLines: 5, + ), + value: boardPrefs.immersiveModeWhilePlaying ?? false, + onChanged: (value) { + ref + .read(boardPreferencesProvider.notifier) + .toggleImmersiveModeWhilePlaying(); + }, + ) + : const SizedBox.shrink(), orElse: () => const SizedBox.shrink(), ), SettingsListTile( @@ -205,9 +191,10 @@ class _Body extends ConsumerWidget { choices: ClockPosition.values, selectedItem: boardPrefs.clockPosition, labelBuilder: (t) => Text(t.label), - onSelectedItemChanged: (ClockPosition? value) => ref - .read(boardPreferencesProvider.notifier) - .setClockPosition(value ?? ClockPosition.right), + onSelectedItemChanged: + (ClockPosition? value) => ref + .read(boardPreferencesProvider.notifier) + .setClockPosition(value ?? ClockPosition.right), ); } else { pushPlatformRoute( @@ -222,24 +209,19 @@ class _Body extends ConsumerWidget { title: Text(context.l10n.preferencesPieceDestinations), value: boardPrefs.showLegalMoves, onChanged: (value) { - ref - .read(boardPreferencesProvider.notifier) - .toggleShowLegalMoves(); + ref.read(boardPreferencesProvider.notifier).toggleShowLegalMoves(); }, ), SwitchSettingTile( title: Text(context.l10n.preferencesBoardHighlights), value: boardPrefs.boardHighlights, onChanged: (value) { - ref - .read(boardPreferencesProvider.notifier) - .toggleBoardHighlights(); + ref.read(boardPreferencesProvider.notifier).toggleBoardHighlights(); }, ), SettingsListTile( settingsLabel: const Text('Material'), //TODO: l10n - settingsValue: boardPrefs.materialDifferenceFormat - .l10n(AppLocalizations.of(context)), + settingsValue: boardPrefs.materialDifferenceFormat.l10n(AppLocalizations.of(context)), onTap: () { if (Theme.of(context).platform == TargetPlatform.android) { showChoicePicker( @@ -247,20 +229,18 @@ class _Body extends ConsumerWidget { choices: MaterialDifferenceFormat.values, selectedItem: boardPrefs.materialDifferenceFormat, labelBuilder: (t) => Text(t.label), - onSelectedItemChanged: (MaterialDifferenceFormat? value) => - ref + onSelectedItemChanged: + (MaterialDifferenceFormat? value) => ref .read(boardPreferencesProvider.notifier) .setMaterialDifferenceFormat( - value ?? - MaterialDifferenceFormat.materialDifference, + value ?? MaterialDifferenceFormat.materialDifference, ), ); } else { pushPlatformRoute( context, title: 'Material', - builder: (context) => - const MaterialDifferenceFormatScreen(), + builder: (context) => const MaterialDifferenceFormatScreen(), ); } }, @@ -275,9 +255,7 @@ class _Body extends ConsumerWidget { ), value: boardPrefs.enableShapeDrawings, onChanged: (value) { - ref - .read(boardPreferencesProvider.notifier) - .toggleEnableShapeDrawings(); + ref.read(boardPreferencesProvider.notifier).toggleEnableShapeDrawings(); }, ), ], @@ -327,15 +305,11 @@ class CastlingMethodSettingsScreen extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { final castlingMethod = ref.watch( - boardPreferencesProvider.select( - (state) => state.castlingMethod, - ), + boardPreferencesProvider.select((state) => state.castlingMethod), ); void onChanged(CastlingMethod? value) { - ref - .read(boardPreferencesProvider.notifier) - .setCastlingMethod(value ?? CastlingMethod.either); + ref.read(boardPreferencesProvider.notifier).setCastlingMethod(value ?? CastlingMethod.either); } return CupertinoPageScaffold( @@ -365,9 +339,8 @@ class BoardClockPositionScreen extends ConsumerWidget { final clockPosition = ref.watch( boardPreferencesProvider.select((state) => state.clockPosition), ); - void onChanged(ClockPosition? value) => ref - .read(boardPreferencesProvider.notifier) - .setClockPosition(value ?? ClockPosition.right); + void onChanged(ClockPosition? value) => + ref.read(boardPreferencesProvider.notifier).setClockPosition(value ?? ClockPosition.right); return CupertinoPageScaffold( navigationBar: const CupertinoNavigationBar(), child: SafeArea( @@ -392,12 +365,11 @@ class MaterialDifferenceFormatScreen extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { final materialDifferenceFormat = ref.watch( - boardPreferencesProvider - .select((state) => state.materialDifferenceFormat), + boardPreferencesProvider.select((state) => state.materialDifferenceFormat), ); - void onChanged(MaterialDifferenceFormat? value) => - ref.read(boardPreferencesProvider.notifier).setMaterialDifferenceFormat( - value ?? MaterialDifferenceFormat.materialDifference); + void onChanged(MaterialDifferenceFormat? value) => ref + .read(boardPreferencesProvider.notifier) + .setMaterialDifferenceFormat(value ?? MaterialDifferenceFormat.materialDifference); return CupertinoPageScaffold( navigationBar: const CupertinoNavigationBar(), child: ListView( @@ -424,9 +396,7 @@ class DragTargetKindSettingsScreen extends ConsumerWidget { ); void onChanged(DragTargetKind? value) { - ref - .read(boardPreferencesProvider.notifier) - .setDragTargetKind(value ?? DragTargetKind.circle); + ref.read(boardPreferencesProvider.notifier).setDragTargetKind(value ?? DragTargetKind.circle); } return CupertinoPageScaffold( @@ -435,10 +405,8 @@ class DragTargetKindSettingsScreen extends ConsumerWidget { child: ListView( children: [ Padding( - padding: - Styles.horizontalBodyPadding.add(Styles.sectionTopPadding), - child: const Text( - 'How the target square is highlighted when dragging a piece.'), + padding: Styles.horizontalBodyPadding.add(Styles.sectionTopPadding), + child: const Text('How the target square is highlighted when dragging a piece.'), ), ChoicePicker( notchedTile: true, @@ -454,8 +422,7 @@ class DragTargetKindSettingsScreen extends ConsumerWidget { } } -String pieceShiftMethodl10n( - BuildContext context, PieceShiftMethod pieceShiftMethod) => +String pieceShiftMethodl10n(BuildContext context, PieceShiftMethod pieceShiftMethod) => switch (pieceShiftMethod) { // TODO add this to mobile translations PieceShiftMethod.either => 'Either tap or drag', @@ -463,16 +430,10 @@ String pieceShiftMethodl10n( PieceShiftMethod.tapTwoSquares => 'Tap two squares', }; -String castlingMethodl10n( - BuildContext context, - CastlingMethod castlingMethod, -) => +String castlingMethodl10n(BuildContext context, CastlingMethod castlingMethod) => switch (castlingMethod) { // TODO add this to mobile translations - CastlingMethod.kingOverRook => - context.l10n.preferencesCastleByMovingOntoTheRook, - CastlingMethod.kingTwoSquares => - context.l10n.preferencesCastleByMovingTwoSquares, + CastlingMethod.kingOverRook => context.l10n.preferencesCastleByMovingOntoTheRook, + CastlingMethod.kingTwoSquares => context.l10n.preferencesCastleByMovingTwoSquares, CastlingMethod.either => 'Either', }; -