Skip to content
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
2 changes: 2 additions & 0 deletions BrickController2/BrickController2/UI/Pages/DevicePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@

</StackLayout>
</ScrollView>

<controls:FloatingActionButton ButtonColor="Red" ImageSource="{extensions:ImageResource Source=ic_search.png}" ImageColor="White" Command="{Binding ScanCommand}" IsVisible="{Binding CanBePowerSource}" HorizontalOptions="End" VerticalOptions="End" Margin="10"/>
</Grid>

<controls:Dialogs x:Name="Dialogs" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,21 @@ public DevicePageViewModel(
RenameCommand = new SafeCommand(async () => await RenameDeviceAsync());
BuWizzOutputLevelChangedCommand = new SafeCommand<int>(outputLevel => SetBuWizzOutputLevel(outputLevel));
BuWizz2OutputLevelChangedCommand = new SafeCommand<int>(outputLevel => SetBuWizzOutputLevel(outputLevel));
ScanCommand = new SafeCommand(ScanAsync, () => CanExecuteScan);
}

public Device Device { get; }
public bool IsBuWizzDevice => Device.DeviceType == DeviceType.BuWizz;
public bool IsBuWizz2Device => Device.DeviceType == DeviceType.BuWizz2;
public bool CanBePowerSource => Device.CanBePowerSource;
public bool CanExecuteScan => Device.CanBePowerSource &&
Device.DeviceState == DeviceState.Connected &&
!_deviceManager.IsScanning;

public ICommand RenameCommand { get; }
public ICommand BuWizzOutputLevelChangedCommand { get; }
public ICommand BuWizz2OutputLevelChangedCommand { get; }
public ICommand ScanCommand { get; }

public int BuWizzOutputLevel { get; set; } = 1;
public int BuWizz2OutputLevel { get; set; } = 1;
Expand Down Expand Up @@ -214,6 +220,66 @@ await _dialogService.ShowMessageBoxAsync(
}
}

private async Task ScanAsync()
{
if (!_deviceManager.IsBluetoothOn)
{
await _dialogService.ShowMessageBoxAsync(
Translate("Warning"),
Translate("BluetoothIsTurnedOff"),
Translate("Ok"),
_disappearingTokenSource.Token);
}

var percent = 0;
var scanResult = true;
await _dialogService.ShowProgressDialogAsync(
true,
async (progressDialog, token) =>
{
if (!_isDisappearing)
{
using (var cts = new CancellationTokenSource())
using (_disappearingTokenSource.Token.Register(() => cts.Cancel()))
{
Task<bool> scanTask = null;
try
{
scanTask = _deviceManager.ScanAsync(cts.Token);

while (!token.IsCancellationRequested && percent <= 100 && !scanTask.IsCompleted)
{
progressDialog.Percent = percent;
await Task.Delay(100, token);
percent += 1;
}
}
catch (Exception)
{ }

cts.Cancel();

if (scanTask != null)
{
scanResult = await scanTask;
}
}
}
},
Translate("Scanning"),
Translate("SearchingForDevices"),
Translate("Cancel"));

if (!scanResult && !_isDisappearing)
{
await _dialogService.ShowMessageBoxAsync(
Translate("Warning"),
Translate("ErrorDuringScanning"),
Translate("Ok"),
CancellationToken.None);
}
}

private void OnDeviceDisconnected(Device device)
{
}
Expand Down