Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to GSettings 0.2 #191

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 54 additions & 9 deletions lib/services/settings_service.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import 'dart:ui';

import 'package:dbus/dbus.dart';
import 'package:gsettings/gsettings.dart';

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

Settings? lookup(String schemaId, {String? path}) {
return _settings[schemaId] ??= GSettingsSchema.lookup(schemaId) != null
? Settings(schemaId, path: path)
: null;
try {
return _settings[schemaId] ??= Settings(schemaId, path: path);
} on GSettingsSchemaNotInstalledException catch (_) {
return null;
}
}

void dispose() {
Expand All @@ -20,9 +23,16 @@ class SettingsService {

class Settings {
Settings(String schemaId, {String? path})
: _settings = GSettings(schemaId: schemaId, path: path);
: _settings = GSettings(schemaId, path: path) {
_settings.keysChanged.listen((keys) {
for (final key in keys) {
_updateValue(key);
}
});
}

final GSettings _settings;
final _values = <String, dynamic>{};
final _listeners = <VoidCallback>{};

void addListener(VoidCallback listener) => _listeners.add(listener);
Expand All @@ -33,7 +43,7 @@ class Settings {
}
}

void dispose() => _settings.dispose();
void dispose() => _settings.close();

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

T? getValue<T>(String key) => _settings.value(key) as T?;
void setValue<T>(String key, Object value) => _settings.setValue(key, value);
void resetValue(String key) => _settings.resetValue(key);
T? getValue<T>(String key) => _values[key] ?? _updateValue(key);

T? _updateValue<T>(String key) {
try {
_settings.get(key).then((v) {
final value = v.toNative() as T?;
if (_values[key] != value) {
_values[key] = value;
notifyListeners();
}
});
} on GSettingsUnknownKeyException catch (_) {}
}

Future<void> setValue<T>(String key, T value) async {
if (_values[key] == key) {
return;
}
_values[key] = value;
switch (T) {
case bool:
return _settings.set(key, DBusBoolean(value as bool));
case int:
return _settings.set(key, DBusInt32(value as int));
case double:
return _settings.set(key, DBusDouble(value as double));
case String:
return _settings.set(key, DBusString(value as String));
default:
break;
}
if (value is List<String>) {
return _settings.set(key, DBusArray.string(value));
}
throw UnsupportedError('Unsupported type: $T');
}

void sync() => _settings.sync();
Future<void> resetValue(String key) {
return _settings.setAll(<String, DBusValue?>{key: null});
}
}
1 change: 0 additions & 1 deletion lib/view/app_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class AppTheme extends ValueNotifier<ThemeMode> {
_settings.setValue('gtk-theme', 'Yaru');
break;
}
_settings.sync();
}

@override
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies:
path: package
flutter:
sdk: flutter
gsettings: ^0.1.2+1
gsettings: ^0.2.3
linux_system_info: ^0.0.7
mime: ^1.0.0
bluez: ^0.7.4
Expand Down
4 changes: 2 additions & 2 deletions test/widgets/app_theme_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void main() {
AppTheme theme = AppTheme(settings);

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

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

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

theme.apply(Brightness.light);
Expand Down
18 changes: 9 additions & 9 deletions test/widgets/app_theme_test.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// in settings/test/widgets/app_theme_test.dart.
// Do not manually edit this file.

import 'dart:async' as _i4;
import 'dart:ui' as _i3;

import 'package:mockito/mockito.dart' as _i1;
Expand Down Expand Up @@ -59,16 +60,15 @@ class MockSettings extends _i1.Mock implements _i2.Settings {
T? getValue<T>(String? key) =>
(super.noSuchMethod(Invocation.method(#getValue, [key])) as T?);
@override
void setValue<T>(String? key, Object? value) =>
super.noSuchMethod(Invocation.method(#setValue, [key, value]),
returnValueForMissingStub: null);
@override
void resetValue(String? key) =>
super.noSuchMethod(Invocation.method(#resetValue, [key]),
returnValueForMissingStub: null);
_i4.Future<void> setValue<T>(String? key, T? value) =>
(super.noSuchMethod(Invocation.method(#setValue, [key, value]),
returnValue: Future<void>.value(),
returnValueForMissingStub: Future<void>.value()) as _i4.Future<void>);
@override
void sync() => super.noSuchMethod(Invocation.method(#sync, []),
returnValueForMissingStub: null);
_i4.Future<void> resetValue(String? key) =>
(super.noSuchMethod(Invocation.method(#resetValue, [key]),
returnValue: Future<void>.value(),
returnValueForMissingStub: Future<void>.value()) as _i4.Future<void>);
@override
String toString() => super.toString();
}