Commit 168c4f04021e6d3b17dc227ad079bd80117b41b8

Authored by nvtu11790@gmail.com
1 parent e759cbb03c

Update API history and change some object

Showing 12 changed files with 112 additions and 52 deletions Side-by-side Diff

LifeLog/LifeLog/GroupDetailViewController.m
... ... @@ -172,7 +172,7 @@
172 172 else {
173 173 [cell.imgAvatar setImage:[UIImage imageNamed:@"avatar_default"]];
174 174 }
175   - cell.lblDateTime.text = [Utilities stringFromDate:object.createDate withFormat:@"YYYY/MM/dd hh:mm"];
  175 + cell.lblDateTime.text = [Utilities stringFromDate:object.createDate withFormat:@"YYYY/MM/dd hh:mm" locale:@""];
176 176 cell.lblUsername.text = object.userName;
177 177 cell.lblDes.text = object.content;
178 178 cell.lblMode.text = object.mode;
LifeLog/LifeLog/HistoryObject.h
... ... @@ -18,6 +18,7 @@
18 18 @property (nonatomic) float calories;
19 19 @property (nonatomic, strong) NSString * time;
20 20 @property (nonatomic, strong) NSDate *date;
  21 +@property (nonatomic, strong) NSMutableArray *dataGraph;
21 22  
22 23 -(id) initWithData : (NSDictionary *) dict;
23 24  
LifeLog/LifeLog/HistoryObject.m
... ... @@ -11,17 +11,17 @@
11 11 @implementation HistoryObject
12 12  
13 13 -(id) initWithData : (NSDictionary *) dict {
14   - if([dict objectForKey:@"step"] != nil) {
  14 + if([dict objectForKey:@"steps"] != nil) {
15 15 self.step = [dict[@"step"] intValue];
16 16 }
17 17 if([dict objectForKey:@"target"] != nil) {
18 18 self.target = [dict[@"target"] intValue];
19 19 }
20   - if([dict objectForKey:@"missing"] != nil) {
21   - self.missing = [dict[@"missing"] intValue];
  20 + if([dict objectForKey:@"step_remain"] != nil) {
  21 + self.missing = [dict[@"step_remain"] intValue];
22 22 }
23   - if([dict objectForKey:@"percent"] != nil) {
24   - self.percent = [dict[@"percent"] floatValue];
  23 + if([dict objectForKey:@"complete_percent"] != nil) {
  24 + self.percent = [dict[@"complete_percent"] floatValue];
25 25 }
26 26 if([dict objectForKey:@"distance"] != nil) {
27 27 if([dict[@"distance"] isKindOfClass:[NSString class]]) {
... ... @@ -51,6 +51,15 @@
51 51 }
52 52 else {
53 53 self.date = [NSDate date];
  54 + }
  55 + if([dict objectForKey:@"data_chart"] != nil) {
  56 + self.dataGraph = [[NSMutableArray alloc] init];
  57 + NSDictionary * graph = [dict objectForKey:@"data_chart"];
  58 + if([graph count] == 24) {
  59 + for(int i = 0; i < 24; i++) {
  60 + [self.dataGraph addObject:[graph objectForKey:[NSString stringWithFormat:@"%d", i]]];
  61 + }
  62 + }
54 63 }
55 64 return self;
56 65 }
LifeLog/LifeLog/HistoryViewController.h
... ... @@ -15,10 +15,11 @@
15 15 #import "Entities.h"
16 16  
17 17 @interface HistoryViewController : BaseTableViewController <UIScrollViewDelegate> {
18   - NSDate * _curDate;
19   - HistoryObject * _curHisObj;
20   - HistoryGraphObject * _curHisGraphObj;
  18 + NSDate * _startDate;
  19 + NSDate * _endDate;
  20 + NSArray * _curHisArray;
21 21 }
  22 +@property (weak, nonatomic) IBOutlet UILabel *lblDatetime;
22 23  
23 24 @property (weak, nonatomic) IBOutlet UILabel *lblCircleStep;
24 25 @property (weak, nonatomic) IBOutlet UILabel *lblCircleRemain;
... ... @@ -39,6 +40,8 @@
39 40 @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
40 41  
41 42 - (IBAction)swipeAction:(UISwipeGestureRecognizer *)sender;
  43 +- (IBAction)clickBackward:(UIButton *)sender;
  44 +- (IBAction)clickForward:(UIButton *)sender;
42 45  
43 46 @end
LifeLog/LifeLog/HistoryViewController.m
... ... @@ -26,8 +26,11 @@
26 26 [self setupView];
27 27 [self setupChartView];
28 28  
29   - _curDate = [NSDate date];
  29 + _startDate = [NSDate date];
  30 + _endDate = _startDate;
30 31  
  32 + self.lblDatetime.text = [Utilities stringFromDate:_endDate withFormat:@"YYYYๅนดMMๆœˆddๆ—ฅ EEEE" locale:@"ja_JP"];
  33 +
31 34 [self checkRequestData];
32 35  
33 36 //register nib for table view
... ... @@ -54,7 +57,7 @@
54 57 [self.viewCollectionType setSpacing:2];
55 58 [self.viewCollectionType setArrayTitle:typeTitle];
56 59 self.viewCollectionType.changeCurrentIndex = ^(int index){
57   - [self checkRequestData];
  60 + [self changeDate];
58 61 };
59 62  
60 63 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 @@
64 67 [self.viewCollectionMode setNormalColor:[Utilities convertHecToColor:0x191919] highlightColor:[Utilities convertHecToColor:0x474747] textColor:[UIColor whiteColor]];
65 68 [self.viewCollectionMode setArrayTitle:modeTitle];
66 69 self.viewCollectionMode.changeCurrentIndex = ^(int index){
67   - [self checkRequestData];
  70 + [self updateView];
68 71 };
69 72  
70 73 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];
71 74  
72 75  
73 76  
... ... @@ -102,19 +105,23 @@
102 105 }
103 106  
104 107 -(void) updateView {
105   - self.lblStep.text = [NSString stringWithFormat:@"%d", _curHisObj.step];
106   - self.lblRemaining.text = [NSString stringWithFormat:@"%d", _curHisObj.missing];
107   - self.lblPercent.text = [NSString stringWithFormat:@"%0.2f", _curHisObj.percent * 100];
108   - self.lblCalories.text = [NSString stringWithFormat:@"%0.2f", _curHisObj.calories];
109   - self.lblDistance.text = [NSString stringWithFormat:@"%0.1f", _curHisObj.distance];
110   - self.lblTime.text = _curHisObj.time;
  108 + HistoryObject * obj = [_curHisArray objectAtIndex:self.viewCollectionMode.getCurrentIndex];
  109 + self.lblStep.text = [NSString stringWithFormat:@"%d", obj.step];
  110 + self.lblRemaining.text = [NSString stringWithFormat:@"%d", obj.missing];
  111 + self.lblPercent.text = [NSString stringWithFormat:@"%0.2f", obj.percent * 100];
  112 + self.lblCalories.text = [NSString stringWithFormat:@"%0.2f", obj.calories];
  113 + self.lblDistance.text = [NSString stringWithFormat:@"%0.1f", obj.distance];
  114 + self.lblTime.text = obj.time;
  115 + [self updateGraphView];
111 116 }
112 117  
113 118 -(void) updateGraphView {
  119 + HistoryObject * obj = [_curHisArray objectAtIndex:self.viewCollectionMode.getCurrentIndex];
  120 +
114 121 NSMutableArray *yVals = [[NSMutableArray alloc] init];
115   - for (int i = 0; i < _curHisGraphObj.arrayStep.count; i++)
  122 + for (int i = 0; i < obj.dataGraph.count; i++)
116 123 {
117   - [yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:[[_curHisGraphObj.arrayStep objectAtIndex:i] doubleValue]]];
  124 + [yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:[[obj.dataGraph objectAtIndex:i] doubleValue]]];
118 125 }
119 126  
120 127 BarChartDataSet *set1 = nil;
121 128  
... ... @@ -143,10 +150,8 @@
143 150 -(void) checkRequestData {
144 151 if(self.tableBase.alpha == 0.0) {
145 152 NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
146   - int type = self.viewCollectionType.getCurrentIndex;
147   - int mode = self.viewCollectionMode.getCurrentIndex + 1;
148 153 MBProgressHUD *hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
149   - [[ServerAPI server] requestHistory:token atDate:_curDate withType:type andMode:mode CompletionHandler:^(HistoryObject *object, NSError *error) {
  154 + [[ServerAPI server] requestHistory:token startDate:_startDate endDate:_endDate CompletionHandler:^(NSArray *array, NSError *error) {
150 155 HistoryViewController __weak *weakSelf = self;
151 156 dispatch_async(dispatch_get_main_queue(), ^{
152 157 if(hudView != nil) {
... ... @@ -154,7 +159,7 @@
154 159 }
155 160 });
156 161 if(error == nil) {
157   - _curHisObj = object;
  162 + _curHisArray = array;
158 163 dispatch_async(dispatch_get_main_queue(), ^{
159 164 [weakSelf updateView];
160 165 });
... ... @@ -166,22 +171,6 @@
166 171 });
167 172 }
168 173 }];
169   -
170   - [[ServerAPI server] requestHistoryGraph:token withType:type andMode:mode CompletionHandler:^(HistoryGraphObject *object, NSError *error) {
171   - HistoryViewController __weak *weakSelf = self;
172   - if(error == nil) {
173   - _curHisGraphObj = object;
174   - dispatch_async(dispatch_get_main_queue(), ^{
175   - [self updateGraphView];
176   - });
177   - }
178   - else {
179   - dispatch_async(dispatch_get_main_queue(), ^{
180   - NSString *message = [error.userInfo objectForKey:@"message"];
181   - [Utilities showErrorMessage:message withViewController:weakSelf];
182   - });
183   - }
184   - }];
185 174 }
186 175 else {
187 176 [self resetData];
... ... @@ -209,6 +198,27 @@
209 198 }];
210 199 }
211 200  
  201 +- (void) changeDate {
  202 + switch (self.viewCollectionType.getCurrentIndex) {
  203 + case 1:
  204 + _startDate = [_endDate dateByAddingTimeInterval:-86400 * 7];
  205 + break;
  206 + case 2:
  207 + _startDate = [_endDate dateByAddingTimeInterval:-86400 * 30];
  208 + break;
  209 + case 3:
  210 + _startDate = [_endDate dateByAddingTimeInterval:-86400 * 30 * 3];
  211 + break;
  212 + case 4:
  213 + _startDate = [_endDate dateByAddingTimeInterval:-86400 * 30 * 6];
  214 + break;
  215 + default:
  216 + _startDate = _endDate;
  217 + break;
  218 + }
  219 + [self checkRequestData];
  220 +}
  221 +
212 222 #pragma mark IBAction
213 223 -(void) swipeAction:(UISwipeGestureRecognizer *)sender {
214 224 bool alphaValue = self.scrollView.alpha == 1.0 ? 1.0 : 0.0;
... ... @@ -218,6 +228,18 @@
218 228 } completion:^(BOOL completed) {
219 229 [self checkRequestData];
220 230 }];
  231 +}
  232 +
  233 +- (IBAction)clickBackward:(UIButton *)sender {
  234 + _endDate = [_endDate dateByAddingTimeInterval:-86400];
  235 + self.lblDatetime.text = [Utilities stringFromDate:_endDate withFormat:@"YYYYๅนดMMๆœˆddๆ—ฅ EEEE" locale:@"ja_JP"];
  236 + [self changeDate];
  237 +}
  238 +
  239 +- (IBAction)clickForward:(UIButton *)sender {
  240 + _endDate = [_endDate dateByAddingTimeInterval:86400];
  241 + self.lblDatetime.text = [Utilities stringFromDate:_endDate withFormat:@"YYYYๅนดMMๆœˆddๆ—ฅ EEEE" locale:@"ja_JP"];
  242 + [self changeDate];
221 243 }
222 244  
223 245 -(void)gestureAction:(UITapGestureRecognizer *) sender
LifeLog/LifeLog/HistoryViewController.xib
1 1 <?xml version="1.0" encoding="UTF-8"?>
2   -<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="12121" systemVersion="16A323" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
  2 +<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="12118" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
3 3 <device id="retina4_7" orientation="portrait">
4 4 <adaptation id="fullscreen"/>
5 5 </device>
6 6 <dependencies>
7   - <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12089"/>
  7 + <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12086"/>
8 8 <capability name="Aspect ratio constraints" minToolsVersion="5.1"/>
9 9 <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
10 10 </dependencies>
... ... @@ -14,6 +14,7 @@
14 14 <outlet property="lblCalories" destination="dAE-C8-QLr" id="WaS-S3-Qxh"/>
15 15 <outlet property="lblCircleRemain" destination="Kr7-S0-Fpl" id="UQe-Bt-i0X"/>
16 16 <outlet property="lblCircleStep" destination="oWg-A8-aCr" id="Q9g-UY-hyv"/>
  17 + <outlet property="lblDatetime" destination="EM7-vA-s1e" id="0fK-4u-TaK"/>
17 18 <outlet property="lblDistance" destination="1tR-JC-pyw" id="q4s-Ru-vLO"/>
18 19 <outlet property="lblPercent" destination="8Ru-Jc-Ouv" id="ZHn-Kt-0Qk"/>
19 20 <outlet property="lblRemaining" destination="1NH-b3-ST8" id="hFc-q9-b0h"/>
20 21  
... ... @@ -84,11 +85,17 @@
84 85 <constraint firstAttribute="width" secondItem="6T2-M9-9wV" secondAttribute="height" multiplier="1:1" id="RlM-6u-eer"/>
85 86 </constraints>
86 87 <state key="normal" backgroundImage="arrow_back"/>
  88 + <connections>
  89 + <action selector="clickBackward:" destination="-1" eventType="touchUpInside" id="pwN-yT-qyp"/>
  90 + </connections>
87 91 </button>
88 92 <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Cwb-8M-pDi">
89 93 <rect key="frame" x="320" y="0.0" width="35" height="35"/>
90 94 <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
91 95 <state key="normal" backgroundImage="arrow_next"/>
  96 + <connections>
  97 + <action selector="clickForward:" destination="-1" eventType="touchUpInside" id="1NC-km-Lfe"/>
  98 + </connections>
92 99 </button>
93 100 <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="2017ๅนด1ๆœˆ1ๆ—ฅใ€€ๆ—ฅๆ›œๆ—ฅ" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="EM7-vA-s1e">
94 101 <rect key="frame" x="40" y="2" width="275" height="31"/>
LifeLog/LifeLog/MyGroupViewController.m
... ... @@ -106,7 +106,7 @@
106 106 else {
107 107 [cell.imgAvatar setImage:[UIImage imageNamed:@"avatar_default"]];
108 108 }
109   - cell.lblDateTime.text = [Utilities stringFromDate:object.createDate withFormat:@"YYYY/MM/dd hh:mm"];
  109 + cell.lblDateTime.text = [Utilities stringFromDate:object.createDate withFormat:@"YYYY/MM/dd hh:mm" locale:@""];
110 110 cell.lblUsername.text = object.userName;
111 111 cell.lblDes.text = object.content;
112 112 cell.lblMode.text = object.mode;
LifeLog/LifeLog/SNSViewController.m
... ... @@ -61,7 +61,7 @@
61 61 else {
62 62 [cell.imgAvatar setImage:[UIImage imageNamed:@"avatar_default"]];
63 63 }
64   - cell.lblDateTime.text = [Utilities stringFromDate:object.createDate withFormat:@"YYYY/MM/dd hh:mm"];
  64 + cell.lblDateTime.text = [Utilities stringFromDate:object.createDate withFormat:@"YYYY/MM/dd hh:mm" locale:@""];
65 65 cell.lblUsername.text = object.userName;
66 66 cell.lblDes.text = object.content;
67 67 cell.lblMode.text = object.mode;
LifeLog/LifeLog/ServerAPI.h
... ... @@ -30,7 +30,7 @@
30 30 - (void)requestCreateLog:(int)mode withStep:(int)numStep startDate:(NSString *)startDate endDate:(NSString *)endDate CompletionHandler:(void (^)(NSError *))completion;
31 31  
32 32 #pragma mark - History Screen Function
33   -- (void) requestHistory:(NSString *)token atDate:(NSDate *)date withType:(int)type andMode:(int) mode CompletionHandler:(void (^)(HistoryObject *, NSError *)) completion;
  33 +- (void) requestHistory:(NSString *)token startDate:(NSDate *)startDate endDate:(NSDate *)endDate CompletionHandler:(void (^)(NSArray *, NSError *)) completion;
34 34 - (void) requestHistoryGraph:(NSString *)token withType:(int)type andMode:(int) mode CompletionHandler:(void (^)(HistoryGraphObject *, NSError *)) completion;
35 35 - (void) requestHistoryList:(NSString *)token withType:(int)type andMode:(int) mode AtPage:(int) page CompletionHandler:(void (^)(NSArray *, NSError *)) completion;
36 36 - (void) requestJoinGroup:(NSString *)token groupID: (int) groupID CompletionHandler:(void (^)(NSError *)) completion;
LifeLog/LifeLog/ServerAPI.m
... ... @@ -7,6 +7,7 @@
7 7 //
8 8  
9 9 #import "ServerAPI.h"
  10 +#import "Utilities.h"
10 11  
11 12 NSString *const kServerAddress = @"http://clover.timesfun.jp:9001/";
12 13 NSString *const kUser = @"KEY_USER";
... ... @@ -406,8 +407,8 @@
406 407 }
407 408  
408 409 #pragma mark - History Screen Function
409   -- (void) requestHistory:(NSString *)token atDate:(NSDate *)date withType:(int)type andMode:(int) mode CompletionHandler:(void (^)(HistoryObject *, NSError *)) completion {
410   - NSString *url = [kServerAddress stringByAppendingFormat:@"/api/history/%@/%d", [self convertIntToString:type], mode];
  410 +- (void) requestHistory:(NSString *)token startDate:(NSDate *)startDate endDate:(NSDate *)endDate CompletionHandler:(void (^)(NSArray *, NSError *)) completion {
  411 + NSString *url = [kServerAddress stringByAppendingFormat:@"/api/history/%@/%@", [Utilities stringFromDate:startDate withFormat:@"YYYYMMdd" locale:@""], [Utilities stringFromDate:endDate withFormat:@"YYYYMMdd" locale:@""]];
411 412 NSLog(@"requestHistory link %@", url);
412 413 [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) {
413 414  
... ... @@ -421,8 +422,22 @@
421 422 NSLog(@"%@", dataResult);
422 423 int status = [dataResult[@"status"] intValue];
423 424 if (status == 1) { // status = 1 success
424   - HistoryObject * object = [[HistoryObject alloc] initWithData:dataResult[@"result"]];
425   - completion(object, nil);
  425 + NSMutableArray * arrayHistory = [[NSMutableArray alloc] init];
  426 + NSDictionary * arrResult = dataResult[@"result"];
  427 + if(arrResult != nil) {
  428 + NSArray *arrayKey = [NSArray arrayWithObjects:@"mode_1", @"mode_2", @"mode_3", nil];
  429 + for(NSString * key in arrayKey) {
  430 + NSDictionary *mode = [arrResult objectForKey:key];
  431 + if(mode.count > 0) {
  432 + HistoryObject * objectMode = [[HistoryObject alloc] initWithData:mode];
  433 + [arrayHistory addObject:objectMode];
  434 + }
  435 + else {
  436 + [arrayHistory addObject:[[HistoryObject alloc] init]];
  437 + }
  438 + }
  439 + }
  440 + completion(arrayHistory, nil);
426 441 }
427 442 else {
428 443 NSString *message = dataResult[@"message"];
... ... @@ -433,7 +448,7 @@
433 448 if ([message isEqualToString:@"Token is invalid"]) {
434 449 [self performSelectorOnMainThread:@selector(checkToken) withObject:nil waitUntilDone:YES];
435 450 NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken];
436   - [self requestHistory:tokenNew atDate:date withType:type andMode:mode CompletionHandler:completion];
  451 + [self requestHistory:tokenNew startDate:startDate endDate:endDate CompletionHandler:completion];
437 452 }
438 453 else {
439 454 NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}];
LifeLog/LifeLog/Utilities.h
... ... @@ -24,6 +24,6 @@
24 24  
25 25 //convert date time
26 26 + (NSDate *) dateFromString : (NSString *) dateString withFormat: (NSString *) format;
27   -+ (NSString *) stringFromDate : (NSDate *) date withFormat: (NSString *) format;
  27 ++ (NSString *) stringFromDate : (NSDate *) date withFormat: (NSString *) format locale:(NSString *) locale;
28 28 @end
LifeLog/LifeLog/Utilities.m
... ... @@ -157,9 +157,12 @@
157 157 return date;
158 158 }
159 159  
160   -+ (NSString *) stringFromDate : (NSDate *) date withFormat: (NSString *) format {
  160 ++ (NSString *) stringFromDate : (NSDate *) date withFormat: (NSString *) format locale:(NSString *) locale {
161 161 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
162 162 [dateFormat setDateFormat:format];
  163 + if(![locale isEqual: @""]) {
  164 + [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:locale]];
  165 + }
163 166 NSString *dateString = [dateFormat stringFromDate:date];
164 167 return dateString;
165 168 }