Skip to content

Instantly share code, notes, and snippets.

@rodericj
Created March 24, 2014 22:38
Show Gist options
  • Save rodericj/9750865 to your computer and use it in GitHub Desktop.
Save rodericj/9750865 to your computer and use it in GitHub Desktop.
DeviceSettingsHeaderView *headerView = [[DeviceSettingsHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, DEVICE_HEADER_HEIGHT)];
+
+ [RACObserve(self, device) subscribeNext:^(MorseDevice *newDevice) {
+
+ // When the battery level changes, update the string
+ RACSignal *batteryChangeSignal = RACObserve(newDevice, batteryLevel);
+ RAC(headerView.batteryAndWifiLabel, text) = [[batteryChangeSignal deliverOn:[RACScheduler mainThreadScheduler]] map:^id(id value) {
+
+ return [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"battery is at", nil), newDevice.batteryLevelString];
+ }];
+
+ RACSignal *connectedStateSignal = RACObserve(newDevice, connectedState);
+
+ // When the connection state changes, update the font of the label
+ CGFloat fontSize = headerView.batteryAndWifiLabel.font.pointSize;
+ RAC(headerView.batteryAndWifiLabel, font) = [[connectedStateSignal deliverOn:[RACScheduler mainThreadScheduler]] map:^id(id value) {
+ BOOL isConnected = [value integerValue] == MorseDeviceConnectedStateConnected;
+ return [UIFont fontWithName:isConnected ? FONT_BREE_THIN : FONT_BREE_THIN_OBLIQUE size:fontSize];
+ }];
+
+ // When the connection state changes, update the label's text
+ RAC(headerView.batteryAndWifiLabel, text) = [[connectedStateSignal deliverOn:[RACScheduler mainThreadScheduler]] map:^id(id deviceConnectedState) {
+ BOOL isConnected = [deviceConnectedState integerValue] == MorseDeviceConnectedStateConnected;
+
+ NSString *batteryIsAt = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"battery is at", nil), newDevice.batteryLevelString];
+ 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(newDevice, 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