mirror of
https://github.com/samsonjs/samhuri.net-ios.git
synced 2026-03-25 09:25:47 +00:00
declare notifications once, instead of adding and removing each one explicitly
This commit is contained in:
parent
1f54984542
commit
3cc07aeb80
4 changed files with 86 additions and 16 deletions
|
|
@ -55,6 +55,7 @@
|
|||
7B9E64421A22F3840072FF42 /* BlogService.m in Sources */ = {isa = PBXBuildFile; fileRef = 7B9E64411A22F3840072FF42 /* BlogService.m */; };
|
||||
7B9E644C1A230B940072FF42 /* JSONHTTPClient.m in Sources */ = {isa = PBXBuildFile; fileRef = 7B9E644B1A230B940072FF42 /* JSONHTTPClient.m */; };
|
||||
7B9E644F1A23129B0072FF42 /* BlogStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 7B9E644E1A23129B0072FF42 /* BlogStatus.m */; };
|
||||
7BBA86971B4264AB00FD2320 /* NotificationToSelectorMap.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BBA86961B4264AB00FD2320 /* NotificationToSelectorMap.m */; };
|
||||
7BE3A0351AE461E700E45CCB /* PreviewViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BE3A0341AE461E700E45CCB /* PreviewViewController.m */; };
|
||||
7BF029331A27117200E42EDE /* ModelStore.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BF029321A27117200E42EDE /* ModelStore.m */; };
|
||||
7BF029381A280CB200E42EDE /* BlogController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BF029371A280CB200E42EDE /* BlogController.m */; };
|
||||
|
|
@ -166,6 +167,8 @@
|
|||
7B9E644B1A230B940072FF42 /* JSONHTTPClient.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONHTTPClient.m; sourceTree = "<group>"; };
|
||||
7B9E644D1A23129B0072FF42 /* BlogStatus.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BlogStatus.h; sourceTree = "<group>"; };
|
||||
7B9E644E1A23129B0072FF42 /* BlogStatus.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BlogStatus.m; sourceTree = "<group>"; };
|
||||
7BBA86951B4264AB00FD2320 /* NotificationToSelectorMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NotificationToSelectorMap.h; sourceTree = "<group>"; };
|
||||
7BBA86961B4264AB00FD2320 /* NotificationToSelectorMap.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NotificationToSelectorMap.m; sourceTree = "<group>"; };
|
||||
7BE3A0331AE461E700E45CCB /* PreviewViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PreviewViewController.h; sourceTree = "<group>"; };
|
||||
7BE3A0341AE461E700E45CCB /* PreviewViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PreviewViewController.m; sourceTree = "<group>"; };
|
||||
7BF029311A27117200E42EDE /* ModelStore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ModelStore.h; sourceTree = "<group>"; };
|
||||
|
|
@ -321,6 +324,8 @@
|
|||
1BCFCA73B716677EFBCEC417 /* CommonUI.h */,
|
||||
1BCFC1C7E02F4D1867A5D711 /* BlogSplitViewController.m */,
|
||||
1BCFC63F2FE2EFBC1C513859 /* BlogSplitViewController.h */,
|
||||
7BBA86951B4264AB00FD2320 /* NotificationToSelectorMap.h */,
|
||||
7BBA86961B4264AB00FD2320 /* NotificationToSelectorMap.m */,
|
||||
);
|
||||
path = Blog;
|
||||
sourceTree = "<group>";
|
||||
|
|
@ -679,6 +684,7 @@
|
|||
7B9E642E1A227FA20072FF42 /* NSString+marshmallows.m in Sources */,
|
||||
7B9E64421A22F3840072FF42 /* BlogService.m in Sources */,
|
||||
7B5C4BDF19F2606900667D48 /* main.m in Sources */,
|
||||
7BBA86971B4264AB00FD2320 /* NotificationToSelectorMap.m in Sources */,
|
||||
7BF029331A27117200E42EDE /* ModelStore.m in Sources */,
|
||||
7B2B43511AED5BA500BAC4E2 /* Functions.m in Sources */,
|
||||
7BF029381A280CB200E42EDE /* BlogController.m in Sources */,
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@
|
|||
#import "UIColor+Hex.h"
|
||||
#import "MBProgressHUD.h"
|
||||
#import "CommonUI.h"
|
||||
#import "NotificationToSelectorMap.h"
|
||||
|
||||
@interface EditorViewController () <UITextViewDelegate, UIPopoverPresentationControllerDelegate>
|
||||
|
||||
|
|
@ -34,6 +35,7 @@
|
|||
@property (nonatomic, strong) Post *modifiedPost;
|
||||
@property (nonatomic, readonly, assign, getter=isDirty) BOOL dirty;
|
||||
@property (nonatomic, strong) PMKPromise *savePromise;
|
||||
@property (nonatomic, strong) NotificationToSelectorMap *notificationMap;
|
||||
|
||||
@end
|
||||
|
||||
|
|
@ -42,39 +44,46 @@
|
|||
- (void)viewDidLoad {
|
||||
[super viewDidLoad];
|
||||
[self setupFontAwesomeIcons];
|
||||
[self setupNotifications];
|
||||
self.linkView.hidden = YES;
|
||||
}
|
||||
|
||||
- (void)setupNotifications {
|
||||
NSDictionary *map = @{UIApplicationWillResignActiveNotification: NSStringFromSelector(@selector(applicationWillResignActive:)),
|
||||
UIApplicationDidBecomeActiveNotification: NSStringFromSelector(@selector(applicationDidBecomeActive:)),
|
||||
UIPasteboardChangedNotification: NSStringFromSelector(@selector(configureLinkView)),
|
||||
UIKeyboardWillShowNotification: NSStringFromSelector(@selector(keyboardWillShow:)),
|
||||
UIKeyboardWillHideNotification: NSStringFromSelector(@selector(keyboardWillHide:)),
|
||||
DraftRemovedNotification: NSStringFromSelector(@selector(postDeleted:)),
|
||||
PublishedPostRemovedNotification: NSStringFromSelector(@selector(postDeleted:)),
|
||||
};
|
||||
self.notificationMap = [[NotificationToSelectorMap alloc] initWithNotificationMap:map];
|
||||
}
|
||||
|
||||
- (void)viewWillAppear:(BOOL)animated {
|
||||
[super viewWillAppear:animated];
|
||||
NSAssert(self.blogController, @"blogController is required");
|
||||
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
|
||||
[notificationCenter addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
|
||||
[notificationCenter addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
|
||||
[notificationCenter addObserver:self selector:@selector(configureLinkView) name:UIPasteboardChangedNotification object:nil];
|
||||
[notificationCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
|
||||
[notificationCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
|
||||
[notificationCenter addObserver:self selector:@selector(postDeleted:) name:DraftRemovedNotification object:nil];
|
||||
[notificationCenter addObserver:self selector:@selector(postDeleted:) name:PublishedPostRemovedNotification object:nil];
|
||||
[self configureView];
|
||||
[self restoreScrollOffset];
|
||||
}
|
||||
|
||||
- (void)viewDidAppear:(BOOL)animated {
|
||||
[super viewDidAppear:animated];
|
||||
[self.notificationMap addObserver:self];
|
||||
}
|
||||
|
||||
- (void)viewWillDisappear:(BOOL)animated {
|
||||
[super viewWillDisappear:animated];
|
||||
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
|
||||
[notificationCenter removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
|
||||
[notificationCenter removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
|
||||
[notificationCenter removeObserver:self name:UIPasteboardChangedNotification object:nil];
|
||||
[notificationCenter removeObserver:self name:UIKeyboardWillShowNotification object:nil];
|
||||
[notificationCenter removeObserver:self name:UIKeyboardWillHideNotification object:nil];
|
||||
[notificationCenter removeObserver:self name:DraftRemovedNotification object:nil];
|
||||
[notificationCenter removeObserver:self name:PublishedPostRemovedNotification object:nil];
|
||||
if (self.post) {
|
||||
[self savePostAndWaitForCompilation:NO];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)viewDidDisappear:(BOOL)animated {
|
||||
[super viewDidDisappear:animated];
|
||||
[self.notificationMap removeObserver:self];
|
||||
}
|
||||
|
||||
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
||||
[super prepareForSegue:segue sender:sender];
|
||||
if ([segue.identifier isEqualToString:@"showPreview"]) {
|
||||
|
|
|
|||
19
Blog/NotificationToSelectorMap.h
Normal file
19
Blog/NotificationToSelectorMap.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
//
|
||||
// NotificationToSelectorMap.h
|
||||
// Blog
|
||||
//
|
||||
// Created by Sami Samhuri on 2015-06-29.
|
||||
// Copyright © 2015 Guru Logic Inc. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface NotificationToSelectorMap : NSObject
|
||||
|
||||
@property (nonatomic, copy) NSDictionary *notificationNameToSelectorNameMap;
|
||||
|
||||
- (instancetype)initWithNotificationMap:(nonnull NSDictionary *)notificationMap;
|
||||
- (void)addObserver:(NSObject *)observer;
|
||||
- (void)removeObserver:(NSObject *)observer;
|
||||
|
||||
@end
|
||||
36
Blog/NotificationToSelectorMap.m
Normal file
36
Blog/NotificationToSelectorMap.m
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
//
|
||||
// NotificationToSelectorMap.m
|
||||
// Blog
|
||||
//
|
||||
// Created by Sami Samhuri on 2015-06-29.
|
||||
// Copyright © 2015 Guru Logic Inc. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NotificationToSelectorMap.h"
|
||||
|
||||
@implementation NotificationToSelectorMap
|
||||
|
||||
- (instancetype)initWithNotificationMap:(nonnull NSDictionary *)notificationMap {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_notificationNameToSelectorNameMap = notificationMap;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)addObserver:(NSObject *)observer {
|
||||
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
|
||||
for (NSString *notificationName in self.notificationNameToSelectorNameMap.allKeys) {
|
||||
NSString *selectorName = self.notificationNameToSelectorNameMap[notificationName];
|
||||
[notificationCenter addObserver:observer selector:NSSelectorFromString(selectorName) name:notificationName object:nil];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)removeObserver:(NSObject *)observer {
|
||||
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
|
||||
for (NSString *notificationName in self.notificationNameToSelectorNameMap.allKeys) {
|
||||
[notificationCenter removeObserver:observer name:notificationName object:nil];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
Loading…
Reference in a new issue