-
Notifications
You must be signed in to change notification settings - Fork 0
/
RCManager.m
105 lines (86 loc) · 2.25 KB
/
RCManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// RCManager.m
// RestClient
//
// Created by Alberto García Hierro on 16/04/09.
// Copyright 2009 Alberto García Hierro. All rights reserved.
//
#import "RCCall.h"
#import "RCManager.h"
@implementation RCManager
@synthesize baseURL = baseURL_;
@synthesize delegate = delegate_;
- (id)init {
return [self initWithBaseURL:nil];
}
- (id)initWithBaseURL:(NSString *)theBaseURL {
return [self initWithBaseURL:theBaseURL delegate:nil];
}
- (id)initWithBaseURL:(NSString *)theBaseURL delegate:(id <NSObject>)theDelegate {
if (self = [super init]) {
self.baseURL = theBaseURL;
self.delegate = theDelegate;
managedCalls_ = [[NSMutableSet alloc] init];
}
return self;
}
- (void)dealloc {
[baseURL_ release];
[managedCalls_ makeObjectsPerformSelector:@selector(cancel)];
[managedCalls_ release];
[super dealloc];
}
- (void)mayHideNetworkIndicator {
if (managedCalls_.count == 0) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
}
- (void)cancelCall:(RCCall *)theCall {
if ([managedCalls_ containsObject:theCall]) {
@synchronized(managedCalls_) {
[theCall cancel];
[managedCalls_ removeObject:theCall];
[self mayHideNetworkIndicator];
}
}
}
- (void)cancelCalls {
@synchronized(managedCalls_) {
[managedCalls_ makeObjectsPerformSelector:@selector(cancel)];
[managedCalls_ removeAllObjects];
[self mayHideNetworkIndicator];
}
}
- (void)cancelCallsForDelegate:(NSObject *)theDelegate {
@synchronized(managedCalls_) {
NSSet *theCalls = [NSSet setWithSet:managedCalls_];
for (RCCall *aCall in theCalls) {
if (aCall.delegate == theDelegate) {
[aCall cancel];
[managedCalls_ removeObject:aCall];
}
}
[self mayHideNetworkIndicator];
}
}
- (void)pushCall:(RCCall *)theCall {
theCall.manager = self;
theCall.delegate = delegate_;
if (baseURL_) {
theCall.callURL = [baseURL_ stringByAppendingString:theCall.callURL];
}
@synchronized(managedCalls_) {
[managedCalls_ addObject:theCall];
if (managedCalls_.count == 1) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
}
[theCall perform];
}
- (void)callDidComplete:(RCCall *)theCall {
@synchronized(managedCalls_) {
[managedCalls_ removeObject:theCall];
[self mayHideNetworkIndicator];
}
}
@end