aboutsummaryrefslogtreecommitdiffstats
path: root/Framework/MASShortcut+UserDefaults.m
diff options
context:
space:
mode:
authorTomáš Znamenáček2014-08-05 11:13:13 +0200
committerTomáš Znamenáček2015-01-07 15:05:11 +0100
commit377b44220f2a4a8b7ffc3eda9e93cf073e8a74da (patch)
treeb83239fd741773451bd9a75480ebe5a276c7d885 /Framework/MASShortcut+UserDefaults.m
parenta3a459b4e4e47bf18dccd5dc7f315389346e3d6c (diff)
downloadMASShortcut-377b44220f2a4a8b7ffc3eda9e93cf073e8a74da.tar.bz2
Repackaged the code as a framework and included the demo.
Packaging the code as a framework is mostly just a formality. It doesn’t really change much, it just turns the code into a regular component. What it does change is that the code now has its own Xcode settings, which could make compatibility easier in the long run. Including the demo in the main repository makes it easier to hack on the library, since you can try the changes immediately. It also shows how to bundle the framework into an app that uses it.
Diffstat (limited to 'Framework/MASShortcut+UserDefaults.m')
-rw-r--r--Framework/MASShortcut+UserDefaults.m98
1 files changed, 98 insertions, 0 deletions
diff --git a/Framework/MASShortcut+UserDefaults.m b/Framework/MASShortcut+UserDefaults.m
new file mode 100644
index 0000000..94b035d
--- /dev/null
+++ b/Framework/MASShortcut+UserDefaults.m
@@ -0,0 +1,98 @@
+#import "MASShortcut+UserDefaults.h"
+#import "MASShortcut+Monitoring.h"
+
+@interface MASShortcutUserDefaultsHotKey : NSObject
+
+@property (nonatomic, readonly) NSString *userDefaultsKey;
+@property (nonatomic, copy) void (^handler)();
+@property (nonatomic, weak) id monitor;
+
+- (id)initWithUserDefaultsKey:(NSString *)userDefaultsKey handler:(void (^)())handler;
+
+@end
+
+#pragma mark -
+
+@implementation MASShortcut (UserDefaults)
+
++ (NSMutableDictionary *)registeredUserDefaultsHotKeys
+{
+ static NSMutableDictionary *shared = nil;
+ static dispatch_once_t onceToken;
+ dispatch_once(&onceToken, ^{
+ shared = [NSMutableDictionary dictionary];
+ });
+ return shared;
+}
+
++ (void)registerGlobalShortcutWithUserDefaultsKey:(NSString *)userDefaultsKey handler:(void (^)())handler
+{
+ MASShortcutUserDefaultsHotKey *hotKey = [[MASShortcutUserDefaultsHotKey alloc] initWithUserDefaultsKey:userDefaultsKey handler:handler];
+ [[self registeredUserDefaultsHotKeys] setObject:hotKey forKey:userDefaultsKey];
+}
+
++ (void)unregisterGlobalShortcutWithUserDefaultsKey:(NSString *)userDefaultsKey
+{
+ NSMutableDictionary *registeredHotKeys = [self registeredUserDefaultsHotKeys];
+ [registeredHotKeys removeObjectForKey:userDefaultsKey];
+}
+
++ (void)setGlobalShortcut:(MASShortcut *)shortcut forUserDefaultsKey:(NSString *)userDefaultsKey
+{
+ NSData *shortcutData = shortcut.data;
+ if (shortcutData)
+ [[NSUserDefaults standardUserDefaults] setObject:shortcutData forKey:userDefaultsKey];
+ else
+ [[NSUserDefaults standardUserDefaults] removeObjectForKey:userDefaultsKey];
+}
+
+@end
+
+#pragma mark -
+
+@implementation MASShortcutUserDefaultsHotKey {
+ NSString *_observableKeyPath;
+}
+
+void *MASShortcutUserDefaultsContext = &MASShortcutUserDefaultsContext;
+
+- (id)initWithUserDefaultsKey:(NSString *)userDefaultsKey handler:(void (^)())handler
+{
+ self = [super init];
+ if (self) {
+ _userDefaultsKey = userDefaultsKey.copy;
+ _handler = [handler copy];
+ _observableKeyPath = [@"values." stringByAppendingString:_userDefaultsKey];
+ [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:_observableKeyPath options:NSKeyValueObservingOptionInitial context:MASShortcutUserDefaultsContext];
+ }
+ return self;
+}
+
+- (void)dealloc
+{
+ [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:_observableKeyPath context:MASShortcutUserDefaultsContext];
+ [MASShortcut removeGlobalHotkeyMonitor:self.monitor];
+}
+
+#pragma mark -
+
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
+{
+ if (context == MASShortcutUserDefaultsContext) {
+ [MASShortcut removeGlobalHotkeyMonitor:self.monitor];
+ [self installHotKeyFromUserDefaults];
+ }
+ else {
+ [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
+ }
+}
+
+- (void)installHotKeyFromUserDefaults
+{
+ NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:_userDefaultsKey];
+ MASShortcut *shortcut = [MASShortcut shortcutWithData:data];
+ if (shortcut == nil) return;
+ self.monitor = [MASShortcut addGlobalHotkeyMonitorWithShortcut:shortcut handler:self.handler];
+}
+
+@end