From 6d065e6381dc0f1d37019ac5467855779824a7cc Mon Sep 17 00:00:00 2001 From: David Chiles Date: Tue, 19 May 2015 16:11:19 -0700 Subject: [PATCH] Added KVOController should fix #349 --- .../Classes/Controllers/OTRProtocolManager.m | 78 +++++++++---------- Podfile | 1 + Podfile.lock | 3 + 3 files changed, 43 insertions(+), 39 deletions(-) diff --git a/ChatSecure/Classes/Controllers/OTRProtocolManager.m b/ChatSecure/Classes/Controllers/OTRProtocolManager.m index bd471e36f..bcdfbff74 100644 --- a/ChatSecure/Classes/Controllers/OTRProtocolManager.m +++ b/ChatSecure/Classes/Controllers/OTRProtocolManager.m @@ -30,7 +30,7 @@ #import "OTRDatabaseManager.h" #import "YapDatabaseConnection.h" #import "YapDatabaseTransaction.h" - +#import #import "OTRLog.h" static OTRProtocolManager *sharedManager = nil; @@ -41,7 +41,7 @@ @interface OTRProtocolManager () @property (nonatomic) NSUInteger numberOfConnectingProtocols; @property (nonatomic, strong) OTRPushManager *pushManager; @property (nonatomic, strong) NSMutableDictionary * protocolManagerDictionary; - +@property (nonatomic, strong) FBKVOController *KVOController; @property (nonatomic) dispatch_queue_t internalQueue; @end @@ -56,7 +56,8 @@ -(id)init self.numberOfConnectedProtocols = 0; self.numberOfConnectingProtocols = 0; self.encryptionManager = [[OTREncryptionManager alloc] init]; - self.protocolManagerDictionary = [NSMutableDictionary new]; + self.protocolManagerDictionary = [[NSMutableDictionary alloc] init]; + self.KVOController = [FBKVOController controllerWithObserver:self]; } return self; } @@ -64,10 +65,6 @@ -(id)init - (void)removeProtocolForAccount:(OTRAccount *)account { @synchronized(self.protocolManagerDictionary) { - id protocol = self.protocolManagerDictionary[account.uniqueId]; - if (protocol) { - [protocol removeObserver:self forKeyPath:NSStringFromSelector(@selector(connectionStatus))]; - } [self.protocolManagerDictionary removeObjectForKey:account.uniqueId]; } } @@ -76,7 +73,7 @@ - (void)addProtocol:(id)protocol forAccount:(OTRAccount *)account { @synchronized(self.protocolManagerDictionary){ [self.protocolManagerDictionary setObject:protocol forKey:account.uniqueId]; - [protocol addObserver:self forKeyPath:NSStringFromSelector(@selector(connectionStatus)) options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL]; + [self.KVOController observe:protocol keyPath:NSStringFromSelector(@selector(connectionStatus)) options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld action:@selector(protocolDidChange:)]; } } @@ -175,39 +172,42 @@ - (void)disconnectAllAccounts } } --(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +- (void)protocolDidChange:(NSDictionary *)change { - if ([keyPath isEqualToString:NSStringFromSelector(@selector(connectionStatus))]) { - OTRProtocolConnectionStatus newStatus = [[change objectForKey:NSKeyValueChangeNewKey] integerValue]; - OTRProtocolConnectionStatus oldStatus = [[change objectForKey:NSKeyValueChangeOldKey] integerValue]; - NSInteger connectedInt = 0; - NSInteger connectingInt = 0; - - switch (oldStatus) { - case OTRProtocolConnectionStatusConnected: - connectedInt = -1; - break; - case OTRProtocolConnectionStatusConnecting: - connectingInt = -1; - default: - break; - } - - switch (newStatus) { - case OTRProtocolConnectionStatusConnected: - connectedInt = 1; - break; - case OTRProtocolConnectionStatusConnecting: - connectedInt = 1; - default: - break; - } - - - self.numberOfConnectedProtocols += connectedInt; - self.numberOfConnectingProtocols += connectingInt; + + OTRProtocolConnectionStatus newStatus = [[change objectForKey:NSKeyValueChangeNewKey] integerValue]; + OTRProtocolConnectionStatus oldStatus = [[change objectForKey:NSKeyValueChangeOldKey] integerValue]; + + if (oldStatus == newStatus) { + return; } - + + NSInteger connectedInt = 0; + NSInteger connectingInt = 0; + + switch (oldStatus) { + case OTRProtocolConnectionStatusConnected: + connectedInt = -1; + break; + case OTRProtocolConnectionStatusConnecting: + connectingInt = -1; + default: + break; + } + + switch (newStatus) { + case OTRProtocolConnectionStatusConnected: + connectedInt = 1; + break; + case OTRProtocolConnectionStatusConnecting: + connectingInt = 1; + default: + break; + } + + + self.numberOfConnectedProtocols += connectedInt; + self.numberOfConnectingProtocols += connectingInt; } -(BOOL)isAccountConnected:(OTRAccount *)account; diff --git a/Podfile b/Podfile index 552592c60..02547488f 100755 --- a/Podfile +++ b/Podfile @@ -35,6 +35,7 @@ pod 'TUSafariActivity', '~> 1.0' pod 'ARChromeActivity', '~> 1.0' pod 'CocoaAsyncSocket', '~> 7.4' pod 'JTSImageViewController', '~> 1.4' +pod 'KVOController', '~> 1.0' # Local Podspecs pod 'gtm-http-fetcher', :podspec => 'Podspecs/gtm-http-fetcher.podspec.json' diff --git a/Podfile.lock b/Podfile.lock index ee37f9349..0cd17bce6 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -58,6 +58,7 @@ PODS: - JTSImageViewController (1.4) - JTTargetActionBlock (1.0.0) - JVFloatLabeledTextField (1.0.2) + - KVOController (1.0.3) - libsqlfs/common (1.3) - libsqlfs/SQLCipher (1.3): - libsqlfs/common @@ -264,6 +265,7 @@ DEPENDENCIES: - JTSImageViewController (~> 1.4) - JTTargetActionBlock (~> 1.0) - JVFloatLabeledTextField (~> 1.0) + - KVOController (~> 1.0) - MagicalRecord (~> 2.2) - Mantle (~> 1.4) - MBProgressHUD (~> 0.9) @@ -344,6 +346,7 @@ SPEC CHECKSUMS: JTSImageViewController: 6a367b144e257db02cdfacdba78d684377d3777b JTTargetActionBlock: 6e9201e5fa88651deb354c91b7c9c6487a1abe55 JVFloatLabeledTextField: 58a3a32cfb800e5b224f676987e7c13abf50a14d + KVOController: 9fd8f0343670994e4b6f9f0b31f5a45663f3e1cf libsqlfs: 179e4fb40b8c07fd0ee7933ec4c0b9f4815cbd0e MagicalRecord: f8a56bb87ab6552f20c4bb8681a1958a197ea3cd Mantle: 60acd0cd363e27d945bda0a6b8e90577112d28f3