Skip to content

Commit

Permalink
Fix clock tool
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Nov 14, 2024
1 parent d7161d6 commit 2cfc4bf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
29 changes: 15 additions & 14 deletions lib/src/model/clock/clock_tool_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ part 'clock_tool_controller.g.dart';

@riverpod
class ClockToolController extends _$ClockToolController {
late ChessClock _clock;
late final ChessClock _clock;

@override
ClockState build() {
Expand All @@ -26,9 +26,7 @@ class ClockToolController extends _$ClockToolController {
_clock = ChessClock(
whiteTime: time,
blackTime: time,
onFlag: () {
setLoser(_clock.activeSide);
},
onFlag: _onFlagged,
);

ref.onDispose(() {
Expand All @@ -43,6 +41,11 @@ class ClockToolController extends _$ClockToolController {
);
}

void _onFlagged() {
_clock.stop();
state = state.copyWith(flagged: _clock.activeSide);
}

void onTap(Side playerType) {
final started = state.started;
if (playerType == Side.white) {
Expand All @@ -69,7 +72,7 @@ class ClockToolController extends _$ClockToolController {
}

void updateDuration(Side playerType, Duration duration) {
if (state.loser != null || state.paused) {
if (state.flagged != null || state.paused) {
return;
}

Expand All @@ -85,7 +88,7 @@ class ClockToolController extends _$ClockToolController {

void updateOptions(TimeIncrement timeIncrement) {
final options = ClockOptions.fromTimeIncrement(timeIncrement);
_clock = ChessClock(
_clock.setTimes(
whiteTime: options.whiteTime,
blackTime: options.blackTime,
);
Expand Down Expand Up @@ -114,7 +117,7 @@ class ClockToolController extends _$ClockToolController {
? Duration(seconds: clock.increment)
: state.options.blackIncrement,
);
_clock = ChessClock(
_clock.setTimes(
whiteTime: options.whiteTime,
blackTime: options.blackTime,
);
Expand All @@ -129,18 +132,16 @@ class ClockToolController extends _$ClockToolController {
void setBottomPlayer(Side playerType) =>
state = state.copyWith(bottomPlayer: playerType);

void setLoser(Side playerType) => state = state.copyWith(loser: playerType);

void reset() {
_clock = ChessClock(
_clock.setTimes(
whiteTime: state.options.whiteTime,
blackTime: state.options.whiteTime,
);
state = state.copyWith(
whiteTime: _clock.whiteTime,
blackTime: _clock.blackTime,
activeSide: Side.white,
loser: null,
flagged: null,
started: false,
paused: false,
whiteMoves: 0,
Expand Down Expand Up @@ -205,7 +206,7 @@ class ClockState with _$ClockState {
required ValueListenable<Duration> blackTime,
required Side activeSide,
@Default(Side.white) Side bottomPlayer,
Side? loser,
Side? flagged,
@Default(false) bool started,
@Default(false) bool paused,
@Default(0) int whiteMoves,
Expand All @@ -219,12 +220,12 @@ class ClockState with _$ClockState {
playerType == Side.white ? whiteMoves : blackMoves;

bool isPlayersTurn(Side playerType) =>
started && activeSide == playerType && loser == null;
started && activeSide == playerType && flagged == null;

bool isPlayersMoveAllowed(Side playerType) =>
isPlayersTurn(playerType) && !paused;

bool isActivePlayer(Side playerType) => isPlayersTurn(playerType) && !paused;

bool isLoser(Side playerType) => loser == playerType;
bool isFlagged(Side playerType) => flagged == playerType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import 'package:lichess_mobile/src/widgets/countdown_clock.dart';

import 'custom_clock_settings.dart';

class ClockScreen extends StatelessWidget {
const ClockScreen({super.key});
class ClockToolScreen extends StatelessWidget {
const ClockToolScreen({super.key});

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -79,7 +79,7 @@ class ClockTile extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final colorScheme = Theme.of(context).colorScheme;
final backgroundColor = clockState.isLoser(playerType)
final backgroundColor = clockState.isFlagged(playerType)
? context.lichessColors.error
: !clockState.paused && clockState.isPlayersTurn(playerType)
? colorScheme.primary
Expand Down Expand Up @@ -152,7 +152,7 @@ class ClockTile extends ConsumerWidget {
},
),
secondChild: const Icon(Icons.flag),
crossFadeState: clockState.isLoser(playerType)
crossFadeState: clockState.isFlagged(playerType)
? CrossFadeState.showSecond
: CrossFadeState.showFirst,
),
Expand Down
4 changes: 2 additions & 2 deletions lib/src/view/tools/tools_tab_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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_screen.dart';
import 'package:lichess_mobile/src/view/board_editor/board_editor_screen.dart';
import 'package:lichess_mobile/src/view/clock/clock_screen.dart';
import 'package:lichess_mobile/src/view/clock/clock_tool_screen.dart';
import 'package:lichess_mobile/src/view/coordinate_training/coordinate_training_screen.dart';
import 'package:lichess_mobile/src/view/opening_explorer/opening_explorer_screen.dart';
import 'package:lichess_mobile/src/view/study/study_list_screen.dart';
Expand Down Expand Up @@ -209,7 +209,7 @@ class _Body extends ConsumerWidget {
title: context.l10n.clock,
onTap: () => pushPlatformRoute(
context,
builder: (context) => const ClockScreen(),
builder: (context) => const ClockToolScreen(),
rootNavigator: true,
),
),
Expand Down
6 changes: 6 additions & 0 deletions lib/src/widgets/countdown_clock.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:lichess_mobile/src/utils/screen.dart';
class CountdownClock extends StatefulWidget {
const CountdownClock({
required this.timeLeft,
this.emergencyThreshold,
this.clockUpdatedAt,
required this.active,
this.clockStyle,
Expand All @@ -21,6 +22,10 @@ class CountdownClock extends StatefulWidget {
/// The duration left on the clock.
final Duration timeLeft;

/// If [timeLeft] is less than [emergencyThreshold], the clock will set
/// its background color to [ClockStyle.emergencyBackgroundColor].
final Duration? emergencyThreshold;

/// The time at which the clock was updated.
///
/// Use this parameter to synchronize the clock with the time at which the clock
Expand Down Expand Up @@ -122,6 +127,7 @@ class _CountdownClockState extends State<CountdownClock> {
return RepaintBoundary(
child: Clock(
padLeft: widget.padLeft,
emergencyThreshold: widget.emergencyThreshold,
timeLeft: timeLeft,
active: widget.active,
clockStyle: widget.clockStyle,
Expand Down

0 comments on commit 2cfc4bf

Please sign in to comment.