Convert AdvancedSettingsTableViewController to OWSTableView.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 7f3d76d8bb
commit db10cbaeea

@ -1,13 +1,9 @@
// //
// AdvancedSettingsTableViewController.h // Copyright (c) 2017 Open Whisper Systems. All rights reserved.
// Signal
//
// Created by Dylan Bourgeois on 05/01/15.
// Copyright (c) 2015 Open Whisper Systems. All rights reserved.
// //
#import <UIKit/UIKit.h> #import "OWSTableViewController.h"
@interface AdvancedSettingsTableViewController : UITableViewController @interface AdvancedSettingsTableViewController : OWSTableViewController
@end @end

@ -15,134 +15,111 @@ NS_ASSUME_NONNULL_BEGIN
@interface AdvancedSettingsTableViewController () @interface AdvancedSettingsTableViewController ()
@property (nonatomic) UITableViewCell *enableLogCell;
@property (nonatomic) UITableViewCell *submitLogCell;
@property (nonatomic) UITableViewCell *registerPushCell;
@property (nonatomic) UISwitch *enableLogSwitch; @property (nonatomic) UISwitch *enableLogSwitch;
@property (nonatomic, readonly) BOOL supportsCallKit;
@end @end
typedef NS_ENUM(NSInteger, AdvancedSettingsTableViewControllerSection) { #pragma mark -
AdvancedSettingsTableViewControllerSectionLogging,
AdvancedSettingsTableViewControllerSectionPushNotifications,
AdvancedSettingsTableViewControllerSection_Count // meta section
};
@implementation AdvancedSettingsTableViewController @implementation AdvancedSettingsTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController.navigationBar setTranslucent:NO];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (instancetype)init
{
return [super initWithStyle:UITableViewStyleGrouped];
}
- (void)loadView - (void)loadView
{ {
[super loadView]; [super loadView];
self.title = NSLocalizedString(@"SETTINGS_ADVANCED_TITLE", @""); self.title = NSLocalizedString(@"SETTINGS_ADVANCED_TITLE", @"");
// Enable Log
self.enableLogCell = [[UITableViewCell alloc] init];
self.enableLogCell.textLabel.text = NSLocalizedString(@"SETTINGS_ADVANCED_DEBUGLOG", @"");
self.enableLogCell.userInteractionEnabled = YES;
self.enableLogSwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; self.enableLogSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[self.enableLogSwitch setOn:[PropertyListPreferences loggingIsEnabled]]; [self.enableLogSwitch setOn:[PropertyListPreferences loggingIsEnabled]];
[self.enableLogSwitch addTarget:self [self.enableLogSwitch addTarget:self
action:@selector(didToggleEnableLogSwitch:) action:@selector(didToggleEnableLogSwitch:)
forControlEvents:UIControlEventValueChanged]; forControlEvents:UIControlEventValueChanged];
self.enableLogCell.accessoryView = self.enableLogSwitch;
// Send Log [self observeNotifications];
self.submitLogCell = [[UITableViewCell alloc] init];
self.submitLogCell.textLabel.text = NSLocalizedString(@"SETTINGS_ADVANCED_SUBMIT_DEBUGLOG", @"");
self.registerPushCell = [[UITableViewCell alloc] init]; [self updateTableContents];
self.registerPushCell.textLabel.text = NSLocalizedString(@"REREGISTER_FOR_PUSH", nil);
} }
#pragma mark - Table view data source - (void)observeNotifications
{
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { [[NSNotificationCenter defaultCenter] addObserver:self
return AdvancedSettingsTableViewControllerSection_Count; selector:@selector(socketStateDidChange)
name:kNSNotification_SocketManagerStateDidChange
object:nil];
} }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - (void)dealloc
{
AdvancedSettingsTableViewControllerSection settingsSection = (AdvancedSettingsTableViewControllerSection)section; [[NSNotificationCenter defaultCenter] removeObserver:self];
switch (settingsSection) {
case AdvancedSettingsTableViewControllerSectionLogging:
return self.enableLogSwitch.isOn ? 2 : 1;
case AdvancedSettingsTableViewControllerSectionPushNotifications:
return 1;
default:
return 0;
}
} }
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section - (void)socketStateDidChange
{ {
AdvancedSettingsTableViewControllerSection settingsSection = (AdvancedSettingsTableViewControllerSection)section; OWSAssert([NSThread isMainThread]);
switch (settingsSection) {
case AdvancedSettingsTableViewControllerSectionLogging: [self updateTableContents];
return NSLocalizedString(@"LOGGING_SECTION", nil);
case AdvancedSettingsTableViewControllerSectionPushNotifications:
return NSLocalizedString(@"PUSH_REGISTER_TITLE", @"Used in table section header and alert view title contexts");
default:
return nil;
}
} }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath #pragma mark - Table Contents
- (void)updateTableContents
{ {
AdvancedSettingsTableViewControllerSection settingsSection = (AdvancedSettingsTableViewControllerSection)indexPath.section; OWSTableContents *contents = [OWSTableContents new];
switch (settingsSection) {
case AdvancedSettingsTableViewControllerSectionLogging:
switch (indexPath.row) {
case 0:
return self.enableLogCell;
case 1:
OWSAssert(self.enableLogSwitch.isOn);
return self.submitLogCell;
}
case AdvancedSettingsTableViewControllerSectionPushNotifications:
return self.registerPushCell;
default:
// Unknown section
OWSAssert(NO);
return nil;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { __weak AdvancedSettingsTableViewController *weakSelf = self;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
OWSTableSection *loggingSection = [OWSTableSection new];
if ([tableView cellForRowAtIndexPath:indexPath] == self.submitLogCell) { loggingSection.headerTitle = NSLocalizedString(@"LOGGING_SECTION", nil);
DDLogInfo(@"%@ Submitting debug logs", self.tag); [loggingSection addItem:[OWSTableItem itemWithCustomCellBlock:^{
[DDLog flushLog]; UITableViewCell *cell = [UITableViewCell new];
[Pastelog submitLogs]; cell.textLabel.text = NSLocalizedString(@"SETTINGS_ADVANCED_DEBUGLOG", @"");
} else if ([tableView cellForRowAtIndexPath:indexPath] == self.registerPushCell) { cell.textLabel.font = [UIFont ows_regularFontWithSize:18.f];
OWSSyncPushTokensJob *job = cell.textLabel.textColor = [UIColor blackColor];
[[OWSSyncPushTokensJob alloc] initWithPushManager:[PushManager sharedManager]
accountManager:[Environment getCurrent].accountManager cell.accessoryView = self.enableLogSwitch;
preferences:[Environment preferences] cell.selectionStyle = UITableViewCellSelectionStyleNone;
showAlerts:YES]; return cell;
job.uploadOnlyIfStale = NO;
[job run];
} else {
DDLogDebug(@"%@ Ignoring cell selection at indexPath: %@", self.tag, indexPath);
} }
actionBlock:nil]];
if (self.enableLogSwitch.isOn) {
[loggingSection
addItem:[OWSTableItem actionItemWithText:NSLocalizedString(@"SETTINGS_ADVANCED_SUBMIT_DEBUGLOG", @"")
actionBlock:^{
DDLogInfo(@"%@ Submitting debug logs", self.tag);
[DDLog flushLog];
[Pastelog submitLogs];
}]];
}
[contents addSection:loggingSection];
OWSTableSection *pushNotificationsSection = [OWSTableSection new];
pushNotificationsSection.headerTitle
= NSLocalizedString(@"PUSH_REGISTER_TITLE", @"Used in table section header and alert view title contexts");
[pushNotificationsSection addItem:[OWSTableItem actionItemWithText:NSLocalizedString(@"REREGISTER_FOR_PUSH", nil)
actionBlock:^{
[weakSelf syncPushTokens];
}]];
[contents addSection:pushNotificationsSection];
self.contents = contents;
} }
#pragma mark - Actions #pragma mark - Actions
- (void)syncPushTokens
{
OWSSyncPushTokensJob *job =
[[OWSSyncPushTokensJob alloc] initWithPushManager:[PushManager sharedManager]
accountManager:[Environment getCurrent].accountManager
preferences:[Environment preferences]
showAlerts:YES];
job.uploadOnlyIfStale = NO;
[job run];
}
- (void)didToggleEnableLogSwitch:(UISwitch *)sender { - (void)didToggleEnableLogSwitch:(UISwitch *)sender {
if (!sender.isOn) { if (!sender.isOn) {
[[DebugLogger sharedLogger] wipeLogs]; [[DebugLogger sharedLogger] wipeLogs];
@ -152,7 +129,8 @@ typedef NS_ENUM(NSInteger, AdvancedSettingsTableViewControllerSection) {
} }
[PropertyListPreferences setLoggingEnabled:sender.isOn]; [PropertyListPreferences setLoggingEnabled:sender.isOn];
[self.tableView reloadData];
[self updateTableContents];
} }
#pragma mark - Logging #pragma mark - Logging

@ -66,6 +66,8 @@ typedef UITableViewCell *_Nonnull (^OWSTableCustomCellBlock)();
+ (OWSTableItem *)disclosureItemWithText:(NSString *)text actionBlock:(nullable OWSTableActionBlock)actionBlock; + (OWSTableItem *)disclosureItemWithText:(NSString *)text actionBlock:(nullable OWSTableActionBlock)actionBlock;
+ (OWSTableItem *)actionItemWithText:(NSString *)text actionBlock:(nullable OWSTableActionBlock)actionBlock;
- (nullable UITableViewCell *)customCell; - (nullable UITableViewCell *)customCell;
- (NSNumber *)customRowHeight; - (NSNumber *)customRowHeight;

@ -169,6 +169,24 @@ const CGFloat kOWSTable_DefaultCellHeight = 45.f;
return item; return item;
} }
+ (OWSTableItem *)actionItemWithText:(NSString *)text actionBlock:(nullable OWSTableActionBlock)actionBlock
{
OWSAssert(text.length > 0);
OWSAssert(actionBlock);
OWSTableItem *item = [OWSTableItem new];
item.itemType = OWSTableItemTypeAction;
item.actionBlock = actionBlock;
item.customCellBlock = ^{
UITableViewCell *cell = [UITableViewCell new];
cell.textLabel.text = text;
cell.textLabel.font = [UIFont ows_regularFontWithSize:18.f];
cell.textLabel.textColor = [UIColor blackColor];
return cell;
};
return item;
}
- (nullable UITableViewCell *)customCell - (nullable UITableViewCell *)customCell
{ {
if (_customCell) { if (_customCell) {

@ -64,7 +64,7 @@
[self.navigationController.navigationBar setTranslucent:NO]; [self.navigationController.navigationBar setTranslucent:NO];
[self initializeObserver]; [self observeNotifications];
self.title = NSLocalizedString(@"SETTINGS_NAV_BAR_TITLE", @"Title for settings activity"); self.title = NSLocalizedString(@"SETTINGS_NAV_BAR_TITLE", @"Title for settings activity");
@ -277,7 +277,8 @@
#pragma mark - Socket Status Notifications #pragma mark - Socket Status Notifications
- (void)initializeObserver { - (void)observeNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(socketStateDidChange) selector:@selector(socketStateDidChange)
name:kNSNotification_SocketManagerStateDidChange name:kNSNotification_SocketManagerStateDidChange

Loading…
Cancel
Save