Skip to content

Commit

Permalink
Merge pull request #1283 from maxmitz/fix/1263-Puzzle-streak-reset-wh…
Browse files Browse the repository at this point in the history
…en-app-is-terminated

fix: 1263 puzzle streak reset when app is terminated
  • Loading branch information
veloce authored Dec 20, 2024
2 parents bd36c5f + b81d942 commit 5dec91a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
13 changes: 8 additions & 5 deletions lib/src/model/puzzle/puzzle_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,17 @@ class PuzzleController extends _$PuzzleController {
return nextPuzzle;
}

void loadPuzzle(PuzzleContext nextContext, {PuzzleStreak? nextStreak}) {
void onLoadPuzzle(PuzzleContext nextContext, {PuzzleStreak? nextStreak}) {
ref.read(evaluationServiceProvider).disposeEngine();

state = _loadNewContext(nextContext, nextStreak ?? state.streak);
_saveCurrentStreakLocally();
}

void saveStreakResultLocally() {
ref.read(streakStorageProvider(initialContext.userId)).saveActiveStreak(state.streak!);
void _saveCurrentStreakLocally() {
if (state.streak != null) {
ref.read(streakStorageProvider(initialContext.userId)).saveActiveStreak(state.streak!);
}
}

void _sendStreakResult() {
Expand Down Expand Up @@ -341,7 +344,7 @@ class PuzzleController extends _$PuzzleController {
if (next != null &&
result == PuzzleResult.win &&
ref.read(puzzlePreferencesProvider).autoNext) {
loadPuzzle(next);
onLoadPuzzle(next);
}
} else {
// one fail and streak is over
Expand All @@ -366,7 +369,7 @@ class PuzzleController extends _$PuzzleController {
if (nextContext != null) {
await Future<void>.delayed(const Duration(milliseconds: 250));
soundService.play(Sound.confirmation);
loadPuzzle(
onLoadPuzzle(
nextContext,
nextStreak: state.streak!.copyWith(index: state.streak!.index + 1),
);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/view/puzzle/puzzle_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ class _BottomBar extends ConsumerWidget {
BottomBarButton(
onTap:
puzzleState.mode == PuzzleMode.view && puzzleState.nextContext != null
? () => ref.read(ctrlProvider.notifier).loadPuzzle(puzzleState.nextContext!)
? () => ref.read(ctrlProvider.notifier).onLoadPuzzle(puzzleState.nextContext!)
: null,
highlighted: true,
label: context.l10n.puzzleContinueTraining,
Expand Down Expand Up @@ -533,7 +533,7 @@ class _DifficultySelector extends ConsumerWidget {
.read(ctrlProvider.notifier)
.changeDifficulty(selectedDifficulty);
if (context.mounted && nextContext != null) {
ref.read(ctrlProvider.notifier).loadPuzzle(nextContext);
ref.read(ctrlProvider.notifier).onLoadPuzzle(nextContext);
}
});
},
Expand Down
2 changes: 1 addition & 1 deletion lib/src/view/puzzle/puzzle_session_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class PuzzleSessionWidgetState extends ConsumerState<PuzzleSessionWidget> {
puzzle: puzzle,
);

ref.read(widget.ctrlProvider.notifier).loadPuzzle(nextContext);
ref.read(widget.ctrlProvider.notifier).onLoadPuzzle(nextContext);
} finally {
if (mounted) {
setState(() {
Expand Down
7 changes: 2 additions & 5 deletions lib/src/view/puzzle/streak_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,7 @@ class _Body extends ConsumerWidget {
(context) => YesNoDialog(
title: Text(context.l10n.mobileAreYouSure),
content: const Text('No worries, your score will be saved locally.'),
onYes: () {
ref.read(ctrlProvider.notifier).saveStreakResultLocally();
return Navigator.of(context).pop(true);
},
onYes: () => Navigator.of(context).pop(true),
onNo: () => Navigator.of(context).pop(false),
),
);
Expand Down Expand Up @@ -345,7 +342,7 @@ class _RetryFetchPuzzleDialog extends ConsumerWidget {
if (data != null) {
ref
.read(ctrlProvider.notifier)
.loadPuzzle(
.onLoadPuzzle(
data,
nextStreak: state.streak!.copyWith(index: state.streak!.index + 1),
);
Expand Down

0 comments on commit 5dec91a

Please sign in to comment.