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