Skip to content

Commit

Permalink
Fix tree view
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Nov 22, 2024
1 parent 3517a44 commit 668c3de
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 44 deletions.
9 changes: 9 additions & 0 deletions lib/src/view/analysis/tree_view.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lichess_mobile/src/model/analysis/analysis_controller.dart';
import 'package:lichess_mobile/src/model/analysis/analysis_preferences.dart';
import 'package:lichess_mobile/src/model/analysis/opening_service.dart';
import 'package:lichess_mobile/src/model/common/chess.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
Expand All @@ -27,6 +28,10 @@ class AnalysisTreeView extends ConsumerWidget {
final pgnRootComments = ref.watch(
ctrlProvider.select((value) => value.requireValue.pgnRootComments),
);
final prefs = ref.watch(analysisPreferencesProvider);
// enable computer analysis takes effect here only if it's a lichess game
final enableComputerAnalysis =
!options.isLichessGameAnalysis || prefs.enableComputerAnalysis;

return CustomScrollView(
slivers: [
Expand All @@ -41,6 +46,10 @@ class AnalysisTreeView extends ConsumerWidget {
currentPath: currentPath,
pgnRootComments: pgnRootComments,
notifier: ref.read(ctrlProvider.notifier),
shouldShowComputerVariations: enableComputerAnalysis,
shouldShowComments: enableComputerAnalysis && prefs.showPgnComments,
shouldShowAnnotations:
enableComputerAnalysis && prefs.showAnnotations,
),
),
],
Expand Down
7 changes: 0 additions & 7 deletions lib/src/view/study/study_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,6 @@ class StudySettings extends ConsumerWidget {
.read(analysisPreferencesProvider.notifier)
.toggleAnnotations(),
),
SwitchSettingTile(
title: Text(context.l10n.mobileShowComments),
value: analysisPrefs.showPgnComments,
onChanged: (_) => ref
.read(analysisPreferencesProvider.notifier)
.togglePgnComments(),
),
SwitchSettingTile(
title: Text(context.l10n.sound),
value: isSoundEnabled,
Expand Down
4 changes: 4 additions & 0 deletions lib/src/view/study/study_tree_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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:lichess_mobile/src/model/analysis/analysis_preferences.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/common/node.dart';
import 'package:lichess_mobile/src/model/study/study_controller.dart';
Expand Down Expand Up @@ -36,6 +37,8 @@ class StudyTreeView extends ConsumerWidget {
.select((value) => value.requireValue.pgnRootComments),
);

final analysisPrefs = ref.watch(analysisPreferencesProvider);

return CustomScrollView(
slivers: [
SliverFillRemaining(
Expand All @@ -49,6 +52,7 @@ class StudyTreeView extends ConsumerWidget {
currentPath: currentPath,
pgnRootComments: pgnRootComments,
notifier: ref.read(studyControllerProvider(id).notifier),
shouldShowAnnotations: analysisPrefs.showAnnotations,
),
),
],
Expand Down
64 changes: 28 additions & 36 deletions lib/src/widgets/pgn.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lichess_mobile/src/model/account/account_preferences.dart';
import 'package:lichess_mobile/src/model/analysis/analysis_preferences.dart';
import 'package:lichess_mobile/src/model/common/node.dart';
import 'package:lichess_mobile/src/model/common/uci.dart';
import 'package:lichess_mobile/src/styles/lichess_colors.dart';
Expand Down Expand Up @@ -113,6 +112,9 @@ class DebouncedPgnTreeView extends ConsumerStatefulWidget {
required this.currentPath,
required this.pgnRootComments,
required this.notifier,
this.shouldShowComputerVariations = true,
this.shouldShowAnnotations = true,
this.shouldShowComments = true,
});

/// Root of the PGN tree to display
Expand All @@ -127,6 +129,17 @@ class DebouncedPgnTreeView extends ConsumerStatefulWidget {
/// Callbacks for when the user interacts with the tree view, e.g. selecting a different move or collapsing variations
final PgnTreeNotifier notifier;

/// Whether to show analysis variations.
///
/// Only applied to lichess game analysis.
final bool shouldShowComputerVariations;

/// Whether to show NAG annotations like '!' and '??'.
final bool shouldShowAnnotations;

/// Whether to show comments associated with the moves.
final bool shouldShowComments;

@override
ConsumerState<DebouncedPgnTreeView> createState() =>
_DebouncedPgnTreeViewState();
Expand Down Expand Up @@ -195,32 +208,13 @@ class _DebouncedPgnTreeViewState extends ConsumerState<DebouncedPgnTreeView> {
// using the fast replay buttons.
@override
Widget build(BuildContext context) {
final withComputerAnalysis = ref.watch(
analysisPreferencesProvider
.select((value) => value.enableComputerAnalysis),
);

final shouldShowComments = withComputerAnalysis &&
ref.watch(
analysisPreferencesProvider.select(
(value) => value.showPgnComments,
),
);

final shouldShowAnnotations = withComputerAnalysis &&
ref.watch(
analysisPreferencesProvider.select(
(value) => value.showAnnotations,
),
);

return _PgnTreeView(
root: widget.root,
rootComments: widget.pgnRootComments,
params: (
withComputerAnalysis: withComputerAnalysis,
shouldShowAnnotations: shouldShowAnnotations,
shouldShowComments: shouldShowComments,
shouldShowComputerVariations: widget.shouldShowComputerVariations,
shouldShowAnnotations: widget.shouldShowAnnotations,
shouldShowComments: widget.shouldShowComments,
currentMoveKey: currentMoveKey,
pathToCurrentMove: pathToCurrentMove,
notifier: widget.notifier,
Expand All @@ -237,10 +231,8 @@ typedef _PgnTreeViewParams = ({
/// Path to the currently selected move in the tree.
UciPath pathToCurrentMove,

/// Whether to show NAG, comments, and analysis variations.
///
/// Takes precedence over [shouldShowAnnotations], and [shouldShowComments],
bool withComputerAnalysis,
/// Whether to show analysis variations.
bool shouldShowComputerVariations,

/// Whether to show NAG annotations like '!' and '??'.
bool shouldShowAnnotations,
Expand All @@ -259,10 +251,10 @@ typedef _PgnTreeViewParams = ({
/// Filter node children when computer analysis is disabled
IList<ViewBranch> _filteredChildren(
ViewNode node,
bool withComputerAnalysis,
bool shouldShowComputerVariations,
) {
return node.children
.where((c) => withComputerAnalysis || !c.isComputerVariation)
.where((c) => shouldShowComputerVariations || !c.isComputerVariation)
.toIList();
}

Expand All @@ -279,7 +271,7 @@ bool _displaySideLineAsInline(ViewBranch node, [int depth = 0]) {

/// Returns whether this node has a sideline that should not be displayed inline.
bool _hasNonInlineSideLine(ViewNode node, _PgnTreeViewParams params) {
final children = _filteredChildren(node, params.withComputerAnalysis);
final children = _filteredChildren(node, params.shouldShowComputerVariations);
return children.length > 2 ||
(children.length == 2 && !_displaySideLineAsInline(children[1]));
}
Expand Down Expand Up @@ -429,8 +421,8 @@ class _PgnTreeViewState extends State<_PgnTreeView> {
super.didUpdateWidget(oldWidget);
_updateLines(
fullRebuild: oldWidget.root != widget.root ||
oldWidget.params.withComputerAnalysis !=
widget.params.withComputerAnalysis ||
oldWidget.params.shouldShowComputerVariations !=
widget.params.shouldShowComputerVariations ||
oldWidget.params.shouldShowComments !=
widget.params.shouldShowComments ||
oldWidget.params.shouldShowAnnotations !=
Expand Down Expand Up @@ -695,14 +687,15 @@ class _MainLinePart extends ConsumerWidget {
TextSpan(
children: nodes
.takeWhile(
(node) => _filteredChildren(node, params.withComputerAnalysis)
.isNotEmpty,
(node) =>
_filteredChildren(node, params.shouldShowComputerVariations)
.isNotEmpty,
)
.mapIndexed(
(i, node) {
final children = _filteredChildren(
node,
params.withComputerAnalysis,
params.shouldShowComputerVariations,
);
final mainlineNode = children.first;
final moves = [
Expand Down Expand Up @@ -1248,7 +1241,6 @@ List<TextSpan> _comments(
text: comment,
style: textStyle.copyWith(
fontSize: textStyle.fontSize! - 2.0,
fontStyle: FontStyle.italic,
),
),
)
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: lichess_mobile
description: Lichess mobile app V2
publish_to: "none"

version: 0.13.1+001301 # See README.md for details about versioning
version: 0.13.2+001302 # See README.md for details about versioning

environment:
sdk: ">=3.5.0 <4.0.0"
Expand Down

0 comments on commit 668c3de

Please sign in to comment.