Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/src/model/account/account_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ typedef AccountPrefState = ({
SubmitMove submitMove,
// clock
Moretime moretime,
BooleanPref clockSound,
// privacy
BooleanPref follow,
});
Expand All @@ -34,6 +35,13 @@ final showRatingsPrefProvider = FutureProvider<bool>((ref) async {
);
});

final clockSoundProvider = FutureProvider<bool>((ref) async {
return ref.watch(
accountPreferencesProvider
.selectAsync((state) => state?.clockSound.value ?? true),
);
});

final defaultAccountPreferences = (
zenMode: Zen.no,
showRatings: const BooleanPref(true),
Expand All @@ -42,6 +50,7 @@ final defaultAccountPreferences = (
autoThreefold: AutoThreefold.always,
takeback: Takeback.always,
moretime: Moretime.always,
clockSound: const BooleanPref(true),
confirmResign: const BooleanPref(true),
submitMove: SubmitMove({
SubmitMoveChoice.correspondence,
Expand Down Expand Up @@ -84,6 +93,8 @@ class AccountPreferences extends _$AccountPreferences {
Future<void> setAutoThreefold(AutoThreefold value) =>
_setPref('autoThreefold', value);
Future<void> setMoretime(Moretime value) => _setPref('moretime', value);
Future<void> setClockSound(BooleanPref value) =>
_setPref('clockSound', value);
Future<void> setConfirmResign(BooleanPref value) =>
_setPref('confirmResign', value);
Future<void> setSubmitMove(SubmitMove value) => _setPref('submitMove', value);
Expand Down
1 change: 1 addition & 0 deletions lib/src/model/account/account_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ AccountPrefState _accountPreferencesFromPick(RequiredPick pick) {
moretime: Moretime.fromInt(
pick('moretime').asIntOrThrow(),
),
clockSound: BooleanPref(pick('clockSound').asBoolOrThrow()),
confirmResign: BooleanPref.fromInt(
pick('confirmResign').asIntOrThrow(),
),
Expand Down
7 changes: 7 additions & 0 deletions lib/src/view/game/game_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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/account/account_preferences.dart';
import 'package:lichess_mobile/src/model/account/account_repository.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/common/speed.dart';
Expand Down Expand Up @@ -104,6 +105,10 @@ class GameBody extends ConsumerWidget {
);

final boardPreferences = ref.watch(boardPreferencesProvider);
final emergencySoundEnabled = ref.watch(clockSoundProvider).maybeWhen(
data: (clockSound) => clockSound,
orElse: () => true,
);

final blindfoldMode = ref.watch(
gamePreferencesProvider.select(
Expand Down Expand Up @@ -152,6 +157,7 @@ class GameBody extends ConsumerWidget {
emergencyThreshold: youAre == Side.black
? gameState.game.meta.clock?.emergency
: null,
emergencySoundEnabled: emergencySoundEnabled,
onFlag: () => ref.read(ctrlProvider.notifier).onFlag(),
)
: gameState.game.correspondenceClock != null
Expand Down Expand Up @@ -192,6 +198,7 @@ class GameBody extends ConsumerWidget {
emergencyThreshold: youAre == Side.white
? gameState.game.meta.clock?.emergency
: null,
emergencySoundEnabled: emergencySoundEnabled,
onFlag: () => ref.read(ctrlProvider.notifier).onFlag(),
)
: gameState.game.correspondenceClock != null
Expand Down
14 changes: 14 additions & 0 deletions lib/src/view/settings/account_preferences_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,20 @@ class _AccountPreferencesScreenState
}
},
),
SwitchSettingTile(
title:
Text(context.l10n.preferencesSoundWhenTimeGetsCritical),
value: data.clockSound.value,
onChanged: isLoading
? null
: (value) {
_setPref(
() => ref
.read(accountPreferencesProvider.notifier)
.setClockSound(BooleanPref(value)),
);
},
),
],
),
ListSection(
Expand Down
8 changes: 7 additions & 1 deletion lib/src/widgets/countdown_clock.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ class CountdownClock extends ConsumerStatefulWidget {

/// If [timeLeft] is less than [emergencyThreshold], the clock will change
/// its background color to [ClockStyle.emergencyBackgroundColor] activeBackgroundColor
/// If [emergencySoundEnabled] is `true`, the clock will also play a sound.
final Duration? emergencyThreshold;

final bool emergencySoundEnabled;

/// If [active] is `true`, the clock starts counting down.
final bool active;

Expand All @@ -38,6 +41,7 @@ class CountdownClock extends ConsumerStatefulWidget {
required this.duration,
required this.active,
this.emergencyThreshold,
this.emergencySoundEnabled = true,
this.onFlag,
this.onStop,
this.lightColorStyle,
Expand Down Expand Up @@ -93,7 +97,9 @@ class _CountdownClockState extends ConsumerState<CountdownClock> {
(_nextEmergency == null || _nextEmergency!.isBefore(DateTime.now()))) {
_shouldPlayEmergencyFeedback = false;
_nextEmergency = DateTime.now().add(_emergencyDelay);
ref.read(soundServiceProvider).play(Sound.lowTime);
if (widget.emergencySoundEnabled) {
ref.read(soundServiceProvider).play(Sound.lowTime);
}
HapticFeedback.heavyImpact();
} else if (widget.emergencyThreshold != null &&
timeLeft > widget.emergencyThreshold! * 1.5) {
Expand Down