Skip to content
Open
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
29 changes: 14 additions & 15 deletions lib/src/model/common/service/move_feedback.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ import 'package:lichess_mobile/src/model/settings/board_preferences.dart';
/// A provider for [MoveFeedbackService].
final moveFeedbackServiceProvider = Provider<MoveFeedbackService>((Ref ref) {
final soundService = ref.watch(soundServiceProvider);
return MoveFeedbackService(soundService, ref);
final hasHapticFeedback = ref.watch(
boardPreferencesProvider.select((prefs) => prefs.hapticFeedback),
);
return MoveFeedbackService(soundService, hasHapticFeedback: hasHapticFeedback);
}, name: 'MoveFeedbackServiceProvider');

class MoveFeedbackService {
MoveFeedbackService(this._soundService, this._ref);
MoveFeedbackService(this._soundService, {required this.hasHapticFeedback});

final SoundService _soundService;
final Ref _ref;
final bool hasHapticFeedback;

void moveFeedback({bool check = false}) {
_soundService.play(Sound.move);

if (_ref.read(boardPreferencesProvider).hapticFeedback) {
void _playHapticFeedback({required bool check}) {
if (hasHapticFeedback) {
if (check) {
HapticFeedback.mediumImpact();
} else {
Expand All @@ -28,15 +29,13 @@ class MoveFeedbackService {
}
}

void moveFeedback({bool check = false}) {
_soundService.play(Sound.move);
_playHapticFeedback(check: check);
}

void captureFeedback(Variant variant, {bool check = false}) {
_soundService.playCaptureSound(variant);

if (_ref.read(boardPreferencesProvider).hapticFeedback) {
if (check) {
HapticFeedback.mediumImpact();
} else {
HapticFeedback.lightImpact();
}
}
_playHapticFeedback(check: check);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CoordinateTrainingController extends Notifier<CoordinateTrainingState> {
CoordinateTrainingState build() {
ref.onDispose(() {
_updateTimer?.cancel();
_stopwatch.stop();
});
final sideChoice = ref.watch(
coordinateTrainingPreferencesProvider.select((value) => value.sideChoice),
Expand Down Expand Up @@ -60,19 +61,19 @@ class CoordinateTrainingController extends Notifier<CoordinateTrainingState> {

void _finishTraining() {
// TODO save score in local storage here (and display high score and/or average score in UI)
final orientation = _getOrientation(ref.read(coordinateTrainingPreferencesProvider).sideChoice);
_updateTimer?.cancel();
_stopwatch.stop();
state = CoordinateTrainingState(
lastGuess: state.lastGuess,
lastScore: state.score,
orientation: orientation,
orientation: state.orientation,
);
}

void abortTraining() {
final orientation = _getOrientation(ref.read(coordinateTrainingPreferencesProvider).sideChoice);
_updateTimer?.cancel();
state = CoordinateTrainingState(orientation: orientation);
_stopwatch.stop();
state = CoordinateTrainingState(orientation: state.orientation);
}

Side _getOrientation(SideChoice choice) => switch (choice) {
Expand Down