Commit 1afd591625864dafd918ee9c979027cf401b34c0

Authored by phong
1 parent 39b57938e7
Exists in master

update performance

Showing 2 changed files with 76 additions and 39 deletions Side-by-side Diff

LifeLog/LifeLog/HomeViewController.m
... ... @@ -15,8 +15,6 @@
15 15 #import <MBProgressHUD/MBProgressHUD.h>
16 16 #import "ServerAPI.h"
17 17  
18   -static NSInteger numberTotal = 10000;
19   -
20 18 @interface HomeViewController ()
21 19 {
22 20 MBProgressHUD *progressHud;
... ... @@ -42,6 +40,7 @@
42 40  
43 41 @property (nonatomic, assign) int totalRequest;
44 42 @property (nonatomic, assign) int countComplete;
  43 +@property (nonatomic, assign) int currentIndex;
45 44  
46 45 @end
47 46  
48 47  
... ... @@ -111,13 +110,12 @@
111 110 progressHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
112 111 progressHud.mode = MBProgressHUDModeIndeterminate;
113 112 progressHud.detailsLabel.text = NSLocalizedString(@"lifelog.home.progressHud.title", nil);
114   - [self saveDataStep7LastDay];
  113 + // [self saveDataStep7LastDay];
115 114 }
116 115  
117 116 - (void)viewWillAppear:(BOOL)animated {
118 117 [super viewWillAppear:animated];
119 118 [self saveStepForDay:self.dateCurrent];
120   - [self requestTopByDate:self.dateCurrent];
121 119 }
122 120  
123 121 - (void)requestTopByDate:(NSDate *)date {
124 122  
125 123  
126 124  
... ... @@ -191,20 +189,21 @@
191 189 {
192 190 self.dateCurrent = [self.dateCurrent dateByAddingTimeInterval:-(24*60*60)];
193 191 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)];
  192 + [self resetStepUI];
194 193 [self saveStepForDay:self.dateCurrent];
195   - [self requestTopByDate:self.dateCurrent];
196 194 }
197 195  
198 196 - (IBAction)rightButtonTouchUpInside:(id)sender
199 197 {
200 198 self.dateCurrent = [self.dateCurrent dateByAddingTimeInterval:24*60*60];
201 199 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)];
202   - [self saveStepForDay:self.dateCurrent];
  200 + [self resetStepUI];
  201 + //[self saveStepForDay:self.dateCurrent];
203 202 [self requestTopByDate:self.dateCurrent];
204 203 }
205 204  
206 205 - (IBAction)segmentValueChange:(id)sender {
207   - // [self updateStepUI];
  206 + [self resetStepUI];
208 207 [self requestTopByDate:self.dateCurrent];
209 208 }
210 209  
... ... @@ -212,8 +211,8 @@
212 211 - (void)saveStepForDay:(NSDate *)date
213 212 {
214 213 if ([CMMotionActivityManager isActivityAvailable]) {
215   -// [progressHud showAnimated:YES];
216   -// [progressHud setHidden:NO];
  214 + [progressHud showAnimated:YES];
  215 + [progressHud setHidden:NO];
217 216 self.bike = 0;
218 217 self.walking = 0;
219 218 self.running = 0;
... ... @@ -252,6 +251,9 @@
252 251 }
253 252 [arrayActivities addObject:activityExtra];
254 253 }
  254 + // PhongNV
  255 + // weakSelf.currentIndex = 0;
  256 + // [weakSelf saveStepFromActivityExtras:arrayActivities];
255 257  
256 258 for (CMMotionActivityExtra *activityExtra in arrayActivities) {
257 259 [weakSelf.pedometer queryPedometerDataFromDate:activityExtra.activity.startDate toDate:activityExtra.endDate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
258 260  
259 261  
260 262  
261 263  
... ... @@ -293,44 +295,69 @@
293 295 });
294 296 }];
295 297 }
  298 +
296 299 }];
297 300 });
298 301 }
299   -// else {
300   -// [progressHud setHidden:YES];
301   -// }
302 302 }
303 303  
  304 +- (void)saveStepFromActivityExtras:(NSArray *)activities
  305 +{
  306 + if (self.currentIndex >= activities.count) {
  307 + self.currentIndex = 0;
  308 + dispatch_async(dispatch_get_main_queue(), ^{
  309 + [self updateStepUI];
  310 + });
  311 + return;
  312 + }
  313 + HomeViewController __weak *weakSelf = self;
  314 + CMMotionActivityExtra *activityExtra = [activities objectAtIndex:self.currentIndex];
  315 + [self.pedometer queryPedometerDataFromDate:activityExtra.activity.startDate toDate:activityExtra.endDate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
  316 + if (weakSelf == nil) {
  317 + return ;
  318 + }
  319 + NSInteger numberStep = [pedometerData.numberOfSteps integerValue];
  320 + int mode = 1;
  321 + if (activityExtra.activity.cycling) {
  322 + mode = 3;
  323 + }
  324 + else if (activityExtra.activity.walking) {
  325 + mode = 1;
  326 + }
  327 + else if (activityExtra.activity.running) {
  328 + mode = 2;
  329 + }
  330 + else {
  331 + // unknown
  332 + }
  333 + self.countComplete += 1;
  334 + // save step to server
  335 + if (numberStep > 0) {
  336 + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  337 + [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  338 + NSString *dateBegin = [dateFormatter stringFromDate:activityExtra.activity.startDate];
  339 + NSString *dateEnd = [dateFormatter stringFromDate:activityExtra.endDate];
  340 + [[ServerAPI server] requestCreateLog:mode withStep:(int)numberStep startDate:dateBegin endDate:dateEnd CompletionHandler:^(NSError *error) {
  341 + if (error) {
  342 + NSLog(@"Error: %@", error);
  343 + }
  344 + weakSelf.currentIndex += 1;
  345 + [weakSelf saveStepFromActivityExtras:activities];
  346 + }];
  347 + }
  348 + else {
  349 + weakSelf.currentIndex += 1;
  350 + [weakSelf saveStepFromActivityExtras:activities];
  351 + }
  352 + }];
  353 +}
  354 +
304 355 - (void)updateStepUI
305 356 {
306 357 if (self.totalRequest == self.countComplete) {
307   - /*
308   - switch (self.segmentHome.selectedSegmentIndex) {
309   - case 0:
310   - self.lblValueStep.text = [Utilities addCommaFromNumber:self.bike];
311   - self.lblValueStepOther.text = [NSString stringWithFormat:@"%@ %ld", NSLocalizedString(@"lifelog.today.remaining.other", nil), numberTotal - self.bike];
312   - self.lblPercent.text = [NSString stringWithFormat:@"%@ %i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), (int)(self.bike*100/numberTotal), NSLocalizedString(@"lifelog.today.percent", nil)];
313   - break;
314   -
315   - case 1:
316   - self.lblValueStep.text = [Utilities addCommaFromNumber:self.walking];
317   - self.lblValueStepOther.text = [NSString stringWithFormat:@"%@ %ld", NSLocalizedString(@"lifelog.today.remaining.other", nil), numberTotal - self.walking];
318   - self.lblPercent.text = [NSString stringWithFormat:@"%@ %i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), (int)(self.walking*100/numberTotal), NSLocalizedString(@"lifelog.today.percent", nil)];
319   - break;
320   -
321   - case 2:
322   - self.lblValueStep.text = [Utilities addCommaFromNumber:self.running];
323   - self.lblValueStepOther.text = [NSString stringWithFormat:@"%@ %ld", NSLocalizedString(@"lifelog.today.remaining.other", nil), numberTotal - self.running];
324   - self.lblPercent.text = [NSString stringWithFormat:@"%@ %i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), (int)(self.running*100/numberTotal), NSLocalizedString(@"lifelog.today.percent", nil)];
325   - break;
326   -
327   - default:
328   - break;
329   - }
330   - */
  358 + [self requestTopByDate:self.dateCurrent];
331 359 self.countComplete = 0;
332 360 self.totalRequest = 0;
333   - //[progressHud setHidden:YES];
334 361 }
335 362 }
336 363  
... ... @@ -417,6 +444,13 @@
417 444 self.lblValueStep.text = targetInfor.num_step;
418 445 self.lblValueStepOther.text = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"lifelog.today.remaining.other", nil), targetInfor.remaining_step];
419 446 self.lblPercent.text = [NSString stringWithFormat:@"%@ %i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), [targetInfor.complete_percent intValue], NSLocalizedString(@"lifelog.today.percent", nil)];
  447 +}
  448 +
  449 +- (void)resetStepUI
  450 +{
  451 + self.lblValueStep.text = @"";
  452 + self.lblValueStepOther.text = @"";
  453 + self.lblPercent.text = @"";
420 454 }
421 455  
422 456 @end
LifeLog/LifeLog/ServerAPI.m
... ... @@ -57,7 +57,7 @@
57 57 {
58 58 self = [super init];
59 59 if (self != nil) {
60   - self.timeOutInterval = 150;
  60 + self.timeOutInterval = 60;
61 61 }
62 62 return self;
63 63 }
... ... @@ -1035,7 +1035,10 @@
1035 1035 [request setHTTPBody: encodedData];
1036 1036 }
1037 1037  
1038   - NSURLSession *session = [NSURLSession sharedSession];
  1038 + //NSURLSession *session = [NSURLSession sharedSession];
  1039 + NSURLSessionConfiguration *defaultConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
  1040 + NSOperationQueue *operationQueue = [NSOperationQueue mainQueue];
  1041 + NSURLSession *session = [NSURLSession sessionWithConfiguration:defaultConfiguration delegate:nil delegateQueue:operationQueue];
1039 1042 NSURLSessionDataTask *task = [session dataTaskWithRequest:request
1040 1043 completionHandler:
1041 1044 ^(NSData *data, NSURLResponse *response, NSError *error) {