Restructured ModelChangeObserving a bit

This commit is contained in:
michael starke
2016-08-28 14:32:18 +02:00
parent e9b9ca9425
commit 793176bf2d
4 changed files with 24 additions and 34 deletions

View File

@@ -12,15 +12,27 @@ NS_ASSUME_NONNULL_BEGIN
@protocol MPModelChangeObserving <NSObject>
/* A class conforming to the protocoll will shoudl always fire the appropriate notifications listes below
You need to overwrite setValue:forKeyPath in a conforming class. For convinience you can just call the helper and lett it do the job
*/
FOUNDATION_EXTERN NSString *const MPWillChangeModelNotification;
FOUNDATION_EXTERN NSString *const MPDidChangreModelNotification;
FOUNDATION_EXTERN NSString *const MPDidChangeModelNotification;
FOUNDATION_EXTERN NSString *const MPModelChangeObservingKeyPathKey;
@required
- (void)observerModelChangesForKeyPath:(NSString *)keyPath;
@end
- (void)willChangeModel;
- (void)didChangeModel;
/* Use this helper to fire the right notifications */
@interface MPModelChangeObservingHelper : NSObject
+ (void)willChangeModelKeyPath:(NSString *)keyPath observer:(id<MPModelChangeObserving>)observer;
+ (void)didChangeModelKeyPath:(NSString *)keyPath observer:(id<MPModelChangeObserving>)observer;
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath forTarget:(id)target;
@end

View File

@@ -6,11 +6,10 @@
// 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 MPDidChangeModelNotification = @"com.hicknhack.macpass.MPDidChangeModelNotification";
NSString *const MPModelChangeObservingKeyPathKey = @"MPModelChangeObservingKeyPathKey";
@@ -21,8 +20,11 @@ NSString *const MPModelChangeObservingKeyPathKey = @"MPModelChangeObservingKeyPa
}
+ (void)didChangeModelKeyPath:(NSString *)keyPath observer:(id<MPModelChangeObserving>)observer {
[[NSNotificationCenter defaultCenter] postNotificationName:MPDidChangreModelNotification object:observer userInfo:@{ MPModelChangeObservingKeyPathKey : keyPath }];
[[NSNotificationCenter defaultCenter] postNotificationName:MPDidChangeModelNotification object:observer userInfo:@{ MPModelChangeObservingKeyPathKey : keyPath }];
}
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath forTarget:(id)target {
[target setValue:value forKeyPath:keyPath];
}
@end

View File

@@ -1,22 +0,0 @@
//
// 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