diff --git a/LifeLog/LifeLog/HomeViewController.m b/LifeLog/LifeLog/HomeViewController.m index 05053b3..2edb466 100644 --- a/LifeLog/LifeLog/HomeViewController.m +++ b/LifeLog/LifeLog/HomeViewController.m @@ -15,8 +15,6 @@ #import #import "ServerAPI.h" -static NSInteger numberTotal = 10000; - @interface HomeViewController () { MBProgressHUD *progressHud; @@ -42,6 +40,7 @@ static NSInteger numberTotal = 10000; @property (nonatomic, assign) int totalRequest; @property (nonatomic, assign) int countComplete; +@property (nonatomic, assign) int currentIndex; @end @@ -111,13 +110,12 @@ static NSInteger numberTotal = 10000; progressHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; progressHud.mode = MBProgressHUDModeIndeterminate; progressHud.detailsLabel.text = NSLocalizedString(@"lifelog.home.progressHud.title", nil); - [self saveDataStep7LastDay]; + // [self saveDataStep7LastDay]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self saveStepForDay:self.dateCurrent]; - [self requestTopByDate:self.dateCurrent]; } - (void)requestTopByDate:(NSDate *)date { @@ -191,20 +189,21 @@ static NSInteger numberTotal = 10000; { self.dateCurrent = [self.dateCurrent dateByAddingTimeInterval:-(24*60*60)]; 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)]; + [self resetStepUI]; [self saveStepForDay:self.dateCurrent]; - [self requestTopByDate:self.dateCurrent]; } - (IBAction)rightButtonTouchUpInside:(id)sender { self.dateCurrent = [self.dateCurrent dateByAddingTimeInterval:24*60*60]; 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)]; - [self saveStepForDay:self.dateCurrent]; + [self resetStepUI]; + //[self saveStepForDay:self.dateCurrent]; [self requestTopByDate:self.dateCurrent]; } - (IBAction)segmentValueChange:(id)sender { - // [self updateStepUI]; + [self resetStepUI]; [self requestTopByDate:self.dateCurrent]; } @@ -212,8 +211,8 @@ static NSInteger numberTotal = 10000; - (void)saveStepForDay:(NSDate *)date { if ([CMMotionActivityManager isActivityAvailable]) { -// [progressHud showAnimated:YES]; -// [progressHud setHidden:NO]; + [progressHud showAnimated:YES]; + [progressHud setHidden:NO]; self.bike = 0; self.walking = 0; self.running = 0; @@ -252,6 +251,9 @@ static NSInteger numberTotal = 10000; } [arrayActivities addObject:activityExtra]; } + // PhongNV + // weakSelf.currentIndex = 0; + // [weakSelf saveStepFromActivityExtras:arrayActivities]; for (CMMotionActivityExtra *activityExtra in arrayActivities) { [weakSelf.pedometer queryPedometerDataFromDate:activityExtra.activity.startDate toDate:activityExtra.endDate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) { @@ -293,44 +295,69 @@ static NSInteger numberTotal = 10000; }); }]; } + }]; }); } -// else { -// [progressHud setHidden:YES]; -// } +} + +- (void)saveStepFromActivityExtras:(NSArray *)activities +{ + if (self.currentIndex >= activities.count) { + self.currentIndex = 0; + dispatch_async(dispatch_get_main_queue(), ^{ + [self updateStepUI]; + }); + return; + } + HomeViewController __weak *weakSelf = self; + CMMotionActivityExtra *activityExtra = [activities objectAtIndex:self.currentIndex]; + [self.pedometer queryPedometerDataFromDate:activityExtra.activity.startDate toDate:activityExtra.endDate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) { + if (weakSelf == nil) { + return ; + } + NSInteger numberStep = [pedometerData.numberOfSteps integerValue]; + int mode = 1; + if (activityExtra.activity.cycling) { + mode = 3; + } + else if (activityExtra.activity.walking) { + mode = 1; + } + else if (activityExtra.activity.running) { + mode = 2; + } + else { + // unknown + } + self.countComplete += 1; + // save step to server + if (numberStep > 0) { + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; + [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; + NSString *dateBegin = [dateFormatter stringFromDate:activityExtra.activity.startDate]; + NSString *dateEnd = [dateFormatter stringFromDate:activityExtra.endDate]; + [[ServerAPI server] requestCreateLog:mode withStep:(int)numberStep startDate:dateBegin endDate:dateEnd CompletionHandler:^(NSError *error) { + if (error) { + NSLog(@"Error: %@", error); + } + weakSelf.currentIndex += 1; + [weakSelf saveStepFromActivityExtras:activities]; + }]; + } + else { + weakSelf.currentIndex += 1; + [weakSelf saveStepFromActivityExtras:activities]; + } + }]; } - (void)updateStepUI { if (self.totalRequest == self.countComplete) { - /* - switch (self.segmentHome.selectedSegmentIndex) { - case 0: - self.lblValueStep.text = [Utilities addCommaFromNumber:self.bike]; - self.lblValueStepOther.text = [NSString stringWithFormat:@"%@ %ld", NSLocalizedString(@"lifelog.today.remaining.other", nil), numberTotal - self.bike]; - self.lblPercent.text = [NSString stringWithFormat:@"%@ %i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), (int)(self.bike*100/numberTotal), NSLocalizedString(@"lifelog.today.percent", nil)]; - break; - - case 1: - self.lblValueStep.text = [Utilities addCommaFromNumber:self.walking]; - self.lblValueStepOther.text = [NSString stringWithFormat:@"%@ %ld", NSLocalizedString(@"lifelog.today.remaining.other", nil), numberTotal - self.walking]; - self.lblPercent.text = [NSString stringWithFormat:@"%@ %i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), (int)(self.walking*100/numberTotal), NSLocalizedString(@"lifelog.today.percent", nil)]; - break; - - case 2: - self.lblValueStep.text = [Utilities addCommaFromNumber:self.running]; - self.lblValueStepOther.text = [NSString stringWithFormat:@"%@ %ld", NSLocalizedString(@"lifelog.today.remaining.other", nil), numberTotal - self.running]; - self.lblPercent.text = [NSString stringWithFormat:@"%@ %i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), (int)(self.running*100/numberTotal), NSLocalizedString(@"lifelog.today.percent", nil)]; - break; - - default: - break; - } - */ + [self requestTopByDate:self.dateCurrent]; self.countComplete = 0; self.totalRequest = 0; - //[progressHud setHidden:YES]; } } @@ -419,4 +446,11 @@ static NSInteger numberTotal = 10000; self.lblPercent.text = [NSString stringWithFormat:@"%@ %i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), [targetInfor.complete_percent intValue], NSLocalizedString(@"lifelog.today.percent", nil)]; } +- (void)resetStepUI +{ + self.lblValueStep.text = @""; + self.lblValueStepOther.text = @""; + self.lblPercent.text = @""; +} + @end diff --git a/LifeLog/LifeLog/ServerAPI.m b/LifeLog/LifeLog/ServerAPI.m index 6c81c8a..496295c 100644 --- a/LifeLog/LifeLog/ServerAPI.m +++ b/LifeLog/LifeLog/ServerAPI.m @@ -57,7 +57,7 @@ NSURLSessionDataTask * searchTask; { self = [super init]; if (self != nil) { - self.timeOutInterval = 150; + self.timeOutInterval = 60; } return self; } @@ -1035,7 +1035,10 @@ NSURLSessionDataTask * searchTask; [request setHTTPBody: encodedData]; } - NSURLSession *session = [NSURLSession sharedSession]; + //NSURLSession *session = [NSURLSession sharedSession]; + NSURLSessionConfiguration *defaultConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; + NSOperationQueue *operationQueue = [NSOperationQueue mainQueue]; + NSURLSession *session = [NSURLSession sessionWithConfiguration:defaultConfiguration delegate:nil delegateQueue:operationQueue]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {