Skip to content

Commit

Permalink
Added KVOController should fix #349
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchiles committed May 19, 2015
1 parent acea8f8 commit 6d065e6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
78 changes: 39 additions & 39 deletions ChatSecure/Classes/Controllers/OTRProtocolManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#import "OTRDatabaseManager.h"
#import "YapDatabaseConnection.h"
#import "YapDatabaseTransaction.h"

#import <KVOController/FBKVOController.h>
#import "OTRLog.h"

static OTRProtocolManager *sharedManager = nil;
Expand All @@ -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
Expand All @@ -56,18 +56,15 @@ -(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;
}

- (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];
}
}
Expand All @@ -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:)];
}
}

Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -344,6 +346,7 @@ SPEC CHECKSUMS:
JTSImageViewController: 6a367b144e257db02cdfacdba78d684377d3777b
JTTargetActionBlock: 6e9201e5fa88651deb354c91b7c9c6487a1abe55
JVFloatLabeledTextField: 58a3a32cfb800e5b224f676987e7c13abf50a14d
KVOController: 9fd8f0343670994e4b6f9f0b31f5a45663f3e1cf
libsqlfs: 179e4fb40b8c07fd0ee7933ec4c0b9f4815cbd0e
MagicalRecord: f8a56bb87ab6552f20c4bb8681a1958a197ea3cd
Mantle: 60acd0cd363e27d945bda0a6b8e90577112d28f3
Expand Down

0 comments on commit 6d065e6

Please sign in to comment.