/
preferences.dart
170 lines (136 loc) · 4.75 KB
/
preferences.dart
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
abstract class Preference<T> extends ChangeNotifier {
final T defaultValue;
final String key;
/// The value of this preference.
///
/// Subclasses must initialize this in their constructors.
late T _value;
Preference({required this.key, required this.defaultValue}) {
_value = defaultValue;
_initializeFromUserPreferences();
}
_initializeFromUserPreferences() async {
var prefs = await SharedPreferences.getInstance();
if (prefs.containsKey(key)) {
value = _loadFrom(prefs);
}
}
/// Read the value of this preference from the given preferences object.
///
/// When this is called, the presence of the key has already been checked.
T _loadFrom(SharedPreferences prefs);
/// Reset this preference to its default value.
reset({bool notifyListeners = true}) {
// If we should notify listeners, simply call the mutator method.
if (notifyListeners) {
value = defaultValue;
}
// Otherwise, set the value and update prefs, but do not notify
else {
_value = defaultValue;
_updatePrefs();
}
}
T get value => _value;
set value(T value) {
_value = value;
notifyListeners();
_updatePrefs();
}
void _updatePrefs() async {
var preferences = await SharedPreferences.getInstance();
// If the user's preference is not the default value, record it.
// Otherwise, just clear it from storage, because the default is used.
if (value != defaultValue) {
_writeValue(preferences);
} else {
preferences.remove(key);
}
}
/// Write this preference's value to the preferences store.
_writeValue(SharedPreferences prefs);
}
class BoolPreference extends Preference<bool> {
BoolPreference({required key, required defaultValue})
: super(key: key, defaultValue: defaultValue);
@override
bool _loadFrom(SharedPreferences prefs) => prefs.getBool(key)!;
@override
_writeValue(SharedPreferences prefs) => prefs.setBool(key, value);
}
class IntPreference extends Preference<int> {
IntPreference({required key, required defaultValue})
: super(key: key, defaultValue: defaultValue);
@override
int _loadFrom(SharedPreferences prefs) => prefs.getInt(key)!;
@override
_writeValue(SharedPreferences prefs) => prefs.setInt(key, value);
}
class DoublePreference extends Preference<double> {
DoublePreference({required key, required defaultValue})
: super(key: key, defaultValue: defaultValue);
@override
double _loadFrom(SharedPreferences prefs) => prefs.getDouble(key)!;
@override
_writeValue(SharedPreferences prefs) => prefs.setDouble(key, value);
}
class StringPreference extends Preference<String> {
StringPreference({required key, required defaultValue})
: super(key: key, defaultValue: defaultValue);
@override
String _loadFrom(SharedPreferences prefs) => prefs.getString(key)!;
@override
_writeValue(SharedPreferences prefs) => prefs.setString(key, value);
}
class BrightnessPreference extends Preference<Brightness> {
BrightnessPreference({required key, required defaultValue})
: super(key: key, defaultValue: defaultValue);
@override
Brightness _loadFrom(SharedPreferences prefs) =>
prefs.getBool(key)! ? Brightness.light : Brightness.dark;
@override
_writeValue(SharedPreferences prefs) =>
prefs.setBool(key, value == Brightness.light);
}
class StringSetPreference extends Preference<Set<String>> {
StringSetPreference({required key}) : super(key: key, defaultValue: Set());
/// Get the list of excluded names.
///
/// This could be simply `_value`, but this alias increases readability.
Set<String> get _excludedNames => _value;
bool includes(String name) => !excludes(name);
bool excludes(String name) => _excludedNames.contains(name);
void exclude(String name) {
bool isNewExclusion = _excludedNames.add(name);
if (isNewExclusion) {
_updatePrefs();
notifyListeners();
}
}
void include(String name) {
bool isNewInclusion = _excludedNames.remove(name);
if (isNewInclusion) {
_updatePrefs();
notifyListeners();
}
}
void _updatePrefs() async {
var preferences = await SharedPreferences.getInstance();
// If the user's preference is not the default value, record it.
// Otherwise, just clear it from storage, because the default is used.
if (_excludedNames.isEmpty) {
preferences.remove(key);
} else {
preferences.setStringList(key, _excludedNames.toList());
}
}
@override
Set<String> _loadFrom(SharedPreferences prefs) =>
prefs.getStringList(key)!.toSet();
@override
_writeValue(SharedPreferences prefs) =>
prefs.setStringList(key, _excludedNames.toList());
}