forked from lichess-org/mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuzzle_preferences.dart
More file actions
61 lines (48 loc) · 2 KB
/
puzzle_preferences.dart
File metadata and controls
61 lines (48 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/puzzle/puzzle_difficulty.dart';
import 'package:lichess_mobile/src/model/settings/preferences_storage.dart';
import 'package:lichess_mobile/src/model/user/user.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'puzzle_preferences.freezed.dart';
part 'puzzle_preferences.g.dart';
@Riverpod(keepAlive: true)
class PuzzlePreferences extends _$PuzzlePreferences with SessionPreferencesStorage<PuzzlePrefs> {
// ignore: avoid_public_notifier_properties
@override
final prefCategory = PrefCategory.puzzle;
@override
PuzzlePrefs defaults({LightUser? user}) => PuzzlePrefs.defaults(id: user?.id);
@override
PuzzlePrefs fromJson(Map<String, dynamic> json) => PuzzlePrefs.fromJson(json);
@override
PuzzlePrefs build() {
return fetch();
}
Future<void> setDifficulty(PuzzleDifficulty difficulty) async {
save(state.copyWith(difficulty: difficulty));
}
Future<void> setAutoNext(bool autoNext) async {
save(state.copyWith(autoNext: autoNext));
}
Future<void> setRated(bool rated) async {
save(state.copyWith(rated: rated));
}
}
@Freezed(fromJson: true, toJson: true)
class PuzzlePrefs with _$PuzzlePrefs implements Serializable {
const factory PuzzlePrefs({
required UserId? id,
required PuzzleDifficulty difficulty,
/// If `true`, will show next puzzle after successful completion. This has
/// no effect on puzzle streaks, which always show next puzzle. Defaults to
/// `false`.
@Default(false) bool autoNext,
/// If `true`, the puzzle will be rated for logged in users.
/// Defaults to `true`.
@Default(true) bool rated,
}) = _PuzzlePrefs;
factory PuzzlePrefs.defaults({UserId? id}) =>
PuzzlePrefs(id: id, difficulty: PuzzleDifficulty.normal, autoNext: false, rated: true);
factory PuzzlePrefs.fromJson(Map<String, dynamic> json) => _$PuzzlePrefsFromJson(json);
}