Commit 2bab9a9ab1748f24cb669774aae02a0ff5a629e1

Authored by phong
1 parent 3b284106de

update api and token

Showing 12 changed files with 252 additions and 2 deletions Side-by-side Diff

LifeLog/LifeLog.xcodeproj/project.pbxproj
... ... @@ -35,6 +35,9 @@
35 35 6E84E3C61F27ACAE001EB88E /* MapViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6E84E3C41F27ACAE001EB88E /* MapViewController.xib */; };
36 36 6E8DA0F71F2BAC3400CCE941 /* TodayViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E8DA0F51F2BAC3400CCE941 /* TodayViewController.m */; };
37 37 6E8DA0F81F2BAC3500CCE941 /* TodayViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6E8DA0F61F2BAC3400CCE941 /* TodayViewController.xib */; };
  38 + 6EA4AF311F40BB7A00F52CFE /* StepObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EA4AF2C1F40BB7A00F52CFE /* StepObject.m */; };
  39 + 6EA4AF321F40BB7A00F52CFE /* TargetInfor.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EA4AF2E1F40BB7A00F52CFE /* TargetInfor.m */; };
  40 + 6EA4AF331F40BB7A00F52CFE /* TopObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EA4AF301F40BB7A00F52CFE /* TopObject.m */; };
38 41 6ECC40711F2A5FEB00AAEB97 /* NSDate+helper.m in Sources */ = {isa = PBXBuildFile; fileRef = 6ECC40701F2A5FEB00AAEB97 /* NSDate+helper.m */; };
39 42 6EDC8BEE1F2C2FB800765ACC /* Utilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EDC8BED1F2C2FB800765ACC /* Utilities.m */; };
40 43 6EDC8BF41F2C68D700765ACC /* CMMotionActivityExtra.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EDC8BF31F2C68D700765ACC /* CMMotionActivityExtra.m */; };
... ... @@ -140,6 +143,12 @@
140 143 6E8DA0F41F2BAC3400CCE941 /* TodayViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TodayViewController.h; sourceTree = "<group>"; };
141 144 6E8DA0F51F2BAC3400CCE941 /* TodayViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TodayViewController.m; sourceTree = "<group>"; };
142 145 6E8DA0F61F2BAC3400CCE941 /* TodayViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = TodayViewController.xib; sourceTree = "<group>"; };
  146 + 6EA4AF2B1F40BB7A00F52CFE /* StepObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StepObject.h; sourceTree = "<group>"; };
  147 + 6EA4AF2C1F40BB7A00F52CFE /* StepObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StepObject.m; sourceTree = "<group>"; };
  148 + 6EA4AF2D1F40BB7A00F52CFE /* TargetInfor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TargetInfor.h; sourceTree = "<group>"; };
  149 + 6EA4AF2E1F40BB7A00F52CFE /* TargetInfor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TargetInfor.m; sourceTree = "<group>"; };
  150 + 6EA4AF2F1F40BB7A00F52CFE /* TopObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TopObject.h; sourceTree = "<group>"; };
  151 + 6EA4AF301F40BB7A00F52CFE /* TopObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TopObject.m; sourceTree = "<group>"; };
143 152 6ECC406F1F2A5FEB00AAEB97 /* NSDate+helper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDate+helper.h"; sourceTree = "<group>"; };
144 153 6ECC40701F2A5FEB00AAEB97 /* NSDate+helper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDate+helper.m"; sourceTree = "<group>"; };
145 154 6EDC8BEC1F2C2FB800765ACC /* Utilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Utilities.h; sourceTree = "<group>"; };
146 155  
... ... @@ -255,9 +264,15 @@
255 264 6E18CED91F2E40AE0029891F /* Model */ = {
256 265 isa = PBXGroup;
257 266 children = (
  267 + 6E18CEDD1F2E46570029891F /* Entities.h */,
  268 + 6EA4AF2B1F40BB7A00F52CFE /* StepObject.h */,
  269 + 6EA4AF2C1F40BB7A00F52CFE /* StepObject.m */,
  270 + 6EA4AF2D1F40BB7A00F52CFE /* TargetInfor.h */,
  271 + 6EA4AF2E1F40BB7A00F52CFE /* TargetInfor.m */,
  272 + 6EA4AF2F1F40BB7A00F52CFE /* TopObject.h */,
  273 + 6EA4AF301F40BB7A00F52CFE /* TopObject.m */,
258 274 6E18CEDA1F2E40F50029891F /* User.h */,
259 275 6E18CEDB1F2E40F50029891F /* User.m */,
260   - 6E18CEDD1F2E46570029891F /* Entities.h */,
261 276 E9373E481F35AA180059355A /* HistoryObject.h */,
262 277 E9373E491F35AA180059355A /* HistoryObject.m */,
263 278 E9373E4B1F361A230059355A /* HistoryGraphObject.h */,
... ... @@ -781,6 +796,7 @@
781 796 6E7F93431F35872B00EFBC71 /* TPKeyboardAvoidingScrollView.m in Sources */,
782 797 6E7F93421F35872B00EFBC71 /* TPKeyboardAvoidingCollectionView.m in Sources */,
783 798 E968550A1F3C444900317CEF /* AutoTransButton.m in Sources */,
  799 + 6EA4AF321F40BB7A00F52CFE /* TargetInfor.m in Sources */,
784 800 E933E6741F3E0E4E002926F5 /* GroupObject.m in Sources */,
785 801 9CBDA0FA1F2ECE2C0055DED1 /* BaseViewController.m in Sources */,
786 802 6E8DA0F71F2BAC3400CCE941 /* TodayViewController.m in Sources */,
... ... @@ -797,6 +813,7 @@
797 813 E9F73DCC1F338A0F004A3A6F /* HistoryListTableViewCell.m in Sources */,
798 814 6EDC8BF41F2C68D700765ACC /* CMMotionActivityExtra.m in Sources */,
799 815 E933E6791F40513C002926F5 /* GroupDetailViewController.m in Sources */,
  816 + 6EA4AF311F40BB7A00F52CFE /* StepObject.m in Sources */,
800 817 9CBDA0FF1F2F03810055DED1 /* RegisterViewController.m in Sources */,
801 818 E9373E4A1F35AA180059355A /* HistoryObject.m in Sources */,
802 819 9CA8AEEA1F31BB4600C5FC74 /* ConfirmForgetPassViewController.m in Sources */,
... ... @@ -807,6 +824,7 @@
807 824 6E18CEDC1F2E40F50029891F /* User.m in Sources */,
808 825 6ECC40711F2A5FEB00AAEB97 /* NSDate+helper.m in Sources */,
809 826 6E18CED31F2E39DB0029891F /* LoginViewController.m in Sources */,
  827 + 6EA4AF331F40BB7A00F52CFE /* TopObject.m in Sources */,
810 828 E9D909241F3D6E8C004CF99F /* BaseTableViewController.m in Sources */,
811 829 6E84E3761F27A701001EB88E /* main.m in Sources */,
812 830 E933E6701F3E09B6002926F5 /* SearchGroupTableViewCell.m in Sources */,
LifeLog/LifeLog/AppDelegate.m
... ... @@ -14,6 +14,7 @@
14 14 #import "MapViewController.h"
15 15 #import "SNSViewController.h"
16 16 #import "ServerAPI.h"
  17 +#import "Utilities.h"
17 18  
18 19 @interface AppDelegate ()
19 20  
20 21  
... ... @@ -40,10 +41,21 @@
40 41 else {
41 42 [self gotoMainMenu];
42 43 }
43   -
  44 +
  45 + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotoLogin) name:kNotificationToken object:nil];
44 46 return YES;
45 47 }
46 48  
  49 +- (void)gotoLogin {
  50 + [[NSUserDefaults standardUserDefaults] removeObjectForKey:kUser];
  51 + [[NSUserDefaults standardUserDefaults] removeObjectForKey:kToken];
  52 + [[NSUserDefaults standardUserDefaults] synchronize];
  53 + LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
  54 + UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:loginVC];
  55 + self.window.rootViewController = navigation;
  56 + [self.window makeKeyAndVisible];
  57 + [Utilities showErrorMessage:@"Token is invalid" withViewController:self.window.rootViewController];
  58 +}
47 59  
48 60 - (void)applicationWillResignActive:(UIApplication *)application {
49 61 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
LifeLog/LifeLog/Entities.h
... ... @@ -16,6 +16,7 @@
16 16 #import "HistoryGraphObject.h"
17 17 #import "HistoryObject.h"
18 18 #import "User.h"
  19 +#import "TopObject.h"
19 20  
20 21 #endif /* Entities_h */
LifeLog/LifeLog/HomeViewController.m
... ... @@ -120,6 +120,26 @@
120 120 // _timer = [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(countStep) userInfo:nil repeats:YES];
121 121 // [_timer fire];
122 122 [self countStep];
  123 +
  124 + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  125 + [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  126 + NSString *dateString = [dateFormatter stringFromDate:_dateCurrent];
  127 +
  128 + HomeViewController __weak *weakSelf = self;
  129 + [[ServerAPI server] requestTopWithMode:(int)_segmentHome.selectedSegmentIndex andDate:dateString CompletionHandler:^(TopObject *topObject, NSError *error) {
  130 + if(weakSelf == nil) {
  131 + return ;
  132 + }
  133 + if (error == nil) {
  134 + NSLog(@"TopObject: %@", topObject);
  135 + }
  136 + else {
  137 + dispatch_async(dispatch_get_main_queue(), ^{
  138 + NSString *message = [error.userInfo objectForKey:@"message"];
  139 + [Utilities showErrorMessage:message withViewController:weakSelf];
  140 + });
  141 + }
  142 + }];
123 143 }
124 144  
125 145 - (void)viewWillDisappear:(BOOL)animated {
LifeLog/LifeLog/ServerAPI.h
... ... @@ -12,15 +12,21 @@
12 12 extern NSString *const kServerAddress;
13 13 extern NSString *const kUser;
14 14 extern NSString *const kToken;
  15 +extern NSString *const kNotificationToken;
15 16  
16 17 @interface ServerAPI : NSObject
17 18 + (instancetype) server;
18 19 @property (nonatomic, assign) NSTimeInterval timeOutInterval;
  20 +
  21 +#pragma mark - Login and Register
19 22 - (void)loginWithEmail:(NSString *)email Password:(NSString *)password CompletionHandler: (void (^)(User *, NSString *, NSError *)) completion;
20 23 - (void)registerUserWithParams:(NSDictionary *)params CompletionHandler: (void (^)(User *, NSString *, NSError *)) completion;
21 24 - (void)uploadImage:(NSString *)token andImageData:(NSData *)data CompletionHandler:(void (^)(NSString *, NSError *)) completion;
22 25 - (void)forgetPass:(NSString *)email CompletionHandler:(void (^)(NSError *)) completion;
23 26 - (void)confirmForgetPass:(NSString *)email withConfirm:(NSString *)confirm CompletionHandler:(void (^)(NSError *)) completion;
  27 +
  28 +#pragma mark - Home Screen Function
  29 +- (void)requestTopWithMode:(int)mode andDate:(NSString *)date CompletionHandler:(void (^)(TopObject *, NSError *)) completion;
24 30  
25 31 #pragma mark - History Screen Function
26 32 - (void) requestHistory:(NSString *)token atDate:(NSDate *)date withType:(int)type andMode:(int) mode CompletionHandler:(void (^)(HistoryObject *, NSError *)) completion;
LifeLog/LifeLog/ServerAPI.m
... ... @@ -11,6 +11,7 @@
11 11 NSString *const kServerAddress = @"http://clover.timesfun.jp:9001/";
12 12 NSString *const kUser = @"KEY_USER";
13 13 NSString *const kToken = @"KEY_TOKEN";
  14 +NSString *const kNotificationToken = @"TOKEN_INVALID";
14 15  
15 16 @implementation NSString (NSString_Extended)
16 17 - (NSString *)urlencode {
... ... @@ -60,6 +61,7 @@
60 61 return self;
61 62 }
62 63  
  64 +#pragma mark - Login and Register
63 65 // Login
64 66 - (void)loginWithEmail:(NSString *)email Password:(NSString *)password CompletionHandler: (void (^)(User *, NSString *, NSError *)) completion
65 67 {
... ... @@ -283,6 +285,80 @@
283 285 }
284 286 }
285 287  
  288 +#pragma mark - Home Screen Function
  289 +- (void)requestTopWithMode:(int)mode andDate:(NSString *)date CompletionHandler:(void (^)(TopObject *, NSError *)) completion
  290 +{
  291 + NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
  292 + NSString *url = [kServerAddress stringByAppendingFormat:@"/api/top/%d/%@", mode, date];
  293 + [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) {
  294 +
  295 + if (completion == NULL) {
  296 + return ;
  297 + }
  298 +
  299 + if (error == nil)
  300 + {
  301 + NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error];
  302 + int status = [dataResult[@"status"] intValue];
  303 + if (status == 1) { // status = 1 success
  304 + if(dataResult[@"result"] != nil) {
  305 + NSDictionary * dictResult = dataResult[@"result"];
  306 + TopObject *topObject = [[TopObject alloc] init];
  307 + TargetInfor *targetInfor = [[TargetInfor alloc] initWithData:dictResult[@"targetInf"]];
  308 + topObject.targetInfor = targetInfor;
  309 + topObject.kcal = [[NSString stringWithFormat:@"%@", dictResult[@"kcal"]] intValue];
  310 + topObject.distance = [[NSString stringWithFormat:@"%@", dictResult[@"distance"]] floatValue];
  311 + topObject.time = [NSString stringWithFormat:@"%@", dictResult[@"time"]];
  312 + NSMutableArray *arrayStep = [[NSMutableArray alloc] init];
  313 + NSArray *array = dictResult[@"dataChart"];
  314 + for(NSDictionary *dict in array) {
  315 + StepObject *object = [[StepObject alloc] init];
  316 + if([dict objectForKey:@"numStep"] != nil) {
  317 + object.step = [dict[@"numStep"] intValue];
  318 + }
  319 + if([dict objectForKey:@"hour"] != nil) {
  320 + object.hour = [dict[@"hour"] intValue];
  321 + }
  322 + switch (mode) {
  323 + case 1:
  324 + object.typeStep = @"walking";
  325 + break;
  326 + case 2:
  327 + object.typeStep = @"running";
  328 + break;
  329 + case 3:
  330 + object.typeStep = @"bike";
  331 + break;
  332 + default:
  333 + break;
  334 + }
  335 + [arrayStep addObject:object];
  336 + }
  337 + topObject.dataChart = [[NSMutableArray alloc] initWithArray:arrayStep];
  338 + completion(topObject, nil);
  339 + }
  340 + else {
  341 + NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Unknown Error"}];
  342 + completion(nil, errorObject);
  343 + }
  344 + }
  345 + else {
  346 + NSString *message = dataResult[@"message"];
  347 + if (message == nil) {
  348 + message = @"Unknown error";
  349 + }
  350 + NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}];
  351 + completion(nil, errorObject);
  352 + [self checkToken:message];
  353 + }
  354 + }
  355 + else
  356 + {
  357 + completion(nil, error);
  358 + }
  359 + }];
  360 +}
  361 +
286 362 #pragma mark - History Screen Function
287 363 - (void) requestHistory:(NSString *)token atDate:(NSDate *)date withType:(int)type andMode:(int) mode CompletionHandler:(void (^)(HistoryObject *, NSError *)) completion {
288 364 NSString *url = [kServerAddress stringByAppendingFormat:@"/api/history/%@/%d", [self convertIntToString:type], mode];
... ... @@ -309,6 +385,7 @@
309 385 }
310 386 NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}];
311 387 completion(nil, errorObject);
  388 + [self checkToken:message];
312 389 }
313 390 }
314 391 else
... ... @@ -343,6 +420,7 @@
343 420 }
344 421 NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}];
345 422 completion(nil, errorObject);
  423 + [self checkToken:message];
346 424 }
347 425 }
348 426 else
... ... @@ -388,6 +466,7 @@
388 466 }
389 467 NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}];
390 468 completion(nil, errorObject);
  469 + [self checkToken:message];
391 470 }
392 471 }
393 472 else
... ... @@ -434,6 +513,7 @@
434 513 NSString *message = dataResult[@"message"];
435 514 NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}];
436 515 completion(nil, errorObject);
  516 + [self checkToken:message];
437 517 }
438 518 }
439 519 else
... ... @@ -490,6 +570,15 @@
490 570 }
491 571  
492 572 #pragma mark - Private Function
  573 +- (BOOL) checkToken:(NSString *)message {
  574 + if ([message isEqualToString:@"Token is invalid"]) {
  575 + [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationToken object:nil];
  576 + return YES;
  577 + }
  578 + else {
  579 + return NO;
  580 + }
  581 +}
493 582 - (NSData *) _encodeDictionary: (NSDictionary *) dictionary
494 583 {
495 584 NSMutableArray *parts = [[NSMutableArray alloc] init];
LifeLog/LifeLog/StepObject.h
  1 +//
  2 +// StepObject.h
  3 +// LifeLog
  4 +//
  5 +// Created by Nguyen Van Phong on 8/11/17.
  6 +// Copyright © 2017 PhongNV. All rights reserved.
  7 +//
  8 +
  9 +#import <Foundation/Foundation.h>
  10 +
  11 +@interface StepObject : NSObject
  12 +@property (nonatomic) int step;
  13 +@property (nonatomic, strong) NSString *typeStep;
  14 +@property (nonatomic) int hour;
  15 +@end
LifeLog/LifeLog/StepObject.m
  1 +//
  2 +// StepObject.m
  3 +// LifeLog
  4 +//
  5 +// Created by Nguyen Van Phong on 8/11/17.
  6 +// Copyright © 2017 PhongNV. All rights reserved.
  7 +//
  8 +
  9 +#import "StepObject.h"
  10 +
  11 +@implementation StepObject
  12 +
  13 +@end
LifeLog/LifeLog/TargetInfor.h
  1 +//
  2 +// TargetInfor.h
  3 +// LifeLog
  4 +//
  5 +// Created by Nguyen Van Phong on 8/13/17.
  6 +// Copyright © 2017 PhongNV. All rights reserved.
  7 +//
  8 +
  9 +#import <Foundation/Foundation.h>
  10 +
  11 +@interface TargetInfor : NSObject
  12 +@property (nonatomic) int target_step;
  13 +@property (nonatomic) int num_step;
  14 +@property (nonatomic) int remaining_step;
  15 +@property (nonatomic) float complete_percent;
  16 +-(id) initWithData : (NSDictionary *) dict;
  17 +@end
LifeLog/LifeLog/TargetInfor.m
  1 +//
  2 +// TargetInfor.m
  3 +// LifeLog
  4 +//
  5 +// Created by Nguyen Van Phong on 8/13/17.
  6 +// Copyright © 2017 PhongNV. All rights reserved.
  7 +//
  8 +
  9 +#import "TargetInfor.h"
  10 +
  11 +@implementation TargetInfor
  12 +-(id) initWithData : (NSDictionary *) dict {
  13 + if([dict objectForKey:@"target_step"] != nil) {
  14 + self.target_step = [dict[@"target_step"] intValue];
  15 + }
  16 + if([dict objectForKey:@"num_step"] != nil) {
  17 + self.num_step = [dict[@"num_step"] intValue];
  18 + }
  19 + if([dict objectForKey:@"remaining_step"] != nil) {
  20 + self.remaining_step = [dict[@"remaining_step"] intValue];
  21 + }
  22 + if([dict objectForKey:@"complete_percent"] != nil) {
  23 + self.complete_percent = [dict[@"complete_percent"] floatValue];
  24 + }
  25 + return self;
  26 +}
  27 +@end
LifeLog/LifeLog/TopObject.h
  1 +//
  2 +// TopObject.h
  3 +// LifeLog
  4 +//
  5 +// Created by Nguyen Van Phong on 8/13/17.
  6 +// Copyright © 2017 PhongNV. All rights reserved.
  7 +//
  8 +
  9 +#import <Foundation/Foundation.h>
  10 +#import "StepObject.h"
  11 +#import "TargetInfor.h"
  12 +
  13 +@interface TopObject : NSObject
  14 +@property (nonatomic) int kcal;
  15 +@property (nonatomic) float distance;
  16 +@property (nonatomic, strong) NSString *time;
  17 +@property (nonatomic, strong) TargetInfor *targetInfor;
  18 +@property (nonatomic, strong) NSMutableArray *dataChart;
  19 +@end
LifeLog/LifeLog/TopObject.m
  1 +//
  2 +// TopObject.m
  3 +// LifeLog
  4 +//
  5 +// Created by Nguyen Van Phong on 8/13/17.
  6 +// Copyright © 2017 PhongNV. All rights reserved.
  7 +//
  8 +
  9 +#import "TopObject.h"
  10 +
  11 +@implementation TopObject
  12 +
  13 +@end