forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentFeedModel.m
More file actions
206 lines (165 loc) · 6.19 KB
/
Copy pathCommentFeedModel.m
File metadata and controls
206 lines (165 loc) · 6.19 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// CommentFeedModel.m
// Sample
//
// Created by Hannah Troisi on 3/9/16.
//
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "CommentFeedModel.h"
#import "Utilities.h"
#define NUM_COMMENTS_TO_SHOW 3
#define fiveHundredPX_ENDPOINT_HOST @"https://api.500px.com/v1/"
#define fiveHundredPX_ENDPOINT_COMMENTS @"photos/4928401/comments"
#define fiveHundredPX_ENDPOINT_SEARCH @"photos/search?geo=" //latitude,longitude,radius<units>
#define fiveHundredPX_ENDPOINT_USER @"photos?user_id="
#define fiveHundredPX_CONSUMER_KEY_PARAM @"&consumer_key=Fi13GVb8g53sGvHICzlram7QkKOlSDmAmp9s9aqC"
@implementation CommentFeedModel
{
NSMutableArray *_comments; // array of CommentModel objects
NSString *_photoID;
NSString *_urlString;
NSUInteger _currentPage;
NSUInteger _totalPages;
NSUInteger _totalItems;
BOOL _fetchPageInProgress;
BOOL _refreshFeedInProgress;
}
#pragma mark - Properties
- (NSMutableArray *)comments
{
return _comments;
}
#pragma mark - Lifecycle
- (instancetype)initWithPhotoID:(NSString *)photoID
{
self = [super init];
if (self) {
_photoID = photoID;
_currentPage = 0;
_totalPages = 0;
_totalItems = 0;
_comments = [[NSMutableArray alloc] init];
_urlString = [NSString stringWithFormat:@"https://api.500px.com/v1/photos/%@/comments?",photoID];
}
return self;
}
#pragma mark - Instance Methods
- (NSUInteger)numberOfItemsInFeed
{
return [_comments count];
}
- (CommentModel *)objectAtIndex:(NSUInteger)index
{
return [_comments objectAtIndex:index];
}
- (NSUInteger)numberOfCommentsForPhoto
{
return _totalItems;
}
- (BOOL)numberOfCommentsForPhotoExceedsInteger:(NSUInteger)number
{
return (_totalItems > number);
}
- (NSAttributedString *)viewAllCommentsAttributedString
{
NSString *string = [NSString stringWithFormat:@"View all %@ comments", [NSNumber numberWithUnsignedInteger:_totalItems]];
NSAttributedString *attrString = [NSAttributedString attributedStringWithString:string fontSize:14 color:[UIColor lightGrayColor] firstWordColor:nil];
return attrString;
}
- (void)requestPageWithCompletionBlock:(void (^)(NSArray *))block
{
// only one fetch at a time
if (_fetchPageInProgress) {
return;
} else {
_fetchPageInProgress = YES;
[self fetchPageWithCompletionBlock:block];
}
}
- (void)refreshFeedWithCompletionBlock:(void (^)(NSArray *))block
{
// only one fetch at a time
if (_refreshFeedInProgress) {
return;
} else {
_refreshFeedInProgress = YES;
_currentPage = 0;
// FIXME: blow away any other requests in progress
[self fetchPageWithCompletionBlock:^(NSArray *newPhotos) {
if (block) {
block(newPhotos);
}
_refreshFeedInProgress = NO;
} replaceData:YES];
}
}
#pragma mark - Helper Methods
- (void)fetchPageWithCompletionBlock:(void (^)(NSArray *))block
{
[self fetchPageWithCompletionBlock:block replaceData:NO];
}
- (void)fetchPageWithCompletionBlock:(void (^)(NSArray *))block replaceData:(BOOL)replaceData
{
// early return if reached end of pages
if (_totalPages) {
if (_currentPage == _totalPages) {
return;
}
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableArray *newComments = [NSMutableArray array];
NSUInteger nextPage = _currentPage + 1;
NSString *urlAdditions = [NSString stringWithFormat:@"page=%lu", (unsigned long)nextPage];
NSURL *url = [NSURL URLWithString:[_urlString stringByAppendingString:urlAdditions]];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
if ([response isKindOfClass:[NSDictionary class]]) {
_currentPage = [[response valueForKeyPath:@"current_page"] integerValue];
_totalPages = [[response valueForKeyPath:@"total_pages"] integerValue];
_totalItems = [[response valueForKeyPath:@"total_items"] integerValue];
NSArray *comments = [response valueForKeyPath:@"comments"];
if ([comments isKindOfClass:[NSArray class]]) {
NSUInteger numComments = [comments count];
if (numComments > NUM_COMMENTS_TO_SHOW) {
comments = [comments subarrayWithRange:(NSRange){numComments-NUM_COMMENTS_TO_SHOW, NUM_COMMENTS_TO_SHOW}];
}
for (NSDictionary *commentDictionary in comments) {
if ([response isKindOfClass:[NSDictionary class]]) {
CommentModel *comment = [[CommentModel alloc] initWithDictionary:commentDictionary];
if (comment) {
[newComments addObject:comment];
}
}
}
}
}
}
dispatch_async(dispatch_get_main_queue(), ^{
_fetchPageInProgress = NO;
if (replaceData) {
_comments = [newComments mutableCopy];
} else {
[_comments addObjectsFromArray:newComments];
}
if (block) {
block(newComments);
}
});
}];
[task resume];
});
}
@end