Skip to content

Commit

Permalink
little cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
semvis123 committed Feb 11, 2021
1 parent c855921 commit 2fe3c80
Showing 1 changed file with 45 additions and 40 deletions.
85 changes: 45 additions & 40 deletions Client/macos/ViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ - (void)viewDidLoad {
// Wrap the connector using the bluetoothwrapper
bt = BluetoothWrapper(std::move(connector));
statusItem = [NSStatusBar.systemStatusBar statusItemWithLength: -1];
statusItem.button.image = [NSImage imageNamed: @"NSRefreshTemplate"];
statusItem.button.image = [NSImage imageNamed:@"NSRefreshTemplate"];
[statusItem setTarget:self];
[statusItem setAction:@selector(statusItemClick:)];
}
Expand All @@ -27,6 +27,7 @@ - (void)displayError:(RecoverableException)exc {
[ANCSlider setEnabled:FALSE];
[focusOnVoice setEnabled:FALSE];
[connectButton setTitle:@"Connect to Bluetooth device"];
statusItem.button.image = [NSImage imageNamed:@"NSRefreshTemplate"];
[connectedLabel setStringValue:[@"Unexpected error occurred and disconnected: \n" stringByAppendingString:@(exc.what())]];
} else {
[connectedLabel setStringValue:[@"Unexpected error occurred: \n" stringByAppendingString:@(exc.what())]];
Expand All @@ -36,7 +37,7 @@ - (void)displayError:(RecoverableException)exc {
- (void)statusItemClick:(id)sender {
if (!bt.isConnected())
return [self connectToDevice:self];

if ([focusOnVoice isEnabled]) {
[ANCSlider setIntValue:0];
[focusOnVoice setEnabled:FALSE];
Expand All @@ -52,75 +53,79 @@ - (void)setRepresentedObject:(id)representedObject {
}

- (IBAction)connectToDevice:(id)sender {
statusItem.button.image = [NSImage imageNamed: @"NSRefreshTemplate"];
statusItem.button.image = [NSImage imageNamed:@"NSRefreshTemplate"];

// check if it should disconnect
if (bt.isConnected()) {
bt.disconnect();
[ANCSlider setEnabled:FALSE];
[focusOnVoice setEnabled:FALSE];
[connectButton setTitle:@"Connect to Bluetooth device"];
[connectedLabel setStringValue:@"Not connected"];
return;
}
else {
// launch device selector modal
IOBluetoothDeviceSelectorController *dSelector = [IOBluetoothDeviceSelectorController deviceSelector];
int result = [dSelector runModal];


if (result == kIOBluetoothUISuccess) {
// get device

// launch device selector modal
IOBluetoothDeviceSelectorController *dSelector = [IOBluetoothDeviceSelectorController deviceSelector];
int result = [dSelector runModal];


if (result == kIOBluetoothUISuccess) {
// get device
IOBluetoothDevice *device = [[dSelector getResults] lastObject];
try {
// connect to device
bt.connect([[device addressString] UTF8String]);
} catch (RecoverableException& exc) {
[self displayError:exc];
}
// give it some time to connect
int timeout = 5;
while(!bt.isConnected() and timeout >= 0) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
timeout--;
}
if (bt.isConnected()) {
IOBluetoothDevice *device = [[dSelector getResults] lastObject];
try {
// connect to device
bt.connect([[device addressString] UTF8String]);
} catch (RecoverableException& exc) {
[self displayError:exc];
}
// give it some time to connect
int timeout = 5;
while(!bt.isConnected() and timeout >= 0) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
timeout--;
}
if (bt.isConnected()) {
IOBluetoothDevice *device = [[dSelector getResults] lastObject];
[connectedLabel setStringValue:[@"Connected: " stringByAppendingString:[device nameOrAddress]]];
[connectButton setTitle:@"Disconnect"];
[ANCSlider setEnabled:TRUE];
[focusOnVoice setEnabled:FALSE];
statusItem.button.image = [NSImage imageNamed: @"NSHomeTemplate"];
}
else {
[connectedLabel setStringValue:@"Not connected, connection timed out."];
bt.disconnect();
}
[connectedLabel setStringValue:[@"Connected: " stringByAppendingString:[device nameOrAddress]]];
[connectButton setTitle:@"Disconnect"];
[ANCSlider setEnabled:TRUE];
[focusOnVoice setEnabled:FALSE];
statusItem.button.image = [NSImage imageNamed:@"NSHomeTemplate"];
} else {
[connectedLabel setStringValue:@"Not connected, device selector canceled."];
[connectedLabel setStringValue:@"Not connected, connection timed out."];
bt.disconnect();
}
} else {
[connectedLabel setStringValue:@"Not connected, device selector canceled."];
}

}

- (IBAction)sendData:(id)sender {
// check if the device is still connected
if (!bt.isConnected()) {
[ANCSlider setEnabled:FALSE];
[ANCSlider setIntValue:0];
[focusOnVoice setEnabled:FALSE];
[connectButton setTitle:@"Connect to Bluetooth device"];
[connectedLabel setStringValue:@"Not connected, please reconnect."];
statusItem.button.image = [NSImage imageNamed: @"NSRefreshTemplate"];
statusItem.button.image = [NSImage imageNamed:@"NSRefreshTemplate"];
return;
}

[ANCValueLabel setStringValue:ANCSlider.stringValue];
if (ANCSlider.intValue >= MINIMUM_VOICE_FOCUS_STEP) {
[focusOnVoice setTitle:@"Focus on Voice"];
[focusOnVoice setEnabled:TRUE];
statusItem.button.image = [NSImage imageNamed: @"NSFlowViewTemplate"];
statusItem.button.image = [NSImage imageNamed:@"NSFlowViewTemplate"];
}
else {
[focusOnVoice setTitle:@"Focus on Voice isn't enabled on this level."];
statusItem.button.image = [NSImage imageNamed: @"NSHomeTemplate"];
statusItem.button.image = [NSImage imageNamed:@"NSHomeTemplate"];
[focusOnVoice setEnabled:FALSE];
}

// send current settings
auto ncAsmEffect = NC_ASM_EFFECT::ADJUSTMENT_COMPLETION;
auto asmId = focusOnVoice.state == NSControlStateValueOn ? ASM_ID::VOICE : ASM_ID::NORMAL;
Expand Down

0 comments on commit 2fe3c80

Please sign in to comment.