Skip to content

Commit 229e0bf

Browse files
authored
Migrate to GSettings 0.2 (#191)
Closes: #183
1 parent 1a926e7 commit 229e0bf

File tree

5 files changed

+66
-22
lines changed

5 files changed

+66
-22
lines changed

lib/services/settings_service.dart

+54-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import 'dart:ui';
22

3+
import 'package:dbus/dbus.dart';
34
import 'package:gsettings/gsettings.dart';
45

56
class SettingsService {
67
final _settings = <String, Settings?>{};
78

89
Settings? lookup(String schemaId, {String? path}) {
9-
return _settings[schemaId] ??= GSettingsSchema.lookup(schemaId) != null
10-
? Settings(schemaId, path: path)
11-
: null;
10+
try {
11+
return _settings[schemaId] ??= Settings(schemaId, path: path);
12+
} on GSettingsSchemaNotInstalledException catch (_) {
13+
return null;
14+
}
1215
}
1316

1417
void dispose() {
@@ -20,9 +23,16 @@ class SettingsService {
2023

2124
class Settings {
2225
Settings(String schemaId, {String? path})
23-
: _settings = GSettings(schemaId: schemaId, path: path);
26+
: _settings = GSettings(schemaId, path: path) {
27+
_settings.keysChanged.listen((keys) {
28+
for (final key in keys) {
29+
_updateValue(key);
30+
}
31+
});
32+
}
2433

2534
final GSettings _settings;
35+
final _values = <String, dynamic>{};
2636
final _listeners = <VoidCallback>{};
2737

2838
void addListener(VoidCallback listener) => _listeners.add(listener);
@@ -33,7 +43,7 @@ class Settings {
3343
}
3444
}
3545

36-
void dispose() => _settings.dispose();
46+
void dispose() => _settings.close();
3747

3848
bool? boolValue(String key) => getValue<bool>(key);
3949
int? intValue(String key) => getValue<int>(key);
@@ -42,9 +52,44 @@ class Settings {
4252
Iterable<String>? stringArrayValue(String key) =>
4353
getValue<Iterable>(key)?.cast<String>();
4454

45-
T? getValue<T>(String key) => _settings.value(key) as T?;
46-
void setValue<T>(String key, Object value) => _settings.setValue(key, value);
47-
void resetValue(String key) => _settings.resetValue(key);
55+
T? getValue<T>(String key) => _values[key] ?? _updateValue(key);
56+
57+
T? _updateValue<T>(String key) {
58+
try {
59+
_settings.get(key).then((v) {
60+
final value = v.toNative() as T?;
61+
if (_values[key] != value) {
62+
_values[key] = value;
63+
notifyListeners();
64+
}
65+
});
66+
} on GSettingsUnknownKeyException catch (_) {}
67+
}
68+
69+
Future<void> setValue<T>(String key, T value) async {
70+
if (_values[key] == key) {
71+
return;
72+
}
73+
_values[key] = value;
74+
switch (T) {
75+
case bool:
76+
return _settings.set(key, DBusBoolean(value as bool));
77+
case int:
78+
return _settings.set(key, DBusInt32(value as int));
79+
case double:
80+
return _settings.set(key, DBusDouble(value as double));
81+
case String:
82+
return _settings.set(key, DBusString(value as String));
83+
default:
84+
break;
85+
}
86+
if (value is List<String>) {
87+
return _settings.set(key, DBusArray.string(value));
88+
}
89+
throw UnsupportedError('Unsupported type: $T');
90+
}
4891

49-
void sync() => _settings.sync();
92+
Future<void> resetValue(String key) {
93+
return _settings.setAll(<String, DBusValue?>{key: null});
94+
}
5095
}

lib/view/app_theme.dart

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class AppTheme extends ValueNotifier<ThemeMode> {
1717
_settings.setValue('gtk-theme', 'Yaru');
1818
break;
1919
}
20-
_settings.sync();
2120
}
2221

2322
@override

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies:
1818
path: package
1919
flutter:
2020
sdk: flutter
21-
gsettings: ^0.1.2+1
21+
gsettings: ^0.2.3
2222
linux_system_info: ^0.0.7
2323
mime: ^1.0.0
2424
bluez: ^0.7.4

test/widgets/app_theme_test.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void main() {
1515
AppTheme theme = AppTheme(settings);
1616

1717
when(settings.setValue('gtk-theme', 'Yaru-dark')).thenAnswer(
18-
(realInvocation) => true,
18+
(realInvocation) async {},
1919
);
2020

2121
theme.apply(Brightness.dark);
@@ -30,7 +30,7 @@ void main() {
3030
AppTheme theme = AppTheme(settings);
3131

3232
when(settings.setValue('gtk-theme', 'Yaru')).thenAnswer(
33-
(realInvocation) => true,
33+
(realInvocation) async {},
3434
);
3535

3636
theme.apply(Brightness.light);

test/widgets/app_theme_test.mocks.dart

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// in settings/test/widgets/app_theme_test.dart.
33
// Do not manually edit this file.
44

5+
import 'dart:async' as _i4;
56
import 'dart:ui' as _i3;
67

78
import 'package:mockito/mockito.dart' as _i1;
@@ -59,16 +60,15 @@ class MockSettings extends _i1.Mock implements _i2.Settings {
5960
T? getValue<T>(String? key) =>
6061
(super.noSuchMethod(Invocation.method(#getValue, [key])) as T?);
6162
@override
62-
void setValue<T>(String? key, Object? value) =>
63-
super.noSuchMethod(Invocation.method(#setValue, [key, value]),
64-
returnValueForMissingStub: null);
65-
@override
66-
void resetValue(String? key) =>
67-
super.noSuchMethod(Invocation.method(#resetValue, [key]),
68-
returnValueForMissingStub: null);
63+
_i4.Future<void> setValue<T>(String? key, T? value) =>
64+
(super.noSuchMethod(Invocation.method(#setValue, [key, value]),
65+
returnValue: Future<void>.value(),
66+
returnValueForMissingStub: Future<void>.value()) as _i4.Future<void>);
6967
@override
70-
void sync() => super.noSuchMethod(Invocation.method(#sync, []),
71-
returnValueForMissingStub: null);
68+
_i4.Future<void> resetValue(String? key) =>
69+
(super.noSuchMethod(Invocation.method(#resetValue, [key]),
70+
returnValue: Future<void>.value(),
71+
returnValueForMissingStub: Future<void>.value()) as _i4.Future<void>);
7272
@override
7373
String toString() => super.toString();
7474
}

0 commit comments

Comments
 (0)