Commit c3656cafffb5d95c763fcf277c19dc650ce02657
1 parent
3c805063b5
Exists in
master
update refreshToken
Showing 1 changed file with 118 additions and 37 deletions Inline Diff
LifeLog/LifeLog/ServerAPI.m
1 | // | 1 | // |
2 | // ServerAPI.m | 2 | // ServerAPI.m |
3 | // LifeLog | 3 | // LifeLog |
4 | // | 4 | // |
5 | // Created by Nguyen Van Phong on 7/30/17. | 5 | // Created by Nguyen Van Phong on 7/30/17. |
6 | // Copyright © 2017 PhongNV. All rights reserved. | 6 | // Copyright © 2017 PhongNV. All rights reserved. |
7 | // | 7 | // |
8 | 8 | ||
9 | #import "ServerAPI.h" | 9 | #import "ServerAPI.h" |
10 | #import "Utilities.h" | 10 | #import "Utilities.h" |
11 | 11 | ||
12 | NSString *const kServerAddress = @"http://clover.timesfun.jp:9001/"; | 12 | NSString *const kServerAddress = @"http://clover.timesfun.jp:9001/"; |
13 | NSString *const kUser = @"KEY_USER"; | 13 | NSString *const kUser = @"KEY_USER"; |
14 | NSString *const kToken = @"KEY_TOKEN"; | 14 | NSString *const kToken = @"KEY_TOKEN"; |
15 | NSString *const kNotificationToken = @"TOKEN_INVALID"; | 15 | NSString *const kNotificationToken = @"TOKEN_INVALID"; |
16 | 16 | ||
17 | @implementation NSString (NSString_Extended) | 17 | @implementation NSString (NSString_Extended) |
18 | - (NSString *)urlencode { | 18 | - (NSString *)urlencode { |
19 | NSMutableString *output = [NSMutableString string]; | 19 | NSMutableString *output = [NSMutableString string]; |
20 | const unsigned char *source = (const unsigned char *)[self UTF8String]; | 20 | const unsigned char *source = (const unsigned char *)[self UTF8String]; |
21 | int sourceLen = (int)strlen((const char *)source); | 21 | int sourceLen = (int)strlen((const char *)source); |
22 | for (int i = 0; i < sourceLen; ++i) { | 22 | for (int i = 0; i < sourceLen; ++i) { |
23 | const unsigned char thisChar = source[i]; | 23 | const unsigned char thisChar = source[i]; |
24 | if (thisChar == ' '){ | 24 | if (thisChar == ' '){ |
25 | [output appendString:@"+"]; | 25 | [output appendString:@"+"]; |
26 | } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' || | 26 | } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' || |
27 | (thisChar >= 'a' && thisChar <= 'z') || | 27 | (thisChar >= 'a' && thisChar <= 'z') || |
28 | (thisChar >= 'A' && thisChar <= 'Z') || | 28 | (thisChar >= 'A' && thisChar <= 'Z') || |
29 | (thisChar >= '0' && thisChar <= '9')) { | 29 | (thisChar >= '0' && thisChar <= '9')) { |
30 | [output appendFormat:@"%c", thisChar]; | 30 | [output appendFormat:@"%c", thisChar]; |
31 | } else { | 31 | } else { |
32 | [output appendFormat:@"%%%02X", thisChar]; | 32 | [output appendFormat:@"%%%02X", thisChar]; |
33 | } | 33 | } |
34 | } | 34 | } |
35 | return output; | 35 | return output; |
36 | } | 36 | } |
37 | @end | 37 | @end |
38 | 38 | ||
39 | @implementation ServerAPI | 39 | @implementation ServerAPI |
40 | static ServerAPI *_server = nil; | 40 | static ServerAPI *_server = nil; |
41 | 41 | ||
42 | NSURLSessionDataTask * searchTask; | 42 | NSURLSessionDataTask * searchTask; |
43 | 43 | ||
44 | @synthesize timeOutInterval = _timeOutInterval; | 44 | @synthesize timeOutInterval = _timeOutInterval; |
45 | 45 | ||
46 | + (instancetype)server | 46 | + (instancetype)server |
47 | { | 47 | { |
48 | @synchronized(self) { | 48 | @synchronized(self) { |
49 | if (_server == nil) { | 49 | if (_server == nil) { |
50 | _server = [[ServerAPI alloc] init]; | 50 | _server = [[ServerAPI alloc] init]; |
51 | } | 51 | } |
52 | } | 52 | } |
53 | return _server; | 53 | return _server; |
54 | } | 54 | } |
55 | 55 | ||
56 | - (instancetype)init | 56 | - (instancetype)init |
57 | { | 57 | { |
58 | self = [super init]; | 58 | self = [super init]; |
59 | if (self != nil) { | 59 | if (self != nil) { |
60 | self.timeOutInterval = 60; | 60 | self.timeOutInterval = 60; |
61 | } | 61 | } |
62 | return self; | 62 | return self; |
63 | } | 63 | } |
64 | 64 | ||
65 | #pragma mark - Login and Register | 65 | #pragma mark - Login and Register |
66 | // Login | 66 | // Login |
67 | - (void)loginWithEmail:(NSString *)email Password:(NSString *)password CompletionHandler: (void (^)(User *, NSString *, NSError *)) completion | 67 | - (void)loginWithEmail:(NSString *)email Password:(NSString *)password CompletionHandler: (void (^)(User *, NSString *, NSError *)) completion |
68 | { | 68 | { |
69 | [self _request:[kServerAddress stringByAppendingFormat: @"login"] method:@"POST" token:@"" paras:@{@"email":email, @"password": password} completion:^(NSData *data, NSError *error) { | 69 | [self _request:[kServerAddress stringByAppendingFormat: @"login"] method:@"POST" token:@"" paras:@{@"email":email, @"password": password} completion:^(NSData *data, NSError *error) { |
70 | 70 | ||
71 | if (completion == NULL) { | 71 | if (completion == NULL) { |
72 | return ; | 72 | return ; |
73 | } | 73 | } |
74 | 74 | ||
75 | if (error == nil) | 75 | if (error == nil) |
76 | { | 76 | { |
77 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 77 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
78 | 78 | ||
79 | int status = [dataResult[@"status"] intValue]; | 79 | int status = [dataResult[@"status"] intValue]; |
80 | if (status == 1) { // status = 1 success | 80 | if (status == 1) { // status = 1 success |
81 | NSString *token = dataResult[@"result"][@"token"]; | 81 | NSString *token = dataResult[@"result"][@"token"]; |
82 | NSDictionary *dictUser = dataResult[@"result"][@"user"]; | 82 | NSDictionary *dictUser = dataResult[@"result"][@"user"]; |
83 | User *user = [[User alloc] init]; | 83 | User *user = [[User alloc] init]; |
84 | user.user_id = [NSString stringWithFormat:@"%@",dictUser[@"id"]]; | 84 | user.user_id = [NSString stringWithFormat:@"%@",dictUser[@"id"]]; |
85 | user.username = [NSString stringWithFormat:@"%@",dictUser[@"username"]]; | 85 | user.username = [NSString stringWithFormat:@"%@",dictUser[@"username"]]; |
86 | user.full_name = [NSString stringWithFormat:@"%@",dictUser[@"full_name"]]; | 86 | user.full_name = [NSString stringWithFormat:@"%@",dictUser[@"full_name"]]; |
87 | user.nickname = [NSString stringWithFormat:@"%@",dictUser[@"nickname"]]; | 87 | user.nickname = [NSString stringWithFormat:@"%@",dictUser[@"nickname"]]; |
88 | user.email = [NSString stringWithFormat:@"%@",dictUser[@"email"]]; | 88 | user.email = [NSString stringWithFormat:@"%@",dictUser[@"email"]]; |
89 | user.password = [NSString stringWithFormat:@"%@",dictUser[@"password"]]; | 89 | user.password = [NSString stringWithFormat:@"%@",dictUser[@"password"]]; |
90 | user.birthday = [NSString stringWithFormat:@"%@",dictUser[@"birthday"]]; | 90 | user.birthday = [NSString stringWithFormat:@"%@",dictUser[@"birthday"]]; |
91 | user.address = [NSString stringWithFormat:@"%@",dictUser[@"address"]]; | 91 | user.address = [NSString stringWithFormat:@"%@",dictUser[@"address"]]; |
92 | user.gender = [[NSString stringWithFormat:@"%@",dictUser[@"gender"]] intValue]; | 92 | user.gender = [[NSString stringWithFormat:@"%@",dictUser[@"gender"]] intValue]; |
93 | user.height = [[NSString stringWithFormat:@"%@",dictUser[@"height"]] floatValue]; | 93 | user.height = [[NSString stringWithFormat:@"%@",dictUser[@"height"]] floatValue]; |
94 | user.weight = [[NSString stringWithFormat:@"%@",dictUser[@"weight"]] floatValue]; | 94 | user.weight = [[NSString stringWithFormat:@"%@",dictUser[@"weight"]] floatValue]; |
95 | user.user_description = [NSString stringWithFormat:@"%@",dictUser[@"description"]]; | 95 | user.user_description = [NSString stringWithFormat:@"%@",dictUser[@"description"]]; |
96 | user.created_at = [NSString stringWithFormat:@"%@",dictUser[@"created_at"]]; | 96 | user.created_at = [NSString stringWithFormat:@"%@",dictUser[@"created_at"]]; |
97 | user.physical_activity = [NSString stringWithFormat:@"%@",dictUser[@"physical_activity"]]; | 97 | user.physical_activity = [NSString stringWithFormat:@"%@",dictUser[@"physical_activity"]]; |
98 | user.profile_image = [NSString stringWithFormat:@"%@",dictUser[@"profile_image"]]; | 98 | user.profile_image = [NSString stringWithFormat:@"%@",dictUser[@"profile_image"]]; |
99 | user.updated_at = [NSString stringWithFormat:@"%@",dictUser[@"updated_at"]]; | 99 | user.updated_at = [NSString stringWithFormat:@"%@",dictUser[@"updated_at"]]; |
100 | user.delete_flag = [[NSString stringWithFormat:@"%@",dictUser[@"delete_flag"]] intValue]; | 100 | user.delete_flag = [[NSString stringWithFormat:@"%@",dictUser[@"delete_flag"]] intValue]; |
101 | user.fat_rate = [[NSString stringWithFormat:@"%@",dictUser[@"fat_rate"]] intValue]; | 101 | user.fat_rate = [[NSString stringWithFormat:@"%@",dictUser[@"fat_rate"]] intValue]; |
102 | user.profiles_share = [[NSString stringWithFormat:@"%@",dictUser[@"profiles_share"]] intValue]; | 102 | user.profiles_share = [[NSString stringWithFormat:@"%@",dictUser[@"profiles_share"]] intValue]; |
103 | user.remember_me = [[NSString stringWithFormat:@"%@",dictUser[@"remember_me"]] intValue]; | 103 | user.remember_me = [[NSString stringWithFormat:@"%@",dictUser[@"remember_me"]] intValue]; |
104 | user.sound_notifications_share = [[NSString stringWithFormat:@"%@",dictUser[@"sound_notifications_share"]] intValue]; | 104 | user.sound_notifications_share = [[NSString stringWithFormat:@"%@",dictUser[@"sound_notifications_share"]] intValue]; |
105 | user.spend_calo_in_day = [[NSString stringWithFormat:@"%@",dictUser[@"spend_calo_in_day"]] intValue]; | 105 | user.spend_calo_in_day = [[NSString stringWithFormat:@"%@",dictUser[@"spend_calo_in_day"]] intValue]; |
106 | user.target = [[NSString stringWithFormat:@"%@",dictUser[@"target"]] intValue]; | 106 | user.target = [[NSString stringWithFormat:@"%@",dictUser[@"target"]] intValue]; |
107 | completion(user, token, nil); | 107 | completion(user, token, nil); |
108 | } | 108 | } |
109 | else { // status = 0 error | 109 | else { // status = 0 error |
110 | NSString *message = dataResult[@"message"]; | 110 | NSString *message = dataResult[@"message"]; |
111 | if (message == nil) { | 111 | if (message == nil) { |
112 | message = @"Unknown error"; | 112 | message = @"Unknown error"; |
113 | } | 113 | } |
114 | NSError *loginFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 114 | NSError *loginFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
115 | completion(nil, nil, loginFaild); | 115 | completion(nil, nil, loginFaild); |
116 | } | 116 | } |
117 | } | 117 | } |
118 | else | 118 | else |
119 | { | 119 | { |
120 | completion(nil, nil, error); | 120 | completion(nil, nil, error); |
121 | } | 121 | } |
122 | }]; | 122 | }]; |
123 | } | 123 | } |
124 | 124 | ||
125 | // Register | 125 | // Register |
126 | - (void)registerUserWithParams:(NSDictionary *)params CompletionHandler: (void (^)(User *, NSString *, NSError *)) completion { | 126 | - (void)registerUserWithParams:(NSDictionary *)params CompletionHandler: (void (^)(User *, NSString *, NSError *)) completion { |
127 | [self _request:[kServerAddress stringByAppendingFormat: @"register"] method:@"POST" token:@"" paras:params completion:^(NSData *data, NSError *error) { | 127 | [self _request:[kServerAddress stringByAppendingFormat: @"register"] method:@"POST" token:@"" paras:params completion:^(NSData *data, NSError *error) { |
128 | 128 | ||
129 | if (completion == NULL) { | 129 | if (completion == NULL) { |
130 | return ; | 130 | return ; |
131 | } | 131 | } |
132 | 132 | ||
133 | if (error == nil) | 133 | if (error == nil) |
134 | { | 134 | { |
135 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 135 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
136 | 136 | ||
137 | int status = [dataResult[@"status"] intValue]; | 137 | int status = [dataResult[@"status"] intValue]; |
138 | if (status == 1) { // status = 1 success | 138 | if (status == 1) { // status = 1 success |
139 | NSString *token = dataResult[@"result"][@"token"]; | 139 | NSString *token = dataResult[@"result"][@"token"]; |
140 | NSDictionary *dictUser = dataResult[@"result"][@"user"]; | 140 | NSDictionary *dictUser = dataResult[@"result"][@"user"]; |
141 | User *user = [[User alloc] init]; | 141 | User *user = [[User alloc] init]; |
142 | user.user_id = [NSString stringWithFormat:@"%@",dictUser[@"id"]]; | 142 | user.user_id = [NSString stringWithFormat:@"%@",dictUser[@"id"]]; |
143 | user.username = [NSString stringWithFormat:@"%@",dictUser[@"username"]]; | 143 | user.username = [NSString stringWithFormat:@"%@",dictUser[@"username"]]; |
144 | user.full_name = [NSString stringWithFormat:@"%@",dictUser[@"full_name"]]; | 144 | user.full_name = [NSString stringWithFormat:@"%@",dictUser[@"full_name"]]; |
145 | user.nickname = [NSString stringWithFormat:@"%@",dictUser[@"nickname"]]; | 145 | user.nickname = [NSString stringWithFormat:@"%@",dictUser[@"nickname"]]; |
146 | user.email = [NSString stringWithFormat:@"%@",dictUser[@"email"]]; | 146 | user.email = [NSString stringWithFormat:@"%@",dictUser[@"email"]]; |
147 | user.password = [NSString stringWithFormat:@"%@",dictUser[@"password"]]; | 147 | user.password = [NSString stringWithFormat:@"%@",dictUser[@"password"]]; |
148 | user.birthday = [NSString stringWithFormat:@"%@",dictUser[@"birthday"]]; | 148 | user.birthday = [NSString stringWithFormat:@"%@",dictUser[@"birthday"]]; |
149 | user.address = [NSString stringWithFormat:@"%@",dictUser[@"address"]]; | 149 | user.address = [NSString stringWithFormat:@"%@",dictUser[@"address"]]; |
150 | user.gender = [[NSString stringWithFormat:@"%@",dictUser[@"gender"]] intValue]; | 150 | user.gender = [[NSString stringWithFormat:@"%@",dictUser[@"gender"]] intValue]; |
151 | user.height = [[NSString stringWithFormat:@"%@",dictUser[@"height"]] floatValue]; | 151 | user.height = [[NSString stringWithFormat:@"%@",dictUser[@"height"]] floatValue]; |
152 | user.weight = [[NSString stringWithFormat:@"%@",dictUser[@"weight"]] floatValue]; | 152 | user.weight = [[NSString stringWithFormat:@"%@",dictUser[@"weight"]] floatValue]; |
153 | user.user_description = [NSString stringWithFormat:@"%@",dictUser[@"description"]]; | 153 | user.user_description = [NSString stringWithFormat:@"%@",dictUser[@"description"]]; |
154 | user.created_at = [NSString stringWithFormat:@"%@",dictUser[@"created_at"]]; | 154 | user.created_at = [NSString stringWithFormat:@"%@",dictUser[@"created_at"]]; |
155 | user.physical_activity = [NSString stringWithFormat:@"%@",dictUser[@"physical_activity"]]; | 155 | user.physical_activity = [NSString stringWithFormat:@"%@",dictUser[@"physical_activity"]]; |
156 | user.profile_image = [NSString stringWithFormat:@"%@",dictUser[@"profile_image"]]; | 156 | user.profile_image = [NSString stringWithFormat:@"%@",dictUser[@"profile_image"]]; |
157 | user.updated_at = [NSString stringWithFormat:@"%@",dictUser[@"updated_at"]]; | 157 | user.updated_at = [NSString stringWithFormat:@"%@",dictUser[@"updated_at"]]; |
158 | user.delete_flag = [[NSString stringWithFormat:@"%@",dictUser[@"delete_flag"]] intValue]; | 158 | user.delete_flag = [[NSString stringWithFormat:@"%@",dictUser[@"delete_flag"]] intValue]; |
159 | user.fat_rate = [[NSString stringWithFormat:@"%@",dictUser[@"fat_rate"]] intValue]; | 159 | user.fat_rate = [[NSString stringWithFormat:@"%@",dictUser[@"fat_rate"]] intValue]; |
160 | user.profiles_share = [[NSString stringWithFormat:@"%@",dictUser[@"profiles_share"]] intValue]; | 160 | user.profiles_share = [[NSString stringWithFormat:@"%@",dictUser[@"profiles_share"]] intValue]; |
161 | user.remember_me = [[NSString stringWithFormat:@"%@",dictUser[@"remember_me"]] intValue]; | 161 | user.remember_me = [[NSString stringWithFormat:@"%@",dictUser[@"remember_me"]] intValue]; |
162 | user.sound_notifications_share = [[NSString stringWithFormat:@"%@",dictUser[@"sound_notifications_share"]] intValue]; | 162 | user.sound_notifications_share = [[NSString stringWithFormat:@"%@",dictUser[@"sound_notifications_share"]] intValue]; |
163 | user.spend_calo_in_day = [[NSString stringWithFormat:@"%@",dictUser[@"spend_calo_in_day"]] intValue]; | 163 | user.spend_calo_in_day = [[NSString stringWithFormat:@"%@",dictUser[@"spend_calo_in_day"]] intValue]; |
164 | user.target = [[NSString stringWithFormat:@"%@",dictUser[@"target"]] intValue]; | 164 | user.target = [[NSString stringWithFormat:@"%@",dictUser[@"target"]] intValue]; |
165 | completion(user, token, nil); | 165 | completion(user, token, nil); |
166 | } | 166 | } |
167 | else { // status = 0 error | 167 | else { // status = 0 error |
168 | NSString *message = dataResult[@"message"]; | 168 | NSString *message = dataResult[@"message"]; |
169 | if (message == nil) { | 169 | if (message == nil) { |
170 | message = @"Unknown error"; | 170 | message = @"Unknown error"; |
171 | } | 171 | } |
172 | NSError *loginFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 172 | NSError *loginFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
173 | completion(nil, nil, loginFaild); | 173 | completion(nil, nil, loginFaild); |
174 | } | 174 | } |
175 | } | 175 | } |
176 | else | 176 | else |
177 | { | 177 | { |
178 | completion(nil, nil, error); | 178 | completion(nil, nil, error); |
179 | } | 179 | } |
180 | }]; | 180 | }]; |
181 | } | 181 | } |
182 | 182 | ||
183 | - (void)forgetPass:(NSString *)email CompletionHandler:(void (^)(NSError *)) completion { | 183 | - (void)forgetPass:(NSString *)email CompletionHandler:(void (^)(NSError *)) completion { |
184 | [self _request:[kServerAddress stringByAppendingFormat: @"forgetPass"] method:@"POST" token:@"" paras:@{@"email":email} completion:^(NSData *data, NSError *error) { | 184 | [self _request:[kServerAddress stringByAppendingFormat: @"forgetPass"] method:@"POST" token:@"" paras:@{@"email":email} completion:^(NSData *data, NSError *error) { |
185 | 185 | ||
186 | if (completion == NULL) { | 186 | if (completion == NULL) { |
187 | return ; | 187 | return ; |
188 | } | 188 | } |
189 | 189 | ||
190 | if (error == nil) | 190 | if (error == nil) |
191 | { | 191 | { |
192 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 192 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
193 | 193 | ||
194 | int status = [dataResult[@"status"] intValue]; | 194 | int status = [dataResult[@"status"] intValue]; |
195 | if (status == 1) { // status = 1 success | 195 | if (status == 1) { // status = 1 success |
196 | completion(nil); | 196 | completion(nil); |
197 | } | 197 | } |
198 | else { // status = 0 error | 198 | else { // status = 0 error |
199 | NSString *message = dataResult[@"message"]; | 199 | NSString *message = dataResult[@"message"]; |
200 | if (message == nil) { | 200 | if (message == nil) { |
201 | message = @"Unknown error"; | 201 | message = @"Unknown error"; |
202 | } | 202 | } |
203 | NSError *forgetPass = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 203 | NSError *forgetPass = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
204 | completion(forgetPass); | 204 | completion(forgetPass); |
205 | } | 205 | } |
206 | } | 206 | } |
207 | else | 207 | else |
208 | { | 208 | { |
209 | completion(error); | 209 | completion(error); |
210 | } | 210 | } |
211 | }]; | 211 | }]; |
212 | } | 212 | } |
213 | - (void)confirmForgetPass:(NSString *)email withConfirm:(NSString *)confirm CompletionHandler:(void (^)(NSError *)) completion { | 213 | - (void)confirmForgetPass:(NSString *)email withConfirm:(NSString *)confirm CompletionHandler:(void (^)(NSError *)) completion { |
214 | [self _request:[kServerAddress stringByAppendingFormat: @"forgetPass/confirm"] method:@"POST" token:@"" paras:@{@"email":email, @"code_confirm": confirm} completion:^(NSData *data, NSError *error) { | 214 | [self _request:[kServerAddress stringByAppendingFormat: @"forgetPass/confirm"] method:@"POST" token:@"" paras:@{@"email":email, @"code_confirm": confirm} completion:^(NSData *data, NSError *error) { |
215 | 215 | ||
216 | if (completion == NULL) { | 216 | if (completion == NULL) { |
217 | return ; | 217 | return ; |
218 | } | 218 | } |
219 | 219 | ||
220 | if (error == nil) | 220 | if (error == nil) |
221 | { | 221 | { |
222 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 222 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
223 | 223 | ||
224 | int status = [dataResult[@"status"] intValue]; | 224 | int status = [dataResult[@"status"] intValue]; |
225 | if (status == 1) { // status = 1 success | 225 | if (status == 1) { // status = 1 success |
226 | completion(nil); | 226 | completion(nil); |
227 | } | 227 | } |
228 | else { // status = 0 error | 228 | else { // status = 0 error |
229 | NSString *message = dataResult[@"message"]; | 229 | NSString *message = dataResult[@"message"]; |
230 | NSError *confirmForgetPass = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 230 | NSError *confirmForgetPass = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
231 | completion(confirmForgetPass); | 231 | completion(confirmForgetPass); |
232 | } | 232 | } |
233 | } | 233 | } |
234 | else | 234 | else |
235 | { | 235 | { |
236 | completion(error); | 236 | completion(error); |
237 | } | 237 | } |
238 | }]; | 238 | }]; |
239 | } | 239 | } |
240 | 240 | ||
241 | - (void)uploadImage:(NSString *)token andImageData:(NSData *)data CompletionHandler:(void (^)(NSString *, NSError *)) completion { | 241 | - (void)uploadImage:(NSString *)token andImageData:(NSData *)data CompletionHandler:(void (^)(NSString *, NSError *)) completion { |
242 | NSDictionary *dict = nil; | 242 | NSDictionary *dict = nil; |
243 | NSString *base64Encoded = [data base64EncodedStringWithOptions:0]; | 243 | NSString *base64Encoded = [data base64EncodedStringWithOptions:0]; |
244 | if (token != nil) { | 244 | if (token != nil) { |
245 | dict = @{@"token":token, @"img": base64Encoded}; | 245 | dict = @{@"token":token, @"img": base64Encoded}; |
246 | } | 246 | } |
247 | else { | 247 | else { |
248 | dict = @{@"img": base64Encoded}; | 248 | dict = @{@"img": base64Encoded}; |
249 | } | 249 | } |
250 | [self _request:[kServerAddress stringByAppendingFormat: @"upload-image"] method:@"POST" token:token paras:dict completion:^(NSData *data, NSError *error) { | 250 | [self _request:[kServerAddress stringByAppendingFormat: @"upload-image"] method:@"POST" token:token paras:dict completion:^(NSData *data, NSError *error) { |
251 | 251 | ||
252 | if (completion == NULL) { | 252 | if (completion == NULL) { |
253 | return ; | 253 | return ; |
254 | } | 254 | } |
255 | 255 | ||
256 | if (error == nil) | 256 | if (error == nil) |
257 | { | 257 | { |
258 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 258 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
259 | NSString *image_profile = [NSString stringWithFormat:@"%@", dataResult[@"message"]]; | 259 | NSString *image_profile = [NSString stringWithFormat:@"%@", dataResult[@"message"]]; |
260 | completion(image_profile, nil); | 260 | completion(image_profile, nil); |
261 | } | 261 | } |
262 | else | 262 | else |
263 | { | 263 | { |
264 | completion(nil, error); | 264 | completion(nil, error); |
265 | } | 265 | } |
266 | }]; | 266 | }]; |
267 | } | 267 | } |
268 | 268 | ||
269 | -(NSString *) convertIntToString : (int) type { | 269 | -(NSString *) convertIntToString : (int) type { |
270 | switch (type) { | 270 | switch (type) { |
271 | case 1: | 271 | case 1: |
272 | return @"week"; | 272 | return @"week"; |
273 | break; | 273 | break; |
274 | case 2: | 274 | case 2: |
275 | return @"oneMonth"; | 275 | return @"oneMonth"; |
276 | break; | 276 | break; |
277 | case 3: | 277 | case 3: |
278 | return @"threeMonth"; | 278 | return @"threeMonth"; |
279 | break; | 279 | break; |
280 | case 4: | 280 | case 4: |
281 | return @"sixMonth"; | 281 | return @"sixMonth"; |
282 | break; | 282 | break; |
283 | default: | 283 | default: |
284 | return @"today"; | 284 | return @"today"; |
285 | break; | 285 | break; |
286 | } | 286 | } |
287 | } | 287 | } |
288 | 288 | ||
289 | #pragma mark - Home Screen Function | 289 | #pragma mark - Home Screen Function |
290 | - (void)requestTopWithMode:(int)mode andDate:(NSString *)date CompletionHandler:(void (^)(TopObject *, NSError *)) completion | 290 | - (void)requestTopWithMode:(int)mode andDate:(NSString *)date CompletionHandler:(void (^)(TopObject *, NSError *)) completion |
291 | { | 291 | { |
292 | NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken]; | 292 | NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken]; |
293 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/top/%d/%@", mode, date]; | 293 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/top/%d/%@", mode, date]; |
294 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { | 294 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { |
295 | 295 | ||
296 | if (completion == NULL) { | 296 | if (completion == NULL) { |
297 | return ; | 297 | return ; |
298 | } | 298 | } |
299 | 299 | ||
300 | if (error == nil) | 300 | if (error == nil) |
301 | { | 301 | { |
302 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 302 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
303 | int status = [dataResult[@"status"] intValue]; | 303 | int status = [dataResult[@"status"] intValue]; |
304 | if (status == 1) { // status = 1 success | 304 | if (status == 1) { // status = 1 success |
305 | if(dataResult[@"result"] != nil) { | 305 | if(dataResult[@"result"] != nil) { |
306 | NSDictionary * dictResult = dataResult[@"result"]; | 306 | NSDictionary * dictResult = dataResult[@"result"]; |
307 | TopObject *topObject = [[TopObject alloc] init]; | 307 | TopObject *topObject = [[TopObject alloc] init]; |
308 | TargetInfor *targetInfor = [[TargetInfor alloc] initWithData:dictResult[@"targetInf"]]; | 308 | TargetInfor *targetInfor = [[TargetInfor alloc] initWithData:dictResult[@"targetInf"]]; |
309 | topObject.targetInfor = targetInfor; | 309 | topObject.targetInfor = targetInfor; |
310 | topObject.kcal = [[NSString stringWithFormat:@"%@", dictResult[@"kcal"]] intValue]; | 310 | topObject.kcal = [[NSString stringWithFormat:@"%@", dictResult[@"kcal"]] intValue]; |
311 | topObject.distance = [[NSString stringWithFormat:@"%@", dictResult[@"distance"]] floatValue]; | 311 | topObject.distance = [[NSString stringWithFormat:@"%@", dictResult[@"distance"]] floatValue]; |
312 | topObject.time = [NSString stringWithFormat:@"%@", dictResult[@"time"]]; | 312 | topObject.time = [NSString stringWithFormat:@"%@", dictResult[@"time"]]; |
313 | NSMutableArray *arrayStep = [[NSMutableArray alloc] init]; | 313 | NSMutableArray *arrayStep = [[NSMutableArray alloc] init]; |
314 | NSArray *array = dictResult[@"dataChart"]; | 314 | NSArray *array = dictResult[@"dataChart"]; |
315 | for(NSDictionary *dict in array) { | 315 | for(NSDictionary *dict in array) { |
316 | StepObject *object = [[StepObject alloc] init]; | 316 | StepObject *object = [[StepObject alloc] init]; |
317 | if([dict objectForKey:@"numStep"] != nil) { | 317 | if([dict objectForKey:@"numStep"] != nil) { |
318 | object.step = [dict[@"numStep"] intValue]; | 318 | object.step = [dict[@"numStep"] intValue]; |
319 | } | 319 | } |
320 | if([dict objectForKey:@"hour"] != nil) { | 320 | if([dict objectForKey:@"hour"] != nil) { |
321 | object.hour = [dict[@"hour"] intValue]; | 321 | object.hour = [dict[@"hour"] intValue]; |
322 | } | 322 | } |
323 | switch (mode) { | 323 | switch (mode) { |
324 | case 1: | 324 | case 1: |
325 | object.typeStep = @"walking"; | 325 | object.typeStep = @"walking"; |
326 | break; | 326 | break; |
327 | case 2: | 327 | case 2: |
328 | object.typeStep = @"running"; | 328 | object.typeStep = @"running"; |
329 | break; | 329 | break; |
330 | case 3: | 330 | case 3: |
331 | object.typeStep = @"bike"; | 331 | object.typeStep = @"bike"; |
332 | break; | 332 | break; |
333 | default: | 333 | default: |
334 | break; | 334 | break; |
335 | } | 335 | } |
336 | [arrayStep addObject:object]; | 336 | [arrayStep addObject:object]; |
337 | } | 337 | } |
338 | topObject.dataChart = [[NSMutableArray alloc] initWithArray:arrayStep]; | 338 | topObject.dataChart = [[NSMutableArray alloc] initWithArray:arrayStep]; |
339 | completion(topObject, nil); | 339 | completion(topObject, nil); |
340 | } | 340 | } |
341 | else { | 341 | else { |
342 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; | 342 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; |
343 | completion(nil, errorObject); | 343 | completion(nil, errorObject); |
344 | } | 344 | } |
345 | } | 345 | } |
346 | else { | 346 | else { |
347 | NSString *message = dataResult[@"message"]; | 347 | NSString *message = dataResult[@"message"]; |
348 | if (message == nil) { | 348 | if (message == nil) { |
349 | message = @"Unknown error"; | 349 | message = @"Unknown error"; |
350 | } | 350 | } |
351 | 351 | ||
352 | if ([message isEqualToString:@"Token is invalid"]) { | 352 | if ([message isEqualToString:@"Token is invalid"]) { |
353 | [self performSelectorOnMainThread:@selector(checkToken) withObject:nil waitUntilDone:YES]; | 353 | [self checkTokenCompletionHandler:^(NSString *token, NSError *error) { |
354 | [self requestTopWithMode:mode andDate:date CompletionHandler:completion]; | 354 | if (error == nil) { |
355 | [self requestTopWithMode:mode andDate:date CompletionHandler:completion]; | ||
356 | } | ||
357 | else { | ||
358 | completion(nil, error); | ||
359 | } | ||
360 | }]; | ||
355 | } | 361 | } |
356 | else { | 362 | else { |
357 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 363 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
358 | completion(nil, errorObject); | 364 | completion(nil, errorObject); |
359 | } | 365 | } |
360 | } | 366 | } |
361 | } | 367 | } |
362 | else | 368 | else |
363 | { | 369 | { |
364 | completion(nil, error); | 370 | completion(nil, error); |
365 | } | 371 | } |
366 | }]; | 372 | }]; |
367 | } | 373 | } |
368 | 374 | ||
369 | - (void)requestHomeWithMode:(int)mode andDate:(NSString *)date CompletionHandler:(void (^)(HomeObject *, NSError *)) completion | 375 | - (void)requestHomeWithMode:(int)mode andDate:(NSString *)date CompletionHandler:(void (^)(HomeObject *, NSError *)) completion |
370 | { | 376 | { |
371 | NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken]; | 377 | NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken]; |
372 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/home/%d/%@", mode, date]; | 378 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/home/%d/%@", mode, date]; |
373 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { | 379 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { |
374 | 380 | ||
375 | if (completion == NULL) { | 381 | if (completion == NULL) { |
376 | return ; | 382 | return ; |
377 | } | 383 | } |
378 | 384 | ||
379 | if (error == nil) | 385 | if (error == nil) |
380 | { | 386 | { |
381 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 387 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
382 | int status = [dataResult[@"status"] intValue]; | 388 | int status = [dataResult[@"status"] intValue]; |
383 | if (status == 1) { // status = 1 success | 389 | if (status == 1) { // status = 1 success |
384 | if(dataResult[@"result"] != nil) { | 390 | if(dataResult[@"result"] != nil) { |
385 | NSDictionary * dictResult = dataResult[@"result"]; | 391 | NSDictionary * dictResult = dataResult[@"result"]; |
386 | HomeObject *homeObject = [[HomeObject alloc] init]; | 392 | HomeObject *homeObject = [[HomeObject alloc] init]; |
387 | TargetInfor *targetInfor = [[TargetInfor alloc] initWithData:dictResult[@"targetInf"]]; | 393 | TargetInfor *targetInfor = [[TargetInfor alloc] initWithData:dictResult[@"targetInf"]]; |
388 | homeObject.targetInfor = targetInfor; | 394 | homeObject.targetInfor = targetInfor; |
389 | NSMutableArray *arrayNotice = [[NSMutableArray alloc] init]; | 395 | NSMutableArray *arrayNotice = [[NSMutableArray alloc] init]; |
390 | NSArray *array = dictResult[@"listNotice"]; | 396 | NSArray *array = dictResult[@"listNotice"]; |
391 | for(NSDictionary *dict in array) { | 397 | for(NSDictionary *dict in array) { |
392 | NoticeInfor *object = [[NoticeInfor alloc] initWithData:dict]; | 398 | NoticeInfor *object = [[NoticeInfor alloc] initWithData:dict]; |
393 | [arrayNotice addObject:object]; | 399 | [arrayNotice addObject:object]; |
394 | } | 400 | } |
395 | homeObject.listNotice = [[NSMutableArray alloc] initWithArray:arrayNotice]; | 401 | homeObject.listNotice = [[NSMutableArray alloc] initWithArray:arrayNotice]; |
396 | completion(homeObject, nil); | 402 | completion(homeObject, nil); |
397 | } | 403 | } |
398 | else { | 404 | else { |
399 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; | 405 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; |
400 | completion(nil, errorObject); | 406 | completion(nil, errorObject); |
401 | } | 407 | } |
402 | } | 408 | } |
403 | else { | 409 | else { |
404 | NSString *message = dataResult[@"message"]; | 410 | NSString *message = dataResult[@"message"]; |
405 | if (message == nil) { | 411 | if (message == nil) { |
406 | message = @"Unknown error"; | 412 | message = @"Unknown error"; |
407 | } | 413 | } |
408 | 414 | ||
409 | if ([message isEqualToString:@"Token is invalid"]) { | 415 | if ([message isEqualToString:@"Token is invalid"]) { |
410 | [self performSelectorOnMainThread:@selector(checkToken) withObject:nil waitUntilDone:YES]; | 416 | [self checkTokenCompletionHandler:^(NSString *token, NSError *error) { |
411 | [self requestHomeWithMode:mode andDate:date CompletionHandler:completion]; | 417 | if (error == nil) { |
418 | [self requestHomeWithMode:mode andDate:date CompletionHandler:completion]; | ||
419 | } | ||
420 | else { | ||
421 | completion(nil, error); | ||
422 | } | ||
423 | }]; | ||
412 | } | 424 | } |
413 | else { | 425 | else { |
414 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 426 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
415 | completion(nil, errorObject); | 427 | completion(nil, errorObject); |
416 | } | 428 | } |
417 | } | 429 | } |
418 | } | 430 | } |
419 | else | 431 | else |
420 | { | 432 | { |
421 | completion(nil, error); | 433 | completion(nil, error); |
422 | } | 434 | } |
423 | }]; | 435 | }]; |
424 | } | 436 | } |
425 | 437 | ||
426 | - (void)requestCreateLog:(int)mode withStep:(int)numStep startDate:(NSString *)startDate endDate:(NSString *)endDate CompletionHandler:(void (^)(NSError *))completion { | 438 | - (void)requestCreateLog:(int)mode withStep:(int)numStep startDate:(NSString *)startDate endDate:(NSString *)endDate CompletionHandler:(void (^)(NSError *))completion { |
427 | NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken]; | 439 | NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken]; |
428 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/createLog"]; | 440 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/createLog"]; |
429 | NSDictionary *dict = @{@"mode": [NSNumber numberWithInt:mode], @"numStep": [NSNumber numberWithInt:numStep], @"startTime": startDate, @"endTime": endDate}; | 441 | NSDictionary *dict = @{@"mode": [NSNumber numberWithInt:mode], @"numStep": [NSNumber numberWithInt:numStep], @"startTime": startDate, @"endTime": endDate}; |
430 | [self _request:url method:@"POST" token:token paras:dict completion:^(NSData *data, NSError *error) { | 442 | [self _request:url method:@"POST" token:token paras:dict completion:^(NSData *data, NSError *error) { |
431 | 443 | ||
432 | if (completion == NULL) { | 444 | if (completion == NULL) { |
433 | return ; | 445 | return ; |
434 | } | 446 | } |
435 | 447 | ||
436 | if (error == nil) | 448 | if (error == nil) |
437 | { | 449 | { |
438 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 450 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
439 | int status = [dataResult[@"status"] intValue]; | 451 | int status = [dataResult[@"status"] intValue]; |
440 | if (status == 1) { // status = 1 success | 452 | if (status == 1) { // status = 1 success |
441 | completion(nil); | 453 | completion(nil); |
442 | } | 454 | } |
443 | else { | 455 | else { |
444 | NSString *message = dataResult[@"message"]; | 456 | NSString *message = dataResult[@"message"]; |
445 | if (message == nil) { | 457 | if (message == nil) { |
446 | message = @"Unknown error"; | 458 | message = @"Unknown error"; |
447 | } | 459 | } |
448 | 460 | ||
449 | if ([message isEqualToString:@"Token is invalid"]) { | 461 | if ([message isEqualToString:@"Token is invalid"]) { |
450 | [self performSelectorOnMainThread:@selector(checkToken) withObject:nil waitUntilDone:YES]; | 462 | [self checkTokenCompletionHandler:^(NSString *token, NSError *error) { |
451 | [self requestCreateLog:mode withStep:numStep startDate:startDate endDate:endDate CompletionHandler:completion]; | 463 | if (error == nil) { |
464 | [self requestCreateLog:mode withStep:numStep startDate:startDate endDate:endDate CompletionHandler:completion]; | ||
465 | } | ||
466 | else { | ||
467 | completion(error); | ||
468 | } | ||
469 | }]; | ||
452 | } | 470 | } |
453 | else { | 471 | else { |
454 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 472 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
455 | completion(errorObject); | 473 | completion(errorObject); |
456 | } | 474 | } |
457 | } | 475 | } |
458 | } | 476 | } |
459 | else | 477 | else |
460 | { | 478 | { |
461 | completion(error); | 479 | completion(error); |
462 | } | 480 | } |
463 | }]; | 481 | }]; |
464 | } | 482 | } |
465 | 483 | ||
466 | #pragma mark - History Screen Function | 484 | #pragma mark - History Screen Function |
467 | - (void) requestHistory:(NSString *)token startDate:(NSDate *)startDate endDate:(NSDate *)endDate CompletionHandler:(void (^)(NSArray *, NSError *)) completion { | 485 | - (void) requestHistory:(NSString *)token startDate:(NSDate *)startDate endDate:(NSDate *)endDate CompletionHandler:(void (^)(NSArray *, NSError *)) completion { |
468 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/history/%@/%@", [Utilities stringFromDate:startDate withFormat:@"YYYYMMdd" locale:@""], [Utilities stringFromDate:endDate withFormat:@"YYYYMMdd" locale:@""]]; | 486 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/history/%@/%@", [Utilities stringFromDate:startDate withFormat:@"YYYYMMdd" locale:@""], [Utilities stringFromDate:endDate withFormat:@"YYYYMMdd" locale:@""]]; |
469 | NSLog(@"requestHistory link %@", url); | 487 | NSLog(@"requestHistory link %@", url); |
470 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { | 488 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { |
471 | 489 | ||
472 | if (completion == NULL) { | 490 | if (completion == NULL) { |
473 | return ; | 491 | return ; |
474 | } | 492 | } |
475 | 493 | ||
476 | if (error == nil) | 494 | if (error == nil) |
477 | { | 495 | { |
478 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 496 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
479 | NSLog(@"%@", dataResult); | 497 | NSLog(@"%@", dataResult); |
480 | int status = [dataResult[@"status"] intValue]; | 498 | int status = [dataResult[@"status"] intValue]; |
481 | if (status == 1) { // status = 1 success | 499 | if (status == 1) { // status = 1 success |
482 | NSMutableArray * arrayHistory = [[NSMutableArray alloc] init]; | 500 | NSMutableArray * arrayHistory = [[NSMutableArray alloc] init]; |
483 | NSDictionary * arrResult = dataResult[@"result"]; | 501 | NSDictionary * arrResult = dataResult[@"result"]; |
484 | if(arrResult != nil) { | 502 | if(arrResult != nil) { |
485 | /* mode_1: Data for mode walking | 503 | /* mode_1: Data for mode walking |
486 | mode_2: Data for mode running | 504 | mode_2: Data for mode running |
487 | mode_3: Data for mode bike */ | 505 | mode_3: Data for mode bike */ |
488 | NSArray *arrayKey = [NSArray arrayWithObjects:@"mode_3", @"mode_1", @"mode_2", nil]; | 506 | NSArray *arrayKey = [NSArray arrayWithObjects:@"mode_3", @"mode_1", @"mode_2", nil]; |
489 | for(NSString * key in arrayKey) { | 507 | for(NSString * key in arrayKey) { |
490 | NSDictionary *mode = [arrResult objectForKey:key]; | 508 | NSDictionary *mode = [arrResult objectForKey:key]; |
491 | if(![[arrResult objectForKey:key] isKindOfClass:[NSNull class]]) { | 509 | if(![[arrResult objectForKey:key] isKindOfClass:[NSNull class]]) { |
492 | HistoryObject * objectMode = [[HistoryObject alloc] initWithData:mode]; | 510 | HistoryObject * objectMode = [[HistoryObject alloc] initWithData:mode]; |
493 | [arrayHistory addObject:objectMode]; | 511 | [arrayHistory addObject:objectMode]; |
494 | } | 512 | } |
495 | else { | 513 | else { |
496 | [arrayHistory addObject:[[HistoryObject alloc] init]]; | 514 | [arrayHistory addObject:[[HistoryObject alloc] init]]; |
497 | } | 515 | } |
498 | } | 516 | } |
499 | } | 517 | } |
500 | completion(arrayHistory, nil); | 518 | completion(arrayHistory, nil); |
501 | } | 519 | } |
502 | else { | 520 | else { |
503 | NSString *message = dataResult[@"message"]; | 521 | NSString *message = dataResult[@"message"]; |
504 | if (message == nil) { | 522 | if (message == nil) { |
505 | message = @"Unknown error"; | 523 | message = @"Unknown error"; |
506 | } | 524 | } |
507 | 525 | ||
508 | if ([message isEqualToString:@"Token is invalid"]) { | 526 | if ([message isEqualToString:@"Token is invalid"]) { |
509 | [self performSelectorOnMainThread:@selector(checkToken) withObject:nil waitUntilDone:YES]; | 527 | [self checkTokenCompletionHandler:^(NSString *token, NSError *error) { |
510 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; | 528 | if (error == nil) { |
511 | [self requestHistory:tokenNew startDate:startDate endDate:endDate CompletionHandler:completion]; | 529 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; |
530 | [self requestHistory:tokenNew startDate:startDate endDate:endDate CompletionHandler:completion]; | ||
531 | } | ||
532 | else { | ||
533 | completion(nil, error); | ||
534 | } | ||
535 | }]; | ||
512 | } | 536 | } |
513 | else { | 537 | else { |
514 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 538 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
515 | completion(nil, errorObject); | 539 | completion(nil, errorObject); |
516 | } | 540 | } |
517 | } | 541 | } |
518 | } | 542 | } |
519 | else | 543 | else |
520 | { | 544 | { |
521 | completion(nil, error); | 545 | completion(nil, error); |
522 | } | 546 | } |
523 | }]; | 547 | }]; |
524 | } | 548 | } |
525 | 549 | ||
526 | - (void) requestHistoryList:(NSString *)token startDate:(NSDate *)startDate endDate:(NSDate *)endDate CompletionHandler:(void (^)(NSArray *, NSError *)) completion { | 550 | - (void) requestHistoryList:(NSString *)token startDate:(NSDate *)startDate endDate:(NSDate *)endDate CompletionHandler:(void (^)(NSArray *, NSError *)) completion { |
527 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/history/detail/%@/%@", [Utilities stringFromDate:startDate withFormat:@"YYYYMMdd" locale:@""], [Utilities stringFromDate:endDate withFormat:@"YYYYMMdd" locale:@""]]; | 551 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/history/detail/%@/%@", [Utilities stringFromDate:startDate withFormat:@"YYYYMMdd" locale:@""], [Utilities stringFromDate:endDate withFormat:@"YYYYMMdd" locale:@""]]; |
528 | NSLog(@"requestHistoryList link %@", url); | 552 | NSLog(@"requestHistoryList link %@", url); |
529 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { | 553 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { |
530 | 554 | ||
531 | if (completion == NULL) { | 555 | if (completion == NULL) { |
532 | return ; | 556 | return ; |
533 | } | 557 | } |
534 | 558 | ||
535 | if (error == nil) | 559 | if (error == nil) |
536 | { | 560 | { |
537 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 561 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
538 | NSLog(@"%@", dataResult); | 562 | NSLog(@"%@", dataResult); |
539 | int status = [dataResult[@"status"] intValue]; | 563 | int status = [dataResult[@"status"] intValue]; |
540 | if (status == 1) { // status = 1 success | 564 | if (status == 1) { // status = 1 success |
541 | if(dataResult[@"result"] != nil) { | 565 | if(dataResult[@"result"] != nil) { |
542 | NSMutableArray * arrayHistory = [[NSMutableArray alloc] init]; | 566 | NSMutableArray * arrayHistory = [[NSMutableArray alloc] init]; |
543 | NSDictionary * arrResult = dataResult[@"result"]; | 567 | NSDictionary * arrResult = dataResult[@"result"]; |
544 | if(arrResult != nil) { | 568 | if(arrResult != nil) { |
545 | /* mode_1: Data for mode walking | 569 | /* mode_1: Data for mode walking |
546 | mode_2: Data for mode running | 570 | mode_2: Data for mode running |
547 | mode_3: Data for mode bike */ | 571 | mode_3: Data for mode bike */ |
548 | NSArray *arrayKey = [NSArray arrayWithObjects:@"mode_3", @"mode_1", @"mode_2", nil]; | 572 | NSArray *arrayKey = [NSArray arrayWithObjects:@"mode_3", @"mode_1", @"mode_2", nil]; |
549 | for(NSString * key in arrayKey) { | 573 | for(NSString * key in arrayKey) { |
550 | NSDictionary *mode = [arrResult objectForKey:key]; | 574 | NSDictionary *mode = [arrResult objectForKey:key]; |
551 | if(![[arrResult objectForKey:key] isKindOfClass:[NSNull class]]) { | 575 | if(![[arrResult objectForKey:key] isKindOfClass:[NSNull class]]) { |
552 | NSMutableArray *array = [[NSMutableArray alloc] init]; | 576 | NSMutableArray *array = [[NSMutableArray alloc] init]; |
553 | for(NSString *key in mode.allKeys) { | 577 | for(NSString *key in mode.allKeys) { |
554 | HistoryObject * objectMode = [[HistoryObject alloc] initWithData:mode[key]]; | 578 | HistoryObject * objectMode = [[HistoryObject alloc] initWithData:mode[key]]; |
555 | objectMode.date = [Utilities dateFromString:key withFormat:@"yyyy-MM-dd"]; | 579 | objectMode.date = [Utilities dateFromString:key withFormat:@"yyyy-MM-dd"]; |
556 | [array addObject:objectMode]; | 580 | [array addObject:objectMode]; |
557 | } | 581 | } |
558 | [arrayHistory addObject:array]; | 582 | [arrayHistory addObject:array]; |
559 | } | 583 | } |
560 | else { | 584 | else { |
561 | [arrayHistory addObject:[[NSArray alloc] init]]; | 585 | [arrayHistory addObject:[[NSArray alloc] init]]; |
562 | } | 586 | } |
563 | } | 587 | } |
564 | } | 588 | } |
565 | completion(arrayHistory, nil); | 589 | completion(arrayHistory, nil); |
566 | } | 590 | } |
567 | else { | 591 | else { |
568 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; | 592 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; |
569 | completion(nil, errorObject); | 593 | completion(nil, errorObject); |
570 | } | 594 | } |
571 | } | 595 | } |
572 | else { | 596 | else { |
573 | NSString *message = dataResult[@"message"]; | 597 | NSString *message = dataResult[@"message"]; |
574 | if (message == nil) { | 598 | if (message == nil) { |
575 | message = @"Unknown error"; | 599 | message = @"Unknown error"; |
576 | } | 600 | } |
577 | 601 | ||
578 | if ([message isEqualToString:@"Token is invalid"]) { | 602 | if ([message isEqualToString:@"Token is invalid"]) { |
579 | [self performSelectorOnMainThread:@selector(checkToken) withObject:nil waitUntilDone:YES]; | 603 | [self checkTokenCompletionHandler:^(NSString *token, NSError *error) { |
580 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; | 604 | if (error == nil) { |
581 | [self requestHistoryList:tokenNew startDate:startDate endDate:endDate CompletionHandler:completion]; | 605 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; |
606 | [self requestHistoryList:tokenNew startDate:startDate endDate:endDate CompletionHandler:completion]; | ||
607 | } | ||
608 | else { | ||
609 | completion(nil, error); | ||
610 | } | ||
611 | }]; | ||
582 | } | 612 | } |
583 | else { | 613 | else { |
584 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 614 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
585 | completion(nil, errorObject); | 615 | completion(nil, errorObject); |
586 | } | 616 | } |
587 | } | 617 | } |
588 | } | 618 | } |
589 | else | 619 | else |
590 | { | 620 | { |
591 | completion(nil, error); | 621 | completion(nil, error); |
592 | } | 622 | } |
593 | }]; | 623 | }]; |
594 | } | 624 | } |
595 | 625 | ||
596 | #pragma mark - SNS Screen Function | 626 | #pragma mark - SNS Screen Function |
597 | - (void) requestTweetsList:(NSString *)token groupID: (int) groupID withPage:(int)page CompletionHandler:(void (^)(NSArray *, NSError *)) completion { | 627 | - (void) requestTweetsList:(NSString *)token groupID: (int) groupID withPage:(int)page CompletionHandler:(void (^)(NSArray *, NSError *)) completion { |
598 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/tweet/list?page=%d&record=10", page]; | 628 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/tweet/list?page=%d&record=10", page]; |
599 | if(groupID > -1) { | 629 | if(groupID > -1) { |
600 | url = [kServerAddress stringByAppendingFormat:@"api/tweet/list?group_id=%d&page=%d&record=10", groupID, page]; | 630 | url = [kServerAddress stringByAppendingFormat:@"api/tweet/list?group_id=%d&page=%d&record=10", groupID, page]; |
601 | } | 631 | } |
602 | NSLog(@"requestTweetsList link %@", url); | 632 | NSLog(@"requestTweetsList link %@", url); |
603 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { | 633 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { |
604 | 634 | ||
605 | if (completion == NULL) { | 635 | if (completion == NULL) { |
606 | return ; | 636 | return ; |
607 | } | 637 | } |
608 | 638 | ||
609 | if (error == nil) | 639 | if (error == nil) |
610 | { | 640 | { |
611 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 641 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
612 | NSLog(@"%@", dataResult); | 642 | NSLog(@"%@", dataResult); |
613 | int status = [dataResult[@"status"] intValue]; | 643 | int status = [dataResult[@"status"] intValue]; |
614 | if (status == 1) { // status = 1 success | 644 | if (status == 1) { // status = 1 success |
615 | if(dataResult[@"result"] != nil) { | 645 | if(dataResult[@"result"] != nil) { |
616 | NSArray * array = dataResult[@"result"]; | 646 | NSArray * array = dataResult[@"result"]; |
617 | NSMutableArray * arrayTweets = [[NSMutableArray alloc] init]; | 647 | NSMutableArray * arrayTweets = [[NSMutableArray alloc] init]; |
618 | for(NSDictionary * dict in array) { | 648 | for(NSDictionary * dict in array) { |
619 | TweetObject * object = [[TweetObject alloc] initWithData:dict]; | 649 | TweetObject * object = [[TweetObject alloc] initWithData:dict]; |
620 | [arrayTweets addObject:object]; | 650 | [arrayTweets addObject:object]; |
621 | } | 651 | } |
622 | completion(arrayTweets, nil); | 652 | completion(arrayTweets, nil); |
623 | } | 653 | } |
624 | else { | 654 | else { |
625 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; | 655 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; |
626 | completion(nil, errorObject); | 656 | completion(nil, errorObject); |
627 | } | 657 | } |
628 | } | 658 | } |
629 | else { | 659 | else { |
630 | NSString *message = dataResult[@"message"]; | 660 | NSString *message = dataResult[@"message"]; |
631 | if (message == nil) { | 661 | if (message == nil) { |
632 | message = @"Unknown error"; | 662 | message = @"Unknown error"; |
633 | } | 663 | } |
634 | 664 | ||
635 | if ([message isEqualToString:@"Token is invalid"]) { | 665 | if ([message isEqualToString:@"Token is invalid"]) { |
636 | [self performSelectorOnMainThread:@selector(checkToken) withObject:nil waitUntilDone:YES]; | 666 | [self checkTokenCompletionHandler:^(NSString *token, NSError *error) { |
637 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; | 667 | if (error == nil) { |
638 | [self requestTweetsList:tokenNew groupID:groupID withPage:page CompletionHandler:completion]; | 668 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; |
669 | [self requestTweetsList:tokenNew groupID:groupID withPage:page CompletionHandler:completion]; | ||
670 | } | ||
671 | else { | ||
672 | completion(nil, error); | ||
673 | } | ||
674 | }]; | ||
639 | } | 675 | } |
640 | else { | 676 | else { |
641 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 677 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
642 | completion(nil, errorObject); | 678 | completion(nil, errorObject); |
643 | } | 679 | } |
644 | } | 680 | } |
645 | } | 681 | } |
646 | else | 682 | else |
647 | { | 683 | { |
648 | completion(nil, error); | 684 | completion(nil, error); |
649 | } | 685 | } |
650 | }]; | 686 | }]; |
651 | } | 687 | } |
652 | 688 | ||
653 | - (void) searchGroup:(NSString *)token withKey:(NSString *)key andPage:(int)page CompletionHandler:(void (^)(NSArray *, NSError *)) completion { | 689 | - (void) searchGroup:(NSString *)token withKey:(NSString *)key andPage:(int)page CompletionHandler:(void (^)(NSArray *, NSError *)) completion { |
654 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/sns/group/search"]; | 690 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/sns/group/search"]; |
655 | NSLog(@"searchGroup link %@ page %d", url, page); | 691 | NSLog(@"searchGroup link %@ page %d", url, page); |
656 | if(searchTask != nil) { | 692 | if(searchTask != nil) { |
657 | [searchTask cancel]; | 693 | [searchTask cancel]; |
658 | } | 694 | } |
659 | 695 | ||
660 | searchTask = [self _request:url method:@"POST" token:token paras:@{@"keyword":key, @"page": [NSNumber numberWithInt:page]} completion:^(NSData *data, NSError *error) { | 696 | searchTask = [self _request:url method:@"POST" token:token paras:@{@"keyword":key, @"page": [NSNumber numberWithInt:page]} completion:^(NSData *data, NSError *error) { |
661 | searchTask = nil; | 697 | searchTask = nil; |
662 | if (completion == NULL) { | 698 | if (completion == NULL) { |
663 | return ; | 699 | return ; |
664 | } | 700 | } |
665 | 701 | ||
666 | if (error == nil) | 702 | if (error == nil) |
667 | { | 703 | { |
668 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 704 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
669 | NSLog(@"%@", dataResult); | 705 | NSLog(@"%@", dataResult); |
670 | int status = [dataResult[@"status"] intValue]; | 706 | int status = [dataResult[@"status"] intValue]; |
671 | if (status == 1) { // status = 1 success | 707 | if (status == 1) { // status = 1 success |
672 | if(dataResult[@"result"] != nil) { | 708 | if(dataResult[@"result"] != nil) { |
673 | NSArray * array = dataResult[@"result"]; | 709 | NSArray * array = dataResult[@"result"]; |
674 | NSMutableArray * arrayTweets = [[NSMutableArray alloc] init]; | 710 | NSMutableArray * arrayTweets = [[NSMutableArray alloc] init]; |
675 | for(NSDictionary * dict in array) { | 711 | for(NSDictionary * dict in array) { |
676 | GroupObject * object = [[GroupObject alloc] initWithData:dict]; | 712 | GroupObject * object = [[GroupObject alloc] initWithData:dict]; |
677 | [arrayTweets addObject:object]; | 713 | [arrayTweets addObject:object]; |
678 | } | 714 | } |
679 | completion(arrayTweets, nil); | 715 | completion(arrayTweets, nil); |
680 | } | 716 | } |
681 | else { | 717 | else { |
682 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; | 718 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; |
683 | completion(nil, errorObject); | 719 | completion(nil, errorObject); |
684 | } | 720 | } |
685 | } | 721 | } |
686 | else { | 722 | else { |
687 | NSString *message = dataResult[@"message"]; | 723 | NSString *message = dataResult[@"message"]; |
688 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 724 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
689 | completion(nil, errorObject); | 725 | completion(nil, errorObject); |
690 | } | 726 | } |
691 | } | 727 | } |
692 | else | 728 | else |
693 | { | 729 | { |
694 | completion(nil, error); | 730 | completion(nil, error); |
695 | } | 731 | } |
696 | }]; | 732 | }]; |
697 | } | 733 | } |
698 | 734 | ||
699 | #pragma mark - Group Function | 735 | #pragma mark - Group Function |
700 | -(void) requestCreateGroup:(NSString *)token withGroup:(GroupObject *)group CompletionHandler:(void (^)(GroupObject *, NSError *)) completion { | 736 | -(void) requestCreateGroup:(NSString *)token withGroup:(GroupObject *)group CompletionHandler:(void (^)(GroupObject *, NSError *)) completion { |
701 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/groups/newGroup"]; | 737 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/groups/newGroup"]; |
702 | NSLog(@"requestCreateGroup link %@", url); | 738 | NSLog(@"requestCreateGroup link %@", url); |
703 | NSDictionary * dict = @{@"group_name":group.name, | 739 | NSDictionary * dict = @{@"group_name":group.name, |
704 | @"goal":group.goal, | 740 | @"goal":group.goal, |
705 | @"walk_mode_active":[NSNumber numberWithBool:group.walkMode], | 741 | @"walk_mode_active":[NSNumber numberWithBool:group.walkMode], |
706 | @"run_mode_active":[NSNumber numberWithBool:group.runMode], | 742 | @"run_mode_active":[NSNumber numberWithBool:group.runMode], |
707 | @"bike_mode_active":[NSNumber numberWithBool:group.bikeMode], | 743 | @"bike_mode_active":[NSNumber numberWithBool:group.bikeMode], |
708 | @"step_mode_active":[NSNumber numberWithBool:group.stepMode], | 744 | @"step_mode_active":[NSNumber numberWithBool:group.stepMode], |
709 | @"gym_mode_active":[NSNumber numberWithBool:group.gymMode], | 745 | @"gym_mode_active":[NSNumber numberWithBool:group.gymMode], |
710 | @"beginer_mode_active":[NSNumber numberWithBool:group.beginMode], | 746 | @"beginer_mode_active":[NSNumber numberWithBool:group.beginMode], |
711 | @"walk_mode_goal":[NSNumber numberWithBool:group.walkGoal], | 747 | @"walk_mode_goal":[NSNumber numberWithBool:group.walkGoal], |
712 | @"run_mode_goal":[NSNumber numberWithBool:group.runGoal], | 748 | @"run_mode_goal":[NSNumber numberWithBool:group.runGoal], |
713 | @"bike_mode_goal":[NSNumber numberWithBool:group.bikeGoal]}; | 749 | @"bike_mode_goal":[NSNumber numberWithBool:group.bikeGoal]}; |
714 | 750 | ||
715 | [self _request:url method:@"POST" token:token paras:dict completion:^(NSData *data, NSError *error) { | 751 | [self _request:url method:@"POST" token:token paras:dict completion:^(NSData *data, NSError *error) { |
716 | 752 | ||
717 | if (completion == NULL) { | 753 | if (completion == NULL) { |
718 | return ; | 754 | return ; |
719 | } | 755 | } |
720 | 756 | ||
721 | if (error == nil) | 757 | if (error == nil) |
722 | { | 758 | { |
723 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 759 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
724 | NSLog(@"%@", dataResult); | 760 | NSLog(@"%@", dataResult); |
725 | int status = [dataResult[@"status"] intValue]; | 761 | int status = [dataResult[@"status"] intValue]; |
726 | if (status == 1) { // status = 1 success | 762 | if (status == 1) { // status = 1 success |
727 | if(dataResult[@"result"] != nil) { | 763 | if(dataResult[@"result"] != nil) { |
728 | NSDictionary * dict = dataResult[@"result"]; | 764 | NSDictionary * dict = dataResult[@"result"]; |
729 | group.groupID = [dict[@"group_id"] intValue]; | 765 | group.groupID = [dict[@"group_id"] intValue]; |
730 | completion(group, nil); | 766 | completion(group, nil); |
731 | } | 767 | } |
732 | else { | 768 | else { |
733 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; | 769 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; |
734 | completion(nil, errorObject); | 770 | completion(nil, errorObject); |
735 | } | 771 | } |
736 | } | 772 | } |
737 | else { | 773 | else { |
738 | NSString *message = dataResult[@"message"]; | 774 | NSString *message = dataResult[@"message"]; |
739 | if (message == nil) { | 775 | if (message == nil) { |
740 | message = @"Unknown error"; | 776 | message = @"Unknown error"; |
741 | } | 777 | } |
742 | 778 | ||
743 | if ([message isEqualToString:@"Token is invalid"]) { | 779 | if ([message isEqualToString:@"Token is invalid"]) { |
744 | [self performSelectorOnMainThread:@selector(checkToken) withObject:nil waitUntilDone:YES]; | 780 | [self checkTokenCompletionHandler:^(NSString *token, NSError *error) { |
745 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; | 781 | if (error == nil) { |
746 | [self requestCreateGroup:tokenNew withGroup:group CompletionHandler:completion]; | 782 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; |
783 | [self requestCreateGroup:tokenNew withGroup:group CompletionHandler:completion]; | ||
784 | } | ||
785 | else { | ||
786 | completion(nil, error); | ||
787 | } | ||
788 | }]; | ||
747 | } | 789 | } |
748 | else { | 790 | else { |
749 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 791 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
750 | completion(nil, errorObject); | 792 | completion(nil, errorObject); |
751 | } | 793 | } |
752 | } | 794 | } |
753 | } | 795 | } |
754 | else | 796 | else |
755 | { | 797 | { |
756 | completion(nil, error); | 798 | completion(nil, error); |
757 | } | 799 | } |
758 | }]; | 800 | }]; |
759 | } | 801 | } |
760 | 802 | ||
761 | - (void) getGroupDetail:(NSString *)token withGroupID:(int)groupID CompletionHandler:(void (^)(GroupObject *, NSError *)) completion { | 803 | - (void) getGroupDetail:(NSString *)token withGroupID:(int)groupID CompletionHandler:(void (^)(GroupObject *, NSError *)) completion { |
762 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/sns/group/detail/%d", groupID]; | 804 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/sns/group/detail/%d", groupID]; |
763 | NSLog(@"getGroupDetail link %@", url); | 805 | NSLog(@"getGroupDetail link %@", url); |
764 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { | 806 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { |
765 | 807 | ||
766 | if (completion == NULL) { | 808 | if (completion == NULL) { |
767 | return ; | 809 | return ; |
768 | } | 810 | } |
769 | 811 | ||
770 | if (error == nil) | 812 | if (error == nil) |
771 | { | 813 | { |
772 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 814 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
773 | NSLog(@"%@", dataResult); | 815 | NSLog(@"%@", dataResult); |
774 | int status = [dataResult[@"status"] intValue]; | 816 | int status = [dataResult[@"status"] intValue]; |
775 | if (status == 1) { // status = 1 success | 817 | if (status == 1) { // status = 1 success |
776 | if(dataResult[@"result"] != nil) { | 818 | if(dataResult[@"result"] != nil) { |
777 | NSArray * array = dataResult[@"result"]; | 819 | NSArray * array = dataResult[@"result"]; |
778 | GroupObject * object = [[GroupObject alloc] initWithData:array[0] andGroupID:groupID]; | 820 | GroupObject * object = [[GroupObject alloc] initWithData:array[0] andGroupID:groupID]; |
779 | completion(object, nil); | 821 | completion(object, nil); |
780 | } | 822 | } |
781 | else { | 823 | else { |
782 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; | 824 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; |
783 | completion(nil, errorObject); | 825 | completion(nil, errorObject); |
784 | } | 826 | } |
785 | } | 827 | } |
786 | else { | 828 | else { |
787 | NSString *message = dataResult[@"message"]; | 829 | NSString *message = dataResult[@"message"]; |
788 | if (message == nil) { | 830 | if (message == nil) { |
789 | message = @"Unknown error"; | 831 | message = @"Unknown error"; |
790 | } | 832 | } |
791 | 833 | ||
792 | if ([message isEqualToString:@"Token is invalid"]) { | 834 | if ([message isEqualToString:@"Token is invalid"]) { |
793 | [self performSelectorOnMainThread:@selector(checkToken) withObject:nil waitUntilDone:YES]; | 835 | [self checkTokenCompletionHandler:^(NSString *token, NSError *error) { |
794 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; | 836 | if (error == nil) { |
795 | [self getGroupDetail:tokenNew withGroupID:groupID CompletionHandler:completion]; | 837 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; |
838 | [self getGroupDetail:tokenNew withGroupID:groupID CompletionHandler:completion]; | ||
839 | } | ||
840 | else { | ||
841 | completion(nil, error); | ||
842 | } | ||
843 | }]; | ||
796 | } | 844 | } |
797 | else { | 845 | else { |
798 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 846 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
799 | completion(nil, errorObject); | 847 | completion(nil, errorObject); |
800 | } | 848 | } |
801 | } | 849 | } |
802 | } | 850 | } |
803 | else | 851 | else |
804 | { | 852 | { |
805 | completion(nil, error); | 853 | completion(nil, error); |
806 | } | 854 | } |
807 | }]; | 855 | }]; |
808 | } | 856 | } |
809 | 857 | ||
810 | - (void) requestMemberList:(NSString *)token groupID: (int) groupID withPage:(int)page CompletionHandler:(void (^)(NSArray *, NSError *)) completion { | 858 | - (void) requestMemberList:(NSString *)token groupID: (int) groupID withPage:(int)page CompletionHandler:(void (^)(NSArray *, NSError *)) completion { |
811 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/sns/group/member/%d/%d", groupID, page]; | 859 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/sns/group/member/%d/%d", groupID, page]; |
812 | NSLog(@"requestMemberList link %@ page %d", url, page); | 860 | NSLog(@"requestMemberList link %@ page %d", url, page); |
813 | 861 | ||
814 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { | 862 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { |
815 | if (completion == NULL) { | 863 | if (completion == NULL) { |
816 | return ; | 864 | return ; |
817 | } | 865 | } |
818 | 866 | ||
819 | if (error == nil) | 867 | if (error == nil) |
820 | { | 868 | { |
821 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 869 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
822 | NSLog(@"%@", dataResult); | 870 | NSLog(@"%@", dataResult); |
823 | int status = [dataResult[@"status"] intValue]; | 871 | int status = [dataResult[@"status"] intValue]; |
824 | if (status == 1) { // status = 1 success | 872 | if (status == 1) { // status = 1 success |
825 | if(dataResult[@"result"] != nil) { | 873 | if(dataResult[@"result"] != nil) { |
826 | NSArray * array = dataResult[@"result"]; | 874 | NSArray * array = dataResult[@"result"]; |
827 | NSMutableArray * arrayTweets = [[NSMutableArray alloc] init]; | 875 | NSMutableArray * arrayTweets = [[NSMutableArray alloc] init]; |
828 | for(NSDictionary * dict in array) { | 876 | for(NSDictionary * dict in array) { |
829 | MemberObject * object = [[MemberObject alloc] initWithData:dict]; | 877 | MemberObject * object = [[MemberObject alloc] initWithData:dict]; |
830 | [arrayTweets addObject:object]; | 878 | [arrayTweets addObject:object]; |
831 | } | 879 | } |
832 | completion(arrayTweets, nil); | 880 | completion(arrayTweets, nil); |
833 | } | 881 | } |
834 | else { | 882 | else { |
835 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; | 883 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; |
836 | completion(nil, errorObject); | 884 | completion(nil, errorObject); |
837 | } | 885 | } |
838 | } | 886 | } |
839 | else { | 887 | else { |
840 | NSString *message = dataResult[@"message"]; | 888 | NSString *message = dataResult[@"message"]; |
841 | if (message == nil) { | 889 | if (message == nil) { |
842 | message = @"Unknown error"; | 890 | message = @"Unknown error"; |
843 | } | 891 | } |
844 | 892 | ||
845 | if ([message isEqualToString:@"Token is invalid"]) { | 893 | if ([message isEqualToString:@"Token is invalid"]) { |
846 | [self performSelectorOnMainThread:@selector(checkToken) withObject:nil waitUntilDone:YES]; | 894 | [self checkTokenCompletionHandler:^(NSString *token, NSError *error) { |
847 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; | 895 | if (error == nil) { |
848 | [self requestMemberList:tokenNew groupID:groupID withPage:page CompletionHandler:completion]; | 896 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; |
897 | [self requestMemberList:tokenNew groupID:groupID withPage:page CompletionHandler:completion]; | ||
898 | } | ||
899 | else { | ||
900 | completion(nil, error); | ||
901 | } | ||
902 | }]; | ||
849 | } | 903 | } |
850 | else { | 904 | else { |
851 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 905 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
852 | completion(nil, errorObject); | 906 | completion(nil, errorObject); |
853 | } | 907 | } |
854 | } | 908 | } |
855 | } | 909 | } |
856 | else | 910 | else |
857 | { | 911 | { |
858 | completion(nil, error); | 912 | completion(nil, error); |
859 | } | 913 | } |
860 | }]; | 914 | }]; |
861 | } | 915 | } |
862 | 916 | ||
863 | - (void) requestJoinGroup:(NSString *)token groupID: (int) groupID CompletionHandler:(void (^)(NSError *)) completion { | 917 | - (void) requestJoinGroup:(NSString *)token groupID: (int) groupID CompletionHandler:(void (^)(NSError *)) completion { |
864 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/sns/group/join"]; | 918 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/sns/group/join"]; |
865 | NSLog(@"requestJoinGroup link %@", url); | 919 | NSLog(@"requestJoinGroup link %@", url); |
866 | 920 | ||
867 | [self _request:url method:@"POST" token:token paras:@{@"group_id": [NSNumber numberWithInt:groupID]} completion:^(NSData *data, NSError *error) { | 921 | [self _request:url method:@"POST" token:token paras:@{@"group_id": [NSNumber numberWithInt:groupID]} completion:^(NSData *data, NSError *error) { |
868 | if (completion == NULL) { | 922 | if (completion == NULL) { |
869 | return ; | 923 | return ; |
870 | } | 924 | } |
871 | 925 | ||
872 | if (error == nil) | 926 | if (error == nil) |
873 | { | 927 | { |
874 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 928 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
875 | NSLog(@"%@", dataResult); | 929 | NSLog(@"%@", dataResult); |
876 | int status = [dataResult[@"status"] intValue]; | 930 | int status = [dataResult[@"status"] intValue]; |
877 | if (status == 1) { // status = 1 success | 931 | if (status == 1) { // status = 1 success |
878 | if(dataResult[@"result"] != nil) { | 932 | if(dataResult[@"result"] != nil) { |
879 | completion(nil); | 933 | completion(nil); |
880 | } | 934 | } |
881 | else { | 935 | else { |
882 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; | 936 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; |
883 | completion(errorObject); | 937 | completion(errorObject); |
884 | } | 938 | } |
885 | } | 939 | } |
886 | else { | 940 | else { |
887 | NSString *message = dataResult[@"message"]; | 941 | NSString *message = dataResult[@"message"]; |
888 | if ([message isEqualToString:@"Token is invalid"]) { | 942 | if ([message isEqualToString:@"Token is invalid"]) { |
889 | [self performSelectorOnMainThread:@selector(checkToken) withObject:nil waitUntilDone:YES]; | 943 | [self checkTokenCompletionHandler:^(NSString *token, NSError *error) { |
890 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; | 944 | if (error == nil) { |
891 | [self requestJoinGroup:tokenNew groupID:groupID CompletionHandler:completion]; | 945 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; |
946 | [self requestJoinGroup:tokenNew groupID:groupID CompletionHandler:completion]; | ||
947 | } | ||
948 | else { | ||
949 | completion(error); | ||
950 | } | ||
951 | }]; | ||
892 | } | 952 | } |
893 | else { | 953 | else { |
894 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 954 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
895 | completion(errorObject); | 955 | completion(errorObject); |
896 | } | 956 | } |
897 | } | 957 | } |
898 | } | 958 | } |
899 | else | 959 | else |
900 | { | 960 | { |
901 | completion(error); | 961 | completion(error); |
902 | } | 962 | } |
903 | }]; | 963 | }]; |
904 | } | 964 | } |
905 | 965 | ||
906 | - (void) requestGroupList:(NSString *)token CompletionHandler:(void (^)(NSArray *, NSError *)) completion { | 966 | - (void) requestGroupList:(NSString *)token CompletionHandler:(void (^)(NSArray *, NSError *)) completion { |
907 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/groups/list"]; | 967 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/groups/list"]; |
908 | NSLog(@"requestGroupList link %@", url); | 968 | NSLog(@"requestGroupList link %@", url); |
909 | 969 | ||
910 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { | 970 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { |
911 | if (completion == NULL) { | 971 | if (completion == NULL) { |
912 | return ; | 972 | return ; |
913 | } | 973 | } |
914 | 974 | ||
915 | if (error == nil) | 975 | if (error == nil) |
916 | { | 976 | { |
917 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 977 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
918 | NSLog(@"%@", dataResult); | 978 | NSLog(@"%@", dataResult); |
919 | int status = [dataResult[@"status"] intValue]; | 979 | int status = [dataResult[@"status"] intValue]; |
920 | if (status == 1) { // status = 1 success | 980 | if (status == 1) { // status = 1 success |
921 | if(dataResult[@"result"] != nil) { | 981 | if(dataResult[@"result"] != nil) { |
922 | NSArray * array = dataResult[@"result"]; | 982 | NSArray * array = dataResult[@"result"]; |
923 | NSMutableArray * arrayGroup = [[NSMutableArray alloc] init]; | 983 | NSMutableArray * arrayGroup = [[NSMutableArray alloc] init]; |
924 | for(NSDictionary * dict in array) { | 984 | for(NSDictionary * dict in array) { |
925 | GroupObject * object = [[GroupObject alloc] initWithShortData:dict]; | 985 | GroupObject * object = [[GroupObject alloc] initWithShortData:dict]; |
926 | [arrayGroup addObject:object]; | 986 | [arrayGroup addObject:object]; |
927 | } | 987 | } |
928 | completion(arrayGroup, nil); | 988 | completion(arrayGroup, nil); |
929 | } | 989 | } |
930 | else { | 990 | else { |
931 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; | 991 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; |
932 | completion(nil, errorObject); | 992 | completion(nil, errorObject); |
933 | } | 993 | } |
934 | } | 994 | } |
935 | else { | 995 | else { |
936 | NSString *message = dataResult[@"message"]; | 996 | NSString *message = dataResult[@"message"]; |
937 | if (message == nil) { | 997 | if (message == nil) { |
938 | message = @"Unknown error"; | 998 | message = @"Unknown error"; |
939 | } | 999 | } |
940 | 1000 | ||
941 | if ([message isEqualToString:@"Token is invalid"]) { | 1001 | if ([message isEqualToString:@"Token is invalid"]) { |
942 | [self performSelectorOnMainThread:@selector(checkToken) withObject:nil waitUntilDone:YES]; | 1002 | [self checkTokenCompletionHandler:^(NSString *token, NSError *error) { |
943 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; | 1003 | if (error == nil) { |
944 | [self requestGroupList:tokenNew CompletionHandler:completion]; | 1004 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; |
1005 | [self requestGroupList:tokenNew CompletionHandler:completion]; | ||
1006 | } | ||
1007 | else { | ||
1008 | completion(nil, error); | ||
1009 | } | ||
1010 | }]; | ||
945 | } | 1011 | } |
946 | else { | 1012 | else { |
947 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 1013 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
948 | completion(nil, errorObject); | 1014 | completion(nil, errorObject); |
949 | } | 1015 | } |
950 | } | 1016 | } |
951 | } | 1017 | } |
952 | else | 1018 | else |
953 | { | 1019 | { |
954 | completion(nil, error); | 1020 | completion(nil, error); |
955 | } | 1021 | } |
956 | }]; | 1022 | }]; |
957 | } | 1023 | } |
958 | 1024 | ||
959 | #pragma mark - Ranking API | 1025 | #pragma mark - Ranking API |
960 | - (void) requestRankingList:(NSString *)token startDate:(NSDate *)startDate endDate:(NSDate *)endDate mode:(int) mode page:(int) page CompletionHandler:(void (^)(NSArray *, NSError *)) completion { | 1026 | - (void) requestRankingList:(NSString *)token startDate:(NSDate *)startDate endDate:(NSDate *)endDate mode:(int) mode page:(int) page CompletionHandler:(void (^)(NSArray *, NSError *)) completion { |
961 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/ranking/%d/%@/%@/%d", mode, [Utilities stringFromDate:startDate withFormat:@"YYYYMMdd" locale:@""], [Utilities stringFromDate:endDate withFormat:@"YYYYMMdd" locale:@""], page]; | 1027 | NSString *url = [kServerAddress stringByAppendingFormat:@"api/ranking/%d/%@/%@/%d", mode, [Utilities stringFromDate:startDate withFormat:@"YYYYMMdd" locale:@""], [Utilities stringFromDate:endDate withFormat:@"YYYYMMdd" locale:@""], page]; |
962 | NSLog(@"requestRaningList link %@", url); | 1028 | NSLog(@"requestRaningList link %@", url); |
963 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { | 1029 | [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { |
964 | 1030 | ||
965 | if (completion == NULL) { | 1031 | if (completion == NULL) { |
966 | return ; | 1032 | return ; |
967 | } | 1033 | } |
968 | 1034 | ||
969 | if (error == nil) | 1035 | if (error == nil) |
970 | { | 1036 | { |
971 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 1037 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
972 | NSLog(@"%@", dataResult); | 1038 | NSLog(@"%@", dataResult); |
973 | int status = [dataResult[@"status"] intValue]; | 1039 | int status = [dataResult[@"status"] intValue]; |
974 | if (status == 1) { // status = 1 success | 1040 | if (status == 1) { // status = 1 success |
975 | NSArray * array = dataResult[@"result"]; | 1041 | NSArray * array = dataResult[@"result"]; |
976 | NSMutableArray * arrayRanking = [[NSMutableArray alloc] init]; | 1042 | NSMutableArray * arrayRanking = [[NSMutableArray alloc] init]; |
977 | for(NSDictionary * dict in array) { | 1043 | for(NSDictionary * dict in array) { |
978 | RankingObject * object = [[RankingObject alloc] initWithData:dict]; | 1044 | RankingObject * object = [[RankingObject alloc] initWithData:dict]; |
979 | [arrayRanking addObject:object]; | 1045 | [arrayRanking addObject:object]; |
980 | } | 1046 | } |
981 | completion(arrayRanking, nil); | 1047 | completion(arrayRanking, nil); |
982 | } | 1048 | } |
983 | else { | 1049 | else { |
984 | NSString *message = dataResult[@"message"]; | 1050 | NSString *message = dataResult[@"message"]; |
985 | if (message == nil) { | 1051 | if (message == nil) { |
986 | message = @"Unknown error"; | 1052 | message = @"Unknown error"; |
987 | } | 1053 | } |
988 | 1054 | ||
989 | if ([message isEqualToString:@"Token is invalid"]) { | 1055 | if ([message isEqualToString:@"Token is invalid"]) { |
990 | [self performSelectorOnMainThread:@selector(checkToken) withObject:nil waitUntilDone:YES]; | 1056 | [self checkTokenCompletionHandler:^(NSString *token, NSError *error) { |
991 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; | 1057 | if (error == nil) { |
992 | [self requestRankingList:tokenNew startDate:startDate endDate:endDate mode:mode page:page CompletionHandler:completion]; | 1058 | NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; |
1059 | [self requestRankingList:tokenNew startDate:startDate endDate:endDate mode:mode page:page CompletionHandler:completion]; | ||
1060 | } | ||
1061 | else { | ||
1062 | completion(nil, error); | ||
1063 | } | ||
1064 | }]; | ||
993 | } | 1065 | } |
994 | else { | 1066 | else { |
995 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 1067 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
996 | completion(nil, errorObject); | 1068 | completion(nil, errorObject); |
997 | } | 1069 | } |
998 | } | 1070 | } |
999 | } | 1071 | } |
1000 | else | 1072 | else |
1001 | { | 1073 | { |
1002 | completion(nil, error); | 1074 | completion(nil, error); |
1003 | } | 1075 | } |
1004 | }]; | 1076 | }]; |
1005 | } | 1077 | } |
1006 | 1078 | ||
1007 | #pragma mark - Common API | 1079 | #pragma mark - Common API |
1008 | - (void)refreshToken: (NSString *)userID CompletionHandler:(void (^)(NSString *, NSError *))completion { | 1080 | - (void)refreshToken: (NSString *)userID CompletionHandler:(void (^)(NSString *, NSError *))completion { |
1009 | [self _request:[kServerAddress stringByAppendingFormat: @"refreshToken"] method:@"POST" token:@"" paras:@{@"userId":userID} completion:^(NSData *data, NSError *error) { | 1081 | [self _request:[kServerAddress stringByAppendingFormat: @"refreshToken"] method:@"POST" token:@"" paras:@{@"userId":userID} completion:^(NSData *data, NSError *error) { |
1010 | 1082 | ||
1011 | if (completion == NULL) { | 1083 | if (completion == NULL) { |
1012 | return ; | 1084 | return ; |
1013 | } | 1085 | } |
1014 | 1086 | ||
1015 | if (error == nil) | 1087 | if (error == nil) |
1016 | { | 1088 | { |
1017 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 1089 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
1018 | 1090 | ||
1019 | int status = [dataResult[@"status"] intValue]; | 1091 | int status = [dataResult[@"status"] intValue]; |
1020 | if (status == 1) { // status = 1 success | 1092 | if (status == 1) { // status = 1 success |
1021 | NSArray *arrayResult = dataResult[@"result"]; | 1093 | NSArray *arrayResult = dataResult[@"result"]; |
1022 | if (arrayResult.count > 0) { | 1094 | if (arrayResult.count > 0) { |
1023 | NSString *token = arrayResult[0]; | 1095 | NSString *token = arrayResult[0]; |
1024 | completion(token, nil); | 1096 | completion(token, nil); |
1025 | } | 1097 | } |
1026 | else { | 1098 | else { |
1027 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}]; | 1099 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"refreshToken Error"}]; |
1028 | completion(nil, errorObject); | 1100 | completion(nil, errorObject); |
1029 | } | 1101 | } |
1030 | 1102 | ||
1031 | } | 1103 | } |
1032 | else { // status = 0 error | 1104 | else { // status = 0 error |
1033 | NSString *message = dataResult[@"message"]; | 1105 | NSString *message = dataResult[@"message"]; |
1034 | if (message == nil) { | 1106 | if (message == nil) { |
1035 | message = @"Unknown error"; | 1107 | message = @"refreshToken Error"; |
1036 | } | 1108 | } |
1037 | NSError *loginFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 1109 | NSError *loginFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
1038 | completion(nil, loginFaild); | 1110 | completion(nil, loginFaild); |
1039 | } | 1111 | } |
1040 | } | 1112 | } |
1041 | else | 1113 | else |
1042 | { | 1114 | { |
1043 | completion(nil, error); | 1115 | completion(nil, error); |
1044 | } | 1116 | } |
1045 | }]; | 1117 | }]; |
1046 | } | 1118 | } |
1047 | 1119 | ||
1048 | - (void)getNoticeByDate: (NSString *)date CompletionHandler:(void (^)(NSArray *, NSError *)) completion | 1120 | - (void)getNoticeByDate: (NSString *)date CompletionHandler:(void (^)(NSArray *, NSError *)) completion |
1049 | { | 1121 | { |
1050 | [self _request:[kServerAddress stringByAppendingFormat: @"api/getnoticesbydate/%@", date] method:@"GET" token:@"" paras:nil completion:^(NSData *data, NSError *error) { | 1122 | [self _request:[kServerAddress stringByAppendingFormat: @"api/getnoticesbydate/%@", date] method:@"GET" token:@"" paras:nil completion:^(NSData *data, NSError *error) { |
1051 | 1123 | ||
1052 | if (completion == NULL) { | 1124 | if (completion == NULL) { |
1053 | return ; | 1125 | return ; |
1054 | } | 1126 | } |
1055 | 1127 | ||
1056 | if (error == nil) | 1128 | if (error == nil) |
1057 | { | 1129 | { |
1058 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 1130 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
1059 | 1131 | ||
1060 | int status = [dataResult[@"status"] intValue]; | 1132 | int status = [dataResult[@"status"] intValue]; |
1061 | if (status == 1) { // status = 1 success | 1133 | if (status == 1) { // status = 1 success |
1062 | NSArray *arrayResult = dataResult[@"result"][@"listNotice"]; | 1134 | NSArray *arrayResult = dataResult[@"result"][@"listNotice"]; |
1063 | if (arrayResult.count > 0) { | 1135 | if (arrayResult.count > 0) { |
1064 | NSMutableArray *notices = [[NSMutableArray alloc] init]; | 1136 | NSMutableArray *notices = [[NSMutableArray alloc] init]; |
1065 | for(NSDictionary *dict in arrayResult) { | 1137 | for(NSDictionary *dict in arrayResult) { |
1066 | NSString *object = [dict objectForKey:@"notice_content"]; | 1138 | NSString *object = [dict objectForKey:@"notice_content"]; |
1067 | [notices addObject:object]; | 1139 | [notices addObject:object]; |
1068 | } | 1140 | } |
1069 | completion(notices, nil); | 1141 | completion(notices, nil); |
1070 | } | 1142 | } |
1071 | else { | 1143 | else { |
1072 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Today is not notice"}]; | 1144 | NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Today is not notice"}]; |
1073 | completion(nil, errorObject); | 1145 | completion(nil, errorObject); |
1074 | } | 1146 | } |
1075 | 1147 | ||
1076 | } | 1148 | } |
1077 | else { // status = 0 error | 1149 | else { // status = 0 error |
1078 | NSString *message = dataResult[@"message"]; | 1150 | NSString *message = dataResult[@"message"]; |
1079 | if (message == nil) { | 1151 | if (message == nil) { |
1080 | message = @"Unknown error"; | 1152 | message = @"Unknown error"; |
1081 | } | 1153 | } |
1082 | NSError *loginFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 1154 | NSError *loginFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
1083 | completion(nil, loginFaild); | 1155 | completion(nil, loginFaild); |
1084 | } | 1156 | } |
1085 | } | 1157 | } |
1086 | else | 1158 | else |
1087 | { | 1159 | { |
1088 | completion(nil, error); | 1160 | completion(nil, error); |
1089 | } | 1161 | } |
1090 | }]; | 1162 | }]; |
1091 | } | 1163 | } |
1092 | 1164 | ||
1093 | #pragma mark - Private Function | 1165 | #pragma mark - Private Function |
1094 | - (void) checkToken { | 1166 | - (void) checkTokenCompletionHandler:(void (^)(NSString *, NSError *)) completion |
1095 | // [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationToken object:nil]; | 1167 | { |
1096 | NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kUser]; | 1168 | NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kUser]; |
1097 | User *user = (User *)[NSKeyedUnarchiver unarchiveObjectWithData:data]; | 1169 | User *user = (User *)[NSKeyedUnarchiver unarchiveObjectWithData:data]; |
1098 | if (user != nil) { | 1170 | if (user != nil) { |
1099 | [self refreshToken:user.user_id CompletionHandler:^(NSString *token, NSError *error) { | 1171 | [self refreshToken:user.user_id CompletionHandler:^(NSString *token, NSError *error) { |
1100 | if (error == nil) { | 1172 | if (error == nil) { |
1101 | [[NSUserDefaults standardUserDefaults] setObject:token forKey:kToken]; | 1173 | [[NSUserDefaults standardUserDefaults] setObject:token forKey:kToken]; |
1102 | [[NSUserDefaults standardUserDefaults] synchronize]; | 1174 | [[NSUserDefaults standardUserDefaults] synchronize]; |
1175 | completion(token, nil); | ||
1103 | } | 1176 | } |
1177 | else { | ||
1178 | completion(nil, error); | ||
1179 | } | ||
1104 | }]; | 1180 | }]; |
1105 | } | 1181 | } |
1182 | else { | ||
1183 | NSError *error = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"User not found"}]; | ||
1184 | completion(nil, error); | ||
1185 | } | ||
1106 | } | 1186 | } |
1187 | |||
1107 | - (NSData *) _encodeDictionary: (NSDictionary *) dictionary | 1188 | - (NSData *) _encodeDictionary: (NSDictionary *) dictionary |
1108 | { | 1189 | { |
1109 | NSMutableArray *parts = [[NSMutableArray alloc] init]; | 1190 | NSMutableArray *parts = [[NSMutableArray alloc] init]; |
1110 | for (id key in dictionary) | 1191 | for (id key in dictionary) |
1111 | { | 1192 | { |
1112 | NSString *encodedValue = [[dictionary[key] description] urlencode]; | 1193 | NSString *encodedValue = [[dictionary[key] description] urlencode]; |
1113 | NSString *encodedKey = [[key description] urlencode];//[[key description] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; | 1194 | NSString *encodedKey = [[key description] urlencode];//[[key description] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; |
1114 | NSString *part = [NSString stringWithFormat: @"%@=%@", encodedKey, encodedValue]; | 1195 | NSString *part = [NSString stringWithFormat: @"%@=%@", encodedKey, encodedValue]; |
1115 | [parts addObject:part]; | 1196 | [parts addObject:part]; |
1116 | } | 1197 | } |
1117 | NSString *encodedDictionary = [parts componentsJoinedByString:@"&"]; | 1198 | NSString *encodedDictionary = [parts componentsJoinedByString:@"&"]; |
1118 | return [encodedDictionary dataUsingEncoding: NSUTF8StringEncoding]; | 1199 | return [encodedDictionary dataUsingEncoding: NSUTF8StringEncoding]; |
1119 | } | 1200 | } |
1120 | 1201 | ||
1121 | - (NSURLSessionDataTask *) _request:(NSString *)address method:(NSString *)method token:(NSString *) token paras:(NSDictionary *)paras completion:(void (^)(NSData *data, NSError *error))completion | 1202 | - (NSURLSessionDataTask *) _request:(NSString *)address method:(NSString *)method token:(NSString *) token paras:(NSDictionary *)paras completion:(void (^)(NSData *data, NSError *error))completion |
1122 | { | 1203 | { |
1123 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:address]]; | 1204 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:address]]; |
1124 | request.HTTPMethod = method; | 1205 | request.HTTPMethod = method; |
1125 | [request setValue: @"application/json" forHTTPHeaderField: @"Accept"]; | 1206 | [request setValue: @"application/json" forHTTPHeaderField: @"Accept"]; |
1126 | [request setValue: @"application/json" forHTTPHeaderField: @"Content-Type"]; | 1207 | [request setValue: @"application/json" forHTTPHeaderField: @"Content-Type"]; |
1127 | if(token != nil && ![token isEqual: @""]) { | 1208 | if(token != nil && ![token isEqual: @""]) { |
1128 | [request setValue: token forHTTPHeaderField: @"token"]; | 1209 | [request setValue: token forHTTPHeaderField: @"token"]; |
1129 | } | 1210 | } |
1130 | [request setTimeoutInterval:self.timeOutInterval]; | 1211 | [request setTimeoutInterval:self.timeOutInterval]; |
1131 | 1212 | ||
1132 | if (paras != nil) | 1213 | if (paras != nil) |
1133 | { | 1214 | { |
1134 | NSData *encodedData = [self _encodeDictionary: paras]; | 1215 | NSData *encodedData = [self _encodeDictionary: paras]; |
1135 | [request setValue: [NSString stringWithFormat: @"%lu", (unsigned long) encodedData.length] forHTTPHeaderField: @"Content-Length"]; | 1216 | [request setValue: [NSString stringWithFormat: @"%lu", (unsigned long) encodedData.length] forHTTPHeaderField: @"Content-Length"]; |
1136 | [request setValue: @"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField: @"Content-Type"]; | 1217 | [request setValue: @"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField: @"Content-Type"]; |
1137 | [request setHTTPBody: encodedData]; | 1218 | [request setHTTPBody: encodedData]; |
1138 | } | 1219 | } |
1139 | 1220 | ||
1140 | //NSURLSession *session = [NSURLSession sharedSession]; | 1221 | //NSURLSession *session = [NSURLSession sharedSession]; |
1141 | NSURLSessionConfiguration *defaultConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; | 1222 | NSURLSessionConfiguration *defaultConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; |
1142 | [defaultConfiguration setHTTPMaximumConnectionsPerHost:20]; | 1223 | [defaultConfiguration setHTTPMaximumConnectionsPerHost:20]; |
1143 | NSOperationQueue *operationQueue = [NSOperationQueue mainQueue]; | 1224 | NSOperationQueue *operationQueue = [NSOperationQueue mainQueue]; |
1144 | operationQueue.maxConcurrentOperationCount = 20; | 1225 | operationQueue.maxConcurrentOperationCount = 20; |
1145 | NSURLSession *session = [NSURLSession sessionWithConfiguration:defaultConfiguration delegate:nil delegateQueue:operationQueue]; | 1226 | NSURLSession *session = [NSURLSession sessionWithConfiguration:defaultConfiguration delegate:nil delegateQueue:operationQueue]; |
1146 | NSURLSessionDataTask *task = [session dataTaskWithRequest:request | 1227 | NSURLSessionDataTask *task = [session dataTaskWithRequest:request |
1147 | completionHandler: | 1228 | completionHandler: |
1148 | ^(NSData *data, NSURLResponse *response, NSError *error) { | 1229 | ^(NSData *data, NSURLResponse *response, NSError *error) { |
1149 | if (completion == NULL) { | 1230 | if (completion == NULL) { |
1150 | return ; | 1231 | return ; |
1151 | } | 1232 | } |
1152 | if (error == nil) | 1233 | if (error == nil) |
1153 | { | 1234 | { |
1154 | completion(data, nil); | 1235 | completion(data, nil); |
1155 | } | 1236 | } |
1156 | else | 1237 | else |
1157 | { | 1238 | { |
1158 | completion(nil, error); | 1239 | completion(nil, error); |
1159 | } | 1240 | } |
1160 | }]; | 1241 | }]; |
1161 | [task resume]; | 1242 | [task resume]; |
1162 | return task; | 1243 | return task; |
1163 | } | 1244 | } |
1164 | 1245 | ||
1165 | -(void)waitUntilDone:(void(^)(void))waitBlock { | 1246 | -(void)waitUntilDone:(void(^)(void))waitBlock { |
1166 | //use your statement or call method here | 1247 | //use your statement or call method here |
1167 | if(waitBlock){ | 1248 | if(waitBlock){ |
1168 | waitBlock(); | 1249 | waitBlock(); |
1169 | } | 1250 | } |
1170 | } | 1251 | } |
1171 | 1252 | ||
1172 | @end | 1253 | @end |
1173 | 1254 |