44static NSString * const KeysFileName = @" keys" ;
55static NSString * const AboutURL = @" https://github.com/fe9lix/CodingKeys" ;
66
7- @interface AppService ()
7+ NSString * const AppServiceDidChangeHotKeys = @" AppServiceDidChangeHotKeys" ;
8+
9+ @interface AppService () <NSFilePresenter >
810
911@property (nonatomic , strong ) NSDictionary *hotKeysForAppName;
12+ @property (nonatomic , strong ) NSFileCoordinator *fileCoordinator;
13+ @property (strong ) NSURL *presentedItemURL;
14+ @property (strong ) NSOperationQueue *presentedItemOperationQueue;
15+ @property (nonatomic , strong ) NSTimer *timer;
1016
1117@end
1218
@@ -30,6 +36,11 @@ - (id)init {
3036}
3137
3238- (void )setup {
39+ [self setupHotKeysForAppName ];
40+ [self watchKeyFile ];
41+ }
42+
43+ - (void )setupHotKeysForAppName {
3344 NSArray *keyMappings = [self loadKeyMappingsFile ];
3445
3546 NSMutableDictionary *hotKeysForAppName = [NSMutableDictionary dictionary ];
@@ -69,7 +80,43 @@ - (NSArray *)loadKeyMappingsFile {
6980
7081- (NSString *)keysFilePath {
7182 return [[NSBundle mainBundle ] pathForResource: KeysFileName
72- ofType: @" json" ];
83+ ofType: @" json" ];
84+ }
85+
86+ - (void )watchKeyFile {
87+ self.presentedItemOperationQueue = [[NSOperationQueue alloc ] init ];
88+ self.presentedItemOperationQueue .maxConcurrentOperationCount = 1 ;
89+
90+ self.presentedItemURL = [[NSBundle mainBundle ] resourceURL ];
91+
92+ self.fileCoordinator = [[NSFileCoordinator alloc ] initWithFilePresenter: self ];
93+
94+ [NSFileCoordinator addFilePresenter: self ];
95+ }
96+
97+ - (void )presentedItemDidChange {
98+ [self .timer invalidate ];
99+
100+ dispatch_async (dispatch_get_main_queue (), ^{
101+ self.timer = [NSTimer scheduledTimerWithTimeInterval: 0 .5f
102+ target: self
103+ selector: @selector (keysDidChange: )
104+ userInfo: nil
105+ repeats: NO ];
106+ });
107+ }
108+
109+ - (void )keysDidChange : (id )obj {
110+ [self setupHotKeysForAppName ];
111+ [self dispatchKeysDidChangeNotification ];
112+ }
113+
114+ - (void )dispatchKeysDidChangeNotification {
115+ NSNotification *notification = [NSNotification notificationWithName: AppServiceDidChangeHotKeys
116+ object: nil
117+ userInfo: nil ];
118+
119+ [[NSNotificationCenter defaultCenter ] postNotification: notification];
73120}
74121
75122- (BOOL )isAppRegistered : (NSString *)appName {
0 commit comments