Skip to content

Commit

Permalink
Adding a dispose into services to close/dispose all resources (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
atsen-dev authored Mar 13, 2022
1 parent 37446ca commit 55f286e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
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

0 comments on commit 55f286e

Please sign in to comment.