Removed unnecessary object controller, moving bindings to xib

This commit is contained in:
michael starke
2016-08-23 18:34:31 +02:00
parent d640434012
commit b7fe98b410
3 changed files with 57 additions and 68 deletions

View File

@@ -50,7 +50,7 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
@property (nonatomic, assign) BOOL showPassword;
@property (nonatomic, assign) MPEntryTab activeTab;
@property (strong) NSPopover *activePopover;
@property (strong) NSObjectController *entryController;
@property (nonatomic, readonly) KPKEntry *representedEntry;
//@property (nonatomic, weak) KPKEntry *entry;
@@ -83,15 +83,13 @@ static NSString *kMPContentBindingString3 = @"content.%@.%@.%@";
_attachmentTableDelegate.viewController = self;
_customFieldTableDelegate.viewController = self;
_activeTab = MPEntryTabGeneral;
_entryController = [[NSObjectController alloc] init];
_entryController.objectClass = [KPKEntry class];
}
return self;
}
- (KPKEntry *)contentEntry {
if([self.entryController.content isKindOfClass:[KPKEntry class]]) {
return self.entryController.content;
- (KPKEntry *)representedEntry {
if([self.representedObject isKindOfClass:[KPKEntry class]]) {
return self.representedObject;
}
return nil;
}
@@ -129,7 +127,7 @@ static NSString *kMPContentBindingString3 = @"content.%@.%@.%@";
}
- (void)setupBindings:(MPDocument *)document {
[self.entryController bind:NSContentObjectBinding toObject:self withKeyPath:NSStringFromSelector(@selector(representedObject)) options:nil];
//[self.entryController bind:NSContentObjectBinding toObject:self withKeyPath:NSStringFromSelector(@selector(representedObject)) options:nil];
}
- (void)registerNotificationsForDocument:(MPDocument *)document {
@@ -147,13 +145,12 @@ static NSString *kMPContentBindingString3 = @"content.%@.%@.%@";
#pragma mark Actions
- (void)addCustomField:(id)sender {
MPDocument *document = [[self windowController] document];
[document createCustomAttribute:self.entryController.content];
[self.windowController.document createCustomAttribute:self.representedObject];
}
- (void)removeCustomField:(id)sender {
NSUInteger index = [sender tag];
KPKAttribute *attribute = self.contentEntry.customAttributes[index];
[self.contentEntry removeCustomAttribute:attribute];
KPKAttribute *attribute = self.representedEntry.customAttributes[index];
[self.representedEntry removeCustomAttribute:attribute];
}
- (void)saveAttachment:(id)sender {
@@ -161,7 +158,7 @@ static NSString *kMPContentBindingString3 = @"content.%@.%@.%@";
if(row < 0) {
return; // No selection
}
KPKBinary *binary = self.contentEntry.binaries[row];
KPKBinary *binary = self.representedEntry.binaries[row];
NSSavePanel *savePanel = [NSSavePanel savePanel];
savePanel.canCreateDirectories = YES;
savePanel.nameFieldStringValue = binary.name;
@@ -186,7 +183,7 @@ static NSString *kMPContentBindingString3 = @"content.%@.%@.%@";
if(result == NSFileHandlingPanelOKButton) {
for (NSURL *attachmentURL in openPanel.URLs) {
KPKBinary *binary = [[KPKBinary alloc] initWithContentsOfURL:attachmentURL];
[self.contentEntry addBinary:binary];
[self.representedEntry addBinary:binary];
}
}
}];
@@ -198,18 +195,18 @@ static NSString *kMPContentBindingString3 = @"content.%@.%@.%@";
return; // no selection
}
KPKBinary *binary = self.contentEntry.binaries[row];
[self.contentEntry removeBinary:binary];
[self.representedEntry removeBinary:binary];
}
- (void)addWindowAssociation:(id)sender {
KPKWindowAssociation *associtation = [[KPKWindowAssociation alloc] initWithWindowTitle:NSLocalizedString(@"DEFAULT_WINDOW_TITLE", "") keystrokeSequence:nil];
[self.contentEntry.autotype addAssociation:associtation];
[self.representedEntry.autotype addAssociation:associtation];
}
- (void)removeWindowAssociation:(id)sender {
NSInteger row = self.windowAssociationsTableView.selectedRow;
if(row > - 1 && row < [self.contentEntry.autotype.associations count]) {
[self.contentEntry.autotype removeAssociation:self.contentEntry.autotype.associations[row]];
[self.representedEntry.autotype removeAssociation:self.contentEntry.autotype.associations[row]];
}
}
@@ -262,7 +259,7 @@ static NSString *kMPContentBindingString3 = @"content.%@.%@.%@";
- (void)_updatePreviewItemForPanel:(QLPreviewPanel *)panel {
NSInteger row = [self.attachmentTableView selectedRow];
NSAssert(row > -1, @"Row needs to be selected");
KPKBinary *binary = self.contentEntry.binaries[row];
KPKBinary *binary = self.representedEntry.binaries[row];
MPTemporaryFileStorage *oldStorage = (MPTemporaryFileStorage *)panel.dataSource;
[[MPTemporaryFileStorageCenter defaultCenter] unregisterStorage:oldStorage];
panel.dataSource = [[MPTemporaryFileStorageCenter defaultCenter] storageForBinary:binary];
@@ -275,7 +272,7 @@ static NSString *kMPContentBindingString3 = @"content.%@.%@.%@";
[self.generatePasswordButton setEnabled:NO];
MPPasswordCreatorViewController *viewController = [[MPPasswordCreatorViewController alloc] init];
viewController.allowsEntryDefaults = YES;
viewController.entry = self.contentEntry;
viewController.representedObject = self.representedObject;
[self _showPopopver:viewController atView:self.passwordTextField onEdge:NSMinYEdge];
}

View File

@@ -65,7 +65,6 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
BOOL _didUnlock;
}
//@property (strong) NSArrayController *entryArrayController;
@property (strong) MPContextBarViewController *contextBarViewController;
@property (strong) NSArray *filteredEntries;
@@ -107,8 +106,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
}
- (void)didLoadView {
[self.view setWantsLayer:YES];
self.view.wantsLayer = YES;
self.entryTable.delegate = self;
self.entryTable.doubleAction = @selector(_columnDoubleClick:);
@@ -155,14 +153,14 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
parentColumn.sortDescriptorPrototype = [NSSortDescriptor sortDescriptorWithKey:parentTitleKeyPath ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
modifiedColumn.sortDescriptorPrototype = [NSSortDescriptor sortDescriptorWithKey:timeInfoModificationTimeKeyPath ascending:YES selector:@selector(compare:)];
[parentColumn.headerCell setStringValue:NSLocalizedString(@"GROUP", "")];
[titleColumn.headerCell setStringValue:NSLocalizedString(@"TITLE", "")];
[userNameColumn.headerCell setStringValue:NSLocalizedString(@"USERNAME", "")];
[passwordColumn.headerCell setStringValue:NSLocalizedString(@"PASSWORD", "")];
[urlColumn.headerCell setStringValue:NSLocalizedString(@"URL", "")];
[notesColumn.headerCell setStringValue:NSLocalizedString(@"NOTES", "")];
[attachmentsColumn.headerCell setStringValue:NSLocalizedString(@"ATTACHMENTS", "")];
[modifiedColumn.headerCell setStringValue:NSLocalizedString(@"MODIFIED", "")];
parentColumn.headerCell.stringValue = NSLocalizedString(@"GROUP", "");
titleColumn.headerCell.stringValue = NSLocalizedString(@"TITLE", "");
userNameColumn.headerCell.stringValue = NSLocalizedString(@"USERNAME", "");
passwordColumn.headerCell.stringValue = NSLocalizedString(@"PASSWORD", "");
urlColumn.headerCell.stringValue = NSLocalizedString(@"URL", "");
notesColumn.headerCell.stringValue = NSLocalizedString(@"NOTES", "");
attachmentsColumn.headerCell.stringValue = NSLocalizedString(@"ATTACHMENTS", "");
modifiedColumn.headerCell.stringValue = NSLocalizedString(@"MODIFIED", "");
[self.entryTable bind:NSContentBinding toObject:self.entryArrayController withKeyPath:NSStringFromSelector(@selector(arrangedObjects)) options:nil];
[self.entryTable bind:NSSortDescriptorsBinding toObject:self.entryArrayController withKeyPath:NSStringFromSelector(@selector(sortDescriptors)) options:nil];
@@ -252,7 +250,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
[view.imageView bind:NSValueBinding toObject:view withKeyPath:iconImageKeyPath options:nil];
}
else {
KPKEntry *entry = [self.entryArrayController arrangedObjects][row];
KPKEntry *entry = self.entryArrayController.arrangedObjects[row];
NSAssert(entry.parent != nil, @"Entry needs to have a parent");
NSString *parentTitleKeyPath = [NSString stringWithFormat:@"%@.%@.%@",
@@ -288,8 +286,8 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
formatter.dateStyle = NSDateFormatterMediumStyle;
formatter.timeStyle = NSDateFormatterMediumStyle;
});
view.textField.formatter = formatter;
}
@@ -368,7 +366,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
#pragma mark MPDocument Notifications
- (void)_didChangeCurrentItem:(NSNotification *)notification {
MPDocument *document = [notification object];
MPDocument *document = notification.object;
if(document.selectedGroups.count != 1 && !document.hasSearch) {
/* no group selection out of search is wrong */
@@ -423,7 +421,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
- (void)_didUpdateSearchResults:(NSNotification *)notification {
[self _showContextBar];
NSArray *result = [notification userInfo][kMPDocumentSearchResultsKey];
NSArray *result = notification.userInfo[kMPDocumentSearchResultsKey];
NSAssert(result != nil, @"Resutls should never be nil");
self.filteredEntries = result;
[self.entryArrayController unbind:NSContentArrayBinding];
@@ -482,11 +480,11 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
return;
}
_isDisplayingContextBar = YES;
if(![[self.contextBarViewController view] superview]) {
[[self view] addSubview:[self.contextBarViewController view]];
if(!self.contextBarViewController.view.superview) {
[self.view addSubview:[self.contextBarViewController view]];
[self.contextBarViewController updateResponderChain];
NSView *contextBar = [self.contextBarViewController view];
NSView *scrollView = [_entryTable enclosingScrollView];
NSView *contextBar = self.contextBarViewController.view;
NSView *scrollView = self.entryTable.enclosingScrollView;
NSDictionary *views = NSDictionaryOfVariableBindings(scrollView, contextBar);
/* Pin to the left */
@@ -497,15 +495,15 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
self.contextBarTopConstraint = [NSLayoutConstraint constraintWithItem:contextBar
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:[self view]
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1
constant:-31];
}
/* Add the view for the first time */
[[self view] removeConstraint:self.tableToTopConstraint];
[[self view] addConstraint:self.contextBarTopConstraint];
[[self view] layout];
[self.view removeConstraint:self.tableToTopConstraint];
[self.view addConstraint:self.contextBarTopConstraint];
[self.view layout];
self.contextBarTopConstraint.constant = 0;
[NSAnimationContext runAnimationGroup:^(NSAnimationContext* context) {
@@ -520,7 +518,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
return; // nothing to do;
}
self.contextBarTopConstraint.constant = -31;
[[self view] addConstraint:self.tableToTopConstraint];
[self.view addConstraint:self.tableToTopConstraint];
[NSAnimationContext runAnimationGroup:^(NSAnimationContext* context) {
context.duration = STATUS_BAR_ANIMATION_TIME;
@@ -565,8 +563,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
#pragma mark Validation
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
/* Validation is solely handled in the document */
MPDocument *document = [[self windowController] document];
return [document validateMenuItem:menuItem];
return [self.windowController.document validateMenuItem:menuItem];
}
#pragma mark ContextMenu
@@ -577,8 +574,8 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
for(NSMenuItem *item in items) {
[menu addItem:item];
}
[menu setDelegate:_menuDelegate];
[self.entryTable setMenu:menu];
menu.delegate = _menuDelegate;
self.entryTable.menu = menu;
}
- (void)_setupHeaderMenu {
@@ -601,7 +598,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
MPEntryTableModfiedColumnIdentifier ];
NSDictionary *options = @{ NSValueTransformerNameBindingOption : NSNegateBooleanTransformerName };
for(NSMenuItem *item in [headerMenu itemArray]) {
for(NSMenuItem *item in headerMenu.itemArray) {
NSUInteger index = [headerMenu indexOfItem:item];
NSTableColumn *column= [self.entryTable tableColumnWithIdentifier:identifier[index]];
[item bind:NSValueBinding toObject:column withKeyPath:NSHiddenBinding options:options];

View File

@@ -47,8 +47,6 @@ typedef NS_ENUM(NSUInteger, MPContentTab) {
@property (weak) IBOutlet NSSplitView *splitView;
@property (unsafe_unretained) IBOutlet NSTextView *notesTextView;
@property (strong) NSObjectController *nodeController;
@end
@implementation MPInspectorViewController
@@ -63,7 +61,6 @@ typedef NS_ENUM(NSUInteger, MPContentTab) {
self.activeTab = MPEmptyTab;
self.entryViewController = [[MPEntryInspectorViewController alloc] init];
self.groupViewController = [[MPGroupInspectorViewController alloc] init];
self.nodeController = [[NSObjectController alloc] init];
}
return self;
}
@@ -105,8 +102,6 @@ typedef NS_ENUM(NSUInteger, MPContentTab) {
[self.view layout];
self.cancelEditButton.hidden = YES;
[self _establishBindings];
}
- (void)registerNotificationsForDocument:(MPDocument *)document {
@@ -219,13 +214,13 @@ typedef NS_ENUM(NSUInteger, MPContentTab) {
BOOL useDefault = (iconId == -1);
switch (self.activeTab) {
case MPGroupTab: {
KPKGroup *group = self.nodeController.content;
KPKGroup *group = self.representedObject;
group.iconId = useDefault ? [KPKGroup defaultIcon] : iconId;
break;
}
case MPEntryTab: {
KPKEntry *entry = self.nodeController.content;
KPKEntry *entry = self.representedObject;
entry.iconId = useDefault ? [KPKEntry defaultIcon]: iconId;
break;
}
@@ -244,18 +239,18 @@ typedef NS_ENUM(NSUInteger, MPContentTab) {
#pragma mark Bindings
- (void)_establishBindings {
[self.itemImageView bind:NSValueBinding
toObject:self.nodeController
withKeyPath:[NSString stringWithFormat:@"%@.%@", NSStringFromSelector(@selector(content)), NSStringFromSelector(@selector(iconImage))]
options:nil];
[self.notesTextView bind:NSValueBinding
toObject:self.nodeController
withKeyPath:[NSString stringWithFormat:@"%@.%@", NSStringFromSelector(@selector(content)), NSStringFromSelector(@selector(notes))]
options:@{ NSNullPlaceholderBindingOption: NSLocalizedString(@"NONE", "")}];
[self.itemNameTextField bind:NSValueBinding
toObject:self.nodeController
withKeyPath:[NSString stringWithFormat:@"%@.%@", NSStringFromSelector(@selector(content)), NSStringFromSelector(@selector(title))]
options:@{NSNullPlaceholderBindingOption: NSLocalizedString(@"NONE", "")}];
// [self.itemImageView bind:NSValueBinding
// toObject:self
// withKeyPath:[NSString stringWithFormat:@"%@.%@", NSStringFromSelector(@selector(representedObject)), NSStringFromSelector(@selector(iconImage))]
// options:nil];
// [self.notesTextView bind:NSValueBinding
// toObject:self
// withKeyPath:[NSString stringWithFormat:@"%@.%@", NSStringFromSelector(@selector(representedObject)), NSStringFromSelector(@selector(notes))]
// options:@{ NSNullPlaceholderBindingOption: NSLocalizedString(@"NONE", "")}];
// [self.itemNameTextField bind:NSValueBinding
// toObject:self
// withKeyPath:[NSString stringWithFormat:@"%@.%@", NSStringFromSelector(@selector(representedObject)), NSStringFromSelector(@selector(title))]
// options:@{NSNullPlaceholderBindingOption: NSLocalizedString(@"NONE", "")}];
}
#pragma mark -
@@ -281,7 +276,7 @@ typedef NS_ENUM(NSUInteger, MPContentTab) {
else {
self.activeTab = MPEmptyTab;
}
self.nodeController.content = node;
self.representedObject = node;
self.entryViewController.representedObject = node.asEntry;
self.groupViewController.representedObject = node.asGroup;
[self _toggleEditors:(nil != node.asGroup)];