TodayViewController.m
5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// TodayViewController.m
// LifeLog
//
// Created by Nguyen Van Phong on 7/29/17.
// Copyright © 2017 PhongNV. All rights reserved.
//
#import "TodayViewController.h"
#import <CircleProgressBar/CircleProgressBar.h>
#import <CoreMotion/CoreMotion.h>
#import "NSDate+helper.h"
#import "Utilities.h"
static NSInteger numberTotal = 10000;
@interface TodayViewController ()
@property (nonatomic, weak) IBOutlet UILabel *lblTitle;
@property (weak, nonatomic) IBOutlet CircleProgressBar *circleProgressToday;
@property (weak, nonatomic) IBOutlet UILabel *lblValueStep;
@property (weak, nonatomic) IBOutlet UILabel *lblTotalStep;
@property (weak, nonatomic) IBOutlet UILabel *lblRemainingStep;
@property (weak, nonatomic) IBOutlet UILabel *lblTotalStepOther;
@property (weak, nonatomic) IBOutlet UILabel *lblRemainingStepOther;
@property (weak, nonatomic) IBOutlet UILabel *lblPercent;
@property (nonatomic, strong) CMPedometer *pedometer;
@property (nonatomic, strong) CMMotionActivityManager *motionActivityManager;
@property (nonatomic, strong) NSOperationQueue *operationQueue;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) BOOL isRequesting;
@end
@implementation TodayViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.lblTitle.text = NSLocalizedString(@"lifelog.today.title", nil);
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.lblTitle attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
if ([CMPedometer isStepCountingAvailable]) {
_pedometer = [[CMPedometer alloc] init];
}
if ([CMMotionActivityManager isActivityAvailable]) {
_motionActivityManager = [[CMMotionActivityManager alloc] init];
}
self.isRequesting = NO;
if (_targetStep <= 0) {
_targetStep = numberTotal;
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
_timer = [NSTimer scheduledTimerWithTimeInterval:0.3f target:self selector:@selector(countStep) userInfo:nil repeats:YES];
[_timer fire];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_timer invalidate];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - IBAction
- (IBAction)menuButtonTouchUpInside:(id)sender
{
}
- (IBAction)backButtonTouchUpInside:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)buttonShareTouchUpInside:(id)sender {
}
- (IBAction)buttonFacebookTouchUpInside:(id)sender {
NSString * content = @"Finish 500 steps";
TodayViewController __weak *weakSelf = self;
[Utilities shareFacebook:content withViewController:weakSelf];
}
- (IBAction)buttonLineTouchUpInside:(id)sender {
NSString * content = @"Finish 500 steps";
TodayViewController __weak *weakSelf = self;
[Utilities shareLine:content withViewController:weakSelf];
}
#pragma mark - Functions Private
- (void)countStep
{
if (self.isRequesting == YES) {
return;
}
if ([CMMotionActivityManager isActivityAvailable]) {
self.isRequesting = YES;
NSDate *endDate = [NSDate date];
NSDate *startDate = [endDate beginningAtMidnightOfDay];
TodayViewController __weak *weakSelf = self;
dispatch_queue_t myQueue = dispatch_queue_create("mobileworld.jp.lifelog", NULL);
dispatch_async(myQueue, ^{
if (weakSelf == nil) {
return ;
}
[weakSelf.pedometer queryPedometerDataFromDate:startDate toDate:endDate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
NSInteger numberStep = [pedometerData.numberOfSteps integerValue];
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf updateStepUI:numberStep];
});
}];
});
}
}
- (void)updateStepUI:(NSInteger)numberStep
{
// NSLog(@"Number of step: %ld", numberStep);
self.isRequesting = NO;
float valueProgress = numberStep*1.f/_targetStep;
[self.circleProgressToday setProgress:valueProgress animated:YES];
self.lblValueStep.text = [Utilities addCommaFromNumber:numberStep];
self.lblTotalStep.text = [NSString stringWithFormat:@"/ %@", [Utilities addCommaFromNumber:_targetStep]];
self.lblRemainingStep.text = [NSString stringWithFormat:@"%@%ld%@", NSLocalizedString(@"lifelog.today.remaining.step.1", nil), (_targetStep - numberStep), NSLocalizedString(@"lifelog.today.remaining.step.2", nil)];
self.lblTotalStepOther.text = [NSString stringWithFormat:@"%@%@%@", NSLocalizedString(@"lifelog.today.total.other", nil), [Utilities addCommaFromNumber:_targetStep], NSLocalizedString(@"lifelog.today.unit.step", nil)];
self.lblRemainingStepOther.text = [NSString stringWithFormat:@"%@%ld%@", NSLocalizedString(@"lifelog.today.remaining.other", nil), (_targetStep - numberStep), NSLocalizedString(@"lifelog.today.unit.step", nil)];
self.lblPercent.text = [NSString stringWithFormat:@"%@%i%@", NSLocalizedString(@"lifelog.today.text.percent", nil), (int)(numberStep*100/_targetStep), NSLocalizedString(@"lifelog.today.percent", nil)];
}
@end