Skip to content

Commit

Permalink
Merge pull request #728 from lichess-org/fix_zen_mode
Browse files Browse the repository at this point in the history
Fix zen mode
  • Loading branch information
veloce authored May 30, 2024
2 parents 3167e99 + 36a1586 commit 2b80d17
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 99 deletions.
7 changes: 5 additions & 2 deletions lib/src/model/game/game_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -930,10 +930,14 @@ class GameState with _$GameState {
GameFullId? redirectGameId,
}) = _GameState;

// preferences
/// Whether the zen mode is active
bool get isZenModeActive => game.playable && isZenModeEnabled;

/// Whether zen mode is enabled by account preference or local game setting
bool get isZenModeEnabled =>
zenModeGameSetting ??
game.prefs?.zenMode == Zen.yes || game.prefs?.zenMode == Zen.gameAuto;

bool get canPremove =>
game.meta.speed != Speed.correspondence &&
(game.prefs?.enablePremove ?? true);
Expand All @@ -943,7 +947,6 @@ class GameState with _$GameState {
bool get shouldConfirmMove =>
moveConfirmSettingOverride ?? game.prefs?.submitMove ?? false;

// game state
bool get isReplaying => stepCursor < game.steps.length - 1;
bool get canGoForward => stepCursor < game.steps.length - 1;
bool get canGoBackward => stepCursor > 0;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/model/game/playable_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ class PlayableGame
bool get imported => source == GameSource.import;

bool get isPlayerTurn => lastPosition.turn == youAre;

/// Whether the game is properly finished (not aborted).
bool get finished => status.value >= GameStatus.mate.value;
bool get aborted => status == GameStatus.aborted;

/// Whether the game is still playable (not finished or aborted and not imported).
bool get playable => status.value < GameStatus.aborted.value && !imported;
bool get abortable =>
playable &&
Expand Down
6 changes: 3 additions & 3 deletions lib/src/view/game/game_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class GameBody extends ConsumerWidget {
: null,
shouldLinkToUserProfile: youAre != Side.black,
mePlaying: youAre == Side.black,
zenMode: gameState.isZenModeEnabled,
zenMode: gameState.isZenModeActive,
confirmMoveCallbacks:
youAre == Side.black && gameState.moveToConfirm != null
? (
Expand Down Expand Up @@ -172,7 +172,7 @@ class GameBody extends ConsumerWidget {
: null,
shouldLinkToUserProfile: youAre != Side.white,
mePlaying: youAre == Side.white,
zenMode: gameState.isZenModeEnabled,
zenMode: gameState.isZenModeActive,
confirmMoveCallbacks:
youAre == Side.white && gameState.moveToConfirm != null
? (
Expand Down Expand Up @@ -420,7 +420,7 @@ class _GameBottomBar extends ConsumerWidget {
final List<Widget> children = gameStateAsync.when(
data: (gameState) {
final isChatEnabled =
chatStateAsync != null && !gameState.isZenModeEnabled;
chatStateAsync != null && !gameState.isZenModeActive;

final chatUnreadChip = isChatEnabled
? chatStateAsync.maybeWhen(
Expand Down
134 changes: 68 additions & 66 deletions lib/src/view/game/game_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,81 +56,82 @@ class GamePlayer extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Icon(
player.onGame == true ? Icons.cloud : Icons.cloud_off,
color: player.onGame == true ? LichessColors.green : null,
size: 14,
),
const SizedBox(width: 5),
if (player.user?.isPatron == true) ...[
if (!zenMode)
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Icon(
LichessIcons.patron,
size: playerFontSize,
semanticLabel: context.l10n.patronLichessPatron,
player.onGame == true ? Icons.cloud : Icons.cloud_off,
color: player.onGame == true ? LichessColors.green : null,
size: 14,
),
const SizedBox(width: 5),
],
if (player.user?.title != null) ...[
Text(
player.user!.title!,
style: TextStyle(
fontSize: playerFontSize,
fontWeight: FontWeight.bold,
color: context.lichessColors.brag,
if (player.user?.isPatron == true) ...[
Icon(
LichessIcons.patron,
size: playerFontSize,
semanticLabel: context.l10n.patronLichessPatron,
),
),
const SizedBox(width: 5),
],
Flexible(
child: Text(
player.displayName(context),
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: playerFontSize,
fontWeight: FontWeight.w600,
),
),
),
if (player.user?.flair != null) ...[
const SizedBox(width: 5),
CachedNetworkImage(
imageUrl: lichessFlairSrc(player.user!.flair!),
errorWidget: (_, __, ___) => kEmptyWidget,
width: 16,
height: 16,
),
],
if (player.rating != null)
RatingPrefAware(
child: Text.rich(
TextSpan(
text:
' ${player.rating}${player.provisional == true ? '?' : ''}',
children: [
if (player.ratingDiff != null)
TextSpan(
text:
' ${player.ratingDiff! > 0 ? '+' : ''}${player.ratingDiff}',
style: TextStyle(
color: player.ratingDiff! > 0
? context.lichessColors.good
: context.lichessColors.error,
),
),
],
const SizedBox(width: 5),
],
if (player.user?.title != null) ...[
Text(
player.user!.title!,
style: TextStyle(
fontSize: playerFontSize,
fontWeight: FontWeight.bold,
color: context.lichessColors.brag,
),
),
const SizedBox(width: 5),
],
Flexible(
child: Text(
player.displayName(context),
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 14,
color: textShade(context, 0.7),
fontSize: playerFontSize,
fontWeight: FontWeight.w600,
),
),
),
],
),
if (player.user?.flair != null) ...[
const SizedBox(width: 5),
CachedNetworkImage(
imageUrl: lichessFlairSrc(player.user!.flair!),
errorWidget: (_, __, ___) => kEmptyWidget,
width: 16,
height: 16,
),
],
if (player.rating != null)
RatingPrefAware(
child: Text.rich(
TextSpan(
text:
' ${player.rating}${player.provisional == true ? '?' : ''}',
children: [
if (player.ratingDiff != null)
TextSpan(
text:
' ${player.ratingDiff! > 0 ? '+' : ''}${player.ratingDiff}',
style: TextStyle(
color: player.ratingDiff! > 0
? context.lichessColors.good
: context.lichessColors.error,
),
),
],
),
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 14,
color: textShade(context, 0.7),
),
),
),
],
),
if (timeToMove != null)
MoveExpiration(timeToMove: timeToMove!, mePlaying: mePlaying)
else if (materialDiff != null)
Expand All @@ -156,6 +157,7 @@ class GamePlayer extends StatelessWidget {
],
)
else
// to avoid shifts use an empty text widget
const Text('', style: TextStyle(fontSize: 13)),
],
);
Expand All @@ -175,7 +177,7 @@ class GamePlayer extends StatelessWidget {
),
),
)
else if (!zenMode || timeToMove != null)
else
Expanded(
flex: 7,
child: Padding(
Expand Down
67 changes: 39 additions & 28 deletions lib/src/view/game/game_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,34 @@ class GameSettings extends ConsumerWidget {
subtitle: const SizedBox.shrink(),
),
const SizedBox(height: 8.0),
...userPrefsAsync.maybeWhen(
data: (data) {
return [
if (data.prefs?.submitMove == true)
SwitchSettingTile(
title: Text(
context.l10n.preferencesMoveConfirmation,
),
value: data.shouldConfirmMove,
onChanged: (value) {
ref
.read(gameControllerProvider(id).notifier)
.toggleMoveConfirmation();
},
),
SwitchSettingTile(
title: Text(
context.l10n.preferencesZenMode,
),
value: data.isZenModeEnabled,
onChanged: (value) {
ref.read(gameControllerProvider(id).notifier).toggleZenMode();
},
),
];
},
orElse: () => [],
),
SwitchSettingTile(
title: Text(context.l10n.sound),
value: isSoundEnabled,
Expand All @@ -58,6 +86,17 @@ class GameSettings extends ConsumerWidget {
ref.read(boardPreferencesProvider.notifier).togglePieceAnimation();
},
),
SwitchSettingTile(
title: Text(
context.l10n.preferencesMaterialDifference,
),
value: boardPrefs.showMaterialDifference,
onChanged: (value) {
ref
.read(boardPreferencesProvider.notifier)
.toggleShowMaterialDifference();
},
),
SwitchSettingTile(
title: Text(
context.l10n.toggleTheChat,
Expand All @@ -68,34 +107,6 @@ class GameSettings extends ConsumerWidget {
ref.read(gameControllerProvider(id).notifier).onToggleChat(value);
},
),
...userPrefsAsync.maybeWhen(
data: (data) {
return [
if (data.prefs?.submitMove == true)
SwitchSettingTile(
title: Text(
context.l10n.preferencesMoveConfirmation,
),
value: data.shouldConfirmMove,
onChanged: (value) {
ref
.read(gameControllerProvider(id).notifier)
.toggleMoveConfirmation();
},
),
SwitchSettingTile(
title: Text(
context.l10n.preferencesZenMode,
),
value: data.isZenModeEnabled,
onChanged: (value) {
ref.read(gameControllerProvider(id).notifier).toggleZenMode();
},
),
];
},
orElse: () => [],
),
SwitchSettingTile(
title: const Text('Blindfold'),
value: gamePrefs.blindfoldMode ?? false,
Expand Down

0 comments on commit 2b80d17

Please sign in to comment.