session-ios/SignalUtilitiesKit/Shared View Controllers/OWSTableViewController.m

841 lines
25 KiB
Mathematica
Raw Normal View History

//
2019-01-08 16:44:22 +01:00
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
#import "OWSTableViewController.h"
#import "OWSNavigationController.h"
#import "Theme.h"
2018-07-13 00:01:43 +02:00
#import "UIColor+OWS.h"
#import "UIFont+OWS.h"
2017-04-17 21:13:15 +02:00
#import "UIView+OWS.h"
2020-11-09 06:03:59 +01:00
#import <SessionUIKit/SessionUIKit.h>
NS_ASSUME_NONNULL_BEGIN
2017-04-17 21:13:15 +02:00
const CGFloat kOWSTable_DefaultCellHeight = 45.f;
@interface OWSTableContents ()
@property (nonatomic) NSMutableArray<OWSTableSection *> *sections;
@end
#pragma mark -
@implementation OWSTableContents
2017-04-03 23:12:29 +02:00
- (instancetype)init
{
if (self = [super init]) {
_sections = [NSMutableArray new];
}
return self;
}
2017-04-03 23:12:29 +02:00
- (void)addSection:(OWSTableSection *)section
{
OWSAssertDebug(section);
[_sections addObject:section];
}
@end
#pragma mark -
@interface OWSTableSection ()
@property (nonatomic) NSMutableArray<OWSTableItem *> *items;
@end
#pragma mark -
@implementation OWSTableSection
+ (OWSTableSection *)sectionWithTitle:(nullable NSString *)title items:(NSArray<OWSTableItem *> *)items
2017-04-03 23:12:29 +02:00
{
OWSTableSection *section = [OWSTableSection new];
section.headerTitle = title;
section.items = [items mutableCopy];
return section;
}
2017-04-03 23:12:29 +02:00
- (instancetype)init
{
if (self = [super init]) {
_items = [NSMutableArray new];
}
return self;
}
2017-04-03 23:12:29 +02:00
- (void)addItem:(OWSTableItem *)item
{
OWSAssertDebug(item);
2017-04-03 23:12:29 +02:00
[_items addObject:item];
}
- (NSUInteger)itemCount
{
return _items.count;
}
@end
#pragma mark -
@interface OWSTableItem ()
2017-04-03 23:12:29 +02:00
@property (nonatomic, nullable) NSString *title;
@property (nonatomic, nullable) OWSTableActionBlock actionBlock;
@property (nonatomic) OWSTableCustomCellBlock customCellBlock;
@property (nonatomic) UITableViewCell *customCell;
@property (nonatomic) NSNumber *customRowHeight;
@end
#pragma mark -
@implementation OWSTableItem
2018-07-13 00:01:43 +02:00
+ (UITableViewCell *)newCell
{
UITableViewCell *cell = [UITableViewCell new];
2018-07-13 00:38:55 +02:00
[self configureCell:cell];
return cell;
}
+ (void)configureCell:(UITableViewCell *)cell
{
2020-03-17 06:18:53 +01:00
cell.backgroundColor = LKColors.cellBackground;
2019-09-24 08:42:51 +02:00
if (@available(iOS 13, *)) {
cell.contentView.backgroundColor = UIColor.clearColor;
} else {
2020-03-17 06:18:53 +01:00
cell.contentView.backgroundColor = LKColors.cellBackground;
2019-09-24 08:42:51 +02:00
}
2020-03-17 06:18:53 +01:00
cell.textLabel.font = [UIFont systemFontOfSize:LKValues.mediumFontSize];
cell.textLabel.textColor = LKColors.text;
cell.detailTextLabel.font = [UIFont systemFontOfSize:LKValues.mediumFontSize];
cell.detailTextLabel.textColor = LKColors.text;
2018-07-13 00:22:25 +02:00
UIView *selectedBackgroundView = [UIView new];
2020-03-17 06:18:53 +01:00
selectedBackgroundView.backgroundColor = LKColors.cellSelected;
2018-07-13 00:22:25 +02:00
cell.selectedBackgroundView = selectedBackgroundView;
2018-07-13 00:01:43 +02:00
}
2017-04-05 15:09:09 +02:00
+ (OWSTableItem *)itemWithTitle:(NSString *)title actionBlock:(nullable OWSTableActionBlock)actionBlock
2017-04-03 23:12:29 +02:00
{
OWSAssertDebug(title.length > 0);
OWSTableItem *item = [OWSTableItem new];
item.actionBlock = actionBlock;
item.title = title;
return item;
}
+ (OWSTableItem *)itemWithCustomCell:(UITableViewCell *)customCell
customRowHeight:(CGFloat)customRowHeight
2017-04-05 15:09:09 +02:00
actionBlock:(nullable OWSTableActionBlock)actionBlock
{
OWSAssertDebug(customCell);
OWSAssertDebug(customRowHeight > 0 || customRowHeight == UITableViewAutomaticDimension);
OWSTableItem *item = [OWSTableItem new];
item.actionBlock = actionBlock;
item.customCell = customCell;
item.customRowHeight = @(customRowHeight);
return item;
}
+ (OWSTableItem *)itemWithCustomCellBlock:(OWSTableCustomCellBlock)customCellBlock
customRowHeight:(CGFloat)customRowHeight
2017-04-05 15:09:09 +02:00
actionBlock:(nullable OWSTableActionBlock)actionBlock
{
OWSAssertDebug(customRowHeight > 0 || customRowHeight == UITableViewAutomaticDimension);
OWSTableItem *item = [self itemWithCustomCellBlock:customCellBlock actionBlock:actionBlock];
item.customRowHeight = @(customRowHeight);
return item;
}
+ (OWSTableItem *)itemWithCustomCellBlock:(OWSTableCustomCellBlock)customCellBlock
2017-04-05 15:09:09 +02:00
actionBlock:(nullable OWSTableActionBlock)actionBlock
{
OWSAssertDebug(customCellBlock);
OWSTableItem *item = [OWSTableItem new];
item.actionBlock = actionBlock;
item.customCellBlock = customCellBlock;
return item;
}
+ (OWSTableItem *)disclosureItemWithText:(NSString *)text actionBlock:(nullable OWSTableActionBlock)actionBlock
2018-02-21 04:13:01 +01:00
{
return [self itemWithText:text actionBlock:actionBlock accessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
+ (OWSTableItem *)disclosureItemWithText:(NSString *)text
accessibilityIdentifier:(nullable NSString *)accessibilityIdentifier
actionBlock:(nullable OWSTableActionBlock)actionBlock
{
return [self itemWithText:text
accessibilityIdentifier:accessibilityIdentifier
actionBlock:actionBlock
accessoryType:UITableViewCellAccessoryNone];
}
2018-02-21 04:13:01 +01:00
+ (OWSTableItem *)checkmarkItemWithText:(NSString *)text actionBlock:(nullable OWSTableActionBlock)actionBlock
{
return [self checkmarkItemWithText:text accessibilityIdentifier:nil actionBlock:actionBlock];
}
+ (OWSTableItem *)checkmarkItemWithText:(NSString *)text
accessibilityIdentifier:(nullable NSString *)accessibilityIdentifier
actionBlock:(nullable OWSTableActionBlock)actionBlock;
{
return [self itemWithText:text
accessibilityIdentifier:accessibilityIdentifier
actionBlock:actionBlock
accessoryType:UITableViewCellAccessoryCheckmark];
2018-02-21 04:13:01 +01:00
}
+ (OWSTableItem *)itemWithText:(NSString *)text
actionBlock:(nullable OWSTableActionBlock)actionBlock
accessoryType:(UITableViewCellAccessoryType)accessoryType
{
return [self itemWithText:text accessibilityIdentifier:nil actionBlock:actionBlock accessoryType:accessoryType];
}
+ (OWSTableItem *)itemWithText:(NSString *)text
accessibilityIdentifier:(nullable NSString *)accessibilityIdentifier
actionBlock:(nullable OWSTableActionBlock)actionBlock
accessoryType:(UITableViewCellAccessoryType)accessoryType
{
OWSAssertDebug(text.length > 0);
OWSAssertDebug(actionBlock);
OWSTableItem *item = [OWSTableItem new];
item.actionBlock = actionBlock;
item.customCellBlock = ^{
2018-07-13 00:01:43 +02:00
UITableViewCell *cell = [OWSTableItem newCell];
cell.textLabel.text = text;
2018-02-21 04:13:01 +01:00
cell.accessoryType = accessoryType;
cell.accessibilityIdentifier = accessibilityIdentifier;
2020-03-17 06:18:53 +01:00
cell.tintColor = LKColors.accent;
return cell;
};
return item;
}
+ (OWSTableItem *)disclosureItemWithText:(NSString *)text
customRowHeight:(CGFloat)customRowHeight
actionBlock:(nullable OWSTableActionBlock)actionBlock
{
return [self disclosureItemWithText:text
accessibilityIdentifier:nil
customRowHeight:customRowHeight
actionBlock:actionBlock];
}
+ (OWSTableItem *)disclosureItemWithText:(NSString *)text
accessibilityIdentifier:(nullable NSString *)accessibilityIdentifier
customRowHeight:(CGFloat)customRowHeight
actionBlock:(nullable OWSTableActionBlock)actionBlock
{
OWSAssertDebug(customRowHeight > 0 || customRowHeight == UITableViewAutomaticDimension);
OWSTableItem *item =
[self disclosureItemWithText:text accessibilityIdentifier:accessibilityIdentifier actionBlock:actionBlock];
item.customRowHeight = @(customRowHeight);
return item;
}
2018-02-28 15:58:54 +01:00
+ (OWSTableItem *)disclosureItemWithText:(NSString *)text
detailText:(NSString *)detailText
actionBlock:(nullable OWSTableActionBlock)actionBlock
{
return [self disclosureItemWithText:text detailText:detailText accessibilityIdentifier:nil actionBlock:actionBlock];
}
+ (OWSTableItem *)disclosureItemWithText:(NSString *)text
detailText:(NSString *)detailText
accessibilityIdentifier:(nullable NSString *)accessibilityIdentifier
actionBlock:(nullable OWSTableActionBlock)actionBlock
2018-02-28 15:58:54 +01:00
{
OWSAssertDebug(text.length > 0);
OWSAssertDebug(actionBlock);
2018-02-28 15:58:54 +01:00
OWSTableItem *item = [OWSTableItem new];
item.actionBlock = actionBlock;
item.customCellBlock = ^{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:@"UITableViewCellStyleValue1"];
2018-07-13 00:38:55 +02:00
[OWSTableItem configureCell:cell];
2018-02-28 15:58:54 +01:00
cell.textLabel.text = text;
cell.detailTextLabel.text = detailText;
cell.accessibilityIdentifier = accessibilityIdentifier;
2018-02-28 15:58:54 +01:00
return cell;
};
return item;
}
2018-01-05 20:42:50 +01:00
+ (OWSTableItem *)subPageItemWithText:(NSString *)text actionBlock:(nullable OWSTableSubPageBlock)actionBlock
{
OWSAssertDebug(text.length > 0);
OWSAssertDebug(actionBlock);
2018-01-05 20:42:50 +01:00
OWSTableItem *item = [OWSTableItem new];
__weak OWSTableItem *weakItem = item;
item.actionBlock = ^{
OWSTableItem *strongItem = weakItem;
OWSAssertDebug(strongItem);
OWSAssertDebug(strongItem.tableViewController);
2018-01-05 20:42:50 +01:00
if (actionBlock) {
actionBlock(strongItem.tableViewController);
}
};
item.customCellBlock = ^{
2018-07-13 00:01:43 +02:00
UITableViewCell *cell = [OWSTableItem newCell];
2018-01-05 20:42:50 +01:00
cell.textLabel.text = text;
return cell;
};
return item;
}
+ (OWSTableItem *)subPageItemWithText:(NSString *)text
customRowHeight:(CGFloat)customRowHeight
actionBlock:(nullable OWSTableSubPageBlock)actionBlock
{
OWSAssertDebug(customRowHeight > 0 || customRowHeight == UITableViewAutomaticDimension);
2018-01-05 20:42:50 +01:00
OWSTableItem *item = [self subPageItemWithText:text actionBlock:actionBlock];
item.customRowHeight = @(customRowHeight);
return item;
}
+ (OWSTableItem *)actionItemWithText:(NSString *)text actionBlock:(nullable OWSTableActionBlock)actionBlock
{
return [self actionItemWithText:text accessibilityIdentifier:nil actionBlock:actionBlock];
}
+ (OWSTableItem *)actionItemWithText:(NSString *)text
accessibilityIdentifier:(nullable NSString *)accessibilityIdentifier
actionBlock:(nullable OWSTableActionBlock)actionBlock;
{
OWSAssertDebug(text.length > 0);
OWSAssertDebug(actionBlock);
OWSTableItem *item = [OWSTableItem new];
item.actionBlock = actionBlock;
item.customCellBlock = ^{
2018-07-13 00:01:43 +02:00
UITableViewCell *cell = [OWSTableItem newCell];
cell.textLabel.text = text;
cell.accessibilityIdentifier = accessibilityIdentifier;
return cell;
};
return item;
}
+ (OWSTableItem *)softCenterLabelItemWithText:(NSString *)text
{
OWSAssertDebug(text.length > 0);
OWSTableItem *item = [OWSTableItem new];
item.customCellBlock = ^{
2018-07-13 00:01:43 +02:00
UITableViewCell *cell = [OWSTableItem newCell];
cell.textLabel.text = text;
// These cells look quite different.
//
// Smaller font.
2020-03-17 06:18:53 +01:00
cell.textLabel.font = [UIFont systemFontOfSize:LKValues.mediumFontSize];
// Soft color.
2018-07-13 00:01:43 +02:00
// TODO: Theme, review with design.
2020-03-17 06:18:53 +01:00
cell.textLabel.textColor = LKColors.text;
// Centered.
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.userInteractionEnabled = NO;
return cell;
};
return item;
}
+ (OWSTableItem *)softCenterLabelItemWithText:(NSString *)text customRowHeight:(CGFloat)customRowHeight
{
OWSAssertDebug(customRowHeight > 0 || customRowHeight == UITableViewAutomaticDimension);
OWSTableItem *item = [self softCenterLabelItemWithText:text];
item.customRowHeight = @(customRowHeight);
return item;
}
+ (OWSTableItem *)labelItemWithText:(NSString *)text
{
OWSAssertDebug(text.length > 0);
OWSTableItem *item = [OWSTableItem new];
item.customCellBlock = ^{
2018-07-13 00:01:43 +02:00
UITableViewCell *cell = [OWSTableItem newCell];
cell.textLabel.text = text;
cell.userInteractionEnabled = NO;
return cell;
};
return item;
}
+ (OWSTableItem *)labelItemWithText:(NSString *)text accessoryText:(NSString *)accessoryText
{
OWSAssertDebug(text.length > 0);
OWSAssertDebug(accessoryText.length > 0);
OWSTableItem *item = [OWSTableItem new];
item.customCellBlock = ^{
2018-07-13 00:01:43 +02:00
UITableViewCell *cell = [OWSTableItem newCell];
cell.textLabel.text = text;
UILabel *accessoryLabel = [UILabel new];
accessoryLabel.text = accessoryText;
2020-03-17 06:18:53 +01:00
accessoryLabel.textColor = LKColors.text;
accessoryLabel.font = [UIFont systemFontOfSize:LKValues.mediumFontSize];
accessoryLabel.textAlignment = NSTextAlignmentRight;
[accessoryLabel sizeToFit];
cell.accessoryView = accessoryLabel;
cell.userInteractionEnabled = NO;
return cell;
};
return item;
}
2018-11-27 17:15:09 +01:00
+ (OWSTableItem *)longDisclosureItemWithText:(NSString *)text actionBlock:(nullable OWSTableActionBlock)actionBlock
{
OWSAssertDebug(text.length > 0);
OWSTableItem *item = [OWSTableItem new];
item.customCellBlock = ^{
UITableViewCell *cell = [OWSTableItem newCell];
cell.textLabel.text = text;
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
return cell;
};
item.customRowHeight = @(UITableViewAutomaticDimension);
item.actionBlock = actionBlock;
return item;
}
+ (OWSTableItem *)switchItemWithText:(NSString *)text
isOnBlock:(OWSTableSwitchBlock)isOnBlock
target:(id)target
selector:(SEL)selector
{
return [self switchItemWithText:text
isOnBlock:(OWSTableSwitchBlock)isOnBlock
isEnabledBlock:^{
return YES;
}
target:target
selector:selector];
}
+ (OWSTableItem *)switchItemWithText:(NSString *)text
isOnBlock:(OWSTableSwitchBlock)isOnBlock
isEnabledBlock:(OWSTableSwitchBlock)isEnabledBlock
target:(id)target
selector:(SEL)selector
{
return [self switchItemWithText:text
accessibilityIdentifier:nil
isOnBlock:isOnBlock
isEnabledBlock:isEnabledBlock
target:target
selector:selector];
}
+ (OWSTableItem *)switchItemWithText:(NSString *)text
accessibilityIdentifier:(nullable NSString *)accessibilityIdentifier
isOnBlock:(OWSTableSwitchBlock)isOnBlock
isEnabledBlock:(OWSTableSwitchBlock)isEnabledBlock
target:(id)target
selector:(SEL)selector
{
OWSAssertDebug(text.length > 0);
OWSAssertDebug(target);
OWSAssertDebug(selector);
OWSTableItem *item = [OWSTableItem new];
__weak id weakTarget = target;
item.customCellBlock = ^{
2018-07-13 00:01:43 +02:00
UITableViewCell *cell = [OWSTableItem newCell];
cell.textLabel.text = text;
UISwitch *cellSwitch = [UISwitch new];
cell.accessoryView = cellSwitch;
2020-03-17 06:18:53 +01:00
cellSwitch.onTintColor = LKColors.accent;
[cellSwitch setOn:isOnBlock()];
[cellSwitch addTarget:weakTarget action:selector forControlEvents:UIControlEventValueChanged];
cellSwitch.enabled = isEnabledBlock();
cellSwitch.accessibilityIdentifier = accessibilityIdentifier;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
};
return item;
}
2017-04-06 21:30:21 +02:00
- (nullable UITableViewCell *)customCell
{
if (_customCell) {
return _customCell;
}
if (_customCellBlock) {
return _customCellBlock();
}
return nil;
}
@end
#pragma mark -
2017-04-17 21:13:15 +02:00
@interface OWSTableViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic) UITableView *tableView;
@end
#pragma mark -
NSString *const kOWSTableCellIdentifier = @"kOWSTableCellIdentifier";
@implementation OWSTableViewController
- (instancetype)init
{
2017-05-04 19:51:39 +02:00
self = [super init];
if (!self) {
return self;
}
2017-05-04 19:51:39 +02:00
[self owsTableCommonInit];
2017-05-04 19:51:39 +02:00
return self;
}
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (!self) {
return self;
}
[self owsTableCommonInit];
return self;
}
- (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (!self) {
return self;
}
[self owsTableCommonInit];
return self;
}
- (void)owsTableCommonInit
2017-05-04 19:51:39 +02:00
{
_contents = [OWSTableContents new];
self.tableViewStyle = UITableViewStyleGrouped;
}
- (void)loadView
{
[super loadView];
OWSAssertDebug(self.contents);
if (self.contents.title.length > 0) {
self.title = self.contents.title;
}
2017-04-17 21:13:15 +02:00
2017-05-04 19:51:39 +02:00
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:self.tableViewStyle];
2017-04-17 21:13:15 +02:00
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
2018-04-09 20:56:33 +02:00
2017-04-17 21:13:15 +02:00
[self.view addSubview:self.tableView];
2018-07-17 15:25:42 +02:00
if ([self.tableView applyScrollViewInsetsFix]) {
// if applyScrollViewInsetsFix disables contentInsetAdjustmentBehavior,
// we need to pin to the top and bottom layout guides since UIKit
// won't adjust our content insets.
2020-06-05 05:43:06 +02:00
[self.tableView autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.view withOffset:0];
[self.tableView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.view withOffset:0];
2019-01-08 16:44:22 +01:00
[self.tableView autoPinEdgeToSuperviewSafeArea:ALEdgeLeading];
[self.tableView autoPinEdgeToSuperviewSafeArea:ALEdgeTrailing];
2018-07-17 15:25:42 +02:00
// We don't need a top or bottom insets, since we pin to the top and bottom layout guides.
self.automaticallyAdjustsScrollViewInsets = NO;
} else {
[self.tableView autoPinEdgesToSuperviewEdges];
}
2017-04-17 21:13:15 +02:00
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kOWSTableCellIdentifier];
2018-07-13 00:01:43 +02:00
[self applyTheme];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(themeDidChange:)
2018-07-23 21:03:07 +02:00
name:ThemeDidChangeNotification
2018-07-13 00:01:43 +02:00
object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (OWSTableSection *)sectionForIndex:(NSInteger)sectionIndex
{
OWSAssertDebug(self.contents);
OWSAssertDebug(sectionIndex >= 0 && sectionIndex < (NSInteger)self.contents.sections.count);
OWSTableSection *section = self.contents.sections[(NSUInteger)sectionIndex];
return section;
}
- (OWSTableItem *)itemForIndexPath:(NSIndexPath *)indexPath
{
OWSAssertDebug(self.contents);
OWSAssertDebug(indexPath.section >= 0 && indexPath.section < (NSInteger)self.contents.sections.count);
OWSTableSection *section = self.contents.sections[(NSUInteger)indexPath.section];
OWSAssertDebug(indexPath.item >= 0 && indexPath.item < (NSInteger)section.items.count);
OWSTableItem *item = section.items[(NSUInteger)indexPath.item];
return item;
}
- (void)setContents:(OWSTableContents *)contents
{
OWSAssertDebug(contents);
2017-12-19 17:38:25 +01:00
OWSAssertIsOnMainThread();
_contents = contents;
[self.tableView reloadData];
}
#pragma mark - Table view data source
2017-04-03 23:12:29 +02:00
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
OWSAssertDebug(self.contents);
return (NSInteger)self.contents.sections.count;
}
2017-04-03 23:12:29 +02:00
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
OWSTableSection *section = [self sectionForIndex:sectionIndex];
OWSAssertDebug(section.items);
return (NSInteger)section.items.count;
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)sectionIndex
{
OWSTableSection *section = [self sectionForIndex:sectionIndex];
return section.headerTitle;
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)sectionIndex
{
OWSTableSection *section = [self sectionForIndex:sectionIndex];
return section.footerTitle;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
OWSTableItem *item = [self itemForIndexPath:indexPath];
2018-01-05 20:42:50 +01:00
item.tableViewController = self;
UITableViewCell *customCell = [item customCell];
if (customCell) {
return customCell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kOWSTableCellIdentifier];
OWSAssertDebug(cell);
2018-08-09 17:23:13 +02:00
[OWSTableItem configureCell:cell];
cell.textLabel.text = item.title;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
OWSTableItem *item = [self itemForIndexPath:indexPath];
if (item.customRowHeight) {
return [item.customRowHeight floatValue];
}
2017-04-17 21:13:15 +02:00
return kOWSTable_DefaultCellHeight;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)sectionIndex
{
OWSTableSection *section = [self sectionForIndex:sectionIndex];
return section.customHeaderView;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)sectionIndex
{
OWSTableSection *section = [self sectionForIndex:sectionIndex];
return section.customFooterView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)sectionIndex
{
OWSTableSection *_Nullable section = [self sectionForIndex:sectionIndex];
if (!section) {
2018-08-27 16:29:51 +02:00
OWSFailDebug(@"Section index out of bounds.");
return 0;
}
if (section.customHeaderHeight) {
2017-04-17 21:13:15 +02:00
return [section.customHeaderHeight floatValue];
} else if (section.headerTitle.length > 0) {
return UITableViewAutomaticDimension;
} else {
return 0;
2017-04-17 21:13:15 +02:00
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)sectionIndex
{
OWSTableSection *_Nullable section = [self sectionForIndex:sectionIndex];
if (!section) {
2018-08-27 16:29:51 +02:00
OWSFailDebug(@"Section index out of bounds.");
return 0;
}
if (section.customFooterHeight) {
OWSAssertDebug([section.customFooterHeight floatValue] > 0);
2017-04-17 21:13:15 +02:00
return [section.customFooterHeight floatValue];
} else if (section.footerTitle.length > 0) {
return UITableViewAutomaticDimension;
} else {
return 0;
2017-04-17 21:13:15 +02:00
}
}
// Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
OWSTableItem *item = [self itemForIndexPath:indexPath];
if (!item.actionBlock) {
return nil;
}
return indexPath;
}
2017-04-03 23:12:29 +02:00
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
OWSTableItem *item = [self itemForIndexPath:indexPath];
if (item.actionBlock) {
item.actionBlock();
}
}
#pragma mark Index
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
if (self.contents.sectionForSectionIndexTitleBlock) {
return self.contents.sectionForSectionIndexTitleBlock(title, index);
} else {
return 0;
}
}
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
if (self.contents.sectionIndexTitlesForTableViewBlock) {
return self.contents.sectionIndexTitlesForTableViewBlock();
} else {
return 0;
}
}
#pragma mark - Presentation
2017-04-03 23:12:29 +02:00
- (void)presentFromViewController:(UIViewController *)fromViewController
{
OWSAssertDebug(fromViewController);
OWSNavigationController *navigationController = [[OWSNavigationController alloc] initWithRootViewController:self];
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self
action:@selector(donePressed:)];
[fromViewController presentViewController:navigationController animated:YES completion:nil];
}
2017-04-03 23:12:29 +02:00
- (void)donePressed:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.delegate tableViewWillBeginDragging];
}
2018-07-13 00:01:43 +02:00
#pragma mark - Theme
2018-08-08 21:49:22 +02:00
- (void)themeDidChange:(NSNotification *)notification
2018-07-13 00:01:43 +02:00
{
OWSAssertIsOnMainThread();
[self applyTheme];
[self.tableView reloadData];
}
- (void)applyTheme
{
OWSAssertIsOnMainThread();
2018-07-13 15:50:49 +02:00
self.view.backgroundColor = Theme.backgroundColor;
self.tableView.backgroundColor = Theme.backgroundColor;
2018-08-22 22:30:12 +02:00
self.tableView.separatorColor = Theme.cellSeparatorColor;
2018-07-13 00:01:43 +02:00
}
@end
NS_ASSUME_NONNULL_END