Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [BoardEditor] add reset board and starting position option #1035

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/src/model/board_editor/board_editor_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,25 @@ class BoardEditorController extends _$BoardEditorController {
);
}

void loadFen(String fen) {
_updatePosition(readFen(fen).lock);
void loadFen(
String fen, {
bool loadSideToPlay = false,
bool loadCastling = false,
}) {
final splits = fen.split(' ');
_updatePosition(readFen(splits[0]).lock);

if (loadSideToPlay && splits.length >= 2) {
setSideToPlay(splits[1].toLowerCase() == 'w' ? Side.white : Side.black);
}

if (loadCastling && splits.length >= 3) {
final castling = splits[2];
setCastling(Side.white, CastlingSide.king, castling.contains('K'));
setCastling(Side.white, CastlingSide.queen, castling.contains('Q'));
setCastling(Side.black, CastlingSide.king, castling.contains('k'));
setCastling(Side.black, CastlingSide.queen, castling.contains('q'));
}
}

/// Calculates the squares where an en passant capture could be possible.
Expand Down
67 changes: 51 additions & 16 deletions lib/src/view/board_editor/board_editor_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import 'package:lichess_mobile/src/utils/navigation.dart';
import 'package:lichess_mobile/src/utils/screen.dart';
import 'package:lichess_mobile/src/utils/share.dart';
import 'package:lichess_mobile/src/view/analysis/analysis_screen.dart';
import 'package:lichess_mobile/src/view/board_editor/board_editor_menu.dart';
import 'package:lichess_mobile/src/view/board_editor/board_editor_settings.dart';
import 'package:lichess_mobile/src/widgets/adaptive_action_sheet.dart';
import 'package:lichess_mobile/src/widgets/adaptive_bottom_sheet.dart';
import 'package:lichess_mobile/src/widgets/bottom_bar.dart';
import 'package:lichess_mobile/src/widgets/bottom_bar_button.dart';
import 'package:lichess_mobile/src/widgets/buttons.dart';
import 'package:lichess_mobile/src/widgets/platform_scaffold.dart';

class BoardEditorScreen extends StatelessWidget {
Expand All @@ -30,6 +32,21 @@ class BoardEditorScreen extends StatelessWidget {
return PlatformScaffold(
appBar: PlatformAppBar(
title: Text(context.l10n.boardEditor),
actions: [
AppBarIconButton(
onPressed: () => showAdaptiveBottomSheet<void>(
context: context,
isScrollControlled: true,
showDragHandle: true,
isDismissible: true,
builder: (context) => BoardEditorPositionSettings(
initialFen: initialFen,
),
),
semanticsLabel: context.l10n.settingsSettings,
icon: const Icon(Icons.settings),
),
],
),
body: _Body(initialFen),
);
Expand Down Expand Up @@ -306,21 +323,39 @@ class _BottomBar extends ConsumerWidget {
children: [
BottomBarButton(
label: context.l10n.menu,
onTap: () => showAdaptiveBottomSheet<void>(
context: context,
builder: (BuildContext context) => BoardEditorMenu(
initialFen: initialFen,
),
),
icon: Icons.tune,
),
BottomBarButton(
key: const Key('flip-button'),
label: context.l10n.flipBoard,
onTap: ref
.read(boardEditorControllerProvider(initialFen).notifier)
.flipBoard,
icon: CupertinoIcons.arrow_2_squarepath,
key: const Key('board-editor-menu-button'),
onTap: () {
final editorProviderData =
ref.read(boardEditorControllerProvider(initialFen).notifier);
showAdaptiveActionSheet<void>(
context: context,
actions: [
BottomSheetAction(
makeLabel: (context) => Text(context.l10n.startPosition),
onPressed: (context) {
editorProviderData.loadFen(
kInitialFEN,
loadCastling: true,
loadSideToPlay: true,
);
},
),
BottomSheetAction(
makeLabel: (context) => Text(context.l10n.clearBoard),
onPressed: (context) {
editorProviderData.loadFen(kEmptyBoardFEN);
},
),
BottomSheetAction(
makeLabel: (context) => Text(context.l10n.flipBoard),
onPressed: (context) {
editorProviderData.flipBoard();
},
),
],
);
},
icon: Icons.menu,
),
BottomBarButton(
label: context.l10n.analysis,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'package:lichess_mobile/src/styles/styles.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/widgets/adaptive_bottom_sheet.dart';

class BoardEditorMenu extends ConsumerWidget {
const BoardEditorMenu({required this.initialFen, super.key});
class BoardEditorPositionSettings extends ConsumerWidget {
const BoardEditorPositionSettings({required this.initialFen, super.key});

final String? initialFen;

Expand Down
75 changes: 56 additions & 19 deletions test/view/board_editor/board_editor_screen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,62 @@ void main() {
);
});

testWidgets('Flip board', (tester) async {
final app = await buildTestApp(
tester,
home: const BoardEditorScreen(),
);
await tester.pumpWidget(app);

await tester.tap(find.byKey(const Key('flip-button')));
await tester.pump();

expect(
tester
.widget<ChessboardEditor>(
find.byType(ChessboardEditor),
)
.orientation,
Side.black,
);
group('Board editor menu', () {
Future<void> loadWidgetAndOpenMenu(WidgetTester tester) async {
final app = await buildTestApp(
tester,
home: const BoardEditorScreen(),
);
await tester.pumpWidget(app);

await tester.tap(find.byKey(const Key('board-editor-menu-button')));
await tester.pump();
}

testWidgets('Flip board', (tester) async {
await loadWidgetAndOpenMenu(tester);
await tester.tap(find.text('Flip board'));
await tester.pump();

expect(
tester
.widget<ChessboardEditor>(
find.byType(ChessboardEditor),
)
.orientation,
Side.black,
);
});

testWidgets('Reset to Starting Position', (tester) async {
await loadWidgetAndOpenMenu(tester);
await tester.tap(find.text('Starting position'));
await tester.pump();

expect(
tester
.widget<ChessboardEditor>(
find.byType(ChessboardEditor),
)
.pieces,
readFen(kInitialFEN),
);
});

testWidgets('Clear board', (tester) async {
await loadWidgetAndOpenMenu(tester);
await tester.tap(find.text('Clear board'));
await tester.pump();

expect(
tester
.widget<ChessboardEditor>(
find.byType(ChessboardEditor),
)
.pieces,
readFen(kEmptyBoardFEN),
);
});
});

testWidgets('Side to play and castling rights', (tester) async {
Expand All @@ -63,7 +101,6 @@ void main() {
);
await tester.pumpWidget(app);

await tester.tap(find.byKey(const Key('flip-button')));
await tester.pump();

final container = ProviderScope.containerOf(
Expand Down