Skip to content

Commit

Permalink
Refactor analysis settings
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Nov 26, 2024
1 parent 4282f1a commit a7406f4
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 218 deletions.
1 change: 0 additions & 1 deletion lib/src/utils/json.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:deep_pick/deep_pick.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:lichess_mobile/src/model/common/uci.dart';

extension UciExtension on Pick {
Expand Down
116 changes: 14 additions & 102 deletions lib/src/view/analysis/analysis_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import 'package:flutter/cupertino.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/engine/evaluation_service.dart';
import 'package:lichess_mobile/src/model/settings/general_preferences.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/view/analysis/stockfish_settings.dart';
import 'package:lichess_mobile/src/view/opening_explorer/opening_explorer_settings.dart';
import 'package:lichess_mobile/src/widgets/adaptive_bottom_sheet.dart';
import 'package:lichess_mobile/src/widgets/feedback.dart';
import 'package:lichess_mobile/src/widgets/list.dart';
import 'package:lichess_mobile/src/widgets/non_linear_slider.dart';
import 'package:lichess_mobile/src/widgets/platform_scaffold.dart';
import 'package:lichess_mobile/src/widgets/settings.dart';

Expand Down Expand Up @@ -95,106 +94,19 @@ class AnalysisSettings extends ConsumerWidget {
? CrossFadeState.showSecond
: CrossFadeState.showFirst,
firstChild: const SizedBox.shrink(),
secondChild: ListSection(
header: const SettingsSectionTitle('Stockfish 16'),
children: [
SwitchSettingTile(
title: Text(context.l10n.toggleLocalEvaluation),
value: prefs.enableLocalEvaluation,
onChanged: (_) {
ref.read(ctrlProvider.notifier).toggleLocalEvaluation();
},
),
PlatformListTile(
title: Text.rich(
TextSpan(
text: 'Search time: ',
style: const TextStyle(
fontWeight: FontWeight.normal,
),
children: [
TextSpan(
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
text: prefs.engineSearchTime.inSeconds == 3600
? '∞'
: '${prefs.engineSearchTime.inSeconds}s',
),
],
),
),
subtitle: NonLinearSlider(
labelBuilder: (value) =>
value == 3600 ? '∞' : '${value}s',
value: prefs.engineSearchTime.inSeconds,
values: kAvailableEngineSearchTimes
.map((e) => e.inSeconds)
.toList(),
onChangeEnd: (value) =>
ref.read(ctrlProvider.notifier).setEngineSearchTime(
Duration(seconds: value.toInt()),
),
),
),
PlatformListTile(
title: Text.rich(
TextSpan(
text: '${context.l10n.multipleLines}: ',
style: const TextStyle(
fontWeight: FontWeight.normal,
),
children: [
TextSpan(
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
text: prefs.numEvalLines.toString(),
),
],
),
),
subtitle: NonLinearSlider(
value: prefs.numEvalLines,
values: const [0, 1, 2, 3],
onChangeEnd: (value) => ref
.read(ctrlProvider.notifier)
.setNumEvalLines(value.toInt()),
),
),
if (maxEngineCores > 1)
PlatformListTile(
title: Text.rich(
TextSpan(
text: '${context.l10n.cpus}: ',
style: const TextStyle(
fontWeight: FontWeight.normal,
),
children: [
TextSpan(
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
text: prefs.numEngineCores.toString(),
),
],
),
),
subtitle: NonLinearSlider(
value: prefs.numEngineCores,
values: List.generate(
maxEngineCores,
(index) => index + 1,
),
onChangeEnd: (value) => ref
.read(ctrlProvider.notifier)
.setEngineCores(value.toInt()),
),
),
],
secondChild: StockfishSettingsWidget(
onToggleLocalEvaluation: () {
ref.read(ctrlProvider.notifier).toggleLocalEvaluation();
},
onSetEngineSearchTime: (value) {
ref.read(ctrlProvider.notifier).setEngineSearchTime(value);
},
onSetEngineCores: (value) {
ref.read(ctrlProvider.notifier).setEngineCores(value);
},
onSetNumEvalLines: (value) {
ref.read(ctrlProvider.notifier).setNumEvalLines(value);
},
),
),
ListSection(
Expand Down
122 changes: 122 additions & 0 deletions lib/src/view/analysis/stockfish_settings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lichess_mobile/src/model/analysis/analysis_preferences.dart';
import 'package:lichess_mobile/src/model/engine/evaluation_service.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/widgets/list.dart';
import 'package:lichess_mobile/src/widgets/non_linear_slider.dart';
import 'package:lichess_mobile/src/widgets/settings.dart';

class StockfishSettingsWidget extends ConsumerWidget {
const StockfishSettingsWidget({
required this.onToggleLocalEvaluation,
required this.onSetEngineSearchTime,
required this.onSetNumEvalLines,
required this.onSetEngineCores,
super.key,
});

final VoidCallback onToggleLocalEvaluation;
final void Function(Duration) onSetEngineSearchTime;
final void Function(int) onSetNumEvalLines;
final void Function(int) onSetEngineCores;

@override
Widget build(BuildContext context, WidgetRef ref) {
final prefs = ref.watch(analysisPreferencesProvider);

return ListSection(
header: const SettingsSectionTitle('Stockfish 16'),
children: [
SwitchSettingTile(
title: Text(context.l10n.toggleLocalEvaluation),
value: prefs.enableLocalEvaluation,
onChanged: (_) {
onToggleLocalEvaluation();
},
),
PlatformListTile(
title: Text.rich(
TextSpan(
text: 'Search time: ',
style: const TextStyle(
fontWeight: FontWeight.normal,
),
children: [
TextSpan(
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
text: prefs.engineSearchTime.inSeconds == 3600
? '∞'
: '${prefs.engineSearchTime.inSeconds}s',
),
],
),
),
subtitle: NonLinearSlider(
labelBuilder: (value) => value == 3600 ? '∞' : '${value}s',
value: prefs.engineSearchTime.inSeconds,
values:
kAvailableEngineSearchTimes.map((e) => e.inSeconds).toList(),
onChangeEnd: (value) =>
onSetEngineSearchTime(Duration(seconds: value.toInt())),
),
),
PlatformListTile(
title: Text.rich(
TextSpan(
text: '${context.l10n.multipleLines}: ',
style: const TextStyle(
fontWeight: FontWeight.normal,
),
children: [
TextSpan(
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
text: prefs.numEvalLines.toString(),
),
],
),
),
subtitle: NonLinearSlider(
value: prefs.numEvalLines,
values: const [0, 1, 2, 3],
onChangeEnd: (value) => onSetNumEvalLines(value.toInt()),
),
),
if (maxEngineCores > 1)
PlatformListTile(
title: Text.rich(
TextSpan(
text: '${context.l10n.cpus}: ',
style: const TextStyle(
fontWeight: FontWeight.normal,
),
children: [
TextSpan(
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
text: prefs.numEngineCores.toString(),
),
],
),
),
subtitle: NonLinearSlider(
value: prefs.numEngineCores,
values: List.generate(
maxEngineCores,
(index) => index + 1,
),
onChangeEnd: (value) => onSetEngineCores(value.toInt()),
),
),
],
);
}
}
14 changes: 7 additions & 7 deletions lib/src/view/study/study_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:lichess_mobile/src/model/settings/board_preferences.dart';
import 'package:lichess_mobile/src/model/study/study_controller.dart';
import 'package:lichess_mobile/src/model/study/study_preferences.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/utils/navigation.dart';
import 'package:lichess_mobile/src/view/analysis/analysis_layout.dart';
import 'package:lichess_mobile/src/view/engine/engine_gauge.dart';
import 'package:lichess_mobile/src/view/engine/engine_lines.dart';
Expand Down Expand Up @@ -54,13 +55,12 @@ class StudyScreen extends ConsumerWidget {
),
actions: [
AppBarIconButton(
onPressed: () => showAdaptiveBottomSheet<void>(
context: context,
isScrollControlled: true,
showDragHandle: true,
isDismissible: true,
builder: (_) => StudySettings(id),
),
onPressed: () {
pushPlatformRoute(
context,
screen: StudySettings(id),
);
},
semanticsLabel: context.l10n.settingsSettings,
icon: const Icon(Icons.settings),
),
Expand Down
Loading

0 comments on commit a7406f4

Please sign in to comment.