11import 'dart:ui' ;
22
3+ import 'package:dbus/dbus.dart' ;
34import 'package:gsettings/gsettings.dart' ;
45
56class 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
2124class 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}
0 commit comments