Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 112 lines (90 sloc) 3.864 kB
a8bce05 @WH-NewMedia Add wh-app-ios files.
WH-NewMedia authored
1 /*
2 * This project constitutes a work of the United States Government and is
3 * not subject to domestic copyright protection under 17 USC § 105.
4 *
5 * However, because the project utilizes code licensed from contributors
6 * and other third parties, it therefore is licensed under the MIT
7 * License. http://opensource.org/licenses/mit-license.php. Under that
8 * license, permission is granted free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the conditions that any appropriate copyright notices and this
15 * permission notice are included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 //
28 // WHSearchController.m
29 // WhiteHouseApp
30 //
31 //
32
33 #import "WHSearchController.h"
34
35 @interface WHSearchController ()
36 @property (nonatomic, strong) NSOperationQueue *queue;
37 @property (nonatomic, assign) int page;
38 @end
39
40 @implementation WHSearchController
41 @synthesize query;
42 @synthesize delegate = _delegate;
43 @synthesize results = _results;
44
45 @synthesize queue;
46 @synthesize page;
47
48
49 - (id)initWithDelegate:(id <WHSearchControllerDelegate>)delegate
50 {
51 if ((self = [super init])) {
52 self.delegate = delegate;
53 self.queue = [NSOperationQueue new];
54 self.queue.maxConcurrentOperationCount = 1;
55 self.page = 0;
56 }
57
58 return self;
59 }
60
61
62 - (void)reportError:(NSError *)error
63 {
64 DebugLog(@"Error in search: %@", error.localizedDescription);
65 [self.delegate searchController:self didFailWithError:error];
66 }
67
68
69 + (NSString *)escapeQueryParameter:(NSString *)param
70 {
71 return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge CFStringRef)param, NULL, (CFStringRef)@"&: _=-?!%", kCFStringEncodingUTF8));
72 }
73
74
75 - (void)fetchResults
76 {
77 self.page = self.page + 1;
78
79 NSString *formatString = AppConfig(@"SearchURLFormat");
80 NSString *escaped = [[self class] escapeQueryParameter:query];
81 NSURL *searchURL = [NSURL URLWithString:[NSString stringWithFormat:formatString, escaped, self.page]];
82
83 DebugLog(@"Search URL = %@", searchURL);
84 NINetworkRequestOperation *op = [[NINetworkRequestOperation alloc] initWithURL:searchURL];
85
86 // this block will be called with the operation itself
87 op.didFinishBlock = ^(id obj) {
88 // cast it to access network-op-specific properties
89 id result = [NSJSONSerialization JSONObjectWithData:op.data options:0 error:nil];
90 DebugLog(@"API result = %@", result);
91 id results = [result objectForKey:@"results"];
92 if (results && [results respondsToSelector:@selector(objectAtIndex:)]) {
93 if (self.results) {
94 self.results = [self.results arrayByAddingObjectsFromArray:results];
95 } else {
96 self.results = results;
97 }
98 [self.delegate searchControllerDidFindResults:self];
99 }
100 };
101
102 op.didFailWithErrorBlock = ^(id obj, NSError *error) {
103 [self reportError:error];
104 };
105
106 // the operation will start when added to the queue
107 [self.queue addOperation:op];
108 }
109
110
111 @end
Something went wrong with that request. Please try again.