Procházet zdrojové kódy

Add more powerful alerts to the keychain screen

Tanner Bennett před 6 roky
rodič
revize
4be2b119d1

+ 85 - 40
Classes/GlobalStateExplorers/Keychain/FLEXKeychainTableViewController.m

@@ -23,29 +23,78 @@
 {
     [super viewDidLoad];
     
-    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(clearKeychain)];
-    
+    self.navigationItem.rightBarButtonItems = @[
+        [[UIBarButtonItem alloc]
+            initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(trashPressed)
+        ],
+        [[UIBarButtonItem alloc]
+            initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPressed)
+        ],
+    ];
+
+    [self refreshKeychainItems];
+}
+
+- (void)refreshKeychainItems
+{
     self.keyChainItems = [FLEXKeychain allAccounts];
     self.title = [NSString stringWithFormat:@"🔑 Keychain Items (%lu)", (unsigned long)self.keyChainItems.count];
 }
 
-- (void)clearKeychain
+#pragma mark Buttons
+
+- (void)trashPressed
 {
-    
-    for (id account in self.keyChainItems) {
-        FLEXKeychainQuery *query = [FLEXKeychainQuery new];
-        query.service = account[kFLEXKeychainWhereKey];
-        query.account = account[kFLEXKeychainAccountKey];
-        
-        if (![query deleteItem:nil]) {
-            NSLog(@"Delete Keychin Item Failed.");
-        }
-    }
-    
-    self.keyChainItems = [FLEXKeychain allAccounts];
-    [self.tableView reloadData];
+    [FLEXAlert makeSheet:^(FLEXAlert *make) {
+        make.title(@"Clear Keychain");
+        make.message(@"This will remove all keychain items for this app.\n");
+        make.message(@"This action cannot be undone. Are you sure?");
+        make.button(@"Yes, clear the keychain").destructiveStyle().handler(^(NSArray *strings) {
+            for (id account in self.keyChainItems) {
+                FLEXKeychainQuery *query = [FLEXKeychainQuery new];
+                query.service = account[kFLEXKeychainWhereKey];
+                query.account = account[kFLEXKeychainAccountKey];
+
+                // Delete item or display error
+                NSError *error = nil;
+                if (![query deleteItem:&error]) {
+                    [FLEXAlert makeAlert:^(FLEXAlert *make) {
+                        make.title(@"Error Deleting Item");
+                        make.message(error.localizedDescription);
+                    } showFrom:self];
+                }
+            }
+
+            [self refreshKeychainItems];
+            [self.tableView reloadData];
+        });
+        make.button(@"Cancel").cancelStyle();
+    } showFrom:self];
 }
 
+- (void)addPressed
+{
+    [FLEXAlert makeAlert:^(FLEXAlert *make) {
+        make.title(@"Add Keychain Item");
+        make.textField(@"Service name, i.e. Instagram");
+        make.textField(@"Account, i.e. username@example.com");
+        make.textField(@"Password");
+        make.button(@"Cancel").cancelStyle();
+        make.button(@"Save").handler(^(NSArray<NSString *> *strings) {
+            // Display errors
+            NSError *error = nil;
+            if (![FLEXKeychain setPassword:strings[2] forService:strings[0] account:strings[1] error:&error]) {
+                [FLEXAlert showAlert:@"Error" message:error.localizedDescription from:self];
+            }
+
+            [self refreshKeychainItems];
+            [self.tableView reloadData];
+        });
+    } showFrom:self];
+}
+
+
+
 #pragma mark - FLEXGlobalsEntry
 
 + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row
@@ -90,31 +139,27 @@
     NSDictionary *item = self.keyChainItems[indexPath.row];
     
     FLEXKeychainQuery *query = [FLEXKeychainQuery new];
-    query.service = [item valueForKey:kFLEXKeychainWhereKey];
-    query.account = [item valueForKey:kFLEXKeychainAccountKey];
+    query.service = item[kFLEXKeychainWhereKey];
+    query.account = item[kFLEXKeychainAccountKey];
     [query fetch:nil];
-    
-    NSString *msg = nil;
-    
-    if (query.password.length) {
-        msg = query.password;
-    } else if (query.passwordData.length) {
-        msg = query.passwordData.description;
-    } else {
-        msg = @"No data";
-    }
-    
-    UIAlertController *cv = [UIAlertController alertControllerWithTitle:@"Password" message:msg preferredStyle:UIAlertControllerStyleAlert];
-    [cv addAction:[UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
-        [cv dismissViewControllerAnimated:YES completion:nil];
-    }]];
-    
-    [cv addAction:[UIAlertAction actionWithTitle:@"Copy" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
-        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
-        pasteboard.string = msg;
-    }]];
-    
-    [self presentViewController:cv animated:YES completion:nil];
+
+    [FLEXAlert makeAlert:^(FLEXAlert *make) {
+        make.title(query.service);
+        make.message(@"Service: ").message(query.service);
+        make.message(@"\nAccount: ").message(query.account);
+        make.message(@"\nPassword: ").message(query.password);
+
+        make.button(@"Copy Service").handler(^(NSArray<NSString *> *strings) {
+            UIPasteboard.generalPasteboard.string = query.service;
+        });
+        make.button(@"Copy Account").handler(^(NSArray<NSString *> *strings) {
+            UIPasteboard.generalPasteboard.string = query.account;
+        });
+        make.button(@"Copy Password").handler(^(NSArray<NSString *> *strings) {
+            UIPasteboard.generalPasteboard.string = query.password;
+        });
+        make.button(@"Dismiss").cancelStyle();
+    } showFrom:self];
 }
 
 @end