From db10cbaeeabf1269e04f8292730db5101aab8437 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 11 May 2017 09:30:19 -0400 Subject: [PATCH] Convert AdvancedSettingsTableViewController to OWSTableView. // FREEBIE --- .../AdvancedSettingsTableViewController.h | 10 +- .../AdvancedSettingsTableViewController.m | 164 ++++++++---------- .../ViewControllers/OWSTableViewController.h | 2 + .../ViewControllers/OWSTableViewController.m | 18 ++ .../SettingsTableViewController.m | 5 +- 5 files changed, 97 insertions(+), 102 deletions(-) diff --git a/Signal/src/ViewControllers/AdvancedSettingsTableViewController.h b/Signal/src/ViewControllers/AdvancedSettingsTableViewController.h index c27e58a46..46c9b0ba4 100644 --- a/Signal/src/ViewControllers/AdvancedSettingsTableViewController.h +++ b/Signal/src/ViewControllers/AdvancedSettingsTableViewController.h @@ -1,13 +1,9 @@ // -// AdvancedSettingsTableViewController.h -// Signal -// -// Created by Dylan Bourgeois on 05/01/15. -// Copyright (c) 2015 Open Whisper Systems. All rights reserved. +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // -#import +#import "OWSTableViewController.h" -@interface AdvancedSettingsTableViewController : UITableViewController +@interface AdvancedSettingsTableViewController : OWSTableViewController @end diff --git a/Signal/src/ViewControllers/AdvancedSettingsTableViewController.m b/Signal/src/ViewControllers/AdvancedSettingsTableViewController.m index 87301b214..791fdf042 100644 --- a/Signal/src/ViewControllers/AdvancedSettingsTableViewController.m +++ b/Signal/src/ViewControllers/AdvancedSettingsTableViewController.m @@ -15,134 +15,111 @@ NS_ASSUME_NONNULL_BEGIN @interface AdvancedSettingsTableViewController () -@property (nonatomic) UITableViewCell *enableLogCell; -@property (nonatomic) UITableViewCell *submitLogCell; -@property (nonatomic) UITableViewCell *registerPushCell; - @property (nonatomic) UISwitch *enableLogSwitch; -@property (nonatomic, readonly) BOOL supportsCallKit; @end -typedef NS_ENUM(NSInteger, AdvancedSettingsTableViewControllerSection) { - AdvancedSettingsTableViewControllerSectionLogging, - AdvancedSettingsTableViewControllerSectionPushNotifications, - AdvancedSettingsTableViewControllerSection_Count // meta section -}; +#pragma mark - @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 { [super loadView]; 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 setOn:[PropertyListPreferences loggingIsEnabled]]; [self.enableLogSwitch addTarget:self action:@selector(didToggleEnableLogSwitch:) forControlEvents:UIControlEventValueChanged]; - self.enableLogCell.accessoryView = self.enableLogSwitch; - // Send Log - self.submitLogCell = [[UITableViewCell alloc] init]; - self.submitLogCell.textLabel.text = NSLocalizedString(@"SETTINGS_ADVANCED_SUBMIT_DEBUGLOG", @""); + [self observeNotifications]; - self.registerPushCell = [[UITableViewCell alloc] init]; - self.registerPushCell.textLabel.text = NSLocalizedString(@"REREGISTER_FOR_PUSH", nil); + [self updateTableContents]; } -#pragma mark - Table view data source - -- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { - return AdvancedSettingsTableViewControllerSection_Count; +- (void)observeNotifications +{ + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(socketStateDidChange) + name:kNSNotification_SocketManagerStateDidChange + object:nil]; } -- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - - AdvancedSettingsTableViewControllerSection settingsSection = (AdvancedSettingsTableViewControllerSection)section; - switch (settingsSection) { - case AdvancedSettingsTableViewControllerSectionLogging: - return self.enableLogSwitch.isOn ? 2 : 1; - case AdvancedSettingsTableViewControllerSectionPushNotifications: - return 1; - default: - return 0; - } +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; } -- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section +- (void)socketStateDidChange { - AdvancedSettingsTableViewControllerSection settingsSection = (AdvancedSettingsTableViewControllerSection)section; - switch (settingsSection) { - case AdvancedSettingsTableViewControllerSectionLogging: - 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; - } + OWSAssert([NSThread isMainThread]); + + [self updateTableContents]; } -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath +#pragma mark - Table Contents + +- (void)updateTableContents { - AdvancedSettingsTableViewControllerSection settingsSection = (AdvancedSettingsTableViewControllerSection)indexPath.section; - 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; - } -} + OWSTableContents *contents = [OWSTableContents new]; -- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { - [tableView deselectRowAtIndexPath:indexPath animated:YES]; - - if ([tableView cellForRowAtIndexPath:indexPath] == self.submitLogCell) { - DDLogInfo(@"%@ Submitting debug logs", self.tag); - [DDLog flushLog]; - [Pastelog submitLogs]; - } else if ([tableView cellForRowAtIndexPath:indexPath] == self.registerPushCell) { - OWSSyncPushTokensJob *job = - [[OWSSyncPushTokensJob alloc] initWithPushManager:[PushManager sharedManager] - accountManager:[Environment getCurrent].accountManager - preferences:[Environment preferences] - showAlerts:YES]; - job.uploadOnlyIfStale = NO; - [job run]; - } else { - DDLogDebug(@"%@ Ignoring cell selection at indexPath: %@", self.tag, indexPath); + __weak AdvancedSettingsTableViewController *weakSelf = self; + + OWSTableSection *loggingSection = [OWSTableSection new]; + loggingSection.headerTitle = NSLocalizedString(@"LOGGING_SECTION", nil); + [loggingSection addItem:[OWSTableItem itemWithCustomCellBlock:^{ + UITableViewCell *cell = [UITableViewCell new]; + cell.textLabel.text = NSLocalizedString(@"SETTINGS_ADVANCED_DEBUGLOG", @""); + cell.textLabel.font = [UIFont ows_regularFontWithSize:18.f]; + cell.textLabel.textColor = [UIColor blackColor]; + + cell.accessoryView = self.enableLogSwitch; + cell.selectionStyle = UITableViewCellSelectionStyleNone; + return cell; } + 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 +- (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 { if (!sender.isOn) { [[DebugLogger sharedLogger] wipeLogs]; @@ -152,7 +129,8 @@ typedef NS_ENUM(NSInteger, AdvancedSettingsTableViewControllerSection) { } [PropertyListPreferences setLoggingEnabled:sender.isOn]; - [self.tableView reloadData]; + + [self updateTableContents]; } #pragma mark - Logging diff --git a/Signal/src/ViewControllers/OWSTableViewController.h b/Signal/src/ViewControllers/OWSTableViewController.h index 416e415c1..51357dd8a 100644 --- a/Signal/src/ViewControllers/OWSTableViewController.h +++ b/Signal/src/ViewControllers/OWSTableViewController.h @@ -66,6 +66,8 @@ typedef UITableViewCell *_Nonnull (^OWSTableCustomCellBlock)(); + (OWSTableItem *)disclosureItemWithText:(NSString *)text actionBlock:(nullable OWSTableActionBlock)actionBlock; ++ (OWSTableItem *)actionItemWithText:(NSString *)text actionBlock:(nullable OWSTableActionBlock)actionBlock; + - (nullable UITableViewCell *)customCell; - (NSNumber *)customRowHeight; diff --git a/Signal/src/ViewControllers/OWSTableViewController.m b/Signal/src/ViewControllers/OWSTableViewController.m index 3d2c974dd..956212f5a 100644 --- a/Signal/src/ViewControllers/OWSTableViewController.m +++ b/Signal/src/ViewControllers/OWSTableViewController.m @@ -169,6 +169,24 @@ const CGFloat kOWSTable_DefaultCellHeight = 45.f; 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 { if (_customCell) { diff --git a/Signal/src/ViewControllers/SettingsTableViewController.m b/Signal/src/ViewControllers/SettingsTableViewController.m index 2d93a66d4..97fb9ad84 100644 --- a/Signal/src/ViewControllers/SettingsTableViewController.m +++ b/Signal/src/ViewControllers/SettingsTableViewController.m @@ -64,7 +64,7 @@ [self.navigationController.navigationBar setTranslucent:NO]; - [self initializeObserver]; + [self observeNotifications]; self.title = NSLocalizedString(@"SETTINGS_NAV_BAR_TITLE", @"Title for settings activity"); @@ -277,7 +277,8 @@ #pragma mark - Socket Status Notifications -- (void)initializeObserver { +- (void)observeNotifications +{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(socketStateDidChange) name:kNSNotification_SocketManagerStateDidChange