switchTimer method
void
switchTimer(
{ - bool stop = false,
})
Implementation
void switchTimer({bool stop = false}) {
_timer?.cancel();
if (stop) {
_stopwatchWhite.stop();
_stopwatchBlack.stop();
return;
}
void startPlayerTimer(
Stopwatch stopwatch,
Stopwatch otherStopwatch,
StreamController<int> controller,
int Function() remainingTime,
) {
stopwatch.start();
otherStopwatch.stop();
_timer = Timer.periodic(Duration(milliseconds: 100), (_) {
final timeLeft = remainingTime();
if (timeLeft <= 0) {
controller.add(0);
_timer?.cancel();
stopwatch.stop();
if (onTimeOut != null) {
switchTimer(stop: true);
onTimeOut!();
}
} else {
controller.add(timeLeft);
}
});
}
if (turn == PieceColor.white) {
startPlayerTimer(
_stopwatchWhite,
_stopwatchBlack,
_whiteTimeController,
() => _whiteRemainingTime,
);
} else if (turn == PieceColor.black) {
startPlayerTimer(
_stopwatchBlack,
_stopwatchWhite,
_blackTimeController,
() => _blackRemainingTime,
);
}
}