mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			162 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			162 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			Matlab
		
	
| 
											9 years ago
										 | // | ||
| 
											8 years ago
										 | //  Copyright (c) 2018 Open Whisper Systems. All rights reserved. | ||
| 
											9 years ago
										 | // | ||
| 
											12 years ago
										 | 
 | ||
| 
											10 years ago
										 | #import "CountryCodeViewController.h" | ||
| 
											9 years ago
										 | #import "PhoneNumberUtil.h" | ||
| 
											9 years ago
										 | #import "UIColor+OWS.h" | ||
|  | #import "UIFont+OWS.h" | ||
| 
											9 years ago
										 | #import "UIView+OWS.h" | ||
| 
											8 years ago
										 | #import <SignalMessaging/NSString+OWS.h> | ||
| 
											12 years ago
										 | 
 | ||
| 
											9 years ago
										 | @interface CountryCodeViewController () <OWSTableViewControllerDelegate, UISearchBarDelegate> | ||
| 
											11 years ago
										 | 
 | ||
| 
											9 years ago
										 | @property (nonatomic, readonly) UISearchBar *searchBar; | ||
| 
											9 years ago
										 | 
 | ||
| 
											9 years ago
										 | @property (nonatomic) NSArray<NSString *> *countryCodes; | ||
| 
											9 years ago
										 | 
 | ||
| 
											12 years ago
										 | @end | ||
|  | 
 | ||
| 
											9 years ago
										 | #pragma mark - | ||
|  | 
 | ||
| 
											12 years ago
										 | @implementation CountryCodeViewController | ||
|  | 
 | ||
| 
											9 years ago
										 | - (void)loadView | ||
|  | { | ||
|  |     [super loadView]; | ||
|  | 
 | ||
| 
											9 years ago
										 |     self.view.backgroundColor = [UIColor whiteColor]; | ||
| 
											11 years ago
										 |     [self.navigationController.navigationBar setTranslucent:NO]; | ||
| 
											9 years ago
										 |     self.title = NSLocalizedString(@"COUNTRYCODE_SELECT_TITLE", @""); | ||
|  | 
 | ||
|  |     self.countryCodes = [PhoneNumberUtil countryCodesForSearchTerm:nil]; | ||
| 
											9 years ago
										 | 
 | ||
|  |     self.navigationItem.leftBarButtonItem = | ||
|  |         [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop | ||
|  |                                                       target:self | ||
|  |                                                       action:@selector(dismissWasPressed:)]; | ||
| 
											9 years ago
										 | 
 | ||
|  |     [self createViews]; | ||
| 
											12 years ago
										 | } | ||
|  | 
 | ||
| 
											9 years ago
										 | - (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]; | ||
| 
											12 years ago
										 | 
 | ||
| 
											9 years ago
										 |     self.tableView.tableHeaderView = searchBar; | ||
|  | 
 | ||
|  |     [self updateTableContents]; | ||
| 
											12 years ago
										 | } | ||
|  | 
 | ||
| 
											9 years ago
										 | #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]; | ||
|  |                              }]]; | ||
| 
											12 years ago
										 |     } | ||
| 
											10 years ago
										 | 
 | ||
| 
											9 years ago
										 |     [contents addSection:section]; | ||
| 
											10 years ago
										 | 
 | ||
| 
											9 years ago
										 |     self.contents = contents; | ||
| 
											12 years ago
										 | } | ||
|  | 
 | ||
| 
											9 years ago
										 | - (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]; | ||
| 
											11 years ago
										 |     [self.searchBar resignFirstResponder]; | ||
| 
											9 years ago
										 |     [self dismissViewControllerAnimated:YES completion:nil]; | ||
| 
											12 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (void)dismissWasPressed:(id)sender | ||
|  | { | ||
| 
											9 years ago
										 |     [self dismissViewControllerAnimated:YES completion:nil]; | ||
|  | } | ||
|  | 
 | ||
| 
											12 years ago
										 | #pragma mark - UISearchBarDelegate | ||
|  | 
 | ||
| 
											9 years ago
										 | - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText | ||
|  | { | ||
|  |     [self searchTextDidChange]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar | ||
|  | { | ||
|  |     [self searchTextDidChange]; | ||
| 
											12 years ago
										 | } | ||
|  | 
 | ||
| 
											9 years ago
										 | - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar | ||
|  | { | ||
|  |     [self searchTextDidChange]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar | ||
|  | { | ||
|  |     [self searchTextDidChange]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope | ||
|  | { | ||
|  |     [self searchTextDidChange]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)searchTextDidChange | ||
|  | { | ||
| 
											8 years ago
										 |     NSString *searchText = [self.searchBar.text ows_stripped]; | ||
| 
											9 years ago
										 | 
 | ||
|  |     self.countryCodes = [PhoneNumberUtil countryCodesForSearchTerm:searchText]; | ||
|  | 
 | ||
|  |     [self updateTableContents]; | ||
| 
											11 years ago
										 | } | ||
| 
											11 years ago
										 | 
 | ||
| 
											9 years ago
										 | #pragma mark - OWSTableViewControllerDelegate | ||
| 
											9 years ago
										 | 
 | ||
| 
											8 years ago
										 | - (void)tableViewWillBeginDragging | ||
| 
											9 years ago
										 | { | ||
| 
											9 years ago
										 |     [self.searchBar resignFirstResponder]; | ||
|  | } | ||
|  | 
 | ||
| 
											12 years ago
										 | @end |