Remove mappingsChanged with a notification. MappingsController no longer needs to...
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Mon, 4 Mar 2013 15:46:54 +0000 (16:46 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Mon, 4 Mar 2013 15:46:54 +0000 (16:46 +0100)
EnjoyableApplicationDelegate.h
EnjoyableApplicationDelegate.m
NJEvents.h
NJMappingsController.m

index 3e744189657c2098356575284327b044a264a0d1..b77f799e6879220960689d4ce1739ae100ac424c 100644 (file)
@@ -18,6 +18,4 @@
 @property (nonatomic, strong) IBOutlet NJOutputController *outputController;
 @property (nonatomic, strong) IBOutlet NJMappingsController *mappingsController;
 
-- (void)mappingsChanged;
-
 @end
index 0dbea223a020f2539bee62e59aee380d839b12b4..8afbb5bd5ab16afccebf3ace687a544697ed9705 100644 (file)
         selector:@selector(mappingDidChange:)
         name:NJEventMappingChanged
         object:nil];
+    [NSNotificationCenter.defaultCenter
+        addObserver:self
+        selector:@selector(mappingListDidChange:)
+        name:NJEventMappingListChanged
+        object:nil];
     [NSNotificationCenter.defaultCenter
         addObserver:self
         selector:@selector(eventTranslationActivated:)
@@ -48,6 +53,7 @@
 
 - (void)applicationWillTerminate:(NSNotification *)aNotification {
        [NSUserDefaults.standardUserDefaults synchronize];
+    [NSNotificationCenter.defaultCenter removeObserver:self];
 }
 
 - (void)eventTranslationActivated:(NSNotification *)note {
     NSLog(@"Ignoring application changes.");
 }
 
-- (void)mappingsChanged {
+- (void)mappingListDidChange:(NSNotification *)note {
+    NSArray *mappings = note.object;
     NSInteger removeFrom = mappingsMenuIndex;
     while (dockMenuBase.numberOfItems > removeFrom)
         [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
     int added = 0;
-    for (NJMapping *mapping in self.mappingsController) {
+    for (NJMapping *mapping in mappings) {
         NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
         [dockMenuBase addItemWithTitle:mapping.name
                                 action:@selector(chooseMapping:)
index 84bdd8273a5f055048b5893bcc6c8d5e7996f8fe..ce87b24725c550af21ec9023151c00ea687b9cbd 100644 (file)
@@ -7,5 +7,6 @@
 //
 
 #define NJEventMappingChanged @"NJEventMappingChanged"
+#define NJEventMappingListChanged @"NJEventMappingListChanged"
 #define NJEventTranslationActivated @"NJEventTranslationActivated"
 #define NJEventTranslationDeactivated @"NJEventTranslationDeactivated"
index f1e068ee23ef35fdaf6f2b1de59f7b10ac8d2336..e2ac93c49a54310bcc8f0ff2cdd7b7552d5f9743 100644 (file)
@@ -7,7 +7,6 @@
 
 #import "NJMappingsController.h"
 
-#import "EnjoyableApplicationDelegate.h"
 #import "NJMapping.h"
 #import "NJMappingsController.h"
 #import "NJOutput.h"
     return idx < _mappings.count ? _mappings[idx] : nil;
 }
 
+- (void)mappingsChanged {
+    [self save];
+    [tableView reloadData];
+    [NSNotificationCenter.defaultCenter
+        postNotificationName:NJEventMappingListChanged
+        object:_mappings];
+}
+
 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
                                   objects:(__unsafe_unretained id [])buffer
                                     count:(NSUInteger)len {
 - (IBAction)addPressed:(id)sender {
     NJMapping *newMapping = [[NJMapping alloc] initWithName:@"Untitled"];
     [_mappings addObject:newMapping];
-    [(EnjoyableApplicationDelegate *)NSApplication.sharedApplication.delegate mappingsChanged];
-    [tableView reloadData];
-    [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_mappings.count - 1] byExtendingSelection:NO];
-    [tableView editColumn:0 row:_mappings.count - 1 withEvent:nil select:YES];
+    [self mappingsChanged];
     [self activateMapping:newMapping];
+    [tableView editColumn:0 row:_mappings.count - 1 withEvent:nil select:YES];
 }
 
 - (IBAction)removePressed:(id)sender {
         return;
     
     [_mappings removeObjectAtIndex:tableView.selectedRow];
-    [tableView reloadData];
-    [(EnjoyableApplicationDelegate *)NSApplication.sharedApplication.delegate mappingsChanged];
+    [self mappingsChanged];
     [self activateMapping:_mappings[0]];
-    [self save];
 }
 
 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
-    if (tableView.selectedRow >= 0)
-        [self activateMapping:_mappings[tableView.selectedRow]];
+    [self activateMapping:self[tableView.selectedRow]];
 }
 
 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger)index {
    forTableColumn:(NSTableColumn *)col
               row:(NSInteger)index {
     self[index].name = obj;
-    [self save];
-    [tableView reloadData];
-    [(EnjoyableApplicationDelegate *)NSApplication.sharedApplication.delegate mappingsChanged];
+    [self mappingsChanged];
 }
 
 - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
         if (current >= newMappings.count)
             current = 0;
         _mappings = newMappings;
-        [tableView reloadData];
-        [(EnjoyableApplicationDelegate *)NSApplication.sharedApplication.delegate mappingsChanged];
+        [self mappingsChanged];
         [self activateMapping:_mappings[current]];
     }
 }
                               mapping = mergeInto;
                           } else {
                               [_mappings addObject:mapping];
-                              [tableView reloadData];
                           }
                           
-                          [self save];
-                          [(EnjoyableApplicationDelegate *)NSApplication.sharedApplication.delegate mappingsChanged];
+                          [self mappingsChanged];
                           [self activateMapping:mapping];
                           [outputController loadCurrent];