DRY up safety number success and failure handling.

* Add “mark as verified” option to success case.

// FREEBIE
pull/1/head
Matthew Chen 9 years ago
parent cc5e810211
commit 8b9a1e41b4

@ -73,6 +73,7 @@ typedef void (^CustomLayoutBlock)();
@interface FingerprintViewController () <OWSCompareSafetyNumbersActivityDelegate> @interface FingerprintViewController () <OWSCompareSafetyNumbersActivityDelegate>
@property (nonatomic) NSString *recipientId; @property (nonatomic) NSString *recipientId;
@property (nonatomic) NSData *identityKey;
@property (nonatomic) TSStorageManager *storageManager; @property (nonatomic) TSStorageManager *storageManager;
@property (nonatomic) OWSFingerprint *fingerprint; @property (nonatomic) OWSFingerprint *fingerprint;
@property (nonatomic) NSString *contactName; @property (nonatomic) NSString *contactName;
@ -97,6 +98,7 @@ typedef void (^CustomLayoutBlock)();
OWSRecipientIdentity *_Nullable recipientIdentity = OWSRecipientIdentity *_Nullable recipientIdentity =
[[OWSIdentityManager sharedManager] recipientIdentityForRecipientId:recipientId]; [[OWSIdentityManager sharedManager] recipientIdentityForRecipientId:recipientId];
OWSAssert(recipientIdentity); OWSAssert(recipientIdentity);
self.identityKey = recipientIdentity.identityKey;
OWSFingerprintBuilder *builder = OWSFingerprintBuilder *builder =
[[OWSFingerprintBuilder alloc] initWithStorageManager:self.storageManager contactsManager:contactsManager]; [[OWSFingerprintBuilder alloc] initWithStorageManager:self.storageManager contactsManager:contactsManager];
@ -285,46 +287,23 @@ typedef void (^CustomLayoutBlock)();
- (void)showVerificationSucceeded - (void)showVerificationSucceeded
{ {
DDLogInfo(@"%@ Successfully verified privacy.", self.tag); [FingerprintViewScanController showVerificationSucceeded:self
NSString *successTitle = NSLocalizedString(@"SUCCESSFUL_VERIFICATION_TITLE", nil); identityKey:self.identityKey
NSString *dismissText = NSLocalizedString(@"DISMISS_BUTTON_TEXT", nil); recipientId:self.recipientId
NSString *descriptionFormat = NSLocalizedString( contactName:self.contactName
@"SUCCESSFUL_VERIFICATION_DESCRIPTION", @"Alert body after verifying privacy with {{other user's name}}"); tag:self.tag];
NSString *successDescription = [NSString stringWithFormat:descriptionFormat, self.contactName];
UIAlertController *successAlertController =
[UIAlertController alertControllerWithTitle:successTitle
message:successDescription
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:dismissText
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self dismissViewControllerAnimated:true completion:nil];
}];
[successAlertController addAction:dismissAction];
[self presentViewController:successAlertController animated:YES completion:nil];
} }
- (void)showVerificationFailedWithError:(NSError *)error - (void)showVerificationFailedWithError:(NSError *)error
{ {
NSString *_Nullable failureTitle;
if (error.code != OWSErrorCodeUserError) {
failureTitle = NSLocalizedString(@"FAILED_VERIFICATION_TITLE", @"alert title");
} // else no title. We don't want to show a big scary "VERIFICATION FAILED" when it's just user error.
UIAlertController *failureAlertController =
[UIAlertController alertControllerWithTitle:failureTitle
message:error.localizedDescription
preferredStyle:UIAlertControllerStyleAlert];
NSString *dismissText = NSLocalizedString(@"DISMISS_BUTTON_TEXT", nil);
UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:dismissText style:UIAlertActionStyleCancel handler: ^(UIAlertAction *action){
}];
[failureAlertController addAction:dismissAction];
[self presentViewController:failureAlertController animated:YES completion:nil];
DDLogWarn(@"%@ Identity verification failed with error: %@", self.tag, error); [FingerprintViewScanController showVerificationFailedWithError:error
viewController:self
retryBlock:nil
cancelBlock:^{
// Do nothing.
}
tag:self.tag];
} }
#pragma mark - Action #pragma mark - Action

@ -8,6 +8,18 @@ NS_ASSUME_NONNULL_BEGIN
- (void)configureWithRecipientId:(NSString *)recipientId NS_SWIFT_NAME(configure(recipientId:)); - (void)configureWithRecipientId:(NSString *)recipientId NS_SWIFT_NAME(configure(recipientId:));
+ (void)showVerificationSucceeded:(UIViewController *)viewController
identityKey:(NSData *)identityKey
recipientId:(NSString *)recipientId
contactName:(NSString *)contactName
tag:(NSString *)tag;
+ (void)showVerificationFailedWithError:(NSError *)error
viewController:(UIViewController *)viewController
retryBlock:(void (^_Nullable)())retryBlock
cancelBlock:(void (^_Nonnull)())cancelBlock
tag:(NSString *)tag;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -15,12 +15,15 @@
#import <SignalServiceKit/OWSError.h> #import <SignalServiceKit/OWSError.h>
#import <SignalServiceKit/OWSFingerprint.h> #import <SignalServiceKit/OWSFingerprint.h>
#import <SignalServiceKit/OWSFingerprintBuilder.h> #import <SignalServiceKit/OWSFingerprintBuilder.h>
#import <SignalServiceKit/OWSIdentityManager.h>
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@interface FingerprintViewScanController () <OWSQRScannerDelegate> @interface FingerprintViewScanController () <OWSQRScannerDelegate>
@property (nonatomic) TSStorageManager *storageManager; @property (nonatomic) TSStorageManager *storageManager;
@property (nonatomic) NSString *recipientId;
@property (nonatomic) NSData *identityKey;
@property (nonatomic) OWSFingerprint *fingerprint; @property (nonatomic) OWSFingerprint *fingerprint;
@property (nonatomic) NSString *contactName; @property (nonatomic) NSString *contactName;
@property (nonatomic) OWSQRCodeScanningViewController *qrScanningController; @property (nonatomic) OWSQRCodeScanningViewController *qrScanningController;
@ -33,6 +36,8 @@ NS_ASSUME_NONNULL_BEGIN
{ {
OWSAssert(recipientId.length > 0); OWSAssert(recipientId.length > 0);
self.recipientId = recipientId;
self.storageManager = [TSStorageManager sharedManager]; self.storageManager = [TSStorageManager sharedManager];
OWSContactsManager *contactsManager = [Environment getCurrent].contactsManager; OWSContactsManager *contactsManager = [Environment getCurrent].contactsManager;
@ -41,6 +46,7 @@ NS_ASSUME_NONNULL_BEGIN
OWSRecipientIdentity *_Nullable recipientIdentity = OWSRecipientIdentity *_Nullable recipientIdentity =
[[OWSIdentityManager sharedManager] recipientIdentityForRecipientId:recipientId]; [[OWSIdentityManager sharedManager] recipientIdentityForRecipientId:recipientId];
OWSAssert(recipientIdentity); OWSAssert(recipientIdentity);
self.identityKey = recipientIdentity.identityKey;
OWSFingerprintBuilder *builder = OWSFingerprintBuilder *builder =
[[OWSFingerprintBuilder alloc] initWithStorageManager:self.storageManager contactsManager:contactsManager]; [[OWSFingerprintBuilder alloc] initWithStorageManager:self.storageManager contactsManager:contactsManager];
@ -143,28 +149,84 @@ NS_ASSUME_NONNULL_BEGIN
- (void)showVerificationSucceeded - (void)showVerificationSucceeded
{ {
DDLogInfo(@"%@ Successfully verified privacy.", self.tag); [self.class showVerificationSucceeded:self
identityKey:self.identityKey
recipientId:self.recipientId
contactName:self.contactName
tag:self.tag];
}
- (void)showVerificationFailedWithError:(NSError *)error
{
[self.class showVerificationFailedWithError:error
viewController:self
retryBlock:^{
[self.qrScanningController startCapture];
}
cancelBlock:^{
[self.navigationController popViewControllerAnimated:YES];
}
tag:self.tag];
}
+ (void)showVerificationSucceeded:(UIViewController *)viewController
identityKey:(NSData *)identityKey
recipientId:(NSString *)recipientId
contactName:(NSString *)contactName
tag:(NSString *)tag
{
OWSAssert(viewController);
OWSAssert(identityKey.length > 0);
OWSAssert(recipientId.length > 0);
OWSAssert(contactName.length > 0);
OWSAssert(tag.length > 0);
DDLogInfo(@"%@ Successfully verified safety numbers.", tag);
NSString *successTitle = NSLocalizedString(@"SUCCESSFUL_VERIFICATION_TITLE", nil); NSString *successTitle = NSLocalizedString(@"SUCCESSFUL_VERIFICATION_TITLE", nil);
NSString *dismissText = NSLocalizedString(@"DISMISS_BUTTON_TEXT", nil); NSString *dismissText = NSLocalizedString(@"DISMISS_BUTTON_TEXT", nil);
NSString *descriptionFormat = NSLocalizedString( NSString *descriptionFormat = NSLocalizedString(
@"SUCCESSFUL_VERIFICATION_DESCRIPTION", @"Alert body after verifying privacy with {{other user's name}}"); @"SUCCESSFUL_VERIFICATION_DESCRIPTION", @"Alert body after verifying privacy with {{other user's name}}");
NSString *successDescription = [NSString stringWithFormat:descriptionFormat, self.contactName]; NSString *successDescription = [NSString stringWithFormat:descriptionFormat, contactName];
UIAlertController *successAlertController = UIAlertController *alertController = [UIAlertController alertControllerWithTitle:successTitle
[UIAlertController alertControllerWithTitle:successTitle message:successDescription
message:successDescription preferredStyle:UIAlertControllerStyleAlert];
preferredStyle:UIAlertControllerStyleAlert]; [alertController
UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:dismissText addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"FINGERPRINT_SCAN_VERIFY_BUTTON",
style:UIAlertActionStyleDefault @"Button that verifies user after a successful fingerprint scan.")
handler:^(UIAlertAction *action) { style:UIAlertActionStyleDefault
[self dismissViewControllerAnimated:true completion:nil]; handler:^(UIAlertAction *action) {
}]; [OWSIdentityManager.sharedManager
[successAlertController addAction:dismissAction]; setVerificationState:OWSVerificationStateVerified
identityKey:identityKey
recipientId:recipientId
sendSyncMessage:YES];
[viewController dismissViewControllerAnimated:true completion:nil];
}]];
UIAlertAction *dismissAction =
[UIAlertAction actionWithTitle:dismissText
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[viewController dismissViewControllerAnimated:true completion:nil];
}];
[alertController addAction:dismissAction];
[self presentViewController:successAlertController animated:YES completion:nil]; [viewController presentViewController:alertController animated:YES completion:nil];
} }
- (void)showVerificationFailedWithError:(NSError *)error + (void)showVerificationFailedWithError:(NSError *)error
viewController:(UIViewController *)viewController
retryBlock:(void (^_Nullable)())retryBlock
cancelBlock:(void (^_Nonnull)())cancelBlock
tag:(NSString *)tag
{ {
OWSAssert(viewController);
OWSAssert(cancelBlock);
OWSAssert(tag.length > 0);
DDLogInfo(@"%@ Failed to verify safety numbers.", tag);
NSString *_Nullable failureTitle; NSString *_Nullable failureTitle;
if (error.code != OWSErrorCodeUserError) { if (error.code != OWSErrorCodeUserError) {
failureTitle = NSLocalizedString(@"FAILED_VERIFICATION_TITLE", @"alert title"); failureTitle = NSLocalizedString(@"FAILED_VERIFICATION_TITLE", @"alert title");
@ -174,25 +236,27 @@ NS_ASSUME_NONNULL_BEGIN
message:error.localizedDescription message:error.localizedDescription
preferredStyle:UIAlertControllerStyleAlert]; preferredStyle:UIAlertControllerStyleAlert];
[alertController if (retryBlock) {
addAction:[UIAlertAction [alertController
actionWithTitle:NSLocalizedString(@"RETRY_BUTTON_TEXT", addAction:[UIAlertAction
@"Generic text for button that retries whatever the last action was.") actionWithTitle:NSLocalizedString(@"RETRY_BUTTON_TEXT",
style:UIAlertActionStyleDefault @"Generic text for button that retries whatever the last action was.")
handler:^(UIAlertAction *action) { style:UIAlertActionStyleDefault
[self.qrScanningController startCapture]; handler:^(UIAlertAction *action) {
}]]; retryBlock();
}]];
}
UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"TXT_CANCEL_TITLE", nil) UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"TXT_CANCEL_TITLE", nil)
style:UIAlertActionStyleCancel style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) { handler:^(UIAlertAction *action) {
[self.navigationController popViewControllerAnimated:YES]; cancelBlock();
}]; }];
[alertController addAction:dismissAction]; [alertController addAction:dismissAction];
[self presentViewController:alertController animated:YES completion:nil]; [viewController presentViewController:alertController animated:YES completion:nil];
DDLogWarn(@"%@ Identity verification failed with error: %@", self.tag, error); DDLogWarn(@"%@ Identity verification failed with error: %@", tag, error);
} }
- (void)dismissViewControllerAnimated:(BOOL)animated completion:(nullable void (^)(void))completion - (void)dismissViewControllerAnimated:(BOOL)animated completion:(nullable void (^)(void))completion

@ -523,6 +523,9 @@
/* alert title */ /* alert title */
"FAILED_VERIFICATION_TITLE" = "Failed to Verify Safety Number!"; "FAILED_VERIFICATION_TITLE" = "Failed to Verify Safety Number!";
/* Button that verifies user after a successful fingerprint scan. */
"FINGERPRINT_SCAN_VERIFY_BUTTON" = "Verify";
/* No comment provided by engineer. */ /* No comment provided by engineer. */
"FINGERPRINT_SHRED_KEYMATERIAL_BUTTON" = "Reset this session."; "FINGERPRINT_SHRED_KEYMATERIAL_BUTTON" = "Reset this session.";
@ -1298,7 +1301,7 @@
"SMS_INVITE_BODY" = "I'm inviting you to install Signal! Here is the link:"; "SMS_INVITE_BODY" = "I'm inviting you to install Signal! Here is the link:";
/* Alert body after verifying privacy with {{other user's name}} */ /* Alert body after verifying privacy with {{other user's name}} */
"SUCCESSFUL_VERIFICATION_DESCRIPTION" = "Your safety number with %@ has been verified. You can be confident your communication is private."; "SUCCESSFUL_VERIFICATION_DESCRIPTION" = "Your safety number with %@ has been verified. You can mark this contact as verified.";
/* No comment provided by engineer. */ /* No comment provided by engineer. */
"SUCCESSFUL_VERIFICATION_TITLE" = "Safety Number Verified!"; "SUCCESSFUL_VERIFICATION_TITLE" = "Safety Number Verified!";

Loading…
Cancel
Save