Skip to content

Commit

Permalink
puzzle(-streak) analysis in analysisboard
Browse files Browse the repository at this point in the history
  • Loading branch information
HaonRekcef committed Mar 24, 2024
1 parent fea545b commit c06d2c6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 9 deletions.
19 changes: 19 additions & 0 deletions lib/src/model/puzzle/puzzle_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:dartchess/dartchess.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:http/http.dart' as http;
import 'package:lichess_mobile/src/constants.dart';
import 'package:lichess_mobile/src/model/common/chess.dart';
import 'package:lichess_mobile/src/model/common/http.dart';
import 'package:lichess_mobile/src/model/common/node.dart';
Expand Down Expand Up @@ -464,6 +465,24 @@ class PuzzleController extends _$PuzzleController {
}
}

String makePgn() {
final initPosition = _gameTree.nodeAt(state.initialPath).position;
var currentPosition = initPosition;
final pgnMoves = state.puzzle.puzzle.solution.fold<List<String>>([],
(List<String> acc, move) {
final moveObj = Move.fromUci(move);
if (moveObj != null) {
final String san;
(currentPosition, san) = currentPosition.makeSan(moveObj);
return acc..add(san);
}
return acc;
});
final pgn =
'[FEN "${initPosition.fen}"][Site "$kLichessHost/training/${state.puzzle.puzzle.id}"]${pgnMoves.join(' ')}';
return pgn;
}

void _startEngineEval() {
if (!state.isEngineEnabled) return;
_engineEvalDebounce(
Expand Down
53 changes: 44 additions & 9 deletions lib/src/view/puzzle/puzzle_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lichess_mobile/src/constants.dart';
import 'package:lichess_mobile/src/model/analysis/analysis_controller.dart';
import 'package:lichess_mobile/src/model/auth/auth_session.dart';
import 'package:lichess_mobile/src/model/common/chess.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/engine/evaluation_service.dart';
import 'package:lichess_mobile/src/model/puzzle/puzzle_angle.dart';
Expand All @@ -25,9 +27,11 @@ import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/utils/navigation.dart';
import 'package:lichess_mobile/src/utils/share.dart';
import 'package:lichess_mobile/src/view/account/rating_pref_aware.dart';
import 'package:lichess_mobile/src/view/analysis/analysis_screen.dart';
import 'package:lichess_mobile/src/view/engine/engine_gauge.dart';
import 'package:lichess_mobile/src/view/puzzle/puzzle_settings_screen.dart';
import 'package:lichess_mobile/src/view/settings/toggle_sound_button.dart';
import 'package:lichess_mobile/src/widgets/adaptive_action_sheet.dart';
import 'package:lichess_mobile/src/widgets/adaptive_choice_picker.dart';
import 'package:lichess_mobile/src/widgets/board_table.dart';
import 'package:lichess_mobile/src/widgets/bottom_bar_button.dart';
Expand Down Expand Up @@ -463,17 +467,11 @@ class _BottomBar extends ConsumerWidget {
if (puzzleState.mode == PuzzleMode.view)
Expanded(
child: BottomBarButton(
label: context.l10n.menu,
onTap: () {
launchShareDialog(
context,
text:
'$kLichessHost/training/${puzzleState.puzzle.puzzle.id}',
);
_showPuzzleMenu(context, ref);
},
label: 'Share this puzzle',
icon: Theme.of(context).platform == TargetPlatform.iOS
? CupertinoIcons.share
: Icons.share,
icon: Icons.menu,
),
),
if (puzzleState.mode == PuzzleMode.view)
Expand Down Expand Up @@ -540,6 +538,43 @@ class _BottomBar extends ConsumerWidget {
);
}

Future<void> _showPuzzleMenu(BuildContext context, WidgetRef ref) {
final puzzleState = ref.watch(ctrlProvider);
return showAdaptiveActionSheet(
context: context,
actions: [
BottomSheetAction(
makeLabel: (context) => const Text('Share this puzzle'),
onPressed: (context) {
launchShareDialog(
context,
text: '$kLichessHost/training/${puzzleState.puzzle.puzzle.id}',
);
},
),
BottomSheetAction(
makeLabel: (context) => Text(context.l10n.analysis),
onPressed: (context) {
pushPlatformRoute(
context,
builder: (context) => AnalysisScreen(
title: context.l10n.analysis,
options: AnalysisOptions(
isLocalEvaluationAllowed: true,
variant: Variant.standard,
pgn: ref.read(ctrlProvider.notifier).makePgn(),
orientation: puzzleState.pov,
id: standaloneAnalysisId,
initialMoveCursor: 0,
),
),
);
},
),
],
);
}

void _moveForward(WidgetRef ref) {
ref.read(ctrlProvider.notifier).userNext();
}
Expand Down
27 changes: 27 additions & 0 deletions lib/src/view/puzzle/streak_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lichess_mobile/src/constants.dart';
import 'package:lichess_mobile/src/model/analysis/analysis_controller.dart';
import 'package:lichess_mobile/src/model/auth/auth_session.dart';
import 'package:lichess_mobile/src/model/common/chess.dart';
import 'package:lichess_mobile/src/model/puzzle/puzzle_angle.dart';
import 'package:lichess_mobile/src/model/puzzle/puzzle_controller.dart';
import 'package:lichess_mobile/src/model/puzzle/puzzle_providers.dart';
Expand All @@ -16,7 +18,9 @@ import 'package:lichess_mobile/src/styles/styles.dart';
import 'package:lichess_mobile/src/utils/chessground_compat.dart';
import 'package:lichess_mobile/src/utils/immersive_mode.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/utils/navigation.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/settings/toggle_sound_button.dart';
import 'package:lichess_mobile/src/widgets/board_table.dart';
import 'package:lichess_mobile/src/widgets/bottom_bar_button.dart';
Expand Down Expand Up @@ -308,6 +312,29 @@ class _BottomBar extends ConsumerWidget {
: Icons.share,
),
),
if (puzzleState.streak!.finished)
Expanded(
child: BottomBarButton(
onTap: () {
pushPlatformRoute(
context,
builder: (context) => AnalysisScreen(
title: context.l10n.analysis,
options: AnalysisOptions(
isLocalEvaluationAllowed: true,
variant: Variant.standard,
pgn: ref.read(ctrlProvider.notifier).makePgn(),
orientation: puzzleState.pov,
id: standaloneAnalysisId,
initialMoveCursor: 0,
),
),
);
},
label: context.l10n.analysis,
icon: Icons.biotech,
),
),
if (puzzleState.streak!.finished)
Expanded(
child: BottomBarButton(
Expand Down

0 comments on commit c06d2c6

Please sign in to comment.