Skip to content

Instantly share code, notes, and snippets.

@rodericj
Created April 11, 2014 22:27
Show Gist options
  • Save rodericj/10506691 to your computer and use it in GitHub Desktop.
Save rodericj/10506691 to your computer and use it in GitHub Desktop.
- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
// The only header is in the first section
if (section != DeviceSettingsTableViewControllerSectionDeviceSettings) {
return [[UIView alloc] initWithFrame:CGRectZero];
}
// If we've already created this headerView, return it, it should never change
if (self.headerView) {
return self.headerView;
}
DeviceSettingsHeaderView *headerView = [[DeviceSettingsHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, DEVICE_HEADER_HEIGHT)];
self.headerView = headerView;
__block DeviceSettingsTableViewController *blockSelf = self;
headerView.tappedBack = ^{
[blockSelf popViewController];
};
// When the battery level changes, update the string
RACSignal *batteryChangeSignal = [[RACObserve(self, device.batteryRemaining) distinctUntilChanged] deliverOn:[RACScheduler mainThreadScheduler]];
RACSignal *connectedStateSignal = [[RACObserve(self, device.connectedState) distinctUntilChanged] deliverOn:[RACScheduler mainThreadScheduler]];
// When the connection state changes, update the font of the label
CGFloat fontSize = headerView.batteryLabel.font.pointSize;
RAC(headerView.batteryLabel, font) = [connectedStateSignal 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 text
RAC(headerView.batteryLabel, text) = [RACSignal combineLatest:@[batteryChangeSignal, connectedStateSignal] reduce:^id(NSNumber *batteryRemaining, NSNumber *deviceConnectedState) {
BOOL isConnected = [deviceConnectedState integerValue] == MorseDeviceConnectedStateConnected;
float batteryRemainingFloat = [batteryRemaining floatValue];
NSString *batteryRemainingString = batteryRemainingFloat == -1 ? @"..." : [NSString stringWithFormat:@"%.0f%%", batteryRemainingFloat * 100];
NSString *batteryIsAt = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"battery is at", nil), batteryRemainingString];
NSString *disconnected = NSLocalizedString(@"disconnected", nil);
return isConnected ? batteryIsAt : disconnected;
}];
// When the connection state changes, update the label's color
RAC(headerView.batteryLabel, textColor) = [connectedStateSignal map:^id(id deviceConnectedState) {
return [deviceConnectedState integerValue] == MorseDeviceConnectedStateConnected ? [AVHexColor colorWithHex:0x666666] : [AVHexColor colorWithHex:0xF26322];
}];
// update the alpha for the cone image
RAC(headerView, coneImageAlpha) = [connectedStateSignal map:^id(id deviceConnectedState) {
return [deviceConnectedState integerValue] == MorseDeviceConnectedStateConnected ? @1 : @.5;
}];
return headerView;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment