Skip to content

Instantly share code, notes, and snippets.

@bobspryn
Created March 24, 2014 22:56
Show Gist options
  • Select an option

  • Save bobspryn/9751122 to your computer and use it in GitHub Desktop.

Select an option

Save bobspryn/9751122 to your computer and use it in GitHub Desktop.
- (void)viewDidLoad
{
[super viewDidLoad];
DeviceSettingsHeaderView *headerView = [[DeviceSettingsHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, DEVICE_HEADER_HEIGHT)];
// don't need this with the current code
// RACSignal *newDevice = [RACObserve(self, device) ignore: nil];
// When the battery level changes, update the string
RACSignal *batteryChangeSignal = [[RACObserve(self, device.batteryLevel)
ignore: nil]
deliverOn:[RACScheduler mainThreadScheduler]];
// connected state is used in multiple places
RACSignal *connectedStateSignal = [[RACObserve(self, device.connectedState)
ignore: nil]
deliverOn:[RACScheduler mainThreadScheduler]];
// When the connection state changes, update the font of the label
CGFloat fontSize = headerView.batteryAndWifiLabel.font.pointSize;
RAC(headerView.batteryAndWifiLabel, font) = [connectedStateSignal
map:^id(NSNumber *deviceConnectedState) {
BOOL isConnected = deviceConnectedState.integerValue == MorseDeviceConnectedStateConnected;
return [UIFont fontWithName:isConnected ? FONT_BREE_THIN : FONT_BREE_THIN_OBLIQUE size:fontSize];
}];
// When the connection state changes, update the label text
RAC(headerView.batteryAndWifiLabel, text) = [RACSignal combineLatest:@[batteryChangeSignal, connectedStateSignal]
reduce:^id(NSNumber *batteryLevel, NSNumber *deviceConnectedState) {
BOOL isConnected = deviceConnectedState.integerValue == MorseDeviceConnectedStateConnected;
NSString *batteryIsAt = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"battery is at", nil), [DeviceClass stringForBatteryLevel:batteryLevel]];
NSString *disconnected = NSLocalizedString(@"disconnected", nil);
return isConnected ? batteryIsAt : disconnected;
}];
// When the connection state changes, update the label's color
RAC(headerView.batteryAndWifiLabel, textColor) = [[connectedStateSignal deliverOn:[RACScheduler mainThreadScheduler]] map:^id(id deviceConnectedState) {
return [deviceConnectedState integerValue] == MorseDeviceConnectedStateConnected ? [AVHexColor colorWithHex:0x666666] : [AVHexColor colorWithHex:0xF26322];
}];
RAC(headerView.contentLabel, text) = [[RACObserve(self, device.name)
deliverOn:[RACScheduler mainThreadScheduler]]
map:^id(id deviceName) {
return deviceName;
}];
self.tableView.tableHeaderView = headerView;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment