Commit 39b57938e7152a39e55db776201105a82928504a

Authored by phong
1 parent 02fd57747b
Exists in master

update api Top

Showing 3 changed files with 36 additions and 17 deletions Inline Diff

LifeLog/LifeLog/HomeViewController.m
1 // 1 //
2 // HomeViewController.m 2 // HomeViewController.m
3 // LifeLog 3 // LifeLog
4 // 4 //
5 // Created by Nguyen Van Phong on 7/25/17. 5 // Created by Nguyen Van Phong on 7/25/17.
6 // Copyright © 2017 PhongNV. All rights reserved. 6 // Copyright © 2017 PhongNV. All rights reserved.
7 // 7 //
8 8
9 #import "HomeViewController.h" 9 #import "HomeViewController.h"
10 #import "NSDate+helper.h" 10 #import "NSDate+helper.h"
11 #import <CoreMotion/CoreMotion.h> 11 #import <CoreMotion/CoreMotion.h>
12 #import "CMMotionActivityExtra.h" 12 #import "CMMotionActivityExtra.h"
13 #import "TodayViewController.h" 13 #import "TodayViewController.h"
14 #import "Utilities.h" 14 #import "Utilities.h"
15 #import <MBProgressHUD/MBProgressHUD.h> 15 #import <MBProgressHUD/MBProgressHUD.h>
16 #import "ServerAPI.h" 16 #import "ServerAPI.h"
17 17
18 static NSInteger numberTotal = 10000; 18 static NSInteger numberTotal = 10000;
19 19
20 @interface HomeViewController () 20 @interface HomeViewController ()
21 { 21 {
22 MBProgressHUD *progressHud; 22 MBProgressHUD *progressHud;
23 } 23 }
24 @property (nonatomic, weak) IBOutlet UILabel *lblTitle; 24 @property (nonatomic, weak) IBOutlet UILabel *lblTitle;
25 @property (nonatomic, weak) IBOutlet UIImageView *avatar; 25 @property (nonatomic, weak) IBOutlet UIImageView *avatar;
26 @property (nonatomic, weak) IBOutlet UILabel *lblDateCurrent; 26 @property (nonatomic, weak) IBOutlet UILabel *lblDateCurrent;
27 @property (nonatomic, weak) IBOutlet UILabel *lblValueStep; 27 @property (nonatomic, weak) IBOutlet UILabel *lblValueStep;
28 @property (nonatomic, weak) IBOutlet UILabel *lblUnitStep; 28 @property (nonatomic, weak) IBOutlet UILabel *lblUnitStep;
29 @property (nonatomic, weak) IBOutlet UILabel *lblValueStepOther; 29 @property (nonatomic, weak) IBOutlet UILabel *lblValueStepOther;
30 @property (nonatomic, weak) IBOutlet UILabel *lblPercent; 30 @property (nonatomic, weak) IBOutlet UILabel *lblPercent;
31 @property (nonatomic, weak) IBOutlet UILabel *lblNotice; 31 @property (nonatomic, weak) IBOutlet UILabel *lblNotice;
32 @property (weak, nonatomic) IBOutlet UISegmentedControl *segmentHome; 32 @property (weak, nonatomic) IBOutlet UISegmentedControl *segmentHome;
33 33
34 @property (nonatomic, strong) CMPedometer *pedometer; 34 @property (nonatomic, strong) CMPedometer *pedometer;
35 @property (nonatomic, strong) CMMotionActivityManager *motionActivityManager; 35 @property (nonatomic, strong) CMMotionActivityManager *motionActivityManager;
36 @property (nonatomic, strong) NSOperationQueue *operationQueue; 36 @property (nonatomic, strong) NSOperationQueue *operationQueue;
37 @property (nonatomic, strong) NSTimer *timer; 37 @property (nonatomic, strong) NSTimer *timer;
38 @property (nonatomic, assign) NSInteger bike; 38 @property (nonatomic, assign) NSInteger bike;
39 @property (nonatomic, assign) NSInteger walking; 39 @property (nonatomic, assign) NSInteger walking;
40 @property (nonatomic, assign) NSInteger running; 40 @property (nonatomic, assign) NSInteger running;
41 @property (nonatomic, strong) NSDate *dateCurrent; 41 @property (nonatomic, strong) NSDate *dateCurrent;
42 42
43 @property (nonatomic, assign) int totalRequest; 43 @property (nonatomic, assign) int totalRequest;
44 @property (nonatomic, assign) int countComplete; 44 @property (nonatomic, assign) int countComplete;
45 45
46 @end 46 @end
47 47
48 @implementation HomeViewController 48 @implementation HomeViewController
49 49
50 - (void)viewDidLoad { 50 - (void)viewDidLoad {
51 [super viewDidLoad]; 51 [super viewDidLoad];
52 52
53 self.lblTitle.text = NSLocalizedString(@"lifelog.home.title", nil); 53 self.lblTitle.text = NSLocalizedString(@"lifelog.home.title", nil);
54 54
55 [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.lblTitle attribute:NSLayoutAttributeTop multiplier:1 constant:0]]; 55 [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.lblTitle attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
56 56
57 self.avatar.backgroundColor = [UIColor whiteColor]; 57 self.avatar.backgroundColor = [UIColor whiteColor];
58 self.avatar.layer.borderWidth = 2.0f; 58 self.avatar.layer.borderWidth = 2.0f;
59 self.avatar.layer.borderColor = [[UIColor whiteColor] CGColor]; 59 self.avatar.layer.borderColor = [[UIColor whiteColor] CGColor];
60 self.avatar.layer.cornerRadius = self.avatar.frame.size.width/2.0f; 60 self.avatar.layer.cornerRadius = self.avatar.frame.size.width/2.0f;
61 self.avatar.layer.masksToBounds = YES; 61 self.avatar.layer.masksToBounds = YES;
62 NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kUser]; 62 NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kUser];
63 User *user = (User *)[NSKeyedUnarchiver unarchiveObjectWithData:data]; 63 User *user = (User *)[NSKeyedUnarchiver unarchiveObjectWithData:data];
64 if (user != nil) { 64 if (user != nil) {
65 NSString *linkImage = [NSString stringWithFormat:@"%@%@", kServerAddress, user.profile_image]; 65 NSString *linkImage = [NSString stringWithFormat:@"%@%@", kServerAddress, user.profile_image];
66 NSURL *urlImage = [NSURL URLWithString:linkImage]; 66 NSURL *urlImage = [NSURL URLWithString:linkImage];
67 NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; 67 NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
68 sessionConfig.timeoutIntervalForRequest = 30.0; 68 sessionConfig.timeoutIntervalForRequest = 30.0;
69 sessionConfig.timeoutIntervalForResource = 60.0; 69 sessionConfig.timeoutIntervalForResource = 60.0;
70 sessionConfig.HTTPMaximumConnectionsPerHost = 20; 70 sessionConfig.HTTPMaximumConnectionsPerHost = 20;
71 sessionConfig.allowsCellularAccess = YES; 71 sessionConfig.allowsCellularAccess = YES;
72 HomeViewController __weak *weakSelf = self; 72 HomeViewController __weak *weakSelf = self;
73 NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig]; 73 NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
74 NSURLSessionDataTask *downloadPhotoTask = [session 74 NSURLSessionDataTask *downloadPhotoTask = [session
75 dataTaskWithURL:urlImage completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 75 dataTaskWithURL:urlImage completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
76 if (weakSelf == nil) 76 if (weakSelf == nil)
77 { 77 {
78 return; 78 return;
79 } 79 }
80 if (error == nil) { 80 if (error == nil) {
81 UIImage *image = [[UIImage alloc] initWithData:data]; 81 UIImage *image = [[UIImage alloc] initWithData:data];
82 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 82 [[NSOperationQueue mainQueue] addOperationWithBlock:^{
83 weakSelf.avatar.image = image; 83 weakSelf.avatar.image = image;
84 }]; 84 }];
85 } 85 }
86 }]; 86 }];
87 [downloadPhotoTask resume]; 87 [downloadPhotoTask resume];
88 } 88 }
89 89
90 self.lblNotice.text = NSLocalizedString(@"lifelog.home.notice", nil); 90 self.lblNotice.text = NSLocalizedString(@"lifelog.home.notice", nil);
91 91
92 _dateCurrent = [NSDate date]; 92 _dateCurrent = [NSDate date];
93 self.lblDateCurrent.text = [NSString stringWithFormat:@"%ld%@%ld%@%ld%@", (long)[_dateCurrent getYear], NSLocalizedString(@"lifelog.common.year", nil), (long)[_dateCurrent getMonth], NSLocalizedString(@"lifelog.common.month", nil), (long)[_dateCurrent getDay], NSLocalizedString(@"lifelog.common.day", nil)]; 93 self.lblDateCurrent.text = [NSString stringWithFormat:@"%ld%@%ld%@%ld%@", (long)[_dateCurrent getYear], NSLocalizedString(@"lifelog.common.year", nil), (long)[_dateCurrent getMonth], NSLocalizedString(@"lifelog.common.month", nil), (long)[_dateCurrent getDay], NSLocalizedString(@"lifelog.common.day", nil)];
94 94
95 self.lblUnitStep.text = NSLocalizedString(@"lifelog.home.unit.step", nil); 95 self.lblUnitStep.text = NSLocalizedString(@"lifelog.home.unit.step", nil);
96 96
97 if ([CMPedometer isStepCountingAvailable]) { 97 if ([CMPedometer isStepCountingAvailable]) {
98 _pedometer = [[CMPedometer alloc] init]; 98 _pedometer = [[CMPedometer alloc] init];
99 } 99 }
100 if ([CMMotionActivityManager isActivityAvailable]) { 100 if ([CMMotionActivityManager isActivityAvailable]) {
101 _motionActivityManager = [[CMMotionActivityManager alloc] init]; 101 _motionActivityManager = [[CMMotionActivityManager alloc] init];
102 } 102 }
103 _operationQueue = [[NSOperationQueue alloc] init]; 103 _operationQueue = [[NSOperationQueue alloc] init];
104 _bike = 0; 104 _bike = 0;
105 _walking = 0; 105 _walking = 0;
106 _running = 0; 106 _running = 0;
107 _segmentHome.selectedSegmentIndex = 1; 107 _segmentHome.selectedSegmentIndex = 1;
108 _totalRequest = 0; 108 _totalRequest = 0;
109 _countComplete = 0; 109 _countComplete = 0;
110 110
111 progressHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 111 progressHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
112 progressHud.mode = MBProgressHUDModeIndeterminate; 112 progressHud.mode = MBProgressHUDModeIndeterminate;
113 progressHud.detailsLabel.text = NSLocalizedString(@"lifelog.home.progressHud.title", nil); 113 progressHud.detailsLabel.text = NSLocalizedString(@"lifelog.home.progressHud.title", nil);
114 [self saveDataStep7LastDay]; 114 [self saveDataStep7LastDay];
115 } 115 }
116 116
117 - (void)viewWillAppear:(BOOL)animated { 117 - (void)viewWillAppear:(BOOL)animated {
118 [super viewWillAppear:animated]; 118 [super viewWillAppear:animated];
119 [self saveStepForDay:self.dateCurrent]; 119 [self saveStepForDay:self.dateCurrent];
120 [self requestTopByDate:_dateCurrent]; 120 [self requestTopByDate:self.dateCurrent];
121 } 121 }
122 122
123 - (void)requestTopByDate:(NSDate *)date { 123 - (void)requestTopByDate:(NSDate *)date {
124 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 124 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
125 [dateFormatter setDateFormat:@"yyyy-MM-dd"]; 125 [dateFormatter setDateFormat:@"yyyy-MM-dd"];
126 NSString *dateString = [dateFormatter stringFromDate:date]; 126 NSString *dateString = [dateFormatter stringFromDate:date];
127 127 [progressHud showAnimated:YES];
128 [progressHud setHidden:NO];
128 HomeViewController __weak *weakSelf = self; 129 HomeViewController __weak *weakSelf = self;
129 int mode = 1; 130 int mode = 1;
130 switch (_segmentHome.selectedSegmentIndex) { 131 switch (_segmentHome.selectedSegmentIndex) {
131 case 0: 132 case 0:
132 mode = 3; 133 mode = 3;
133 break; 134 break;
134 135
135 case 1: 136 case 1:
136 mode = 1; 137 mode = 1;
137 break; 138 break;
138 139
139 case 2: 140 case 2:
140 mode = 2; 141 mode = 2;
141 break; 142 break;
142 143
143 default: 144 default:
144 break; 145 break;
145 } 146 }
146 [[ServerAPI server] requestTopWithMode:mode andDate:dateString CompletionHandler:^(TopObject *topObject, NSError *error) { 147 [[ServerAPI server] requestTopWithMode:mode andDate:dateString CompletionHandler:^(TopObject *topObject, NSError *error) {
147 if(weakSelf == nil) { 148 if(weakSelf == nil) {
148 return ; 149 return ;
149 } 150 }
150 if (error == nil) { 151 if (error == nil) {
151 NSLog(@"TopObject: %@", topObject); 152 NSLog(@"TopObject: %@", topObject);
153 dispatch_async(dispatch_get_main_queue(), ^{
154 [weakSelf updateStepUI:topObject.targetInfor];
155 });
152 } 156 }
153 else { 157 else {
154 dispatch_async(dispatch_get_main_queue(), ^{ 158 dispatch_async(dispatch_get_main_queue(), ^{
155 NSString *message = [error.userInfo objectForKey:@"message"]; 159 NSString *message = [error.userInfo objectForKey:@"message"];
156 [Utilities showErrorMessage:message withViewController:weakSelf]; 160 [Utilities showErrorMessage:message withViewController:weakSelf];
157 }); 161 });
158 } 162 }
163 dispatch_async(dispatch_get_main_queue(), ^{
164 [progressHud setHidden:YES];
165 });
159 }]; 166 }];
160 } 167 }
161 168
162 - (void)viewWillDisappear:(BOOL)animated { 169 - (void)viewWillDisappear:(BOOL)animated {
163 [super viewWillDisappear:animated]; 170 [super viewWillDisappear:animated];
164 } 171 }
165 172
166 - (void)didReceiveMemoryWarning { 173 - (void)didReceiveMemoryWarning {
167 [super didReceiveMemoryWarning]; 174 [super didReceiveMemoryWarning];
168 // Dispose of any resources that can be recreated. 175 // Dispose of any resources that can be recreated.
169 } 176 }
170 177
171 #pragma mark - IBAction 178 #pragma mark - IBAction
172 - (IBAction)menuButtonTouchUpInside:(id)sender 179 - (IBAction)menuButtonTouchUpInside:(id)sender
173 { 180 {
174 181
175 } 182 }
176 183
177 - (IBAction)todayButtonTouchUpInside:(id)sender 184 - (IBAction)todayButtonTouchUpInside:(id)sender
178 { 185 {
179 TodayViewController *todayVC = [[TodayViewController alloc] initWithNibName:@"TodayViewController" bundle:nil]; 186 TodayViewController *todayVC = [[TodayViewController alloc] initWithNibName:@"TodayViewController" bundle:nil];
180 [self.navigationController pushViewController:todayVC animated:YES]; 187 [self.navigationController pushViewController:todayVC animated:YES];
181 } 188 }
182 189
183 - (IBAction)leftButtonTouchUpInside:(id)sender 190 - (IBAction)leftButtonTouchUpInside:(id)sender
184 { 191 {
185 self.dateCurrent = [self.dateCurrent dateByAddingTimeInterval:-(24*60*60)]; 192 self.dateCurrent = [self.dateCurrent dateByAddingTimeInterval:-(24*60*60)];
186 self.lblDateCurrent.text = [NSString stringWithFormat:@"%ld%@%ld%@%ld%@", (long)[_dateCurrent getYear], NSLocalizedString(@"lifelog.common.year", nil), (long)[_dateCurrent getMonth], NSLocalizedString(@"lifelog.common.month", nil), (long)[_dateCurrent getDay], NSLocalizedString(@"lifelog.common.day", nil)]; 193 self.lblDateCurrent.text = [NSString stringWithFormat:@"%ld%@%ld%@%ld%@", (long)[_dateCurrent getYear], NSLocalizedString(@"lifelog.common.year", nil), (long)[_dateCurrent getMonth], NSLocalizedString(@"lifelog.common.month", nil), (long)[_dateCurrent getDay], NSLocalizedString(@"lifelog.common.day", nil)];
187 [self saveStepForDay:self.dateCurrent]; 194 [self saveStepForDay:self.dateCurrent];
195 [self requestTopByDate:self.dateCurrent];
188 } 196 }
189 197
190 - (IBAction)rightButtonTouchUpInside:(id)sender 198 - (IBAction)rightButtonTouchUpInside:(id)sender
191 { 199 {
192 self.dateCurrent = [self.dateCurrent dateByAddingTimeInterval:24*60*60]; 200 self.dateCurrent = [self.dateCurrent dateByAddingTimeInterval:24*60*60];
193 self.lblDateCurrent.text = [NSString stringWithFormat:@"%ld%@%ld%@%ld%@", (long)[_dateCurrent getYear], NSLocalizedString(@"lifelog.common.year", nil), (long)[_dateCurrent getMonth], NSLocalizedString(@"lifelog.common.month", nil), (long)[_dateCurrent getDay], NSLocalizedString(@"lifelog.common.day", nil)]; 201 self.lblDateCurrent.text = [NSString stringWithFormat:@"%ld%@%ld%@%ld%@", (long)[_dateCurrent getYear], NSLocalizedString(@"lifelog.common.year", nil), (long)[_dateCurrent getMonth], NSLocalizedString(@"lifelog.common.month", nil), (long)[_dateCurrent getDay], NSLocalizedString(@"lifelog.common.day", nil)];
194 [self saveStepForDay:self.dateCurrent]; 202 [self saveStepForDay:self.dateCurrent];
203 [self requestTopByDate:self.dateCurrent];
195 } 204 }
196 205
197 - (IBAction)segmentValueChange:(id)sender { 206 - (IBAction)segmentValueChange:(id)sender {
198 [self updateStepUI]; 207 // [self updateStepUI];
208 [self requestTopByDate:self.dateCurrent];
199 } 209 }
200 210
201 #pragma mark - Functions Private 211 #pragma mark - Functions Private
202 - (void)saveStepForDay:(NSDate *)date 212 - (void)saveStepForDay:(NSDate *)date
203 { 213 {
204 if ([CMMotionActivityManager isActivityAvailable]) { 214 if ([CMMotionActivityManager isActivityAvailable]) {
205 [progressHud showAnimated:YES]; 215 // [progressHud showAnimated:YES];
206 [progressHud setHidden:NO]; 216 // [progressHud setHidden:NO];
207 self.bike = 0; 217 self.bike = 0;
208 self.walking = 0; 218 self.walking = 0;
209 self.running = 0; 219 self.running = 0;
210 NSDate *startDate = [date beginningAtMidnightOfDay]; 220 NSDate *startDate = [date beginningAtMidnightOfDay];
211 NSDate *endDate = [startDate dateByAddingTimeInterval:(24*60*60 - 1)]; 221 NSDate *endDate = [startDate dateByAddingTimeInterval:(24*60*60 - 1)];
212 222
213 HomeViewController __weak *weakSelf = self; 223 HomeViewController __weak *weakSelf = self;
214 dispatch_queue_t myQueue = dispatch_queue_create("mobileworld.jp.lifelog.forDay", NULL); 224 dispatch_queue_t myQueue = dispatch_queue_create("mobileworld.jp.lifelog.forDay", NULL);
215 dispatch_async(myQueue, ^{ 225 dispatch_async(myQueue, ^{
216 if (weakSelf == nil) { 226 if (weakSelf == nil) {
217 return ; 227 return ;
218 } 228 }
219 229
220 [weakSelf.motionActivityManager queryActivityStartingFromDate:startDate toDate:endDate toQueue:_operationQueue withHandler:^(NSArray<CMMotionActivity *> * _Nullable activities, NSError * _Nullable error) { 230 [weakSelf.motionActivityManager queryActivityStartingFromDate:startDate toDate:endDate toQueue:_operationQueue withHandler:^(NSArray<CMMotionActivity *> * _Nullable activities, NSError * _Nullable error) {
221 if (error || activities.count <= 0) { 231 if (error || activities.count <= 0) {
222 weakSelf.totalRequest = 0; 232 weakSelf.totalRequest = 0;
223 weakSelf.countComplete = 0; 233 weakSelf.countComplete = 0;
224 dispatch_async(dispatch_get_main_queue(), ^{ 234 dispatch_async(dispatch_get_main_queue(), ^{
225 [weakSelf updateStepUI]; 235 [weakSelf updateStepUI];
226 }); 236 });
227 return ; 237 return ;
228 } 238 }
229 // set EndDate 239 // set EndDate
230 weakSelf.totalRequest = (int)activities.count; 240 weakSelf.totalRequest = (int)activities.count;
231 NSMutableArray *arrayActivities = [[NSMutableArray alloc] init]; 241 NSMutableArray *arrayActivities = [[NSMutableArray alloc] init];
232 for (int i = 0; i < activities.count; i++) { 242 for (int i = 0; i < activities.count; i++) {
233 CMMotionActivity *activity = [activities objectAtIndex:i]; 243 CMMotionActivity *activity = [activities objectAtIndex:i];
234 CMMotionActivityExtra *activityExtra = [[CMMotionActivityExtra alloc] init]; 244 CMMotionActivityExtra *activityExtra = [[CMMotionActivityExtra alloc] init];
235 activityExtra.activity = activity; 245 activityExtra.activity = activity;
236 if (i == activities.count - 1) { 246 if (i == activities.count - 1) {
237 activityExtra.endDate = endDate; 247 activityExtra.endDate = endDate;
238 } 248 }
239 else { 249 else {
240 CMMotionActivity *activityNext = [activities objectAtIndex:i+1]; 250 CMMotionActivity *activityNext = [activities objectAtIndex:i+1];
241 activityExtra.endDate = activityNext.startDate; 251 activityExtra.endDate = activityNext.startDate;
242 } 252 }
243 [arrayActivities addObject:activityExtra]; 253 [arrayActivities addObject:activityExtra];
244 } 254 }
245 255
246 for (CMMotionActivityExtra *activityExtra in arrayActivities) { 256 for (CMMotionActivityExtra *activityExtra in arrayActivities) {
247 [weakSelf.pedometer queryPedometerDataFromDate:activityExtra.activity.startDate toDate:activityExtra.endDate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) { 257 [weakSelf.pedometer queryPedometerDataFromDate:activityExtra.activity.startDate toDate:activityExtra.endDate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
248 NSInteger numberStep = [pedometerData.numberOfSteps integerValue]; 258 NSInteger numberStep = [pedometerData.numberOfSteps integerValue];
249 int mode = 1; 259 int mode = 1;
250 if (activityExtra.activity.cycling) { 260 if (activityExtra.activity.cycling) {
251 // self.bike 261 // self.bike
252 weakSelf.bike += numberStep; 262 weakSelf.bike += numberStep;
253 mode = 3; 263 mode = 3;
254 } 264 }
255 else if (activityExtra.activity.walking) { 265 else if (activityExtra.activity.walking) {
256 // self.walking 266 // self.walking
257 weakSelf.walking += numberStep; 267 weakSelf.walking += numberStep;
258 mode = 1; 268 mode = 1;
259 } 269 }
260 else if (activityExtra.activity.running) { 270 else if (activityExtra.activity.running) {
261 weakSelf.running += numberStep; 271 weakSelf.running += numberStep;
262 mode = 2; 272 mode = 2;
263 } 273 }
264 else { 274 else {
265 // unknown 275 // unknown
266 } 276 }
267 weakSelf.countComplete += 1; 277 weakSelf.countComplete += 1;
268 278
269 // save step to server 279 // save step to server
270 if (numberStep > 0) { 280 if (numberStep > 0) {
271 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 281 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
272 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 282 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
273 NSString *dateBegin = [dateFormatter stringFromDate:activityExtra.activity.startDate]; 283 NSString *dateBegin = [dateFormatter stringFromDate:activityExtra.activity.startDate];
274 NSString *dateEnd = [dateFormatter stringFromDate:activityExtra.endDate]; 284 NSString *dateEnd = [dateFormatter stringFromDate:activityExtra.endDate];
275 [[ServerAPI server] requestCreateLog:mode withStep:(int)numberStep startDate:dateBegin endDate:dateEnd CompletionHandler:^(NSError *error) { 285 [[ServerAPI server] requestCreateLog:mode withStep:(int)numberStep startDate:dateBegin endDate:dateEnd CompletionHandler:^(NSError *error) {
276 if (error) { 286 if (error) {
277 NSLog(@"Error: %@", error); 287 NSLog(@"Error: %@", error);
278 } 288 }
279 }]; 289 }];
280 } 290 }
281 dispatch_async(dispatch_get_main_queue(), ^{ 291 dispatch_async(dispatch_get_main_queue(), ^{
282 [weakSelf updateStepUI]; 292 [weakSelf updateStepUI];
283 }); 293 });
284 }]; 294 }];
285 } 295 }
286 }]; 296 }];
287 }); 297 });
288 } 298 }
289 else { 299 // else {
290 [progressHud setHidden:YES]; 300 // [progressHud setHidden:YES];
291 } 301 // }
292 } 302 }
293 303
294 - (void)updateStepUI 304 - (void)updateStepUI
295 { 305 {
296 if (self.totalRequest == self.countComplete) { 306 if (self.totalRequest == self.countComplete) {
307 /*
297 switch (self.segmentHome.selectedSegmentIndex) { 308 switch (self.segmentHome.selectedSegmentIndex) {
298 case 0: 309 case 0:
299 self.lblValueStep.text = [Utilities addCommaFromNumber:self.bike]; 310 self.lblValueStep.text = [Utilities addCommaFromNumber:self.bike];
300 self.lblValueStepOther.text = [NSString stringWithFormat:@"%@ %ld", NSLocalizedString(@"lifelog.today.remaining.other", nil), numberTotal - self.bike]; 311 self.lblValueStepOther.text = [NSString stringWithFormat:@"%@ %ld", NSLocalizedString(@"lifelog.today.remaining.other", nil), numberTotal - self.bike];
301 self.lblPercent.text = [NSString stringWithFormat:@"%@ %i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), (int)(self.bike*100/numberTotal), NSLocalizedString(@"lifelog.today.percent", nil)]; 312 self.lblPercent.text = [NSString stringWithFormat:@"%@ %i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), (int)(self.bike*100/numberTotal), NSLocalizedString(@"lifelog.today.percent", nil)];
302 break; 313 break;
303 314
304 case 1: 315 case 1:
305 self.lblValueStep.text = [Utilities addCommaFromNumber:self.walking]; 316 self.lblValueStep.text = [Utilities addCommaFromNumber:self.walking];
306 self.lblValueStepOther.text = [NSString stringWithFormat:@"%@ %ld", NSLocalizedString(@"lifelog.today.remaining.other", nil), numberTotal - self.walking]; 317 self.lblValueStepOther.text = [NSString stringWithFormat:@"%@ %ld", NSLocalizedString(@"lifelog.today.remaining.other", nil), numberTotal - self.walking];
307 self.lblPercent.text = [NSString stringWithFormat:@"%@ %i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), (int)(self.walking*100/numberTotal), NSLocalizedString(@"lifelog.today.percent", nil)]; 318 self.lblPercent.text = [NSString stringWithFormat:@"%@ %i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), (int)(self.walking*100/numberTotal), NSLocalizedString(@"lifelog.today.percent", nil)];
308 break; 319 break;
309 320
310 case 2: 321 case 2:
311 self.lblValueStep.text = [Utilities addCommaFromNumber:self.running]; 322 self.lblValueStep.text = [Utilities addCommaFromNumber:self.running];
312 self.lblValueStepOther.text = [NSString stringWithFormat:@"%@ %ld", NSLocalizedString(@"lifelog.today.remaining.other", nil), numberTotal - self.running]; 323 self.lblValueStepOther.text = [NSString stringWithFormat:@"%@ %ld", NSLocalizedString(@"lifelog.today.remaining.other", nil), numberTotal - self.running];
313 self.lblPercent.text = [NSString stringWithFormat:@"%@ %i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), (int)(self.running*100/numberTotal), NSLocalizedString(@"lifelog.today.percent", nil)]; 324 self.lblPercent.text = [NSString stringWithFormat:@"%@ %i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), (int)(self.running*100/numberTotal), NSLocalizedString(@"lifelog.today.percent", nil)];
314 break; 325 break;
315 326
316 default: 327 default:
317 break; 328 break;
318 } 329 }
330 */
319 self.countComplete = 0; 331 self.countComplete = 0;
320 self.totalRequest = 0; 332 self.totalRequest = 0;
321 [progressHud setHidden:YES]; 333 //[progressHud setHidden:YES];
322 } 334 }
323 } 335 }
324 336
325 - (void)saveDataStep7LastDay 337 - (void)saveDataStep7LastDay
326 { 338 {
327 NSDate *dateNow = [NSDate date]; 339 NSDate *dateNow = [NSDate date];
328 for (int index = 0; index < 7; index++) { 340 for (int index = 0; index < 7; index++) {
329 dateNow = [dateNow dateByAddingTimeInterval:-(24*60*60)]; 341 dateNow = [dateNow dateByAddingTimeInterval:-(24*60*60)];
330 [self saveStepByDate:dateNow]; 342 [self saveStepByDate:dateNow];
331 } 343 }
332 } 344 }
333 345
334 - (void)saveStepByDate:(NSDate *)date 346 - (void)saveStepByDate:(NSDate *)date
335 { 347 {
336 if ([CMMotionActivityManager isActivityAvailable]) { 348 if ([CMMotionActivityManager isActivityAvailable]) {
337 NSDate *startDate = [date beginningAtMidnightOfDay]; 349 NSDate *startDate = [date beginningAtMidnightOfDay];
338 NSDate *endDate = [startDate dateByAddingTimeInterval:(24*60*60 - 1)]; 350 NSDate *endDate = [startDate dateByAddingTimeInterval:(24*60*60 - 1)];
339 351
340 HomeViewController __weak *weakSelf = self; 352 HomeViewController __weak *weakSelf = self;
341 dispatch_queue_t myQueue = dispatch_queue_create("mobileworld.jp.lifelog.7lastday", NULL); 353 dispatch_queue_t myQueue = dispatch_queue_create("mobileworld.jp.lifelog.7lastday", NULL);
342 dispatch_async(myQueue, ^{ 354 dispatch_async(myQueue, ^{
343 if (weakSelf == nil) { 355 if (weakSelf == nil) {
344 return ; 356 return ;
345 } 357 }
346 358
347 [weakSelf.motionActivityManager queryActivityStartingFromDate:startDate toDate:endDate toQueue:_operationQueue withHandler:^(NSArray<CMMotionActivity *> * _Nullable activities, NSError * _Nullable error) { 359 [weakSelf.motionActivityManager queryActivityStartingFromDate:startDate toDate:endDate toQueue:_operationQueue withHandler:^(NSArray<CMMotionActivity *> * _Nullable activities, NSError * _Nullable error) {
348 if (error || activities.count <= 0) { 360 if (error || activities.count <= 0) {
349 return ; 361 return ;
350 } 362 }
351 // set EndDate 363 // set EndDate
352 NSMutableArray *arrayActivities = [[NSMutableArray alloc] init]; 364 NSMutableArray *arrayActivities = [[NSMutableArray alloc] init];
353 for (int i = 0; i < activities.count; i++) { 365 for (int i = 0; i < activities.count; i++) {
354 CMMotionActivity *activity = [activities objectAtIndex:i]; 366 CMMotionActivity *activity = [activities objectAtIndex:i];
355 CMMotionActivityExtra *activityExtra = [[CMMotionActivityExtra alloc] init]; 367 CMMotionActivityExtra *activityExtra = [[CMMotionActivityExtra alloc] init];
356 activityExtra.activity = activity; 368 activityExtra.activity = activity;
357 if (i == activities.count - 1) { 369 if (i == activities.count - 1) {
358 activityExtra.endDate = endDate; 370 activityExtra.endDate = endDate;
359 } 371 }
360 else { 372 else {
361 CMMotionActivity *activityNext = [activities objectAtIndex:i+1]; 373 CMMotionActivity *activityNext = [activities objectAtIndex:i+1];
362 activityExtra.endDate = activityNext.startDate; 374 activityExtra.endDate = activityNext.startDate;
363 } 375 }
364 [arrayActivities addObject:activityExtra]; 376 [arrayActivities addObject:activityExtra];
365 } 377 }
366 378
367 for (CMMotionActivityExtra *activityExtra in arrayActivities) { 379 for (CMMotionActivityExtra *activityExtra in arrayActivities) {
368 [weakSelf.pedometer queryPedometerDataFromDate:activityExtra.activity.startDate toDate:activityExtra.endDate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) { 380 [weakSelf.pedometer queryPedometerDataFromDate:activityExtra.activity.startDate toDate:activityExtra.endDate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
369 NSInteger numberStep = [pedometerData.numberOfSteps integerValue]; 381 NSInteger numberStep = [pedometerData.numberOfSteps integerValue];
370 int mode = 1; 382 int mode = 1;
371 if (activityExtra.activity.cycling) { 383 if (activityExtra.activity.cycling) {
372 mode = 3; 384 mode = 3;
373 } 385 }
374 else if (activityExtra.activity.walking) { 386 else if (activityExtra.activity.walking) {
375 mode = 1; 387 mode = 1;
376 } 388 }
377 else if (activityExtra.activity.running) { 389 else if (activityExtra.activity.running) {
378 mode = 2; 390 mode = 2;
379 } 391 }
380 else { 392 else {
381 // unknown 393 // unknown
382 } 394 }
383 395
384 // save step to server 396 // save step to server
385 if (numberStep > 0) { 397 if (numberStep > 0) {
386 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 398 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
387 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 399 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
388 NSString *dateBegin = [dateFormatter stringFromDate:activityExtra.activity.startDate]; 400 NSString *dateBegin = [dateFormatter stringFromDate:activityExtra.activity.startDate];
389 NSString *dateEnd = [dateFormatter stringFromDate:activityExtra.endDate]; 401 NSString *dateEnd = [dateFormatter stringFromDate:activityExtra.endDate];
390 [[ServerAPI server] requestCreateLog:mode withStep:(int)numberStep startDate:dateBegin endDate:dateEnd CompletionHandler:^(NSError *error) { 402 [[ServerAPI server] requestCreateLog:mode withStep:(int)numberStep startDate:dateBegin endDate:dateEnd CompletionHandler:^(NSError *error) {
391 if (error) { 403 if (error) {
392 NSLog(@"Error: %@", error); 404 NSLog(@"Error: %@", error);
393 } 405 }
394 }]; 406 }];
395 } 407 }
396 }]; 408 }];
397 } 409 }
398 }]; 410 }];
399 }); 411 });
400 } 412 }
413 }
414
415 - (void)updateStepUI:(TargetInfor *)targetInfor
416 {
417 self.lblValueStep.text = targetInfor.num_step;
418 self.lblValueStepOther.text = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"lifelog.today.remaining.other", nil), targetInfor.remaining_step];
419 self.lblPercent.text = [NSString stringWithFormat:@"%@ %i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), [targetInfor.complete_percent intValue], NSLocalizedString(@"lifelog.today.percent", nil)];
401 } 420 }
402 421
403 @end 422 @end
404 423
LifeLog/LifeLog/TargetInfor.h
1 // 1 //
2 // TargetInfor.h 2 // TargetInfor.h
3 // LifeLog 3 // LifeLog
4 // 4 //
5 // Created by Nguyen Van Phong on 8/13/17. 5 // Created by Nguyen Van Phong on 8/13/17.
6 // Copyright © 2017 PhongNV. All rights reserved. 6 // Copyright © 2017 PhongNV. All rights reserved.
7 // 7 //
8 8
9 #import <Foundation/Foundation.h> 9 #import <Foundation/Foundation.h>
10 10
11 @interface TargetInfor : NSObject 11 @interface TargetInfor : NSObject
12 @property (nonatomic) int target_step; 12 @property (nonatomic, strong) NSString *target_step;
13 @property (nonatomic) int num_step; 13 @property (nonatomic, strong) NSString *num_step;
14 @property (nonatomic) int remaining_step; 14 @property (nonatomic, strong) NSString *remaining_step;
15 @property (nonatomic) float complete_percent; 15 @property (nonatomic, strong) NSString *complete_percent;
16 -(id) initWithData : (NSDictionary *) dict; 16 -(id) initWithData : (NSDictionary *) dict;
17 @end 17 @end
18 18
LifeLog/LifeLog/TargetInfor.m
1 // 1 //
2 // TargetInfor.m 2 // TargetInfor.m
3 // LifeLog 3 // LifeLog
4 // 4 //
5 // Created by Nguyen Van Phong on 8/13/17. 5 // Created by Nguyen Van Phong on 8/13/17.
6 // Copyright © 2017 PhongNV. All rights reserved. 6 // Copyright © 2017 PhongNV. All rights reserved.
7 // 7 //
8 8
9 #import "TargetInfor.h" 9 #import "TargetInfor.h"
10 10
11 @implementation TargetInfor 11 @implementation TargetInfor
12 -(id) initWithData : (NSDictionary *) dict { 12 -(id) initWithData : (NSDictionary *) dict {
13 if([dict objectForKey:@"target_step"] != nil) { 13 if([dict objectForKey:@"target_step"] != nil) {
14 self.target_step = [dict[@"target_step"] intValue]; 14 self.target_step = dict[@"target_step"];
15 } 15 }
16 if([dict objectForKey:@"num_step"] != nil) { 16 if([dict objectForKey:@"num_step"] != nil) {
17 self.num_step = [dict[@"num_step"] intValue]; 17 self.num_step = dict[@"num_step"];
18 } 18 }
19 if([dict objectForKey:@"remaining_step"] != nil) { 19 if([dict objectForKey:@"remaining_step"] != nil) {
20 self.remaining_step = [dict[@"remaining_step"] intValue]; 20 self.remaining_step = dict[@"remaining_step"];
21 } 21 }
22 if([dict objectForKey:@"complete_percent"] != nil) { 22 if([dict objectForKey:@"complete_percent"] != nil) {
23 self.complete_percent = [dict[@"complete_percent"] floatValue]; 23 self.complete_percent = dict[@"complete_percent"];
24 } 24 }
25 return self; 25 return self;
26 } 26 }
27 @end 27 @end
28 28