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

Adding a dispose into services to close/dispose all resources #303

Merged
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
3 changes: 1 addition & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ void main() async {
),
Provider<DisplayService>(
create: (_) => DisplayService(),
//TODO
//dispose: (_, DisplayService service) => service.dispose(),
dispose: (_, DisplayService service) => service.dispose(),
),
],
child: const UbuntuSettingsApp(),
Expand Down
4 changes: 4 additions & 0 deletions lib/services/display/display_dbus_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class DisplayDBusService {
DBusObjectPath(_displayPath),
);

Future<void> dispose() async {
await _object.client.close();
}

Future<DBusDisplaysConfig> getCurrent() async {
List<DBusValue>? state = await _object.callGetCurrentState();
List<dynamic> list = state.map((e) => _toNative(e)).toList();
Expand Down
18 changes: 14 additions & 4 deletions lib/services/display/display_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class DisplayService {
: _displayDBusService = DisplayDBusService(),
_currentNotifier = ValueNotifier(null),
_initialNotifier = ValueNotifier(null) {

_loadState(notifyStream: true).then((DisplaysConfiguration value) {
_initialNotifier.value = value;
_currentNotifier.value = value;
Expand All @@ -34,15 +33,26 @@ class DisplayService {

/// NOTIFIERS
final ValueNotifier<DisplaysConfiguration?> _initialNotifier;

ValueNotifier<DisplaysConfiguration?> get initialNotifier => _initialNotifier;

final ValueNotifier<DisplaysConfiguration?> _currentNotifier;

ValueNotifier<DisplaysConfiguration?> get currentNotifier => _currentNotifier;

Future<void> dispose() async {
_initialNotifier.dispose();
_currentNotifier.dispose();
await Future.wait([
_displayDBusService.dispose(),
_monitorStateStreamController.close(),
]);
}

Future<void> applyConfig() async {
/// do nothing if no way to perform applyConfig (made to be safe, but we may
/// never pass here)
if(_currentNotifier.value == null){}
if (_currentNotifier.value == null) {}

final DBusDisplaysConfig displayConfig =
await _displayDBusService.getCurrent();
Expand All @@ -51,7 +61,7 @@ class DisplayService {

for (int i = 0; i < displayConfig.monitorsLength; i++) {
final DisplayMonitorConfiguration confMonitor =
_currentNotifier.value!.configurations[i];
_currentNotifier.value!.configurations[i];

// x ; y ; scale ; transform(rotation) ; primary ; monitors
logicalParameterValues.add(DBusStruct([
Expand Down Expand Up @@ -104,7 +114,7 @@ class DisplayService {
/// if no current option
/// => monitor not used
/// => monitor ignored and not displayed
if(dbusConfiguration.currentOption(i) != null){
if (dbusConfiguration.currentOption(i) != null) {
confs.add(
DisplayMonitorConfiguration.newConstructor(dbusConfiguration, i));
}
Expand Down