Skip to content

Commit

Permalink
Implement zen mode, show material diff and move list
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Oct 3, 2023
1 parent 34c07f0 commit 1e1c783
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 25 deletions.
4 changes: 4 additions & 0 deletions lib/src/model/game/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,14 @@ class PlayableGame with _$PlayableGame, BaseGame, IndexableSteps {
}

typedef GamePrefs = ({
bool showRatings,
bool showMaterialDiff,
bool showMoveList,
bool enablePremove,
AutoQueen autoQueen,
bool confirmResign,
bool submitMove,
Zen zenMode,
});

enum GameSource {
Expand Down
16 changes: 16 additions & 0 deletions lib/src/model/game/game_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ class GameController extends _$GameController {
);
}

void toggleZenMode() {
final curState = state.requireValue;
state = AsyncValue.data(
curState.copyWith(
zenModeGameSetting: !(curState.zenModeGameSetting ?? false),
),
);
}

void onFlag() {
_onFlagThrottler(() {
if (state.hasValue) {
Expand Down Expand Up @@ -733,6 +742,9 @@ class GameState with _$GameState {
/// Game only setting to override the account preference
bool? moveConfirmSettingOverride,

/// Zen mode setting if account preference is set to [Zen.gameAuto]
bool? zenModeGameSetting,

/// Set if confirm move preference is enabled and player played a move
Move? moveToConfirm,

Expand All @@ -741,6 +753,10 @@ class GameState with _$GameState {
}) = _GameState;

// preferences
bool get shouldShowMaterialDiff => game.prefs?.showMaterialDiff ?? true;
bool get shouldShowMoveList => game.prefs?.showMoveList ?? true;
bool get isZenModeEnabled =>
zenModeGameSetting ?? game.prefs?.zenMode == Zen.yes;
bool get canPremove => game.prefs?.enablePremove ?? true;
bool get canAutoQueen => game.prefs?.autoQueen == AutoQueen.always;
bool get canAutoQueenOnPremove => game.prefs?.autoQueen == AutoQueen.premove;
Expand Down
4 changes: 4 additions & 0 deletions lib/src/model/game/game_socket_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,14 @@ PlayableGameMeta _playableGameMetaFromPick(RequiredPick pick) {

GamePrefs _gamePrefsFromPick(RequiredPick pick) {
return (
showMaterialDiff: pick('showCaptured').asBoolOrFalse(),
showRatings: pick('showRatings').asBoolOrFalse(),
showMoveList: pick('showReplay').asBoolOrFalse(),
enablePremove: pick('enablePremove').asBoolOrFalse(),
autoQueen: AutoQueen.fromInt(pick('autoQueen').asIntOrThrow()),
confirmResign: pick('confirmResign').asBoolOrFalse(),
submitMove: pick('submitMove').asBoolOrFalse(),
zenMode: Zen.fromInt(pick('zen').asIntOrThrow()),
);
}

Expand Down
25 changes: 16 additions & 9 deletions lib/src/view/game/game_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,13 @@ class _Body extends ConsumerWidget {

final black = BoardPlayer(
player: gameState.game.black,
materialDiff:
gameState.game.materialDiffAt(gameState.stepCursor, Side.black),
materialDiff: gameState.shouldShowMaterialDiff
? gameState.game.materialDiffAt(gameState.stepCursor, Side.black)
: null,
timeToMove: sideToMove == Side.black ? gameState.timeToMove : null,
shouldLinkToUserProfile: youAre != Side.black,
mePlaying: youAre == Side.black,
zenMode: gameState.isZenModeEnabled,
confirmMoveCallbacks:
youAre == Side.black && gameState.moveToConfirm != null
? (
Expand All @@ -294,11 +296,13 @@ class _Body extends ConsumerWidget {
);
final white = BoardPlayer(
player: gameState.game.white,
materialDiff:
gameState.game.materialDiffAt(gameState.stepCursor, Side.white),
materialDiff: gameState.shouldShowMaterialDiff
? gameState.game.materialDiffAt(gameState.stepCursor, Side.white)
: null,
timeToMove: sideToMove == Side.white ? gameState.timeToMove : null,
shouldLinkToUserProfile: youAre != Side.white,
mePlaying: youAre == Side.white,
zenMode: gameState.isZenModeEnabled,
confirmMoveCallbacks:
youAre == Side.white && gameState.moveToConfirm != null
? (
Expand Down Expand Up @@ -371,11 +375,14 @@ class _Body extends ConsumerWidget {
duration: gameState.opponentLeftCountdown!,
)
: bottomPlayer,
moves: gameState.game.steps
.skip(1)
.map((e) => e.sanMove!.san)
.toList(growable: false),
currentMoveIndex: gameState.stepCursor,
moves: gameState.shouldShowMoveList
? gameState.game.steps
.skip(1)
.map((e) => e.sanMove!.san)
.toList(growable: false)
: null,
currentMoveIndex:
gameState.shouldShowMoveList ? gameState.stepCursor : null,
onSelectMove: (moveIndex) {
ref.read(ctrlProvider.notifier).cursorAt(moveIndex);
},
Expand Down
43 changes: 27 additions & 16 deletions lib/src/view/game/game_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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/game/game_controller.dart';
import 'package:lichess_mobile/src/model/settings/board_preferences.dart';
import 'package:lichess_mobile/src/model/settings/general_preferences.dart';
Expand Down Expand Up @@ -61,25 +62,35 @@ class GameSettings extends ConsumerWidget {
.togglePieceAnimation();
},
),
gameState.when(
...gameState.when(
data: (data) {
if (data.game.prefs?.submitMove == true) {
return SwitchSettingTile(
title: Text(
context.l10n.preferencesMoveConfirmation,
maxLines: 2,
return [
if (data.game.prefs?.submitMove == true)
SwitchSettingTile(
title: Text(
context.l10n.preferencesMoveConfirmation,
maxLines: 2,
),
value: data.shouldConfirmMove,
onChanged: (value) {
ref.read(ctrlProvider!.notifier).toggleMoveConfirmation();
},
),
value: data.shouldConfirmMove,
onChanged: (value) {
ref.read(ctrlProvider!.notifier).toggleMoveConfirmation();
},
);
} else {
return const SizedBox.shrink();
}
if (data.game.prefs?.zenMode == Zen.gameAuto)
SwitchSettingTile(
title: Text(
context.l10n.preferencesZenMode,
maxLines: 2,
),
value: data.isZenModeEnabled,
onChanged: (value) {
ref.read(ctrlProvider!.notifier).toggleZenMode();
},
),
];
},
loading: () => const SizedBox.shrink(),
error: (e, s) => const SizedBox.shrink(),
loading: () => [const SizedBox.shrink()],
error: (e, s) => [const SizedBox.shrink()],
),
const SizedBox(height: 16.0),
],
Expand Down

0 comments on commit 1e1c783

Please sign in to comment.