From 55f286e230454eda6528e91a4fbd94a5fbb73693 Mon Sep 17 00:00:00 2001 From: atsen-dev <32575538+atsen-dev@users.noreply.github.com> Date: Sun, 13 Mar 2022 08:44:16 +0100 Subject: [PATCH] Adding a dispose into services to close/dispose all resources (#303) --- lib/main.dart | 3 +-- lib/services/display/display_dbus_service.dart | 4 ++++ lib/services/display/display_service.dart | 18 ++++++++++++++---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index f87e8832..86618bae 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -83,8 +83,7 @@ void main() async { ), Provider( create: (_) => DisplayService(), - //TODO - //dispose: (_, DisplayService service) => service.dispose(), + dispose: (_, DisplayService service) => service.dispose(), ), ], child: const UbuntuSettingsApp(), diff --git a/lib/services/display/display_dbus_service.dart b/lib/services/display/display_dbus_service.dart index 6befb8e0..5b137edf 100644 --- a/lib/services/display/display_dbus_service.dart +++ b/lib/services/display/display_dbus_service.dart @@ -45,6 +45,10 @@ class DisplayDBusService { DBusObjectPath(_displayPath), ); + Future dispose() async { + await _object.client.close(); + } + Future getCurrent() async { List? state = await _object.callGetCurrentState(); List list = state.map((e) => _toNative(e)).toList(); diff --git a/lib/services/display/display_service.dart b/lib/services/display/display_service.dart index 862ee633..c9081ff5 100644 --- a/lib/services/display/display_service.dart +++ b/lib/services/display/display_service.dart @@ -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; @@ -34,15 +33,26 @@ class DisplayService { /// NOTIFIERS final ValueNotifier _initialNotifier; + ValueNotifier get initialNotifier => _initialNotifier; final ValueNotifier _currentNotifier; + ValueNotifier get currentNotifier => _currentNotifier; + Future dispose() async { + _initialNotifier.dispose(); + _currentNotifier.dispose(); + await Future.wait([ + _displayDBusService.dispose(), + _monitorStateStreamController.close(), + ]); + } + Future 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(); @@ -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([ @@ -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)); }