Skip to content

Commit 5c88847

Browse files
committed
settings: Avoid redundant updates in setBool
1 parent 0adf54b commit 5c88847

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

lib/model/settings.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,13 @@ class GlobalSettingsStore extends ChangeNotifier {
376376
/// A value of null means the setting will revert to following
377377
/// the app's default.
378378
///
379+
/// If [value] equals the setting's current value, the database operation
380+
/// and [notifyListeners] are skipped.
381+
///
379382
/// See also [getBool].
380383
Future<void> setBool(BoolGlobalSetting setting, bool? value) async {
384+
if (value == _boolData[setting]) return;
385+
381386
await _backend.doSetBoolGlobalSetting(setting, value);
382387
if (value == null) {
383388
_boolData.remove(setting);

test/model/settings_test.dart

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,66 @@ void main() {
125125
.isFalse();
126126
assert(!BoolGlobalSetting.placeholderIgnore.default_);
127127
});
128+
129+
group('set avoids redundant updates', () {
130+
int notifiedCount = 0;
131+
tearDown(() => notifiedCount = 0);
132+
133+
test('null to null -> no update', () async {
134+
final globalSettings = eg.globalStore(boolGlobalSettings: {}).settings;
135+
globalSettings.addListener(() => notifiedCount++);
136+
137+
await globalSettings.setBool(BoolGlobalSetting.placeholderIgnore, null);
138+
check(notifiedCount).equals(0);
139+
});
140+
141+
test('true to true -> no update', () async {
142+
final globalSettings = eg.globalStore(boolGlobalSettings: {
143+
BoolGlobalSetting.placeholderIgnore: true,
144+
}).settings;
145+
globalSettings.addListener(() => notifiedCount++);
146+
147+
await globalSettings.setBool(BoolGlobalSetting.placeholderIgnore, true);
148+
check(notifiedCount).equals(0);
149+
});
150+
151+
test('false to false -> no update', () async {
152+
final globalSettings = eg.globalStore(boolGlobalSettings: {
153+
BoolGlobalSetting.placeholderIgnore: false,
154+
}).settings;
155+
globalSettings.addListener(() => notifiedCount++);
156+
157+
await globalSettings.setBool(BoolGlobalSetting.placeholderIgnore, false);
158+
check(notifiedCount).equals(0);
159+
});
160+
161+
test('null to false -> does the update', () async {
162+
final globalSettings = eg.globalStore(boolGlobalSettings: {}).settings;
163+
globalSettings.addListener(() => notifiedCount++);
164+
165+
await globalSettings.setBool(BoolGlobalSetting.placeholderIgnore, false);
166+
check(notifiedCount).equals(1);
167+
});
168+
169+
test('true to null -> does the update', () async {
170+
final globalSettings = eg.globalStore(boolGlobalSettings: {
171+
BoolGlobalSetting.placeholderIgnore: true,
172+
}).settings;
173+
globalSettings.addListener(() => notifiedCount++);
174+
175+
await globalSettings.setBool(BoolGlobalSetting.placeholderIgnore, null);
176+
check(notifiedCount).equals(1);
177+
});
178+
179+
test('false to true -> does the update', () async {
180+
final globalSettings = eg.globalStore(boolGlobalSettings: {
181+
BoolGlobalSetting.placeholderIgnore: false,
182+
}).settings;
183+
globalSettings.addListener(() => notifiedCount++);
184+
185+
await globalSettings.setBool(BoolGlobalSetting.placeholderIgnore, true);
186+
check(notifiedCount).equals(1);
187+
});
188+
});
128189
});
129190
}

0 commit comments

Comments
 (0)