From f6dc2193de9ede2d68ddd555680ab34ef4bcb348 Mon Sep 17 00:00:00 2001 From: michael starke Date: Tue, 25 Feb 2014 22:49:41 +0100 Subject: [PATCH] Refactored Notification setup to use only the document as object --- MacPass/MPDocumentWindowController.h | 1 - MacPass/MPDocumentWindowController.m | 12 +++++++----- MacPass/MPEntryViewController.h | 3 ++- MacPass/MPEntryViewController.m | 23 ++++++++++++----------- MacPass/MPInspectorViewController.h | 2 +- MacPass/MPInspectorViewController.m | 3 +-- MacPass/MPOutlineViewController.h | 4 ++-- MacPass/MPOutlineViewController.m | 2 +- MacPass/MPToolbarDelegate.h | 4 +++- MacPass/MPToolbarDelegate.m | 8 +++++++- 10 files changed, 36 insertions(+), 26 deletions(-) diff --git a/MacPass/MPDocumentWindowController.h b/MacPass/MPDocumentWindowController.h index eb22ae98..c34fc8c2 100644 --- a/MacPass/MPDocumentWindowController.h +++ b/MacPass/MPDocumentWindowController.h @@ -31,7 +31,6 @@ - (void)showPasswordInput; #pragma mark Actions -- (IBAction)performFindPanelAction:(id)sender; - (IBAction)saveDocument:(id)sender; - (IBAction)editPassword:(id)sender; diff --git a/MacPass/MPDocumentWindowController.m b/MacPass/MPDocumentWindowController.m index 65737058..61daccda 100644 --- a/MacPass/MPDocumentWindowController.m +++ b/MacPass/MPDocumentWindowController.m @@ -81,9 +81,12 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didRevertDocument:) name:MPDocumentDidRevertNotifiation object:[self document]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showEntries) name:MPDocumentDidUnlockDatabaseNotification object:[self document]]; - [_entryViewController setupNotifications:self]; - [_inspectorViewController setupNotifications:self]; - [_outlineViewController setupNotifications:self]; + MPDocument *document = [self document]; + + [_entryViewController regsiterNotificationsForDocument:document]; + [_inspectorViewController regsiterNotificationsForDocument:document]; + [_outlineViewController regsiterNotificationsForDocument:document]; + [_toolbarDelegate regsiterNotificationsForDocument:document]; @@ -109,8 +112,7 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) { if(!showInspector) { [inspectorView removeFromSuperview]; } - - MPDocument *document = [self document]; + if(document.encrypted) { [self showPasswordInput]; } diff --git a/MacPass/MPEntryViewController.h b/MacPass/MPEntryViewController.h index d23714d5..a1393970 100644 --- a/MacPass/MPEntryViewController.h +++ b/MacPass/MPEntryViewController.h @@ -26,6 +26,7 @@ typedef NS_ENUM( NSUInteger, MPCopyContentTypeTag) { @class KPKEntry; @class MPOutlineViewDelegate; @class MPDocumentWindowController; +@class MPDocument; @interface MPEntryViewController : MPViewController @@ -33,7 +34,7 @@ typedef NS_ENUM( NSUInteger, MPCopyContentTypeTag) { @property (readonly, strong) NSArrayController *entryArrayController; /* Call this after alle viewcontroller are loaded */ -- (void)setupNotifications:(MPDocumentWindowController *)windowController; +- (void)regsiterNotificationsForDocument:(MPDocument *)document; /* Copy/Paste */ - (void)copyUsername:(id)sender; diff --git a/MacPass/MPEntryViewController.m b/MacPass/MPEntryViewController.m index 6b82b580..b4f2dc19 100644 --- a/MacPass/MPEntryViewController.m +++ b/MacPass/MPEntryViewController.m @@ -98,11 +98,6 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell"; _dataSource.viewController = self; _menuDelegate = [[MPEntryContextMenuDelegate alloc] init]; _contextBarViewController = [[MPContextBarViewController alloc] init]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(_updateSearchResults:) - name:MPDocumentDidChangeSearchNotification - object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showFilter:) name:MPDocumentDidEnterSearchNotification object:nil]; } return self; } @@ -196,12 +191,22 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell"; return self.entryTable; } -- (void)setupNotifications:(MPDocumentWindowController *)windowController { - MPDocument *document = [windowController document]; +- (void)regsiterNotificationsForDocument:(MPDocument *)document { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didChangeCurrentItem:) name:MPCurrentItemChangedNotification object:document]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(_updateSearchResults:) + name:MPDocumentDidChangeSearchNotification + object:document]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(showFilter:) + name:MPDocumentDidEnterSearchNotification + object:document]; + + } #pragma mark NSTableViewDelgate @@ -356,10 +361,6 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell"; } - (void)showFilter:(NSNotification *)notification { - MPDocument *currentDocument = [[self windowController] document]; - if(notification && [notification object] != currentDocument) { - return; // Wrong document - } [self.contextBarViewController showFilter]; [self _showContextBar]; } diff --git a/MacPass/MPInspectorViewController.h b/MacPass/MPInspectorViewController.h index 996dab56..e4244cea 100644 --- a/MacPass/MPInspectorViewController.h +++ b/MacPass/MPInspectorViewController.h @@ -26,6 +26,6 @@ - (IBAction)toggleEdit:(id)sender; /* Seperate call to ensure alle registered objects are in place */ -- (void)setupNotifications:(NSWindowController *)windowController; +- (void)regsiterNotificationsForDocument:(NSDocument *)document; @end diff --git a/MacPass/MPInspectorViewController.m b/MacPass/MPInspectorViewController.m index 940665ec..c2e1219b 100644 --- a/MacPass/MPInspectorViewController.m +++ b/MacPass/MPInspectorViewController.m @@ -107,8 +107,7 @@ typedef NS_ENUM(NSUInteger, MPContentTab) { [self _updateBindings:nil]; } -- (void)setupNotifications:(NSWindowController *)windowController { - MPDocument *document = [windowController document]; +- (void)regsiterNotificationsForDocument:(MPDocument *)document { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didChangeCurrentItem:) name:MPCurrentItemChangedNotification diff --git a/MacPass/MPOutlineViewController.h b/MacPass/MPOutlineViewController.h index 3d767782..4d38e6b5 100644 --- a/MacPass/MPOutlineViewController.h +++ b/MacPass/MPOutlineViewController.h @@ -12,7 +12,7 @@ APPKIT_EXTERN NSString *const MPOutlineViewDidChangeGroupSelection; @class MPOutlineViewDelegate; @class HNHGradientView; -@class MPDocumentWindowController; +@class MPDocument; @interface MPOutlineViewController : MPViewController @@ -21,7 +21,7 @@ APPKIT_EXTERN NSString *const MPOutlineViewDidChangeGroupSelection; - (void)clearSelection; - (void)showOutline; -- (void)setupNotifications:(MPDocumentWindowController *)windowController; +- (void)regsiterNotificationsForDocument:(MPDocument *)document; - (void)createGroup:(id)sender; - (void)createEntry:(id)sender; diff --git a/MacPass/MPOutlineViewController.m b/MacPass/MPOutlineViewController.m index 460e1f98..e28aa75b 100644 --- a/MacPass/MPOutlineViewController.m +++ b/MacPass/MPOutlineViewController.m @@ -136,7 +136,7 @@ NSString *const _MPOutlinveViewHeaderViewIdentifier = @"HeaderCell"; } #pragma mark Notifications -- (void)setupNotifications:(MPDocumentWindowController *)windowController { +- (void)regsiterNotificationsForDocument:(MPDocument *)document { // Nothing to do anymore } diff --git a/MacPass/MPToolbarDelegate.h b/MacPass/MPToolbarDelegate.h index c68ade84..5b270f4b 100644 --- a/MacPass/MPToolbarDelegate.h +++ b/MacPass/MPToolbarDelegate.h @@ -22,10 +22,12 @@ #import -@class MPDocumentWindowController; +@class MPDocument; @interface MPToolbarDelegate : NSObject @property (weak, readonly) NSSearchField *searchField; +- (void)regsiterNotificationsForDocument:(MPDocument *)document; + @end diff --git a/MacPass/MPToolbarDelegate.m b/MacPass/MPToolbarDelegate.m index e8c7d9e8..6287ceb4 100644 --- a/MacPass/MPToolbarDelegate.m +++ b/MacPass/MPToolbarDelegate.m @@ -66,7 +66,6 @@ NSString *const MPToolbarItemSearch = @"TOOLBAR_SEARCH"; _toolbarImages = [self createToolbarImages]; _toolbarItems = [[NSMutableDictionary alloc] initWithCapacity:[self.toolbarIdentifiers count]]; _entryMenuDelegate = [[MPAddEntryContextMenuDelegate alloc] init]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didExitSearch:) name:MPDocumentDidExitSearchNotification object:self]; } return self; } @@ -187,6 +186,12 @@ NSString *const MPToolbarItemSearch = @"TOOLBAR_SEARCH"; return imageDict; } + +- (void)regsiterNotificationsForDocument:(MPDocument *)document { + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didExitSearch:) name:MPDocumentDidExitSearchNotification object:document]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didExitSearch:) name:MPDocumentDidEnterSearchNotification object:document]; +} + - (NSString *)_localizedLabelForToolbarItemIdentifier:(NSString *)identifier { static NSDictionary *labelDict; static dispatch_once_t onceToken; @@ -220,6 +225,7 @@ NSString *const MPToolbarItemSearch = @"TOOLBAR_SEARCH"; - (void)_didExitSearch:(NSNotification *)notification { [self.searchField setStringValue:@""]; + [[self.searchField window] makeFirstResponder:nil]; } @end