aboutsummaryrefslogtreecommitdiffstats
path: root/Framework
diff options
context:
space:
mode:
authorTomáš Znamenáček2014-08-07 12:55:59 +0200
committerTomáš Znamenáček2015-01-07 15:42:22 +0100
commit46aa323115c2ebe7128aba39c0296634d232bbf6 (patch)
tree8ac85cbbe5b5470c02cd4049ded389f023cb46af /Framework
parent5f5f6cb9d086853065b2546e72d71a38230c6b59 (diff)
downloadMASShortcut-46aa323115c2ebe7128aba39c0296634d232bbf6.tar.bz2
Added a simplified binding API for MASShortcutView.
This returns the associatedUserDefaultsKey property used in previous code versions, only the implementation uses less magic.
Diffstat (limited to 'Framework')
-rw-r--r--Framework/MASShortcutView+Bindings.h25
-rw-r--r--Framework/MASShortcutView+Bindings.m50
-rw-r--r--Framework/Shortcut.h1
3 files changed, 76 insertions, 0 deletions
diff --git a/Framework/MASShortcutView+Bindings.h b/Framework/MASShortcutView+Bindings.h
new file mode 100644
index 0000000..b0148e7
--- /dev/null
+++ b/Framework/MASShortcutView+Bindings.h
@@ -0,0 +1,25 @@
+#import "MASShortcutView.h"
+
+/**
+ @brief A simplified interface to bind the recorder value to user defaults.
+
+ You can bind the @p shortcutValue to user defaults using the standard
+ @p bind:toObject:withKeyPath:options: call, but since that’s a lot to type
+ and read, here’s a simpler option.
+
+ Setting the @p associatedUserDefaultsKey binds the view’s shortcut value
+ to the given user defaults key. You can supply a value transformer to convert
+ values between user defaults and @p MASShortcut. If you don’t supply
+ a transformer, the @p NSUnarchiveFromDataTransformerName will be used
+ automatically.
+
+ Set @p associatedUserDefaultsKey to @p nil to disconnect the binding.
+*/
+@interface MASShortcutView (Bindings)
+
+@property(copy) NSString *associatedUserDefaultsKey;
+
+- (void) setAssociatedUserDefaultsKey: (NSString*) newKey withTransformer: (NSValueTransformer*) transformer;
+- (void) setAssociatedUserDefaultsKey: (NSString*) newKey withTransformerName: (NSString*) transformerName;
+
+@end
diff --git a/Framework/MASShortcutView+Bindings.m b/Framework/MASShortcutView+Bindings.m
new file mode 100644
index 0000000..7a6064c
--- /dev/null
+++ b/Framework/MASShortcutView+Bindings.m
@@ -0,0 +1,50 @@
+#import "MASShortcutView+Bindings.h"
+
+@implementation MASShortcutView (Bindings)
+
+- (NSString*) associatedUserDefaultsKey
+{
+ NSDictionary* bindingInfo = [self infoForBinding:MASShortcutBinding];
+ if (bindingInfo != nil) {
+ NSString *keyPath = [bindingInfo objectForKey:NSObservedKeyPathKey];
+ NSString *key = [keyPath stringByReplacingOccurrencesOfString:@"values." withString:@""];
+ return key;
+ } else {
+ return nil;
+ }
+}
+
+- (void) setAssociatedUserDefaultsKey: (NSString*) newKey withTransformer: (NSValueTransformer*) transformer
+{
+ // Break previous binding if any
+ NSString *currentKey = [self associatedUserDefaultsKey];
+ if (currentKey != nil) {
+ [self unbind:currentKey];
+ }
+
+ // Stop if the new binding is nil
+ if (newKey == nil) {
+ return;
+ }
+
+ NSDictionary *options = transformer ?
+ @{NSValueTransformerBindingOption:transformer} :
+ nil;
+
+ [self bind:MASShortcutBinding
+ toObject:[NSUserDefaultsController sharedUserDefaultsController]
+ withKeyPath:[@"values." stringByAppendingString:newKey]
+ options:options];
+}
+
+- (void) setAssociatedUserDefaultsKey: (NSString*) newKey withTransformerName: (NSString*) transformerName
+{
+ [self setAssociatedUserDefaultsKey:newKey withTransformer:[NSValueTransformer valueTransformerForName:transformerName]];
+}
+
+- (void) setAssociatedUserDefaultsKey: (NSString*) newKey
+{
+ [self setAssociatedUserDefaultsKey:newKey withTransformerName:NSUnarchiveFromDataTransformerName];
+}
+
+@end
diff --git a/Framework/Shortcut.h b/Framework/Shortcut.h
index df33f17..e131395 100644
--- a/Framework/Shortcut.h
+++ b/Framework/Shortcut.h
@@ -4,3 +4,4 @@
#import "MASShortcutBinder.h"
#import "MASDictionaryTransformer.h"
#import "MASShortcutView.h"
+#import "MASShortcutView+Bindings.h" \ No newline at end of file