Skip to content

Commit

Permalink
chore: Organize Layouts Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
bdlukaa committed Dec 30, 2024
1 parent fadff31 commit 8dd1ec1
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 deletions lib/providers/layouts_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class LayoutsProvider extends UnityProvider {
double? get layoutManagerHeight => _layoutManagerHeight;
set layoutManagerHeight(double? value) {
_layoutManagerHeight = value;
notifyListeners();
save();
}

Expand All @@ -89,7 +88,8 @@ class LayoutsProvider extends UnityProvider {
}

@override
Future<void> save({bool notifyListeners = true}) async {
Future<void> save({bool notifyListeners = false}) async {
this.notifyListeners();
await write({
kStorageDesktopLayouts:
jsonEncode(layouts.map((layout) => layout.toMap()).toList()),
Expand Down Expand Up @@ -161,20 +161,20 @@ class LayoutsProvider extends UnityProvider {
var previousDevice = layout.devices.firstOrNull;
if (previousDevice != null) {
layout.devices.clear();
await _releaseDevice(device);
await maybeReleaseDevice(device);
}
}

UnityPlayers.players[device.uuid] ??= UnityPlayers.forDevice(device);
layout.devices.add(device);
debugPrint('Added $device');

notifyListeners();
await save();
}
}

Future<void> _releaseDevice(Device device) async {
/// Releases the device if it's not used in any layout.
Future<void> maybeReleaseDevice(Device device) async {
if (!UnityPlayers.players.containsKey(device.uuid)) return;
if (!layouts
.any((layout) => layout.devices.any((d) => d.uuid == device.uuid))) {
Expand All @@ -188,9 +188,8 @@ class LayoutsProvider extends UnityProvider {
debugPrint('Removed $device');

currentLayout.devices.remove(device);
_releaseDevice(device);
maybeReleaseDevice(device);
}
notifyListeners();
await save();
}

Expand All @@ -206,10 +205,9 @@ class LayoutsProvider extends UnityProvider {
}

for (final device in devices) {
_releaseDevice(device);
maybeReleaseDevice(device);
}

notifyListeners();
await save();
}

Expand All @@ -221,10 +219,9 @@ class LayoutsProvider extends UnityProvider {
(d1) => devices.any((d2) => d1.uri == d2.uri),
);
for (final device in devices) {
_releaseDevice(device);
maybeReleaseDevice(device);
}

notifyListeners();
await save();
}

Expand All @@ -233,7 +230,6 @@ class LayoutsProvider extends UnityProvider {
if (isLayoutLocked(currentLayout)) return;

currentLayout.devices.insert(end, currentLayout.devices.removeAt(initial));
notifyListeners();
await save();
}

Expand All @@ -244,7 +240,6 @@ class LayoutsProvider extends UnityProvider {
} else {
debugPrint('$layout already exists');
}
notifyListeners();
await save();
return layouts.indexOf(layout);
}
Expand All @@ -258,10 +253,9 @@ class LayoutsProvider extends UnityProvider {
layouts.remove(layout);

for (final device in layout.devices) {
_releaseDevice(device);
maybeReleaseDevice(device);
}
}
notifyListeners();
await save();
}

Expand All @@ -273,15 +267,14 @@ class LayoutsProvider extends UnityProvider {
..insert(layoutIndex, newLayout);
for (final device
in oldLayout.devices.where((d) => !newLayout.devices.contains(d))) {
_releaseDevice(device);
maybeReleaseDevice(device);
}

debugPrint('Replaced $oldLayout at $layoutIndex with $newLayout');
} else {
debugPrint('Layout $oldLayout not found');
}

notifyListeners();
await save();
}

Expand All @@ -291,7 +284,6 @@ class LayoutsProvider extends UnityProvider {
UnityPlayers.players[device.uuid] ??= UnityPlayers.forDevice(device);
}

notifyListeners();
await save();
}

Expand All @@ -316,7 +308,6 @@ class LayoutsProvider extends UnityProvider {
}

layouts.insert(newIndex, layouts.removeAt(oldIndex));
notifyListeners();
await save();
}

Expand Down Expand Up @@ -350,7 +341,6 @@ class LayoutsProvider extends UnityProvider {
UnityPlayers.reloadDevice(device);
}

notifyListeners();
save();

return device;
Expand All @@ -359,15 +349,13 @@ class LayoutsProvider extends UnityProvider {
Future<void> collapseServer(Server server) async {
if (!collapsedServers.contains(server.id)) {
collapsedServers.add(server.id);
notifyListeners();
await save();
}
}

Future<void> expandServer(Server server) async {
if (collapsedServers.contains(server.id)) {
collapsedServers.remove(server.id);
notifyListeners();
await save();
}
}
Expand All @@ -377,16 +365,14 @@ class LayoutsProvider extends UnityProvider {
Future<void> lockLayout(Layout layout) async {
if (!lockedLayouts.contains(layout)) {
lockedLayouts.add(layout);
notifyListeners();
await save();
await save(notifyListeners: false);
}
}

Future<void> unlockLayout(Layout layout) async {
if (lockedLayouts.contains(layout)) {
lockedLayouts.remove(layout);
notifyListeners();
await save();
await save(notifyListeners: false);
}
}

Expand All @@ -400,11 +386,14 @@ class LayoutsProvider extends UnityProvider {

bool isLayoutLocked(Layout layout) => lockedLayouts.contains(layout);

/// Sets the volume for all layouts.
void setVolume(double volume) {
for (final layout in layouts) {
layout.setVolume(volume);
}
save();
}

/// Mutes all layouts.
void mute() => setVolume(0);
}

0 comments on commit 8dd1ec1

Please sign in to comment.