From 168c4f04021e6d3b17dc227ad079bd80117b41b8 Mon Sep 17 00:00:00 2001 From: nvtu Date: Mon, 21 Aug 2017 01:45:06 +0700 Subject: [PATCH] Update API history and change some object --- LifeLog/LifeLog/GroupDetailViewController.m | 2 +- LifeLog/LifeLog/HistoryObject.h | 1 + LifeLog/LifeLog/HistoryObject.m | 19 +++++-- LifeLog/LifeLog/HistoryViewController.h | 9 ++-- LifeLog/LifeLog/HistoryViewController.m | 84 ++++++++++++++++++----------- LifeLog/LifeLog/HistoryViewController.xib | 11 +++- LifeLog/LifeLog/MyGroupViewController.m | 2 +- LifeLog/LifeLog/SNSViewController.m | 2 +- LifeLog/LifeLog/ServerAPI.h | 2 +- LifeLog/LifeLog/ServerAPI.m | 25 +++++++-- LifeLog/LifeLog/Utilities.h | 2 +- LifeLog/LifeLog/Utilities.m | 5 +- 12 files changed, 112 insertions(+), 52 deletions(-) diff --git a/LifeLog/LifeLog/GroupDetailViewController.m b/LifeLog/LifeLog/GroupDetailViewController.m index d092294..50529a3 100644 --- a/LifeLog/LifeLog/GroupDetailViewController.m +++ b/LifeLog/LifeLog/GroupDetailViewController.m @@ -172,7 +172,7 @@ else { [cell.imgAvatar setImage:[UIImage imageNamed:@"avatar_default"]]; } - cell.lblDateTime.text = [Utilities stringFromDate:object.createDate withFormat:@"YYYY/MM/dd hh:mm"]; + cell.lblDateTime.text = [Utilities stringFromDate:object.createDate withFormat:@"YYYY/MM/dd hh:mm" locale:@""]; cell.lblUsername.text = object.userName; cell.lblDes.text = object.content; cell.lblMode.text = object.mode; diff --git a/LifeLog/LifeLog/HistoryObject.h b/LifeLog/LifeLog/HistoryObject.h index 7757cd4..b483f3f 100644 --- a/LifeLog/LifeLog/HistoryObject.h +++ b/LifeLog/LifeLog/HistoryObject.h @@ -18,6 +18,7 @@ @property (nonatomic) float calories; @property (nonatomic, strong) NSString * time; @property (nonatomic, strong) NSDate *date; +@property (nonatomic, strong) NSMutableArray *dataGraph; -(id) initWithData : (NSDictionary *) dict; diff --git a/LifeLog/LifeLog/HistoryObject.m b/LifeLog/LifeLog/HistoryObject.m index cd1ea9d..1e498af 100644 --- a/LifeLog/LifeLog/HistoryObject.m +++ b/LifeLog/LifeLog/HistoryObject.m @@ -11,17 +11,17 @@ @implementation HistoryObject -(id) initWithData : (NSDictionary *) dict { - if([dict objectForKey:@"step"] != nil) { + if([dict objectForKey:@"steps"] != nil) { self.step = [dict[@"step"] intValue]; } if([dict objectForKey:@"target"] != nil) { self.target = [dict[@"target"] intValue]; } - if([dict objectForKey:@"missing"] != nil) { - self.missing = [dict[@"missing"] intValue]; + if([dict objectForKey:@"step_remain"] != nil) { + self.missing = [dict[@"step_remain"] intValue]; } - if([dict objectForKey:@"percent"] != nil) { - self.percent = [dict[@"percent"] floatValue]; + if([dict objectForKey:@"complete_percent"] != nil) { + self.percent = [dict[@"complete_percent"] floatValue]; } if([dict objectForKey:@"distance"] != nil) { if([dict[@"distance"] isKindOfClass:[NSString class]]) { @@ -52,6 +52,15 @@ else { self.date = [NSDate date]; } + if([dict objectForKey:@"data_chart"] != nil) { + self.dataGraph = [[NSMutableArray alloc] init]; + NSDictionary * graph = [dict objectForKey:@"data_chart"]; + if([graph count] == 24) { + for(int i = 0; i < 24; i++) { + [self.dataGraph addObject:[graph objectForKey:[NSString stringWithFormat:@"%d", i]]]; + } + } + } return self; } @end diff --git a/LifeLog/LifeLog/HistoryViewController.h b/LifeLog/LifeLog/HistoryViewController.h index 62eea48..7f60d2f 100644 --- a/LifeLog/LifeLog/HistoryViewController.h +++ b/LifeLog/LifeLog/HistoryViewController.h @@ -15,10 +15,11 @@ #import "Entities.h" @interface HistoryViewController : BaseTableViewController { - NSDate * _curDate; - HistoryObject * _curHisObj; - HistoryGraphObject * _curHisGraphObj; + NSDate * _startDate; + NSDate * _endDate; + NSArray * _curHisArray; } +@property (weak, nonatomic) IBOutlet UILabel *lblDatetime; @property (weak, nonatomic) IBOutlet UILabel *lblCircleStep; @property (weak, nonatomic) IBOutlet UILabel *lblCircleRemain; @@ -39,5 +40,7 @@ @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; - (IBAction)swipeAction:(UISwipeGestureRecognizer *)sender; +- (IBAction)clickBackward:(UIButton *)sender; +- (IBAction)clickForward:(UIButton *)sender; @end diff --git a/LifeLog/LifeLog/HistoryViewController.m b/LifeLog/LifeLog/HistoryViewController.m index eb51035..ed96bf0 100644 --- a/LifeLog/LifeLog/HistoryViewController.m +++ b/LifeLog/LifeLog/HistoryViewController.m @@ -26,7 +26,10 @@ [self setupView]; [self setupChartView]; - _curDate = [NSDate date]; + _startDate = [NSDate date]; + _endDate = _startDate; + + self.lblDatetime.text = [Utilities stringFromDate:_endDate withFormat:@"YYYY年MM月dd日 EEEE" locale:@"ja_JP"]; [self checkRequestData]; @@ -54,7 +57,7 @@ [self.viewCollectionType setSpacing:2]; [self.viewCollectionType setArrayTitle:typeTitle]; self.viewCollectionType.changeCurrentIndex = ^(int index){ - [self checkRequestData]; + [self changeDate]; }; NSArray *modeTitle = [NSArray arrayWithObjects:NSLocalizedString(@"lifelog.history.mode.1", nil), NSLocalizedString(@"lifelog.history.mode.2", nil), NSLocalizedString(@"lifelog.history.mode.3", nil), nil]; @@ -64,7 +67,7 @@ [self.viewCollectionMode setNormalColor:[Utilities convertHecToColor:0x191919] highlightColor:[Utilities convertHecToColor:0x474747] textColor:[UIColor whiteColor]]; [self.viewCollectionMode setArrayTitle:modeTitle]; self.viewCollectionMode.changeCurrentIndex = ^(int index){ - [self checkRequestData]; + [self updateView]; }; NSArray *shareTitle = [NSArray arrayWithObjects:NSLocalizedString(@"lifelog.history.share.1", nil), NSLocalizedString(@"lifelog.history.share.2", nil), NSLocalizedString(@"lifelog.history.share.3", nil), NSLocalizedString(@"lifelog.history.share.4", nil), NSLocalizedString(@"lifelog.history.share.5", nil), nil]; @@ -102,19 +105,23 @@ } -(void) updateView { - self.lblStep.text = [NSString stringWithFormat:@"%d", _curHisObj.step]; - self.lblRemaining.text = [NSString stringWithFormat:@"%d", _curHisObj.missing]; - self.lblPercent.text = [NSString stringWithFormat:@"%0.2f", _curHisObj.percent * 100]; - self.lblCalories.text = [NSString stringWithFormat:@"%0.2f", _curHisObj.calories]; - self.lblDistance.text = [NSString stringWithFormat:@"%0.1f", _curHisObj.distance]; - self.lblTime.text = _curHisObj.time; + HistoryObject * obj = [_curHisArray objectAtIndex:self.viewCollectionMode.getCurrentIndex]; + self.lblStep.text = [NSString stringWithFormat:@"%d", obj.step]; + self.lblRemaining.text = [NSString stringWithFormat:@"%d", obj.missing]; + self.lblPercent.text = [NSString stringWithFormat:@"%0.2f", obj.percent * 100]; + self.lblCalories.text = [NSString stringWithFormat:@"%0.2f", obj.calories]; + self.lblDistance.text = [NSString stringWithFormat:@"%0.1f", obj.distance]; + self.lblTime.text = obj.time; + [self updateGraphView]; } -(void) updateGraphView { + HistoryObject * obj = [_curHisArray objectAtIndex:self.viewCollectionMode.getCurrentIndex]; + NSMutableArray *yVals = [[NSMutableArray alloc] init]; - for (int i = 0; i < _curHisGraphObj.arrayStep.count; i++) + for (int i = 0; i < obj.dataGraph.count; i++) { - [yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:[[_curHisGraphObj.arrayStep objectAtIndex:i] doubleValue]]]; + [yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:[[obj.dataGraph objectAtIndex:i] doubleValue]]]; } BarChartDataSet *set1 = nil; @@ -143,10 +150,8 @@ -(void) checkRequestData { if(self.tableBase.alpha == 0.0) { NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken]; - int type = self.viewCollectionType.getCurrentIndex; - int mode = self.viewCollectionMode.getCurrentIndex + 1; MBProgressHUD *hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true]; - [[ServerAPI server] requestHistory:token atDate:_curDate withType:type andMode:mode CompletionHandler:^(HistoryObject *object, NSError *error) { + [[ServerAPI server] requestHistory:token startDate:_startDate endDate:_endDate CompletionHandler:^(NSArray *array, NSError *error) { HistoryViewController __weak *weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ if(hudView != nil) { @@ -154,7 +159,7 @@ } }); if(error == nil) { - _curHisObj = object; + _curHisArray = array; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf updateView]; }); @@ -166,22 +171,6 @@ }); } }]; - - [[ServerAPI server] requestHistoryGraph:token withType:type andMode:mode CompletionHandler:^(HistoryGraphObject *object, NSError *error) { - HistoryViewController __weak *weakSelf = self; - if(error == nil) { - _curHisGraphObj = object; - dispatch_async(dispatch_get_main_queue(), ^{ - [self updateGraphView]; - }); - } - else { - dispatch_async(dispatch_get_main_queue(), ^{ - NSString *message = [error.userInfo objectForKey:@"message"]; - [Utilities showErrorMessage:message withViewController:weakSelf]; - }); - } - }]; } else { [self resetData]; @@ -209,6 +198,27 @@ }]; } +- (void) changeDate { + switch (self.viewCollectionType.getCurrentIndex) { + case 1: + _startDate = [_endDate dateByAddingTimeInterval:-86400 * 7]; + break; + case 2: + _startDate = [_endDate dateByAddingTimeInterval:-86400 * 30]; + break; + case 3: + _startDate = [_endDate dateByAddingTimeInterval:-86400 * 30 * 3]; + break; + case 4: + _startDate = [_endDate dateByAddingTimeInterval:-86400 * 30 * 6]; + break; + default: + _startDate = _endDate; + break; + } + [self checkRequestData]; +} + #pragma mark IBAction -(void) swipeAction:(UISwipeGestureRecognizer *)sender { bool alphaValue = self.scrollView.alpha == 1.0 ? 1.0 : 0.0; @@ -220,6 +230,18 @@ }]; } +- (IBAction)clickBackward:(UIButton *)sender { + _endDate = [_endDate dateByAddingTimeInterval:-86400]; + self.lblDatetime.text = [Utilities stringFromDate:_endDate withFormat:@"YYYY年MM月dd日 EEEE" locale:@"ja_JP"]; + [self changeDate]; +} + +- (IBAction)clickForward:(UIButton *)sender { + _endDate = [_endDate dateByAddingTimeInterval:86400]; + self.lblDatetime.text = [Utilities stringFromDate:_endDate withFormat:@"YYYY年MM月dd日 EEEE" locale:@"ja_JP"]; + [self changeDate]; +} + -(void)gestureAction:(UITapGestureRecognizer *) sender { CGPoint touchLocation = [sender locationOfTouch:0 inView:self.viewCollectionShare]; diff --git a/LifeLog/LifeLog/HistoryViewController.xib b/LifeLog/LifeLog/HistoryViewController.xib index 79a83d1..70589f6 100644 --- a/LifeLog/LifeLog/HistoryViewController.xib +++ b/LifeLog/LifeLog/HistoryViewController.xib @@ -1,10 +1,10 @@ - + - + @@ -14,6 +14,7 @@ + @@ -84,11 +85,17 @@ + + +