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.
		
		
		
		
		
			
		
			
	
	
		
			145 lines
		
	
	
		
			7.0 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			145 lines
		
	
	
		
			7.0 KiB
		
	
	
	
		
			Matlab
		
	
| 
											9 years ago
										 | // | ||
| 
											8 years ago
										 | //  Copyright (c) 2018 Open Whisper Systems. All rights reserved. | ||
| 
											9 years ago
										 | // | ||
|  | 
 | ||
|  | #import "ViewControllerUtils.h" | ||
| 
											8 years ago
										 | #import "NSString+OWS.h" | ||
| 
											9 years ago
										 | #import "PhoneNumber.h" | ||
| 
											9 years ago
										 | #import <AVFoundation/AVFoundation.h> | ||
| 
											8 years ago
										 | #import <SignalMessaging/Environment.h> | ||
| 
											9 years ago
										 | #import <SignalServiceKit/PhoneNumberUtil.h> | ||
|  | 
 | ||
|  | NS_ASSUME_NONNULL_BEGIN | ||
|  | 
 | ||
| 
											8 years ago
										 | NSString *const TappedStatusBarNotification = @"TappedStatusBarNotification"; | ||
|  | 
 | ||
| 
											8 years ago
										 | const NSUInteger kMin2FAPinLength = 4; | ||
| 
											8 years ago
										 | const NSUInteger kMax2FAPinLength = 16; | ||
| 
											9 years ago
										 | 
 | ||
|  | @implementation ViewControllerUtils | ||
|  | 
 | ||
|  | + (void)phoneNumberTextField:(UITextField *)textField | ||
|  |     shouldChangeCharactersInRange:(NSRange)range | ||
|  |                 replacementString:(NSString *)insertionText | ||
|  |                       countryCode:(NSString *)countryCode | ||
|  | { | ||
|  | 
 | ||
|  |     // Phone numbers takes many forms. | ||
|  |     // | ||
|  |     // * We only want to let the user enter decimal digits. | ||
|  |     // * The user shouldn't have to enter hyphen, parentheses or whitespace; | ||
|  |     //   the phone number should be formatted automatically. | ||
|  |     // * The user should be able to copy and paste freely. | ||
|  |     // * Invalid input should be simply ignored. | ||
|  |     // | ||
|  |     // We accomplish this by being permissive and trying to "take as much of the user | ||
|  |     // input as possible". | ||
|  |     // | ||
|  |     // * Always accept deletes. | ||
|  |     // * Ignore invalid input. | ||
|  |     // * Take partial input if possible. | ||
|  | 
 | ||
|  |     NSString *oldText = textField.text; | ||
|  |     // Construct the new contents of the text field by: | ||
|  |     // 1. Determining the "left" substring: the contents of the old text _before_ the deletion range. | ||
|  |     //    Filtering will remove non-decimal digit characters like hyphen "-". | ||
|  |     NSString *left = [oldText substringToIndex:range.location].digitsOnly; | ||
|  |     // 2. Determining the "right" substring: the contents of the old text _after_ the deletion range. | ||
|  |     NSString *right = [oldText substringFromIndex:range.location + range.length].digitsOnly; | ||
|  |     // 3. Determining the "center" substring: the contents of the new insertion text. | ||
|  |     NSString *center = insertionText.digitsOnly; | ||
|  |     // 4. Construct the "raw" new text by concatenating left, center and right. | ||
|  |     NSString *textAfterChange = [[left stringByAppendingString:center] stringByAppendingString:right]; | ||
| 
											8 years ago
										 |     // 4a. Ensure we don't exceed the maximum length for a e164 phone number, | ||
|  |     //     15 digits, per: https://en.wikipedia.org/wiki/E.164 | ||
|  |     // | ||
|  |     // NOTE: The actual limit is 18, not 15, because of certain invalid phone numbers in Germany. | ||
| 
											8 years ago
										 |     //       https://github.com/googlei18n/libphonenumber/blob/master/FALSEHOODS.md | ||
| 
											8 years ago
										 |     const int kMaxPhoneNumberLength = 18; | ||
|  |     if (textAfterChange.length > kMaxPhoneNumberLength) { | ||
|  |         textAfterChange = [textAfterChange substringToIndex:kMaxPhoneNumberLength]; | ||
|  |     } | ||
| 
											9 years ago
										 |     // 5. Construct the "formatted" new text by inserting a hyphen if necessary. | ||
|  |     // reformat the phone number, trying to keep the cursor beside the inserted or deleted digit | ||
|  |     bool isJustDeletion = insertionText.length == 0; | ||
| 
											9 years ago
										 |     NSUInteger cursorPositionAfterChange = MIN(left.length + center.length, textAfterChange.length); | ||
| 
											9 years ago
										 |     NSString *textAfterReformat = | ||
|  |         [PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:textAfterChange | ||
|  |                                                      withSpecifiedCountryCodeString:countryCode]; | ||
|  |     NSUInteger cursorPositionAfterReformat = [PhoneNumberUtil translateCursorPosition:cursorPositionAfterChange | ||
|  |                                                                                  from:textAfterChange | ||
|  |                                                                                    to:textAfterReformat | ||
|  |                                                                     stickingRightward:isJustDeletion]; | ||
|  |     textField.text = textAfterReformat; | ||
|  |     UITextPosition *pos = | ||
|  |         [textField positionFromPosition:textField.beginningOfDocument offset:(NSInteger)cursorPositionAfterReformat]; | ||
|  |     [textField setSelectedTextRange:[textField textRangeFromPosition:pos toPosition:pos]]; | ||
|  | } | ||
|  | 
 | ||
| 
											8 years ago
										 | + (void)ows2FAPINTextField:(UITextField *)textField | ||
|  |     shouldChangeCharactersInRange:(NSRange)range | ||
|  |                 replacementString:(NSString *)insertionText | ||
|  | { | ||
|  |     // * We only want to let the user enter decimal digits. | ||
|  |     // * The user should be able to copy and paste freely. | ||
|  |     // * Invalid input should be simply ignored. | ||
|  |     // | ||
|  |     // We accomplish this by being permissive and trying to "take as much of the user | ||
|  |     // input as possible". | ||
|  |     // | ||
|  |     // * Always accept deletes. | ||
|  |     // * Ignore invalid input. | ||
|  |     // * Take partial input if possible. | ||
|  | 
 | ||
|  |     NSString *oldText = textField.text; | ||
|  |     // Construct the new contents of the text field by: | ||
|  |     // 1. Determining the "left" substring: the contents of the old text _before_ the deletion range. | ||
|  |     //    Filtering will remove non-decimal digit characters. | ||
|  |     NSString *left = [oldText substringToIndex:range.location].digitsOnly; | ||
|  |     // 2. Determining the "right" substring: the contents of the old text _after_ the deletion range. | ||
|  |     NSString *right = [oldText substringFromIndex:range.location + range.length].digitsOnly; | ||
|  |     // 3. Determining the "center" substring: the contents of the new insertion text. | ||
|  |     NSString *center = insertionText.digitsOnly; | ||
|  |     // 4. Construct the "raw" new text by concatenating left, center and right. | ||
|  |     NSString *textAfterChange = [[left stringByAppendingString:center] stringByAppendingString:right]; | ||
|  |     // 5. Ensure we don't exceed the maximum length for a PIN. | ||
|  |     if (textAfterChange.length > kMax2FAPinLength) { | ||
|  |         textAfterChange = [textAfterChange substringToIndex:kMax2FAPinLength]; | ||
|  |     } | ||
|  |     // 6. Construct the final text. | ||
|  |     textField.text = textAfterChange; | ||
|  |     NSUInteger cursorPositionAfterChange = MIN(left.length + center.length, textAfterChange.length); | ||
|  |     UITextPosition *pos = | ||
|  |         [textField positionFromPosition:textField.beginningOfDocument offset:(NSInteger)cursorPositionAfterChange]; | ||
|  |     [textField setSelectedTextRange:[textField textRangeFromPosition:pos toPosition:pos]]; | ||
|  | } | ||
|  | 
 | ||
| 
											8 years ago
										 | + (NSString *)examplePhoneNumberForCountryCode:(NSString *)countryCode callingCode:(NSString *)callingCode | ||
|  | { | ||
|  |     OWSAssert(countryCode.length > 0); | ||
|  |     OWSAssert(callingCode.length > 0); | ||
|  | 
 | ||
|  |     NSString *examplePhoneNumber = [PhoneNumberUtil examplePhoneNumberForCountryCode:countryCode]; | ||
|  |     OWSAssert(!examplePhoneNumber || [examplePhoneNumber hasPrefix:callingCode]); | ||
|  |     if (examplePhoneNumber && [examplePhoneNumber hasPrefix:callingCode]) { | ||
|  |         NSString *formattedPhoneNumber = | ||
|  |             [PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:examplePhoneNumber | ||
|  |                                                          withSpecifiedCountryCodeString:countryCode]; | ||
|  |         if (formattedPhoneNumber.length > 0) { | ||
|  |             examplePhoneNumber = formattedPhoneNumber; | ||
|  |         } | ||
|  | 
 | ||
|  |         return [NSString | ||
|  |             stringWithFormat: | ||
|  |                 NSLocalizedString(@"PHONE_NUMBER_EXAMPLE_FORMAT", | ||
|  |                     @"A format for a label showing an example phone number. Embeds {{the example phone number}}."), | ||
|  |             [examplePhoneNumber substringFromIndex:callingCode.length]]; | ||
|  |     } else { | ||
|  |         return @""; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | @end | ||
|  | 
 | ||
|  | NS_ASSUME_NONNULL_END |