HistoryViewController.m
12.2 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//
// HistoryViewController.m
// LifeLog
//
// Created by Nguyen Van Phong on 7/25/17.
// Copyright © 2017 PhongNV. All rights reserved.
//
#import "HistoryViewController.h"
#import "Utilities.h"
#import "ServerAPI.h"
#import "HistoryListTableViewCell.h"
@interface HistoryViewController ()
@end
@implementation HistoryViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = NSLocalizedString(@"lifelog.history.title", nil);
_isDisableLoadMore = true;
[self setupView];
[self setupChartView];
_startDate = [NSDate date];
_endDate = _startDate;
self.lblDatetime.text = [Utilities stringFromDate:_endDate withFormat:@"YYYY年MM月dd日 EEEE" locale:@"ja_JP"];
[self checkRequestData];
//register nib for table view
[self.tableBase registerNib:[UINib nibWithNibName:@"HistoryListTableViewCell" bundle:nil] forCellReuseIdentifier:@"HistoryListCell"];
}
- (void)viewDidAppear:(BOOL) animated
{
[super viewDidAppear:animated];
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 400);
[self.scrollView setNeedsLayout];
[self.scrollView setNeedsUpdateConstraints];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setupView {
NSArray *typeTitle = [NSArray arrayWithObjects:NSLocalizedString(@"lifelog.history.type.1", nil), NSLocalizedString(@"lifelog.history.type.2", nil), NSLocalizedString(@"lifelog.history.type.3", nil), NSLocalizedString(@"lifelog.history.type.4", nil), NSLocalizedString(@"lifelog.history.type.5", nil), nil];
[self.viewCollectionType setButtonNumber:typeTitle.count];
[self.viewCollectionType setSpacing:2];
[self.viewCollectionType setArrayTitle:typeTitle];
self.viewCollectionType.changeCurrentIndex = ^(int index){
[self changeDate];
};
NSArray *modeTitle = [NSArray arrayWithObjects:NSLocalizedString(@"lifelog.history.mode.1", nil), NSLocalizedString(@"lifelog.history.mode.2", nil), NSLocalizedString(@"lifelog.history.mode.3", nil), nil];
[self.viewCollectionMode setButtonNumber:modeTitle.count];
[self.viewCollectionMode setSpacing:0];
[self.viewCollectionMode setCornerRadius:0];
[self.viewCollectionMode setNormalColor:[Utilities convertHecToColor:0x191919] highlightColor:[Utilities convertHecToColor:0x474747] textColor:[UIColor whiteColor]];
[self.viewCollectionMode setArrayTitle:modeTitle];
self.viewCollectionMode.changeCurrentIndex = ^(int index){
if(self.tableBase.alpha == 0.0) {
[self updateView];
}
else {
NSArray * list = [_curListArray objectAtIndex:index];
[self updateTableData:list error:nil];
}
};
NSArray *shareTitle = [NSArray arrayWithObjects:NSLocalizedString(@"lifelog.history.share.1", nil), NSLocalizedString(@"lifelog.history.share.2", nil), NSLocalizedString(@"lifelog.history.share.3", nil), NSLocalizedString(@"lifelog.history.share.4", nil), NSLocalizedString(@"lifelog.history.share.5", nil), nil];
[self.viewCollectionShare setButtonNumber:typeTitle.count];
[self.viewCollectionShare setSpacing:3];
[self.viewCollectionShare setArrayTitle:shareTitle];
[self.viewCollectionShare disableSelection];
//add tap gesture for enable tap on gesture on CollectionView in ScrollView
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
[recognizer setNumberOfTapsRequired:1];
self.scrollView.userInteractionEnabled = YES;
[self.scrollView addGestureRecognizer:recognizer];
}
- (void)setupChartView {
self.viewBarChart.chartDescription.enabled = NO;
self.viewBarChart.leftAxis.drawGridLinesEnabled = NO;
self.viewBarChart.rightAxis.drawGridLinesEnabled = NO;
self.viewBarChart.legend.enabled = NO;
ChartXAxis *xAxis = self.viewBarChart.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.drawGridLinesEnabled = NO;
xAxis.drawAxisLineEnabled = NO;
xAxis.drawLabelsEnabled = YES;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.labelFont = [UIFont systemFontOfSize:10.f];
xAxis.labelTextColor = [UIColor whiteColor];
xAxis.granularity = 1.0; // only intervals of 1 day
xAxis.labelCount = 8;
self.viewBarChart.leftAxis.drawAxisLineEnabled = NO;
self.viewBarChart.rightAxis.drawAxisLineEnabled = NO;
}
-(void) updateView {
HistoryObject * obj = [_curHisArray objectAtIndex:self.viewCollectionMode.getCurrentIndex];
self.lblStep.text = [NSString stringWithFormat:@"%d step", obj.step];
self.lblCircleStep.text = self.lblStep.text;
self.lblRemaining.text = [NSString stringWithFormat:@"%d step", obj.missing];
self.lblCircleRemain.text = [NSString stringWithFormat:@"目標まであと\n%d stepです", obj.missing];
self.lblPercent.text = [NSString stringWithFormat:@"%0.2f%%", obj.percent];
self.lblCalories.text = [NSString stringWithFormat:@"%0.2f kcal", obj.calories];
self.lblDistance.text = [NSString stringWithFormat:@"%0.1f KM", obj.distance];
self.lblTime.text = [Utilities convertSecondToShortTime:obj.time];
[self updateGraphView];
}
-(void) updateGraphView {
HistoryObject * obj = [_curHisArray objectAtIndex:self.viewCollectionMode.getCurrentIndex];
NSMutableArray *yVals = [[NSMutableArray alloc] init];
for (int i = 0; i < obj.dataGraph.count; i++)
{
[yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:[[obj.dataGraph objectAtIndex:i] doubleValue]]];
}
BarChartDataSet *set1 = nil;
if (self.viewBarChart.data.dataSetCount > 0)
{
set1 = (BarChartDataSet *)self.viewBarChart.data.dataSets[0];
set1.values = yVals;
[self.viewBarChart.data notifyDataChanged];
[self.viewBarChart notifyDataSetChanged];
}
else
{
set1 = [[BarChartDataSet alloc] initWithValues:yVals label:@""];
[set1 setColor:[UIColor whiteColor]];
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
data.barWidth = 0.5f;
self.viewBarChart.data = data;
}
}
-(void) checkRequestData {
if(self.tableBase.alpha == 0.0) {
NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
MBProgressHUD *hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
[[ServerAPI server] requestHistory:token startDate:_startDate endDate:_endDate CompletionHandler:^(NSArray *array, NSError *error) {
HistoryViewController __weak *weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
if(hudView != nil) {
[hudView hideAnimated:true];
}
});
if(error == nil) {
_curHisArray = array;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf updateView];
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *message = [error.userInfo objectForKey:@"message"];
[Utilities showErrorMessage:message withViewController:weakSelf];
});
}
}];
}
else {
[self resetData];
}
}
-(void) callRequestToUpdateData {
[super callRequestToUpdateData];
NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
MBProgressHUD *hudView = nil;
if(_curPage == 1 && !self.refreshControl.isRefreshing) {
hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
}
[[ServerAPI server] requestHistoryList:token startDate:_startDate endDate:_endDate CompletionHandler:^(NSArray *array, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if(hudView != nil) {
[hudView hideAnimated:true];
}
});
HistoryViewController __weak *weakSelf = self;
if(error == nil) {
_curListArray = array;
NSArray * list = [array objectAtIndex:weakSelf.viewCollectionMode.getCurrentIndex];
[weakSelf updateTableData:list error:error];
}
else {
[weakSelf updateTableData:array error:error];
}
}];
}
- (void) changeDate {
switch (self.viewCollectionType.getCurrentIndex) {
case 1:
_startDate = [_endDate dateByAddingTimeInterval:-86400 * 7];
break;
case 2:
_startDate = [_endDate dateByAddingTimeInterval:-86400 * 30];
break;
case 3:
_startDate = [_endDate dateByAddingTimeInterval:-86400 * 30 * 3];
break;
case 4:
_startDate = [_endDate dateByAddingTimeInterval:-86400 * 30 * 6];
break;
default:
_startDate = _endDate;
break;
}
if(_startDate == _endDate) {
self.lblDatetime.text = [Utilities stringFromDate:_endDate withFormat:@"YYYY年MM月dd日 EEEE" locale:@"ja_JP"];
}
else {
NSString * startDateString = [Utilities stringFromDate:_startDate withFormat:@"YYYY年MM月dd日" locale:@"ja_JP"];
NSString * endDateString = [Utilities stringFromDate:_endDate withFormat:@"YYYY年MM月dd日" locale:@"ja_JP"];
self.lblDatetime.text = [NSString stringWithFormat:@"%@-%@", startDateString, endDateString];
}
[self checkRequestData];
}
#pragma mark IBAction
-(void) swipeAction:(UISwipeGestureRecognizer *)sender {
bool alphaValue = self.scrollView.alpha == 1.0 ? 1.0 : 0.0;
[UIView animateWithDuration:0.5 animations:^{
self.tableBase.alpha = alphaValue;
self.scrollView.alpha = 1.0 - alphaValue;
} completion:^(BOOL completed) {
[self checkRequestData];
}];
}
- (IBAction)clickBackward:(UIButton *)sender {
_endDate = [_endDate dateByAddingTimeInterval:-86400];
[self changeDate];
}
- (IBAction)clickForward:(UIButton *)sender {
_endDate = [_endDate dateByAddingTimeInterval:86400];
[self changeDate];
}
-(void)gestureAction:(UITapGestureRecognizer *) sender
{
CGPoint touchLocation = [sender locationOfTouch:0 inView:self.viewCollectionShare];
NSIndexPath *indexPath = [self.viewCollectionShare.collectionView indexPathForItemAtPoint:touchLocation];
NSString * content = @"Finish 500 steps";
HistoryViewController __weak *weakSelf = self;
if(indexPath != NULL) {
switch (indexPath.row) {
case 0: //share facebook
[Utilities shareFacebook:content withViewController:weakSelf];
break;
case 1: //share twitter
[Utilities shareTwitter:content withViewController:weakSelf];
break;
case 2 : //share line
[Utilities shareLine:content withViewController:weakSelf];
break;
case 3: // share email
[Utilities shareEmail:content withViewController:weakSelf];
break;
default: //share other
[Utilities shareOther:content withViewController:weakSelf];
break;
}
}
}
#pragma mark UITableView Delegate
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HistoryListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HistoryListCell"];
HistoryObject * obj = [_curDataList objectAtIndex:indexPath.row];
cell.lblTitle.text = [Utilities stringFromDate:obj.date withFormat:@"dd日 (EEEE)" locale:@"ja_JP"];
cell.lblStep.text = [NSString stringWithFormat:@"%d", obj.step];
cell.lblDiff.text = [NSString stringWithFormat:@"%d", obj.step_diff];
cell.imgArrow.image = (obj.step_diff > 0) ? [UIImage imageNamed:@"arrow_incre"] : [UIImage imageNamed:@"arrow_decre"];
cell.lblPower.text = [NSString stringWithFormat:@"%0.2f", obj.calories];
cell.lblDistance.text = [NSString stringWithFormat:@"%0.1f", obj.distance];
cell.lblDuration.text = [Utilities convertSecondToShortTime:obj.time];
return cell;
}
@end