2017-03-31 04:32:56 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "BlockListViewController.h"
|
2017-03-31 05:05:26 +02:00
|
|
|
#import "AddToBlockListViewController.h"
|
2017-04-03 16:29:11 +02:00
|
|
|
#import "BlockListUIUtils.h"
|
2017-03-31 23:56:39 +02:00
|
|
|
#import "Environment.h"
|
|
|
|
#import "OWSContactsManager.h"
|
|
|
|
#import "PhoneNumber.h"
|
|
|
|
#import "UIFont+OWS.h"
|
2017-04-03 20:57:17 +02:00
|
|
|
#import <SignalServiceKit/OWSBlockingManager.h>
|
2017-03-31 04:32:56 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2017-03-31 05:05:26 +02:00
|
|
|
@interface BlockListViewController ()
|
2017-03-31 04:32:56 +02:00
|
|
|
|
2017-04-03 20:57:17 +02:00
|
|
|
@property (nonatomic, readonly) OWSBlockingManager *blockingManager;
|
2017-03-31 05:05:26 +02:00
|
|
|
@property (nonatomic, readonly) NSArray<NSString *> *blockedPhoneNumbers;
|
2017-03-31 04:32:56 +02:00
|
|
|
|
2017-03-31 23:56:39 +02:00
|
|
|
@property (nonatomic, readonly) OWSContactsManager *contactsManager;
|
|
|
|
@property (nonatomic) NSArray<Contact *> *contacts;
|
|
|
|
|
2017-03-31 04:32:56 +02:00
|
|
|
@end
|
|
|
|
|
2017-03-31 05:05:26 +02:00
|
|
|
#pragma mark -
|
|
|
|
|
2017-03-31 04:32:56 +02:00
|
|
|
typedef NS_ENUM(NSInteger, BlockListViewControllerSection) {
|
2017-03-31 05:05:26 +02:00
|
|
|
BlockListViewControllerSection_Add,
|
|
|
|
BlockListViewControllerSection_BlockList,
|
2017-03-31 04:32:56 +02:00
|
|
|
BlockListViewControllerSection_Count // meta section
|
|
|
|
};
|
|
|
|
|
|
|
|
@implementation BlockListViewController
|
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
return [super initWithStyle:UITableViewStyleGrouped];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)loadView
|
|
|
|
{
|
|
|
|
[super loadView];
|
2017-03-31 05:05:26 +02:00
|
|
|
|
2017-04-03 20:57:17 +02:00
|
|
|
_blockingManager = [OWSBlockingManager sharedManager];
|
2017-03-31 05:05:26 +02:00
|
|
|
_blockedPhoneNumbers = [_blockingManager blockedPhoneNumbers];
|
2017-03-31 23:56:39 +02:00
|
|
|
_contactsManager = [Environment getCurrent].contactsManager;
|
|
|
|
self.contacts = self.contactsManager.signalContacts;
|
2017-03-31 05:05:26 +02:00
|
|
|
|
2017-04-03 23:12:29 +02:00
|
|
|
self.title
|
|
|
|
= NSLocalizedString(@"SETTINGS_BLOCK_LIST_TITLE", @"Label for the block list section of the settings view");
|
2017-03-31 04:32:56 +02:00
|
|
|
|
2017-03-31 05:05:26 +02:00
|
|
|
[self addNotificationListeners];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addNotificationListeners
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(blockedPhoneNumbersDidChange:)
|
|
|
|
name:kNSNotificationName_BlockedPhoneNumbersDidChange
|
|
|
|
object:nil];
|
2017-03-31 23:56:39 +02:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(signalRecipientsDidChange:)
|
|
|
|
name:OWSContactsManagerSignalRecipientsDidChangeNotification
|
|
|
|
object:nil];
|
2017-03-31 05:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidLoad
|
|
|
|
{
|
|
|
|
[super viewDidLoad];
|
|
|
|
[self.navigationController.navigationBar setTranslucent:NO];
|
2017-03-31 04:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Table view data source
|
|
|
|
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
|
|
|
|
{
|
|
|
|
return BlockListViewControllerSection_Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
|
|
|
|
{
|
2017-03-31 05:05:26 +02:00
|
|
|
switch (section) {
|
|
|
|
case BlockListViewControllerSection_Add:
|
2017-03-31 04:32:56 +02:00
|
|
|
return 1;
|
2017-03-31 05:05:26 +02:00
|
|
|
case BlockListViewControllerSection_BlockList:
|
|
|
|
return (NSInteger) _blockedPhoneNumbers.count;
|
2017-03-31 04:32:56 +02:00
|
|
|
default:
|
2017-03-31 05:05:26 +02:00
|
|
|
OWSAssert(0);
|
2017-03-31 04:32:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
|
|
|
|
{
|
2017-03-31 05:05:26 +02:00
|
|
|
switch (section) {
|
|
|
|
case BlockListViewControllerSection_Add:
|
2017-03-31 04:32:56 +02:00
|
|
|
return NSLocalizedString(
|
2017-03-31 05:05:26 +02:00
|
|
|
@"SETTINGS_BLOCK_LIST_HEADER_TITLE", @"A header title for the block list table.");
|
2017-03-31 04:32:56 +02:00
|
|
|
default:
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
2017-04-03 23:12:29 +02:00
|
|
|
UITableViewCell *cell = [UITableViewCell new];
|
2017-03-31 05:05:26 +02:00
|
|
|
OWSAssert(cell);
|
|
|
|
|
|
|
|
switch (indexPath.section) {
|
|
|
|
case BlockListViewControllerSection_Add:
|
|
|
|
cell.textLabel.text = NSLocalizedString(
|
|
|
|
@"SETTINGS_BLOCK_LIST_ADD_BUTTON", @"A label for the 'add phone number' button in the block list table.");
|
2017-04-04 15:32:26 +02:00
|
|
|
cell.textLabel.font = [UIFont ows_regularFontWithSize:18.f];
|
2017-03-31 05:05:26 +02:00
|
|
|
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
|
|
|
|
break;
|
|
|
|
case BlockListViewControllerSection_BlockList: {
|
2017-03-31 23:56:39 +02:00
|
|
|
NSString *displayName = [self displayNameForIndexPath:indexPath];
|
|
|
|
cell.textLabel.text = displayName;
|
2017-04-04 15:32:26 +02:00
|
|
|
cell.textLabel.font = [UIFont ows_regularFontWithSize:18.f];
|
2017-03-31 05:05:26 +02:00
|
|
|
cell.accessoryType = UITableViewCellAccessoryCheckmark;
|
|
|
|
break;
|
|
|
|
}
|
2017-03-31 04:32:56 +02:00
|
|
|
default:
|
2017-03-31 05:05:26 +02:00
|
|
|
OWSAssert(0);
|
|
|
|
return 0;
|
2017-03-31 04:32:56 +02:00
|
|
|
}
|
2017-03-31 05:05:26 +02:00
|
|
|
|
|
|
|
return cell;
|
2017-03-31 04:32:56 +02:00
|
|
|
}
|
|
|
|
|
2017-03-31 23:56:39 +02:00
|
|
|
- (NSString *)displayNameForIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
NSString *phoneNumber = _blockedPhoneNumbers[(NSUInteger)indexPath.item];
|
2017-04-04 16:19:47 +02:00
|
|
|
NSString *displayName = [_contactsManager displayNameForPhoneIdentifier:phoneNumber];
|
2017-03-31 23:56:39 +02:00
|
|
|
return displayName;
|
|
|
|
}
|
|
|
|
|
2017-03-31 04:32:56 +02:00
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
|
|
|
2017-03-31 05:05:26 +02:00
|
|
|
switch (indexPath.section) {
|
|
|
|
case BlockListViewControllerSection_Add:
|
|
|
|
{
|
|
|
|
AddToBlockListViewController *vc = [[AddToBlockListViewController alloc] init];
|
|
|
|
NSAssert(self.navigationController != nil, @"Navigation controller must not be nil");
|
|
|
|
NSAssert(vc != nil, @"Privacy Settings View Controller must not be nil");
|
|
|
|
[self.navigationController pushViewController:vc animated:YES];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BlockListViewControllerSection_BlockList: {
|
|
|
|
NSString *phoneNumber = _blockedPhoneNumbers[(NSUInteger)indexPath.item];
|
2017-04-03 16:29:11 +02:00
|
|
|
[BlockListUIUtils showUnblockPhoneNumberActionSheet:phoneNumber
|
|
|
|
fromViewController:self
|
|
|
|
blockingManager:_blockingManager
|
2017-04-04 16:19:47 +02:00
|
|
|
contactsManager:_contactsManager
|
2017-04-03 16:29:11 +02:00
|
|
|
completionBlock:nil];
|
2017-03-31 05:05:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
OWSAssert(0);
|
2017-03-31 04:32:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Actions
|
|
|
|
|
2017-03-31 05:05:26 +02:00
|
|
|
- (void)blockedPhoneNumbersDidChange:(id)notification
|
2017-03-31 04:32:56 +02:00
|
|
|
{
|
2017-03-31 05:05:26 +02:00
|
|
|
_blockedPhoneNumbers = [_blockingManager blockedPhoneNumbers];
|
2017-03-31 04:32:56 +02:00
|
|
|
|
|
|
|
[self.tableView reloadData];
|
|
|
|
}
|
|
|
|
|
2017-03-31 23:56:39 +02:00
|
|
|
- (void)signalRecipientsDidChange:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[self updateContacts];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateContacts
|
|
|
|
{
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
self.contacts = self.contactsManager.signalContacts;
|
|
|
|
[self.tableView reloadData];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-31 04:32:56 +02:00
|
|
|
#pragma mark - Logging
|
|
|
|
|
|
|
|
+ (NSString *)tag
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"[%@]", self.class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)tag
|
|
|
|
{
|
|
|
|
return self.class.tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|