Skip to content

Commit

Permalink
Merge pull request #1160 from tom-anders/soundOnMoveExpiration
Browse files Browse the repository at this point in the history
feat: play Sound.lowTime if less than 8 seconds to make first move
  • Loading branch information
veloce authored Nov 19, 2024
2 parents 5eeafc5 + 6a13b39 commit 9121006
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/src/view/game/game_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import 'package:cached_network_image/cached_network_image.dart';
import 'package:dartchess/dartchess.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lichess_mobile/src/constants.dart';
import 'package:lichess_mobile/src/model/common/service/sound_service.dart';
import 'package:lichess_mobile/src/model/game/material_diff.dart';
import 'package:lichess_mobile/src/model/game/player.dart';
import 'package:lichess_mobile/src/styles/lichess_colors.dart';
Expand Down Expand Up @@ -257,7 +259,7 @@ class ConfirmMove extends StatelessWidget {
}
}

class MoveExpiration extends StatefulWidget {
class MoveExpiration extends ConsumerStatefulWidget {
const MoveExpiration({
required this.timeToMove,
required this.mePlaying,
Expand All @@ -268,13 +270,14 @@ class MoveExpiration extends StatefulWidget {
final bool mePlaying;

@override
State<MoveExpiration> createState() => _MoveExpirationState();
ConsumerState<MoveExpiration> createState() => _MoveExpirationState();
}

class _MoveExpirationState extends State<MoveExpiration> {
class _MoveExpirationState extends ConsumerState<MoveExpiration> {
static const _period = Duration(milliseconds: 1000);
Timer? _timer;
Duration timeLeft = Duration.zero;
bool playedEmergencySound = false;

Timer startTimer() {
return Timer.periodic(_period, (timer) {
Expand Down Expand Up @@ -312,6 +315,14 @@ class _MoveExpirationState extends State<MoveExpiration> {
Widget build(BuildContext context) {
final secs = timeLeft.inSeconds.remainder(60);
final emerg = timeLeft <= const Duration(seconds: 8);

if (emerg && widget.mePlaying && !playedEmergencySound) {
ref.read(soundServiceProvider).play(Sound.lowTime);
setState(() {
playedEmergencySound = true;
});
}

return secs <= 20
? Text(
context.l10n.nbSecondsToPlayTheFirstMove(secs),
Expand Down

0 comments on commit 9121006

Please sign in to comment.