forked from CodaFi/AFNetworking-RACExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRAFNMainViewController.m
More file actions
179 lines (140 loc) · 6.53 KB
/
Copy pathRAFNMainViewController.m
File metadata and controls
179 lines (140 loc) · 6.53 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// RAFNMainViewController.m
// Reactive AFNetworking Example
//
// Created by Robert Widmann on 3/28/13.
// Copyright (c) 2013 CodaFi. All rights reserved.
//
#import "RAFNMainViewController.h"
//Preserve double completion blocks for testing
#define RAFN_MAINTAIN_COMPLETION_BLOCKS
#import "RACAFNetworking.h"
@interface RAFNMainViewController ()
@property (nonatomic, strong) UITextView *statusTextView;
@property (nonatomic, strong) UIImageView *afLogoImageView;
@property (nonatomic, strong) UIButton *startTestingButton;
@property (nonatomic, strong) AFHTTPRequestOperationManager *httpClient;
@property (nonatomic, assign) BOOL isTesting;
@property (nonatomic, strong) RACDisposable *currentDisposable;
@property (nonatomic, strong) RACSubject *statusSignal;
@end
@implementation RAFNMainViewController
- (id)init {
self = [super init];
//Signal for the textview's text
_statusSignal = [RACSubject subject];
return self;
}
- (void)viewDidLoad {
// Do any additional setup after loading the view.
CGRect slice, remainder;
CGRectDivide(self.view.bounds, &slice, &remainder, 44, CGRectMaxYEdge);
self.statusTextView = [[UITextView alloc]initWithFrame:remainder];
self.statusTextView.editable = NO;
self.statusTextView.font = [UIFont fontWithName:@"Helvetica" size:20.0f];
[self.statusTextView setTextAlignment:NSTextAlignmentCenter];
[self.statusTextView rac_liftSelector:@selector(setText:) withSignals:self.statusSignal, nil];
self.afLogoImageView = [[UIImageView alloc]initWithFrame:CGRectOffset(remainder, 0, CGRectGetHeight(UIScreen.mainScreen.bounds))];
self.startTestingButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.startTestingButton setTitle:@"Start Testing" forState:UIControlStateNormal];
self.startTestingButton.frame = self.view.bounds;
[[self.startTestingButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(UIButton *testingButton) {
self.isTesting = !self.isTesting;
[testingButton setTitle:(self.isTesting ? @"Cancel Testing..." : @"Start Testing") forState:UIControlStateNormal];
[UIView animateWithDuration:0.5 animations:^{
[testingButton setFrame:(self.isTesting ? slice : self.view.bounds)];
}];
if (self.isTesting) {
[self testImageFetch];
} else {
[self cancelTheShow];
}
}];
[self.view addSubview:self.statusTextView];
[self.view addSubview:self.afLogoImageView];
[self.view addSubview:self.startTestingButton];
//Get network status
self.httpClient = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:@"https://www.google.com"]];
self.httpClient.responseSerializer = [AFJSONResponseSerializer serializer];
[self.httpClient.networkReachabilityStatusSignal subscribeNext:^(NSNumber *status) {
AFNetworkReachabilityStatus networkStatus = [status intValue];
switch (networkStatus) {
case AFNetworkReachabilityStatusUnknown:
case AFNetworkReachabilityStatusNotReachable:
[self.statusSignal sendNext:@"Cannot Reach Host"];
[self cancelTheShow];
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
case AFNetworkReachabilityStatusReachableViaWiFi:
break;
}
}];
[super viewDidLoad];
}
- (void)testImageFetch {
//Fetch the image. WHen fetched, animate the logo image up, then down and start the next test.
[self.statusSignal sendNext:@"Fetching AFNetworking Logo..."];
RACSubject *imageSubject = [RACSubject subject];
[self.afLogoImageView rac_liftSelector:@selector(setImage:) withSignals:imageSubject, nil];
NSString *urlStr = @"https://twimg0-a.akamaihd.net/profile_images/2331579964/jrqzn4q29vwy4mors75s.png";
self.httpClient.responseSerializer = [AFImageResponseSerializer serializer];
_currentDisposable = [[[self.httpClient rac_GET:urlStr parameters:nil] map:^id(RACTuple *value) {
return [value first];
}] subscribeNext:^(UIImage *image) {
[imageSubject sendNext:image];
CGRect slice, remainder;
CGRectDivide(self.view.bounds, &slice, &remainder, 44, CGRectMaxYEdge);
[UIView animateWithDuration:0.5 animations:^{
[self.afLogoImageView setFrame:remainder];
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 delay:1 options:0 animations:^{
[self.afLogoImageView setFrame:CGRectOffset(remainder, 0, CGRectGetHeight(UIScreen.mainScreen.bounds))];
} completion:^(BOOL finished) {
[self testXMLFetch];
}];
}];
}];
}
- (void)testXMLFetch {
//Fetch the Flickr feed for groups.
[self.statusSignal sendNext:@"Fetching Flickr XML..."];
NSString *urlStr = @"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792%40N01&format=rest";
self.httpClient.responseSerializer = [AFXMLParserResponseSerializer serializer];
_currentDisposable = [[[self.httpClient rac_GET:urlStr parameters:nil]map:^id(RACTuple *value) {
return [value second];
}] subscribeNext:^(NSHTTPURLResponse *response) {
[self.statusSignal sendNext:response.allHeaderFields.description];
[self performSelector:@selector(testError) withObject:nil afterDelay:0.5];
}];
}
- (void)testError {
[self.statusSignal sendNext:@"Sending Error-Prone Request..."];
//Send an un-authorized request to show how error blocks work.
NSString *urlStr = @"http://api.flickr.com/";
NSURL *url = [NSURL URLWithString:urlStr];
NSDictionary *params = [[NSDictionary alloc]initWithObjectsAndKeys:@"json", @"format", @"66854529@N00", @"user_id", @"1", @"nojsoncallback", nil];
NSString *path = [[NSString alloc]initWithFormat:@"services/rest/?method=flickr.people.getPhotos"];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:url];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
_currentDisposable = [[manager rac_GET:path parameters:params] subscribeError:^(NSError *error) {
[self.statusSignal sendNext:[error localizedDescription]];
[self performSelector:@selector(finish) withObject:nil afterDelay:0.5];
}];
}
- (void)finish {
[self.statusSignal sendNext:@"Finished!"];
[self.startTestingButton setTitle:@"Restart Tests" forState:UIControlStateNormal];
self.isTesting = !self.isTesting;
}
- (void)cancelTheShow {
//Kills the current disposable and removes the image view.
CGRect slice, remainder;
CGRectDivide(self.view.bounds, &slice, &remainder, 44, CGRectMaxYEdge);
[self.statusSignal sendNext:@""];
[_currentDisposable dispose];
[UIView animateWithDuration:0.5 animations:^{
[self.afLogoImageView setFrame:CGRectOffset(remainder, 0, CGRectGetHeight(UIScreen.mainScreen.bounds))];
}];
}
@end