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