1
1
import 'dart:ui' ;
2
2
3
+ import 'package:dbus/dbus.dart' ;
3
4
import 'package:gsettings/gsettings.dart' ;
4
5
5
6
class SettingsService {
6
7
final _settings = < String , Settings ? > {};
7
8
8
9
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
+ }
12
15
}
13
16
14
17
void dispose () {
@@ -20,9 +23,16 @@ class SettingsService {
20
23
21
24
class Settings {
22
25
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
+ }
24
33
25
34
final GSettings _settings;
35
+ final _values = < String , dynamic > {};
26
36
final _listeners = < VoidCallback > {};
27
37
28
38
void addListener (VoidCallback listener) => _listeners.add (listener);
@@ -33,7 +43,7 @@ class Settings {
33
43
}
34
44
}
35
45
36
- void dispose () => _settings.dispose ();
46
+ void dispose () => _settings.close ();
37
47
38
48
bool ? boolValue (String key) => getValue <bool >(key);
39
49
int ? intValue (String key) => getValue <int >(key);
@@ -42,9 +52,44 @@ class Settings {
42
52
Iterable <String >? stringArrayValue (String key) =>
43
53
getValue <Iterable >(key)? .cast <String >();
44
54
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
+ }
48
91
49
- void sync () => _settings.sync ();
92
+ Future <void > resetValue (String key) {
93
+ return _settings.setAll (< String , DBusValue ? > {key: null });
94
+ }
50
95
}
0 commit comments