Changes on entries now get pushed to the history

This commit is contained in:
michael starke
2016-08-26 16:23:08 +02:00
parent 2958f5255b
commit a7a28063f0
6 changed files with 48 additions and 70 deletions

View File

@@ -167,14 +167,6 @@ APPKIT_EXTERN NSString *const MPDocumentGroupKey;
- (IBAction)duplicateEntryWithOptions:(id)sender; - (IBAction)duplicateEntryWithOptions:(id)sender;
#pragma mark -
#pragma mark Editing
- (void)willChangeEntry:(KPKEntry *)entry;
- (void)commitChangesToEntry:(KPKEntry *)entry;
- (void)discardChangesToEntry:(KPKEntry *)entry;
@end @end
@interface MPDocument (Attachments) @interface MPDocument (Attachments)

View File

@@ -77,8 +77,6 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey
@property (assign) BOOL fileChangeDialogOpen; @property (assign) BOOL fileChangeDialogOpen;
@property (strong) NSMutableDictionary *modifiedEntries;
@end @end
@implementation MPDocument @implementation MPDocument
@@ -771,22 +769,6 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey
[self.trash clear]; [self.trash clear];
} }
- (void)willChangeEntry:(KPKEntry *)entry {
/* we store a copy of the entry */
NSAssert(nil == self.modifiedEntries[entry.uuid], @"Inconsistent state, pending changes present for entry!");
self.modifiedEntries[entry.uuid] = [entry copy];
}
- (void)discardChangesToEntry:(KPKEntry *)entry {
self.modifiedEntries[entry.uuid] = nil;
/* TODO KeePassKit copy entry with info from old entry */
}
- (void)commitChangesToEntry:(KPKEntry *)entry {
/* TODO KeePassKit entry pushHistory:self.modifiedEntries[entry.uuid] */
[self discardChangesToEntry:entry];
}
#pragma mark - #pragma mark -
#pragma mark MPTargetNodeResolving #pragma mark MPTargetNodeResolving

View File

@@ -24,9 +24,6 @@
- (IBAction)pickIcon:(id)sender; - (IBAction)pickIcon:(id)sender;
- (IBAction)pickExpiryDate:(id)sender; - (IBAction)pickExpiryDate:(id)sender;
- (IBAction)saveChanges:(id)sender;
- (IBAction)discardChanges:(id)sender;
/* Separate call to ensure all registered objects are in place */ /* Separate call to ensure all registered objects are in place */
- (void)registerNotificationsForDocument:(NSDocument *)document; - (void)registerNotificationsForDocument:(NSDocument *)document;

View File

@@ -46,6 +46,8 @@ typedef NS_ENUM(NSUInteger, MPContentTab) {
@property (weak) IBOutlet NSSplitView *splitView; @property (weak) IBOutlet NSSplitView *splitView;
@property (unsafe_unretained) IBOutlet NSTextView *notesTextView; @property (unsafe_unretained) IBOutlet NSTextView *notesTextView;
@property BOOL didPushHistory;
@end @end
@implementation MPInspectorViewController @implementation MPInspectorViewController
@@ -60,6 +62,16 @@ typedef NS_ENUM(NSUInteger, MPContentTab) {
self.activeTab = MPEmptyTab; self.activeTab = MPEmptyTab;
self.entryViewController = [[MPEntryInspectorViewController alloc] init]; self.entryViewController = [[MPEntryInspectorViewController alloc] init];
self.groupViewController = [[MPGroupInspectorViewController alloc] init]; self.groupViewController = [[MPGroupInspectorViewController alloc] init];
self.didPushHistory = NO;
/* subviewcontrollers will notify us about a change so we can handle the history pushing */
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_willChangeValueForRepresentedObjectNotification:)
name:MPViewControllerWillChangeValueForRepresentedObjectKeyPathNotification
object:self.entryViewController];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_willChangeValueForRepresentedObjectNotification:)
name:MPViewControllerWillChangeValueForRepresentedObjectKeyPathNotification
object:self.groupViewController];
} }
return self; return self;
} }
@@ -185,18 +197,6 @@ typedef NS_ENUM(NSUInteger, MPContentTab) {
} }
- (void)saveChanges:(id)sender {
MPDocument *document = self.windowController.document;
KPKEntry *entry = self.representedObject;
[document commitChangesToEntry:entry];
}
- (void)discardChanges:(id)sender {
MPDocument *document = self.windowController.document;
KPKEntry *entry = self.representedObject;
[document discardChangesToEntry:entry];
}
#pragma mark - #pragma mark -
#pragma mark NSPopover Delegate #pragma mark NSPopover Delegate
@@ -207,31 +207,28 @@ typedef NS_ENUM(NSUInteger, MPContentTab) {
#pragma mark - #pragma mark -
#pragma mark Bindings #pragma mark Bindings
- (void)_establishBindings {
// [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", "")}];
}
- (void)willChangeValueForRepresentedObjectKeyPath:(NSString *)keyPath { - (void)willChangeValueForRepresentedObjectKeyPath:(NSString *)keyPath {
MPDocument *document = self.windowController.document; [self _recordChangesForCurrentNode];
KPKEntry *entry = [self.representedObject asEntry];
if(entry) {
[document willChangeEntry:entry];
}
} }
#pragma mark -
#pragma mark MPViewController Notifications
- (void)_willChangeValueForRepresentedObjectNotification:(NSNotification *)notification {
[self _recordChangesForCurrentNode];
}
- (void)_recordChangesForCurrentNode {
/* TODO use uuids for pushed item? */
if(self.didPushHistory) {
return;
}
KPKEntry *entry = [self.representedObject asEntry];
if( entry ) {
[entry pushHistory];
self.didPushHistory = YES;
}
}
#pragma mark - #pragma mark -
#pragma mark MPDocument Notifications #pragma mark MPDocument Notifications
@@ -248,9 +245,12 @@ typedef NS_ENUM(NSUInteger, MPContentTab) {
else { else {
self.activeTab = MPEmptyTab; self.activeTab = MPEmptyTab;
} }
self.didPushHistory = NO;
self.representedObject = node; self.representedObject = node;
self.entryViewController.representedObject = node.asEntry; self.entryViewController.representedObject = node.asEntry;
self.groupViewController.representedObject = node.asGroup; self.groupViewController.representedObject = node.asGroup;
} }
@end @end

View File

@@ -10,6 +10,9 @@
@interface MPViewController : NSViewController @interface MPViewController : NSViewController
APPKIT_EXTERN NSString *const MPViewControllerWillChangeValueForRepresentedObjectKeyPathNotification;
APPKIT_EXTERN NSString *const MPViewControllerDidChangeValueForRepresentedObjectKeyPathNotification;
@property (nonatomic, readonly) NSWindowController *windowController; @property (nonatomic, readonly) NSWindowController *windowController;
- (void)didLoadView; - (void)didLoadView;

View File

@@ -7,7 +7,9 @@
// //
#import "MPViewController.h" #import "MPViewController.h"
#import "MPDocument.h"
NSString *const MPViewControllerWillChangeValueForRepresentedObjectKeyPathNotification = @"com.hicknhack.macpass.MPViewControllerWillChangeValueForRepresentedObjectKeyPathNotification";
NSString *const MPViewControllerDidChangeValueForRepresentedObjectKeyPathNotification = @"comt.hicknhack.macpass.MPViewControllerDidChangeValueForRepresentedObjectKeyPathNotification";
@implementation MPViewController @implementation MPViewController
@@ -41,8 +43,10 @@
#pragma mark Binding observation #pragma mark Binding observation
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath { - (void)setValue:(id)value forKeyPath:(NSString *)keyPath {
if([keyPath hasPrefix:@"representedObject."]) { if([keyPath hasPrefix:@"representedObject."]) {
[[NSNotificationCenter defaultCenter] postNotificationName:MPViewControllerWillChangeValueForRepresentedObjectKeyPathNotification object:self];
[self willChangeValueForRepresentedObjectKeyPath:keyPath]; [self willChangeValueForRepresentedObjectKeyPath:keyPath];
[super setValue:value forKeyPath:keyPath]; [super setValue:value forKeyPath:keyPath];
[[NSNotificationCenter defaultCenter] postNotificationName:MPViewControllerDidChangeValueForRepresentedObjectKeyPathNotification object:self];
[self didChangeValueForRepresentedObjectKeyPath:keyPath]; [self didChangeValueForRepresentedObjectKeyPath:keyPath];
} }
else { else {