Skip to content

Commit

Permalink
Merge branch 'HaonRekcef-puzzle-analysis'
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Apr 11, 2024
2 parents afb8523 + 13dfe38 commit aa23a66
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/src/model/engine/work.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Work with _$Work {
Position get position => steps.lastOrNull?.position ?? initialPosition;

/// The work ply.
int get ply => steps.lastOrNull?.position.ply ?? 0;
int get ply => steps.lastOrNull?.position.ply ?? initialPosition.ply;

/// Cached eval for the work position.
ClientEval? get evalCache => steps.lastOrNull?.eval;
Expand Down
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
2 changes: 1 addition & 1 deletion lib/src/model/puzzle/puzzle_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ IList<PuzzleThemeCategory> puzzleThemeCategories(
l10n.strings.puzzleLengths,
[
PuzzleThemeKey.oneMove,
PuzzleThemeKey.long,
PuzzleThemeKey.short,
PuzzleThemeKey.long,
PuzzleThemeKey.veryLong,
],
),
Expand Down
77 changes: 68 additions & 9 deletions lib/src/view/puzzle/puzzle_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ 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/http.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/game/game_repository.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_difficulty.dart';
Expand All @@ -25,9 +29,12 @@ 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/game/archived_game_screen.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 @@ -462,17 +469,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 @@ -539,6 +540,64 @@ 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,
),
),
);
},
),
BottomSheetAction(
makeLabel: (context) => Text(
context.l10n.puzzleFromGameLink(puzzleState.puzzle.game.id.value),
),
onPressed: (_) {
ref
.withClient(
(client) =>
GameRepository(client).getGame(puzzleState.puzzle.game.id),
)
.then((game) {
pushPlatformRoute(
context,
builder: (context) => ArchivedGameScreen(
gameData: game.data,
orientation: puzzleState.pov,
),
);
});
},
),
],
);
}

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 aa23a66

Please sign in to comment.