From 867eb7d7412c238761520c73d614fbe4064c68aa Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 19 May 2017 23:48:48 -0400 Subject: [PATCH] Convert CountryCodeViewController. // FREEBIE --- Signal.xcodeproj/project.pbxproj | 6 - .../src/Storyboards/Registration.storyboard | 107 ----------- .../CountryCodeViewController.h | 6 +- .../CountryCodeViewController.m | 166 ++++++++++++------ .../RegistrationViewController.m | 5 +- .../SelectRecipientViewController.m | 5 +- Signal/src/views/CountryCodeTableViewCell.h | 10 -- Signal/src/views/CountryCodeTableViewCell.m | 19 -- 8 files changed, 116 insertions(+), 208 deletions(-) delete mode 100644 Signal/src/views/CountryCodeTableViewCell.h delete mode 100644 Signal/src/views/CountryCodeTableViewCell.m diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index ff8f1140b..bd24f5e7d 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -233,7 +233,6 @@ A1C32D5017A06538000A904E /* AddressBookUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A1C32D4F17A06537000A904E /* AddressBookUI.framework */; }; A1C32D5117A06544000A904E /* AddressBook.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A1C32D4D17A0652C000A904E /* AddressBook.framework */; }; A5509ECA1A69AB8B00ABA4BC /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A5509EC91A69AB8B00ABA4BC /* Main.storyboard */; }; - A5509ECD1A69B1D600ABA4BC /* CountryCodeTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = A5509ECC1A69B1D600ABA4BC /* CountryCodeTableViewCell.m */; }; A5E9D4BB1A65FAD800E4481C /* TSVideoAttachmentAdapter.m in Sources */ = {isa = PBXBuildFile; fileRef = A5E9D4B91A65FAD800E4481C /* TSVideoAttachmentAdapter.m */; }; AD41D7B51A6F6F0600241130 /* play_button.png in Resources */ = {isa = PBXBuildFile; fileRef = AD41D7B31A6F6F0600241130 /* play_button.png */; }; AD41D7B61A6F6F0600241130 /* play_button@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = AD41D7B41A6F6F0600241130 /* play_button@2x.png */; }; @@ -650,8 +649,6 @@ A1C32D4F17A06537000A904E /* AddressBookUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AddressBookUI.framework; path = System/Library/Frameworks/AddressBookUI.framework; sourceTree = SDKROOT; }; A1FDCBEE16DAA6C300868894 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; }; A5509EC91A69AB8B00ABA4BC /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = Main.storyboard; path = Storyboard/Main.storyboard; sourceTree = ""; }; - A5509ECB1A69B1D600ABA4BC /* CountryCodeTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CountryCodeTableViewCell.h; sourceTree = ""; }; - A5509ECC1A69B1D600ABA4BC /* CountryCodeTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CountryCodeTableViewCell.m; sourceTree = ""; }; A5E9D4B91A65FAD800E4481C /* TSVideoAttachmentAdapter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TSVideoAttachmentAdapter.m; sourceTree = ""; }; A5E9D4BA1A65FAD800E4481C /* TSVideoAttachmentAdapter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TSVideoAttachmentAdapter.h; sourceTree = ""; }; AD2AB1207E8888E4262D781B /* Pods-SignalTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SignalTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SignalTests/Pods-SignalTests.debug.xcconfig"; sourceTree = ""; }; @@ -1328,8 +1325,6 @@ 451764281DE939FD00EDB8B9 /* ContactCell.xib */, 76EB052E18170B33006006FC /* ContactTableViewCell.h */, 76EB052F18170B33006006FC /* ContactTableViewCell.m */, - A5509ECB1A69B1D600ABA4BC /* CountryCodeTableViewCell.h */, - A5509ECC1A69B1D600ABA4BC /* CountryCodeTableViewCell.m */, 45B201751DAECBFE00C461E0 /* HighlightableLabel.swift */, 4531C9C21DD8E6D800F08304 /* JSQMessagesCollectionViewCell+OWS.h */, 4531C9C31DD8E6D800F08304 /* JSQMessagesCollectionViewCell+OWS.m */, @@ -2038,7 +2033,6 @@ 34B3F8761E8DF1700035BE1A /* CodeVerificationViewController.m in Sources */, 76EB063E18170B33006006FC /* Operation.m in Sources */, 34B3F8741E8DF1700035BE1A /* AttachmentSharing.m in Sources */, - A5509ECD1A69B1D600ABA4BC /* CountryCodeTableViewCell.m in Sources */, 34D8C02B1ED3685800188D7C /* DebugUIContacts.m in Sources */, 45C9DEB81DF4E35A0065CA84 /* WebRTCCallMessageHandler.swift in Sources */, 34B3F88E1E8DF1700035BE1A /* PrivacySettingsTableViewController.m in Sources */, diff --git a/Signal/src/Storyboards/Registration.storyboard b/Signal/src/Storyboards/Registration.storyboard index dada992f3..ff03dee07 100644 --- a/Signal/src/Storyboards/Registration.storyboard +++ b/Signal/src/Storyboards/Registration.storyboard @@ -239,112 +239,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -391,7 +285,6 @@ - diff --git a/Signal/src/ViewControllers/CountryCodeViewController.h b/Signal/src/ViewControllers/CountryCodeViewController.h index c78134404..4ceaf10f7 100644 --- a/Signal/src/ViewControllers/CountryCodeViewController.h +++ b/Signal/src/ViewControllers/CountryCodeViewController.h @@ -2,7 +2,7 @@ // Copyright (c) 2017 Open Whisper Systems. All rights reserved. // -#import +#import "OWSTableViewController.h" @class CountryCodeViewController; @@ -17,8 +17,8 @@ #pragma mark - -@interface CountryCodeViewController : UIViewController +@interface CountryCodeViewController : OWSTableViewController -@property (nonatomic, weak) id delegate; +@property (nonatomic, weak) id countryCodeDelegate; @end diff --git a/Signal/src/ViewControllers/CountryCodeViewController.m b/Signal/src/ViewControllers/CountryCodeViewController.m index d2c5f98eb..e76652358 100644 --- a/Signal/src/ViewControllers/CountryCodeViewController.m +++ b/Signal/src/ViewControllers/CountryCodeViewController.m @@ -3,80 +3,108 @@ // #import "CountryCodeViewController.h" -#import "CountryCodeTableViewCell.h" -#import "FunctionalUtil.h" #import "PhoneNumberUtil.h" +#import "UIColor+OWS.h" +#import "UIFont+OWS.h" #import "UIView+OWS.h" -static NSString *const CONTRY_CODE_TABLE_CELL_IDENTIFIER = @"CountryCodeTableViewCell"; +@interface CountryCodeViewController () -@interface CountryCodeViewController () { - NSArray *_countryCodes; -} - -@property (nonatomic) IBOutlet UITableView *countryCodeTableView; -@property (nonatomic) IBOutlet UISearchBar *searchBar; +@property (nonatomic, readonly) UISearchBar *searchBar; -@property (nonatomic) NSString *countryCodeSelected; -@property (nonatomic) NSString *callingCodeSelected; -@property (nonatomic) NSString *countryNameSelected; +@property (nonatomic) NSArray *countryCodes; @end +#pragma mark - + @implementation CountryCodeViewController -- (void)viewDidLoad { - [super viewDidLoad]; +- (void)loadView +{ + [super loadView]; + self.view.backgroundColor = [UIColor whiteColor]; - [self.view addRedBorder]; [self.navigationController.navigationBar setTranslucent:NO]; - _countryCodes = [PhoneNumberUtil countryCodesForSearchTerm:nil]; - self.title = NSLocalizedString(@"COUNTRYCODE_SELECT_TITLE", @""); - self.searchBar.delegate = self; + self.title = NSLocalizedString(@"COUNTRYCODE_SELECT_TITLE", @""); + + self.countryCodes = [PhoneNumberUtil countryCodesForSearchTerm:nil]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(dismissWasPressed:)]; + + [self createViews]; } -#pragma mark - UITableViewDelegate +- (void)createViews +{ + // Search + UISearchBar *searchBar = [UISearchBar new]; + _searchBar = searchBar; + searchBar.searchBarStyle = UISearchBarStyleMinimal; + searchBar.delegate = self; + searchBar.placeholder = NSLocalizedString(@"SEARCH_BYNAMEORNUMBER_PLACEHOLDER_TEXT", @""); + searchBar.backgroundColor = [UIColor whiteColor]; + [searchBar sizeToFit]; -- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return (NSInteger)_countryCodes.count; + self.tableView.tableHeaderView = searchBar; + + [self updateTableContents]; } -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - CountryCodeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CONTRY_CODE_TABLE_CELL_IDENTIFIER]; - if (!cell) { - cell = [[CountryCodeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault - reuseIdentifier:CONTRY_CODE_TABLE_CELL_IDENTIFIER]; +#pragma mark - Table Contents + +- (void)updateTableContents +{ + OWSTableContents *contents = [OWSTableContents new]; + + __weak CountryCodeViewController *weakSelf = self; + OWSTableSection *section = [OWSTableSection new]; + + for (NSString *countryCode in self.countryCodes) { + OWSAssert(countryCode.length > 0); + OWSAssert([PhoneNumberUtil countryNameFromCountryCode:countryCode].length > 0); + OWSAssert([PhoneNumberUtil callingCodeFromCountryCode:countryCode].length > 0); + OWSAssert(![[PhoneNumberUtil callingCodeFromCountryCode:countryCode] isEqualToString:@"+0"]); + + [section addItem:[OWSTableItem itemWithCustomCellBlock:^{ + UITableViewCell *cell = [UITableViewCell new]; + cell.textLabel.text = [PhoneNumberUtil countryNameFromCountryCode:countryCode]; + cell.textLabel.font = [UIFont ows_regularFontWithSize:18.f]; + cell.textLabel.textColor = [UIColor blackColor]; + + UILabel *countryCodeLabel = [UILabel new]; + countryCodeLabel.text = [PhoneNumberUtil callingCodeFromCountryCode:countryCode]; + countryCodeLabel.font = [UIFont ows_regularFontWithSize:16.f]; + countryCodeLabel.textColor = [UIColor ows_darkGrayColor]; + [countryCodeLabel sizeToFit]; + cell.accessoryView = countryCodeLabel; + + return cell; + } + actionBlock:^{ + [weakSelf countryCodeWasSelected:countryCode]; + }]]; } - NSString *countryCode = _countryCodes[(NSUInteger)indexPath.row]; - OWSAssert(countryCode.length > 0); - OWSAssert([PhoneNumberUtil countryNameFromCountryCode:countryCode].length > 0); - OWSAssert([PhoneNumberUtil callingCodeFromCountryCode:countryCode].length > 0); - OWSAssert(![[PhoneNumberUtil callingCodeFromCountryCode:countryCode] isEqualToString:@"+0"]); - - [cell configureWithCountryCode:[PhoneNumberUtil callingCodeFromCountryCode:countryCode] - andCountryName:[PhoneNumberUtil countryNameFromCountryCode:countryCode]]; + [contents addSection:section]; - return cell; + self.contents = contents; } -- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { - NSString *countryCode = _countryCodes[(NSUInteger)indexPath.row]; - _callingCodeSelected = [PhoneNumberUtil callingCodeFromCountryCode:countryCode]; - _countryNameSelected = [PhoneNumberUtil countryNameFromCountryCode:countryCode]; - _countryCodeSelected = countryCode; - [self.delegate countryCodeViewController:self - didSelectCountryCode:_countryCodeSelected - countryName:_countryNameSelected - callingCode:_callingCodeSelected]; +- (void)countryCodeWasSelected:(NSString *)countryCode +{ + OWSAssert(countryCode.length > 0); + + NSString *callingCodeSelected = [PhoneNumberUtil callingCodeFromCountryCode:countryCode]; + NSString *countryNameSelected = [PhoneNumberUtil countryNameFromCountryCode:countryCode]; + NSString *countryCodeSelected = countryCode; + [self.countryCodeDelegate countryCodeViewController:self + didSelectCountryCode:countryCodeSelected + countryName:countryNameSelected + callingCode:callingCodeSelected]; [self.searchBar resignFirstResponder]; [self dismissViewControllerAnimated:YES completion:nil]; } @@ -85,24 +113,48 @@ static NSString *const CONTRY_CODE_TABLE_CELL_IDENTIFIER = @"CountryCodeTable [self dismissViewControllerAnimated:YES completion:nil]; } -- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { - return 44.0f; -} #pragma mark - UISearchBarDelegate -- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { - _countryCodes = [PhoneNumberUtil countryCodesForSearchTerm:searchText]; - [self.countryCodeTableView reloadData]; +- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText +{ + [self searchTextDidChange]; +} + +- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar +{ + [self searchTextDidChange]; } -- (void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope { - _countryCodes = [PhoneNumberUtil countryCodesForSearchTerm:searchText]; +- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar +{ + [self searchTextDidChange]; +} + +- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar +{ + [self searchTextDidChange]; +} + +- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope +{ + [self searchTextDidChange]; +} + +- (void)searchTextDidChange +{ + NSString *searchText = + [self.searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; + + self.countryCodes = [PhoneNumberUtil countryCodesForSearchTerm:searchText]; + + [self updateTableContents]; } -#pragma mark - UIScrollViewDelegate +#pragma mark - OWSTableViewControllerDelegate -- (void)scrollViewDidScroll:(UIScrollView *)scrollView { +- (void)tableViewDidScroll +{ [self.searchBar resignFirstResponder]; } diff --git a/Signal/src/ViewControllers/RegistrationViewController.m b/Signal/src/ViewControllers/RegistrationViewController.m index eb509661f..330a84458 100644 --- a/Signal/src/ViewControllers/RegistrationViewController.m +++ b/Signal/src/ViewControllers/RegistrationViewController.m @@ -194,9 +194,8 @@ static NSString *const kCodeSentSegue = @"codeSent"; - (void)changeCountryCodeTapped { - CountryCodeViewController *countryCodeController = [[UIStoryboard storyboardWithName:@"Registration" bundle:NULL] - instantiateViewControllerWithIdentifier:@"CountryCodeViewController"]; - countryCodeController.delegate = self; + CountryCodeViewController *countryCodeController = [CountryCodeViewController new]; + countryCodeController.countryCodeDelegate = self; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:countryCodeController]; [self presentViewController:navigationController animated:YES completion:[UIUtil modalCompletionBlock]]; diff --git a/Signal/src/ViewControllers/SelectRecipientViewController.m b/Signal/src/ViewControllers/SelectRecipientViewController.m index 9a7dcacf6..eaba770a4 100644 --- a/Signal/src/ViewControllers/SelectRecipientViewController.m +++ b/Signal/src/ViewControllers/SelectRecipientViewController.m @@ -284,9 +284,8 @@ NSString *const kSelectRecipientViewControllerCellIdentifier = @"kSelectRecipien - (void)showCountryCodeView:(nullable id)sender { - CountryCodeViewController *countryCodeController = [[UIStoryboard storyboardWithName:@"Registration" bundle:NULL] - instantiateViewControllerWithIdentifier:@"CountryCodeViewController"]; - countryCodeController.delegate = self; + CountryCodeViewController *countryCodeController = [CountryCodeViewController new]; + countryCodeController.countryCodeDelegate = self; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:countryCodeController]; [self presentViewController:navigationController animated:YES completion:[UIUtil modalCompletionBlock]]; diff --git a/Signal/src/views/CountryCodeTableViewCell.h b/Signal/src/views/CountryCodeTableViewCell.h deleted file mode 100644 index ba6c3eaf1..000000000 --- a/Signal/src/views/CountryCodeTableViewCell.h +++ /dev/null @@ -1,10 +0,0 @@ -#import - -@interface CountryCodeTableViewCell : UITableViewCell - -@property (nonatomic, strong) IBOutlet UILabel *countryCodeLabel; -@property (nonatomic, strong) IBOutlet UILabel *countryNameLabel; - -- (void)configureWithCountryCode:(NSString *)code andCountryName:(NSString *)name; - -@end diff --git a/Signal/src/views/CountryCodeTableViewCell.m b/Signal/src/views/CountryCodeTableViewCell.m deleted file mode 100644 index 10aec4321..000000000 --- a/Signal/src/views/CountryCodeTableViewCell.m +++ /dev/null @@ -1,19 +0,0 @@ -#import "CountryCodeTableViewCell.h" - -@implementation CountryCodeTableViewCell - -- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { - self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; - return self; -} - -- (NSString *)reuseIdentifier { - return NSStringFromClass(self.class); -} - -- (void)configureWithCountryCode:(NSString *)code andCountryName:(NSString *)name { - _countryCodeLabel.text = code; - _countryNameLabel.text = name; -} - -@end