From 94657cb09c0d376ddb3c249c0ea1e23f3ffee3f5 Mon Sep 17 00:00:00 2001 From: michael starke Date: Thu, 23 Oct 2014 00:29:36 +0200 Subject: [PATCH] MPOutlineViewController refactored to use MPTargetItemResolving --- MacPass/MPDocument.m | 3 +++ MacPass/MPOutlineViewController.h | 3 ++- MacPass/MPOutlineViewController.m | 36 ++++++++++++++++--------------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/MacPass/MPDocument.m b/MacPass/MPDocument.m index 86f00418..143f735f 100644 --- a/MacPass/MPDocument.m +++ b/MacPass/MPDocument.m @@ -533,6 +533,9 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey } - (void)deleteGroup:(KPKGroup *)group { + if(!group) { + return; // Nothing to do; + } if(self.useTrash) { if(!self.trash) { [self _createTrashGroup]; diff --git a/MacPass/MPOutlineViewController.h b/MacPass/MPOutlineViewController.h index 04fb8a11..bc49a042 100644 --- a/MacPass/MPOutlineViewController.h +++ b/MacPass/MPOutlineViewController.h @@ -7,13 +7,14 @@ // #import "MPViewController.h" +#import "MPTargetItemResolving.h" APPKIT_EXTERN NSString *const MPOutlineViewDidChangeGroupSelection; @class HNHGradientView; @class MPDocument; -@interface MPOutlineViewController : MPViewController +@interface MPOutlineViewController : MPViewController @property (weak) IBOutlet HNHGradientView *bottomBar; diff --git a/MacPass/MPOutlineViewController.m b/MacPass/MPOutlineViewController.m index 639e8520..7e2488bd 100644 --- a/MacPass/MPOutlineViewController.m +++ b/MacPass/MPOutlineViewController.m @@ -145,6 +145,15 @@ NSString *const _MPOutlinveViewHeaderViewIdentifier = @"HeaderCell"; } } +#pragma mark MPTargetItemResolving +- (KPKNode *)targetItemForAction { + NSInteger row = [self.outlineView clickedRow]; + if( row < 0 ) { + row = [self.outlineView selectedRow]; + } + return [[self.outlineView itemAtRow:row] representedObject]; +} + #pragma mark Notifications - (void)regsiterNotificationsForDocument:(MPDocument *)document { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didAddGroup:) name:MPDocumentDidAddGroupNotification object:document]; @@ -189,7 +198,7 @@ NSString *const _MPOutlinveViewHeaderViewIdentifier = @"HeaderCell"; #pragma mark Actions - (void)createGroup:(id)sender { - KPKGroup *group = [self _clickedOrSelectedGroup]; + KPKGroup *group = [[self targetItemForAction] asGroup]; MPDocument *document = [[self windowController] document]; if(!group) { group = document.root; @@ -199,11 +208,11 @@ NSString *const _MPOutlinveViewHeaderViewIdentifier = @"HeaderCell"; - (void)createEntry:(id)sender { MPDocument *document = [[self windowController] document]; - [document createEntry:[self _clickedOrSelectedGroup]]; + [document createEntry:[[self targetItemForAction] asGroup]]; } - (void)delete:(id)sender { - [[[self windowController] document] deleteGroup:[self _clickedOrSelectedGroup]]; + [[[self windowController] document] deleteGroup:[[self targetItemForAction] asGroup]]; } - (void)_doubleClickedGroup:(id)sender { @@ -281,21 +290,14 @@ NSString *const _MPOutlinveViewHeaderViewIdentifier = @"HeaderCell"; if(![document validateUserInterfaceItem:menuItem]) { return NO; } - id selected = [self _clickedOrSelectedGroup]; - if(!selected) { return NO; } - if(selected == document.trash) { return NO; } - return ![document isItemTrashed:selected]; -} - -#pragma mark - -#pragma mark Private - -- (KPKGroup *)_clickedOrSelectedGroup { - NSInteger row = [self.outlineView clickedRow]; - if( row < 0 ) { - row = [self.outlineView selectedRow]; + id selected = [[self targetItemForAction] asGroup]; + if(!selected) { + return NO; } - return [[self.outlineView itemAtRow:row] representedObject]; + if(selected == document.trash) { + return NO; + } + return ![document isItemTrashed:selected]; } - (NSMenu *)_contextMenu {