Mingling on detecting UI introduces model changes

This commit is contained in:
michael starke
2016-08-27 13:53:27 +02:00
parent 2cf5c765f1
commit bd3eb7950c
11 changed files with 168 additions and 9 deletions

View File

@@ -0,0 +1,13 @@
//
// MPArrayController.h
// MacPass
//
// Created by Michael Starke on 27/08/16.
// Copyright © 2016 HicknHack Software GmbH. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface MPArrayController : NSArrayController
@end

View File

@@ -0,0 +1,18 @@
//
// MPArrayController.m
// MacPass
//
// Created by Michael Starke on 27/08/16.
// Copyright © 2016 HicknHack Software GmbH. All rights reserved.
//
#import "MPArrayController.h"
@implementation MPArrayController
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath {
NSLog(@"%@ setValue:forKeyPath:%@", NSStringFromClass([self class]), keyPath);
[super setValue:value forKeyPath:keyPath];
}
@end

View File

@@ -14,4 +14,9 @@
[super setBackgroundStyle:NSBackgroundStyleLight];
}
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath {
NSLog(@"%@ setValue:forKeyPath:%@", NSStringFromClass([self class]), keyPath);
[super setValue:value forKeyPath:keyPath];
}
@end

View File

@@ -26,6 +26,8 @@
#import "KeePassKit/KeePassKit.h"
#import "MPArrayController.h"
#import "HNHUi/HNHUi.h"
typedef NS_ENUM(NSUInteger, MPEntryTab) {
@@ -68,9 +70,9 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_showPassword = NO;
_attachmentsController = [[NSArrayController alloc] init];
_customFieldsController = [[NSArrayController alloc] init];
_windowAssociationsController = [[NSArrayController alloc] init];
_attachmentsController = [[MPArrayController alloc] init];
_customFieldsController = [[MPArrayController alloc] init];
_windowAssociationsController = [[MPArrayController alloc] init];
_attachmentTableDelegate = [[MPAttachmentTableViewDelegate alloc] init];
_customFieldTableDelegate = [[MPCustomFieldTableViewDelegate alloc] init];
_attachmentDataSource = [[MPAttachmentTableDataSource alloc] init];

View File

@@ -0,0 +1,27 @@
//
// MPModelChangeObserving.h
// MacPass
//
// Created by Michael Starke on 26/08/16.
// Copyright © 2016 HicknHack Software GmbH. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@protocol MPModelChangeObserving <NSObject>
FOUNDATION_EXTERN NSString *const MPWillChangeModelNotification;
FOUNDATION_EXTERN NSString *const MPDidChangreModelNotification;
FOUNDATION_EXTERN NSString *const MPModelChangeObservingKeyPathKey;
@required
- (void)willChangeModel;
- (void)didChangeModel;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,22 @@
//
// MPModelChangeObserver.h
// MacPass
//
// Created by Michael Starke on 26/08/16.
// Copyright © 2016 HicknHack Software GmbH. All rights reserved.
//
#import <Foundation/Foundation.h>
/**
* This class is usefull to implement modelchangeobserving by just forwarding the protocoll calls to the helper
*/
@protocol MPModelChangeObserving;
@interface MPModelChangeObservingHelper : NSObject
+ (void)willChangeModelKeyPath:(NSString *)keyPath observer:(id<MPModelChangeObserving>)observer;
+ (void)didChangeModelKeyPath:(NSString *)keyPath observer:(id<MPModelChangeObserving>)observer;
@end

View File

@@ -0,0 +1,28 @@
//
// MPModelChangeObserver.m
// MacPass
//
// Created by Michael Starke on 26/08/16.
// Copyright © 2016 HicknHack Software GmbH. All rights reserved.
//
#import "MPModelChangeObservingHelper.h"
#import "MPModelChangeObserving.h"
NSString *const MPWillChangeModelNotification = @"com.hicknhack.macpass.MPWillChangeModelNotification";
NSString *const MPDidChangreModelNotification = @"com.hicknhack.macpass.MPDidChangeModelNotification";
NSString *const MPModelChangeObservingKeyPathKey = @"MPModelChangeObservingKeyPathKey";
@implementation MPModelChangeObservingHelper
+ (void)willChangeModelKeyPath:(NSString *)keyPath observer:(id<MPModelChangeObserving>)observer {
[[NSNotificationCenter defaultCenter] postNotificationName:MPWillChangeModelNotification object:observer userInfo:@{ MPModelChangeObservingKeyPathKey : keyPath }];
}
+ (void)didChangeModelKeyPath:(NSString *)keyPath observer:(id<MPModelChangeObserving>)observer {
[[NSNotificationCenter defaultCenter] postNotificationName:MPDidChangreModelNotification object:observer userInfo:@{ MPModelChangeObservingKeyPathKey : keyPath }];
}
@end

View File

@@ -7,8 +7,9 @@
//
#import <Cocoa/Cocoa.h>
#import "MPModelChangeObserving.h"
@interface MPObjectController : NSObjectController
@interface MPObjectController : NSObjectController <MPModelChangeObserving>
@property (weak) NSDocument *document;

View File

@@ -8,6 +8,10 @@
#import "MPObjectController.h"
@interface MPObjectController ()
@property (strong) NSMutableSet *observedModelKeyPaths;
@end
@implementation MPObjectController
- (void)discardEditing {
@@ -19,8 +23,33 @@
}
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath {
NSLog(@"[%@ setValue:%@ forKeyPath:%@]", NSStringFromClass([self class]), value, keyPath);
BOOL observerd = NO;
for(NSString *observedKeyPath in self.observedModelKeyPaths) {
observerd = ([keyPath hasPrefix:observedKeyPath]);
if(observerd) {
break;
}
}
if(observerd) {
}
[super setValue:value forKeyPath:keyPath];
}
- (void)didChangeModel {
}
- (void)willChangeModel {
}
- (void)beginObservingModelKeyPath:(NSString *)keyPath {
[self.observedModelKeyPaths addObject:keyPath];
}
- (void)endObservingModelKeyPath:(NSString *)keyPath {
[self.observedModelKeyPaths removeObject:keyPath];
}
@end

View File

@@ -33,10 +33,10 @@ NSString *const MPViewControllerDidChangeValueForRepresentedObjectKeyPathNotific
}
- (void)updateResponderChain {
if(self.view && [self.view nextResponder] != self) {
NSResponder *nextResponder = [[self view] nextResponder];
[[self view] setNextResponder:self];
[self setNextResponder:nextResponder];
if(self.view && self.view.nextResponder != self) {
NSResponder *nextResponder = self.view.nextResponder;
self.view.nextResponder = self;
self.nextResponder = nextResponder;
}
}