Skip to content

Commit 5aacd77

Browse files
committed
Pass lighthouses as command parameter
1 parent a22f9f2 commit 5aacd77

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

OVRLighthouseManager/Helpers/LighthouseCommands.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -199,46 +199,28 @@ public PowerAllCommandOperation Operation
199199
get; private set;
200200
}
201201

202-
private readonly List<LighthouseObject> lighthouses = new();
203-
204-
public void AddLighthouse(LighthouseObject lighthouse)
205-
{
206-
if (!lighthouses.Any(x => x == lighthouse))
207-
{
208-
lighthouses.Add(lighthouse);
209-
}
210-
}
211-
212-
public void RemoveLighthouse(LighthouseObject lighthouse)
213-
{
214-
lighthouses.Remove(lighthouse);
215-
}
216-
217-
public bool CanExecute()
202+
public override bool CanExecute(object? parameter)
218203
{
219-
return Operation == PowerAllCommandOperation.None && lighthouses.Any(l => l.IsManaged);
204+
var param = parameter as PowerAllCommandParameter;
205+
return Operation == PowerAllCommandOperation.None && param.Lighthouses.Any(l => l.IsManaged);
220206
}
221207

222-
public override bool CanExecute(object? _) => CanExecute();
223-
224208
public async override Task ExecuteAsync(object? parameter)
225209
{
226210
try
227211
{
228-
var command = parameter as string;
212+
var param = parameter as PowerAllCommandParameter;
229213

230-
Operation = command switch
214+
Operation = param.Command switch
231215
{
232216
"powerOn" => PowerAllCommandOperation.PowerOn,
233217
"powerDown" => PowerAllCommandOperation.PowerDown,
234218
_ => PowerAllCommandOperation.None
235219
};
236220
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
237221

238-
var on = command == "powerOn";
239-
240222
var settings = App.GetService<ILighthouseSettingsService>();
241-
foreach (var lighthouse in lighthouses.Where(l => l.IsManaged))
223+
foreach (var lighthouse in param.Lighthouses.Where(l => l.IsManaged))
242224
{
243225
switch (Operation)
244226
{
@@ -270,3 +252,9 @@ public async override Task ExecuteAsync(object? parameter)
270252
}
271253
}
272254
}
255+
256+
public class PowerAllCommandParameter
257+
{
258+
public string Command { get; set; } = string.Empty;
259+
public IEnumerable<LighthouseObject> Lighthouses { get; set; } = Enumerable.Empty<LighthouseObject>();
260+
}

OVRLighthouseManager/ViewModels/MainViewModel.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public partial class MainViewModel : ObservableRecipient
4646
private readonly PowerAllCommand _powerAllCommand = new();
4747

4848
public ICommand PowerAllCommand => _powerAllCommand;
49-
public bool IsDoingPowerOnAll => !_powerAllCommand.CanExecute() && _powerAllCommand.Operation == PowerAllCommandOperation.PowerOn;
50-
public bool IsDoingPowerOffAll => !_powerAllCommand.CanExecute() && _powerAllCommand.Operation == PowerAllCommandOperation.PowerDown;
49+
public bool IsDoingPowerOnAll => _powerAllCommand.Operation == PowerAllCommandOperation.PowerOn;
50+
public bool IsDoingPowerOffAll => _powerAllCommand.Operation == PowerAllCommandOperation.PowerDown;
5151

5252
public MainViewModel(
5353
ILighthouseDiscoveryService lighthouseService,
@@ -78,7 +78,6 @@ ScanCommand scanCommand
7878
var devices = Devices.Select(d => d.Lighthouse).ToArray();
7979
await _lighthouseSettingsService.SetDevicesAsync(devices);
8080
Log.Information($"Found: {arg.Name} ({AddressToStringConverter.AddressToString(address)})");
81-
_powerAllCommand.AddLighthouse(item);
8281
});
8382
}
8483
else
@@ -119,7 +118,6 @@ ScanCommand scanCommand
119118
vm.OnClickRemove += OnClickRemoveDevice;
120119
vm.OnEditId += OnEditId;
121120
vm.IsFound = _lighthouseService.FoundLighthouses.Any(l => l.BluetoothAddressValue == AddressToStringConverter.StringToAddress(d.BluetoothAddress));
122-
_powerAllCommand.AddLighthouse(vm);
123121
return vm;
124122
}).ToArray();
125123
Devices = new(devices);

OVRLighthouseManager/Views/MainPage.xaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
mc:Ignorable="d">
1111

1212
<Page.Resources>
13+
<helpers:PowerAllCommandParameter x:Key="PowerAllCommandParameter" />
1314
<helpers:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
1415
</Page.Resources>
1516

@@ -104,8 +105,10 @@
104105
Grid.Row="1"
105106
x:Name="PowerOnAll"
106107
Command="{x:Bind ViewModel.PowerAllCommand}"
107-
CommandParameter="powerOn"
108108
>
109+
<Button.CommandParameter>
110+
<helpers:PowerAllCommandParameter Command="powerOn" Lighthouses="{x:Bind ViewModel.Devices, Mode=OneWay}" />
111+
</Button.CommandParameter>
109112
<Button.Content>
110113
<StackPanel Orientation="Horizontal">
111114
<ProgressRing
@@ -125,8 +128,10 @@
125128
Grid.Row="1"
126129
x:Name="PowerOffAll"
127130
Command="{x:Bind ViewModel.PowerAllCommand}"
128-
CommandParameter="powerDown"
129131
>
132+
<Button.CommandParameter>
133+
<helpers:PowerAllCommandParameter Command="powerDown" Lighthouses="{x:Bind ViewModel.Devices, Mode=OneWay}" />
134+
</Button.CommandParameter>
130135
<Button.Content>
131136
<StackPanel Orientation="Horizontal">
132137
<ProgressRing

0 commit comments

Comments
 (0)