removed unnecessary weak references in blocks

This commit is contained in:
michael starke
2017-09-02 09:42:28 +02:00
parent e8bdd3b2b8
commit f8b86a386a
2 changed files with 16 additions and 24 deletions

View File

@@ -275,9 +275,8 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou
} }
if(self.encrypted) { if(self.encrypted) {
__weak MPDocument *welf = self;
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[welf _handleFileChangeWithStrategy:MPFileChangeStrategyUseOther setAsDefault:NO]; [self _handleFileChangeWithStrategy:MPFileChangeStrategyUseOther setAsDefault:NO];
}); });
return; // Done! return; // Done!
} }
@@ -290,11 +289,10 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou
self.lockedForFileChange = YES; self.lockedForFileChange = YES;
/* Dispatch the alert to the main queue */ /* Dispatch the alert to the main queue */
__weak MPDocument *welf = self;
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
MPFileChangeStrategy strategy = (MPFileChangeStrategy)[NSUserDefaults.standardUserDefaults integerForKey:kMPSettingsKeyFileChangeStrategy]; MPFileChangeStrategy strategy = (MPFileChangeStrategy)[NSUserDefaults.standardUserDefaults integerForKey:kMPSettingsKeyFileChangeStrategy];
if(strategy != MPFileChangeStrategyAsk) { if(strategy != MPFileChangeStrategyAsk) {
[welf _handleFileChangeWithStrategy:strategy setAsDefault:NO]; [self _handleFileChangeWithStrategy:strategy setAsDefault:NO];
self.lockedForFileChange = NO; self.lockedForFileChange = NO;
return; return;
} }
@@ -307,22 +305,22 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou
[alert addButtonWithTitle:NSLocalizedString(@"FILE_CHANGE_STRATEGY_MERGE", @"Merge changes into file!")]; [alert addButtonWithTitle:NSLocalizedString(@"FILE_CHANGE_STRATEGY_MERGE", @"Merge changes into file!")];
[alert addButtonWithTitle:NSLocalizedString(@"KEEP_OTHER_DISCARD_MINE", @"Reopen the file!")]; [alert addButtonWithTitle:NSLocalizedString(@"KEEP_OTHER_DISCARD_MINE", @"Reopen the file!")];
[alert addButtonWithTitle:NSLocalizedString(@"KEEP_MINE_DISCARD_OTHER", @"Ignore the changes to an open file!")]; [alert addButtonWithTitle:NSLocalizedString(@"KEEP_MINE_DISCARD_OTHER", @"Ignore the changes to an open file!")];
[alert beginSheetModalForWindow:welf.windowForSheet completionHandler:^(NSModalResponse returnCode) { [alert beginSheetModalForWindow:self.windowForSheet completionHandler:^(NSModalResponse returnCode) {
BOOL useAsDefault = (alert.suppressionButton.state == NSOnState); BOOL useAsDefault = (alert.suppressionButton.state == NSOnState);
switch(returnCode) { switch(returnCode) {
case NSAlertFirstButtonReturn: { case NSAlertFirstButtonReturn: {
[welf _handleFileChangeWithStrategy:MPFileChangeStrategyMerge setAsDefault:useAsDefault]; [self _handleFileChangeWithStrategy:MPFileChangeStrategyMerge setAsDefault:useAsDefault];
break; break;
} }
case NSAlertSecondButtonReturn: case NSAlertSecondButtonReturn:
[welf _handleFileChangeWithStrategy:MPFileChangeStrategyUseOther setAsDefault:useAsDefault]; [self _handleFileChangeWithStrategy:MPFileChangeStrategyUseOther setAsDefault:useAsDefault];
break; break;
case NSAlertThirdButtonReturn: case NSAlertThirdButtonReturn:
[welf _handleFileChangeWithStrategy:MPFileChangeStrategyKeepMine setAsDefault:useAsDefault]; [self _handleFileChangeWithStrategy:MPFileChangeStrategyKeepMine setAsDefault:useAsDefault];
default: default:
break; break;
} }
welf.lockedForFileChange = NO; self.lockedForFileChange = NO;
}]; }];
}); });
}]; }];
@@ -383,14 +381,13 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou
backing:NSBackingStoreBuffered backing:NSBackingStoreBuffered
defer:NO]; defer:NO];
MPPasswordInputController *passwordInputController = [[MPPasswordInputController alloc] init]; MPPasswordInputController *passwordInputController = [[MPPasswordInputController alloc] init];
__weak MPDocument *welf = self;
[passwordInputController requestPasswordWithMessage:NSLocalizedString(@"EXTERN_CHANGE_OF_MASTERKEY", @"The master key was changed by an extrenal programm!") [passwordInputController requestPasswordWithMessage:NSLocalizedString(@"EXTERN_CHANGE_OF_MASTERKEY", @"The master key was changed by an extrenal programm!")
cancelLabel:NSLocalizedString(@"ABORT_MERGE_KEEP_MINE", @"Button label to abort a merge on a file with changed master key!") cancelLabel:NSLocalizedString(@"ABORT_MERGE_KEEP_MINE", @"Button label to abort a merge on a file with changed master key!")
completionHandler:^BOOL(NSString *password, NSURL *keyURL, BOOL didCancel, NSError *__autoreleasing *error) { completionHandler:^BOOL(NSString *password, NSURL *keyURL, BOOL didCancel, NSError *__autoreleasing *error) {
[welf.windowForSheet endSheet:sheet returnCode:(didCancel ? NSModalResponseCancel : NSModalResponseOK)]; [self.windowForSheet endSheet:sheet returnCode:(didCancel ? NSModalResponseCancel : NSModalResponseOK)];
if(!didCancel) { if(!didCancel) {
KPKCompositeKey *compositeKey = [[KPKCompositeKey alloc] initWithPassword:password key:keyURL]; KPKCompositeKey *compositeKey = [[KPKCompositeKey alloc] initWithPassword:password key:keyURL];
[welf mergeWithContentsFromURL:url key:compositeKey]; [self mergeWithContentsFromURL:url key:compositeKey];
} }
// just return yes regardless since we will display the sheet again if needed! // just return yes regardless since we will display the sheet again if needed!
return YES; return YES;

View File

@@ -208,17 +208,16 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
[alert addButtonWithTitle:NSLocalizedString(@"SAVE_LOSSY", "Save lossy")]; [alert addButtonWithTitle:NSLocalizedString(@"SAVE_LOSSY", "Save lossy")];
[alert addButtonWithTitle:NSLocalizedString(@"CHANGE_FORMAT", "")]; [alert addButtonWithTitle:NSLocalizedString(@"CHANGE_FORMAT", "")];
[alert addButtonWithTitle:NSLocalizedString(@"CANCEL", "Cancel")]; [alert addButtonWithTitle:NSLocalizedString(@"CANCEL", "Cancel")];
__weak MPDocumentWindowController *welf = self; [alert beginSheetModalForWindow:((NSDocument *)self.document).windowForSheet completionHandler:^(NSModalResponse returnCode) {
[alert beginSheetModalForWindow:[welf.document windowForSheet] completionHandler:^(NSModalResponse returnCode) {
switch(returnCode) { switch(returnCode) {
case NSAlertFirstButtonReturn: case NSAlertFirstButtonReturn:
/* Save lossy */ /* Save lossy */
[welf.document saveDocument:nil]; [self.document saveDocument:nil];
return; return;
case NSAlertSecondButtonReturn: case NSAlertSecondButtonReturn:
[alert.window orderOut:nil]; [alert.window orderOut:nil];
[welf.document saveDocumentAs:nil]; [self.document saveDocumentAs:nil];
return; return;
case NSAlertThirdButtonReturn: case NSAlertThirdButtonReturn:
@@ -323,12 +322,11 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
self.passwordInputController = [[MPPasswordInputController alloc] init]; self.passwordInputController = [[MPPasswordInputController alloc] init];
} }
[self _setContentViewController:self.passwordInputController]; [self _setContentViewController:self.passwordInputController];
__weak MPDocumentWindowController *welf = self;
[self.passwordInputController requestPasswordWithCompletionHandler:^BOOL(NSString *password, NSURL *keyURL, BOOL cancel, NSError *__autoreleasing *error) { [self.passwordInputController requestPasswordWithCompletionHandler:^BOOL(NSString *password, NSURL *keyURL, BOOL cancel, NSError *__autoreleasing *error) {
if(cancel) { if(cancel) {
return NO; return NO;
} }
return [((MPDocument *)welf.document) unlockWithPassword:password keyFileURL:keyURL error:error]; return [((MPDocument *)self.document) unlockWithPassword:password keyFileURL:keyURL error:error];
}]; }];
} }
@@ -403,7 +401,6 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
} }
- (void)duplicateEntryWithOptions:(id)sender { - (void)duplicateEntryWithOptions:(id)sender {
MPDuplicateEntryOptionsWindowController *wc = [[MPDuplicateEntryOptionsWindowController alloc] init]; MPDuplicateEntryOptionsWindowController *wc = [[MPDuplicateEntryOptionsWindowController alloc] init];
__weak MPDocumentWindowController *welf = self;
[self.window beginSheet:wc.window completionHandler:^(NSModalResponse returnCode) { [self.window beginSheet:wc.window completionHandler:^(NSModalResponse returnCode) {
if(returnCode == NSModalResponseOK) { if(returnCode == NSModalResponseOK) {
@@ -418,7 +415,7 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
if(wc.duplicateHistory) { if(wc.duplicateHistory) {
options |= kKPKCopyOptionCopyHistory; options |= kKPKCopyOptionCopyHistory;
} }
[((MPDocument *)welf.document) duplicateEntryWithOptions:options]; [((MPDocument *)self.document) duplicateEntryWithOptions:options];
} }
}]; }];
} }
@@ -555,9 +552,8 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
if(NSAlertSecondButtonReturn == returnCode) { if(NSAlertSecondButtonReturn == returnCode) {
return; return;
} }
id __weak welf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[welf editPassword:nil]; [self editPassword:nil];
}); });
}]; }];
} }
@@ -577,9 +573,8 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
if(returnCode == NSAlertSecondButtonReturn) { if(returnCode == NSAlertSecondButtonReturn) {
return; return;
} }
id __weak welf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[welf editPassword:nil]; [self editPassword:nil];
}); });
}]; }];
} }