-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZSWSummaryViewController.m
341 lines (282 loc) · 17.9 KB
/
ZSWSummaryViewController.m
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//
// ZSWSummaryViewController.m
// Productiv
//
// Created by Zachary Shakked on 7/25/14.
// Copyright (c) 2014 Productiv. All rights reserved.
//
#import "ZSWSummaryViewController.h"
#import "ZSWTaskStore.h"
#import <MagicPieLayer.h>
#import "PieLayer.h"
#import "ZSWSummaryTableViewController.h"
@interface ZSWSummaryViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) PieLayer *pieLayer;
@property (nonatomic, strong) PieElement *pieElementGreen;
@property (nonatomic, strong) PieElement *pieElementGray;
@property (nonatomic, strong) UILabel *percentageOfTasksLabel;
@property (nonatomic, strong) UILabel *totalNumberOfTasksLabel;
@property (nonatomic, strong) UILabel *totalNumberOfCompleteTasksLabel;
@property (nonatomic, strong) UILabel *totalNumberOfIncompleteTasksLabel;
@property (nonatomic, strong) UILabel *averageNumberOfTasksPerDayLabel;
@property (nonatomic, strong) UILabel *favoriteCategoryLabel;
@end
@implementation ZSWSummaryViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.view.backgroundColor = [UIColor whiteColor];
CGRect screenRect = self.view.bounds; //the overall big one
CGRect bigRect = screenRect; //the big rect
bigRect.size.height *= 1.3; //enlarge the size by 2.0
self.navigationItem.title = @"Task Summary";
//Create a screen-sized scroll view and add it to the window
self.scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
self.scrollView.contentSize = bigRect.size;
[self.view addSubview:self.scrollView];
//Main View
UIView *view = [[UIView alloc] initWithFrame:bigRect];
view.backgroundColor = [UIColor whiteColor];
[self.scrollView addSubview:view];
//Colors
NSDictionary *colors = [[ZSWTaskStore sharedStore] colors];
UIColor *color = [colors valueForKey:@"productiveGreen"];
UIColor *lightGray = [colors valueForKey:@"lightGray"];
//pieLayer
self.pieLayer = [[PieLayer alloc] init];
self.pieLayer.frame = CGRectMake(0, 30, self.view.frame.size.width, 300);
self.pieElementGreen = [PieElement pieElementWithValue:3 color:color];
self.pieElementGray = [PieElement pieElementWithValue:3 color:lightGray];
[self.pieLayer addValues:@[self.pieElementGreen, self.pieElementGray] animated:YES];
[self.scrollView.layer addSublayer:self.pieLayer];
//percentageOfTasks
CGRect percentageOfTasksLabelFrame = CGRectMake(10, 10, self.view.frame.size.width - 20, 60);
self.percentageOfTasksLabel = [[UILabel alloc] initWithFrame:percentageOfTasksLabelFrame];
self.percentageOfTasksLabel.textAlignment = NSTextAlignmentCenter;
self.percentageOfTasksLabel.textColor = color;
self.percentageOfTasksLabel.backgroundColor = [UIColor whiteColor];
self.percentageOfTasksLabel.numberOfLines = 0;
[[self.percentageOfTasksLabel layer] setBorderColor:color.CGColor];
[[self.percentageOfTasksLabel layer] setBorderWidth:1.0];
[[self.percentageOfTasksLabel layer] setCornerRadius:7.0];
self.percentageOfTasksLabel.font = [UIFont fontWithName:@"Avenir" size:17.0];
[self.scrollView addSubview:self.percentageOfTasksLabel];
//totalNumberOfTasks
UIButton *totalNumberOfTasksButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 290, self.view.frame.size.width - 20, 30)];
[totalNumberOfTasksButton addTarget:self
action:@selector(showAllTasks)
forControlEvents:UIControlEventTouchUpInside];
totalNumberOfTasksButton.backgroundColor = [UIColor clearColor];
[totalNumberOfTasksButton setTitle:@"" forState:UIControlStateNormal];
CGRect totalNumberOfTasksLabelFrame = CGRectMake(10, 290, self.view.frame.size.width - 20, 30);
self.totalNumberOfTasksLabel = [[UILabel alloc] initWithFrame:totalNumberOfTasksLabelFrame];
[[self.totalNumberOfTasksLabel layer] setCornerRadius:7.0];
self.totalNumberOfTasksLabel.clipsToBounds = YES;
self.totalNumberOfTasksLabel.textAlignment = NSTextAlignmentCenter;
self.totalNumberOfTasksLabel.textColor = [UIColor whiteColor];
self.totalNumberOfTasksLabel.backgroundColor = color;
self.totalNumberOfTasksLabel.numberOfLines = 0;
self.totalNumberOfTasksLabel.font = [UIFont fontWithName:@"Avenir" size:16.0];
[self.scrollView addSubview:totalNumberOfTasksButton];
[self.scrollView addSubview:self.totalNumberOfTasksLabel];
//totalNumberOfCompleteTasks
UIButton *totalNumberOfCompleteTasksButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 330, self.view.frame.size.width - 20, 30)];
[totalNumberOfCompleteTasksButton addTarget:self
action:@selector(showAllCompleteTasks)
forControlEvents:UIControlEventTouchUpInside];
totalNumberOfTasksButton.backgroundColor = [UIColor clearColor];
[totalNumberOfTasksButton setTitle:@"" forState:UIControlStateNormal];
CGRect totalNumberOfCompleteTasksLabelFrame = CGRectMake(10, 330, self.view.frame.size.width - 20, 30);
self.totalNumberOfCompleteTasksLabel = [[UILabel alloc] initWithFrame:totalNumberOfCompleteTasksLabelFrame];
self.totalNumberOfCompleteTasksLabel.textAlignment = NSTextAlignmentCenter;
self.totalNumberOfCompleteTasksLabel.textColor = [UIColor whiteColor];
self.totalNumberOfCompleteTasksLabel.backgroundColor = color;
self.totalNumberOfCompleteTasksLabel.numberOfLines = 0;
[[self.totalNumberOfCompleteTasksLabel layer] setCornerRadius:7.0];
self.totalNumberOfCompleteTasksLabel.clipsToBounds = YES;
self.totalNumberOfCompleteTasksLabel.font = [UIFont fontWithName:@"Avenir" size:16.0];
[self.scrollView addSubview:totalNumberOfCompleteTasksButton];
[self.scrollView addSubview:self.totalNumberOfCompleteTasksLabel];
//totalNumberOfIncompleteTasks
UIButton *totalNumberOfIncompleteTasksButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 370, self.view.frame.size.width - 20, 30)];
[totalNumberOfIncompleteTasksButton addTarget:self
action:@selector(showAllIncompleteTasks)
forControlEvents:UIControlEventTouchUpInside];
totalNumberOfIncompleteTasksButton.backgroundColor = [UIColor clearColor];
[totalNumberOfIncompleteTasksButton setTitle:@"" forState:UIControlStateNormal];
CGRect totalNumberOfIncompleteTasksLabelFrame = CGRectMake(10, 370, self.view.frame.size.width - 20, 30);
self.totalNumberOfIncompleteTasksLabel = [[UILabel alloc] initWithFrame:totalNumberOfIncompleteTasksLabelFrame];
self.totalNumberOfIncompleteTasksLabel.textAlignment = NSTextAlignmentCenter;
self.totalNumberOfIncompleteTasksLabel.textColor = [UIColor whiteColor];
self.totalNumberOfIncompleteTasksLabel.backgroundColor = color;
self.totalNumberOfIncompleteTasksLabel.numberOfLines = 0;
[[self.totalNumberOfIncompleteTasksLabel layer] setCornerRadius:7.0];
self.totalNumberOfIncompleteTasksLabel.clipsToBounds = YES;
self.totalNumberOfIncompleteTasksLabel.font = [UIFont fontWithName:@"Avenir" size:16.0];
[self.scrollView addSubview:totalNumberOfIncompleteTasksButton];
[self.scrollView addSubview:self.totalNumberOfIncompleteTasksLabel];
//Avg number of tasks per day
CGRect averageNumberOfTasksPerDayFrame = CGRectMake(10, 410, self.view.frame.size.width - 20, 30);
self.averageNumberOfTasksPerDayLabel = [[UILabel alloc] initWithFrame:averageNumberOfTasksPerDayFrame];
self.averageNumberOfTasksPerDayLabel.textAlignment = NSTextAlignmentCenter;
self.averageNumberOfTasksPerDayLabel.textColor = color;
self.averageNumberOfTasksPerDayLabel.backgroundColor = [UIColor whiteColor];
self.averageNumberOfTasksPerDayLabel.numberOfLines = 0;
[[self.averageNumberOfTasksPerDayLabel layer] setBorderColor:color.CGColor];
[[self.averageNumberOfTasksPerDayLabel layer] setBorderWidth:1.0];
[[self.averageNumberOfTasksPerDayLabel layer] setCornerRadius:7.0];
self.averageNumberOfTasksPerDayLabel.font = [UIFont fontWithName:@"Avenir" size:16.0];
[self.scrollView addSubview:self.averageNumberOfTasksPerDayLabel];
//Favorite Category
CGRect favoriteCategoryFrame = CGRectMake(10, 450, self.view.frame.size.width - 20, 30);
self.favoriteCategoryLabel = [[UILabel alloc] initWithFrame:favoriteCategoryFrame];
self.favoriteCategoryLabel.textAlignment = NSTextAlignmentCenter;
self.favoriteCategoryLabel.textColor = color;
self.favoriteCategoryLabel.backgroundColor = [UIColor whiteColor];
self.favoriteCategoryLabel.numberOfLines = 0;
[[self.favoriteCategoryLabel layer] setBorderColor:color.CGColor];
[[self.favoriteCategoryLabel layer] setBorderWidth:1.0];
[[self.favoriteCategoryLabel layer] setCornerRadius:7.0];
self.favoriteCategoryLabel.font = [UIFont fontWithName:@"Avenir" size:16.0];
[self.scrollView addSubview:self.favoriteCategoryLabel];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
NSDictionary *stats = [[ZSWTaskStore sharedStore] stats];
//Colors
NSDictionary *colors = [[ZSWTaskStore sharedStore] colors];
UIColor *color = [colors valueForKey:@"productiveGreen"];
UIColor *lightGray = [colors valueForKey:@"lightGray"];
//data
NSNumber *totalNumberOfTasks = [stats valueForKey:@"totalNumberOfTasks"];
NSNumber *totalNumberOfCompleteTasks = [stats valueForKey:@"totalNumberOfCompleteTasks"];
NSNumber *totalNumberOfIncompleteTasks = [stats valueForKey:@"totalNumberOfIncompleteTasks"];
NSNumber *averageNumberOfTasksPerDay = [stats valueForKey:@"averageNumberOfTasksPerDay"];
NSManagedObject *favoriteCategory = [stats valueForKey:@"favoriteCategory"];
float percentageOfTasksCompleted = totalNumberOfCompleteTasks.floatValue / totalNumberOfTasks.floatValue;
percentageOfTasksCompleted *= 100;
//totalNumberOfTasks
NSString *percentageCompletedString = [NSString stringWithFormat:@"You've completed %.2f%% of your tasks.", percentageOfTasksCompleted];
NSRange rangeForBoldedPercentage = [percentageCompletedString rangeOfString:@"."];
NSRange range;
if (percentageOfTasksCompleted > 99.99) {
range = NSMakeRange(rangeForBoldedPercentage.location - 3, 7);
}else if (percentageOfTasksCompleted > 9.99) {
range = NSMakeRange(rangeForBoldedPercentage.location - 2, 6);
}else {
range = NSMakeRange(rangeForBoldedPercentage.location - 1, 5);
}
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:percentageCompletedString];
if (totalNumberOfTasks.intValue != 0) {
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Avenir-Heavy" size:16.0] range:range];
self.percentageOfTasksLabel.attributedText = string;
} else {
self.percentageOfTasksLabel.text = @"You haven't made any tasks.";
}
//Pie Graph
[PieElement animateChanges:^{
NSLog(@"complete: %@", totalNumberOfCompleteTasks);
NSLog(@"incomplete: %@", totalNumberOfIncompleteTasks);
self.pieElementGreen.val = totalNumberOfCompleteTasks.intValue;
self.pieElementGreen.color = color;
self.pieElementGray.val = totalNumberOfIncompleteTasks.intValue;
self.pieElementGray.color = lightGray;
}];
//totalNumberOfTasks
NSString *totalNumberOfTasksString = [NSString stringWithFormat:@"Total number of tasks: %d", totalNumberOfTasks.intValue];
NSRange rangeForColon = [totalNumberOfTasksString rangeOfString:@":"];
NSInteger length = [totalNumberOfTasks.stringValue length];
NSRange rangeForTotalNumberOfTasks = NSMakeRange(rangeForColon.location + 2, length);
NSMutableAttributedString *totalNumberOfTasksStringFormatted = [[NSMutableAttributedString alloc] initWithString:totalNumberOfTasksString];
[totalNumberOfTasksStringFormatted addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Avenir-Heavy" size:16.0] range:rangeForTotalNumberOfTasks];
self.totalNumberOfTasksLabel.attributedText = totalNumberOfTasksStringFormatted;
//totalNumberOfCompleteTasks
NSString *totalNumberOfCompleteTasksString = [NSString stringWithFormat:@"Total number of complete tasks: %d", totalNumberOfCompleteTasks.intValue];
rangeForColon = [totalNumberOfCompleteTasksString rangeOfString:@":"];
length = [totalNumberOfCompleteTasks.stringValue length];
NSRange rangeForTotalNumberOfCompleteTasks = NSMakeRange(rangeForColon.location + 2, length);
NSMutableAttributedString *totalNumberOfCompleteTasksFormatted = [[NSMutableAttributedString alloc] initWithString:totalNumberOfCompleteTasksString];
[totalNumberOfCompleteTasksFormatted addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Avenir-Heavy" size:16.0] range:rangeForTotalNumberOfCompleteTasks];
self.totalNumberOfCompleteTasksLabel.attributedText = totalNumberOfCompleteTasksFormatted;
//totalNumberOfIncompleteTasks
NSString *totalNumberOfIncompleteTasksString = [NSString stringWithFormat:@"Total number of incomplete tasks: %d", totalNumberOfIncompleteTasks.intValue];
rangeForColon = [totalNumberOfIncompleteTasksString rangeOfString:@":"];
length = [totalNumberOfIncompleteTasks.stringValue length];
NSRange rangeForTotalNumberOfIncompleteTasks = NSMakeRange(rangeForColon.location + 2, length);
NSMutableAttributedString *totalNumberOfIncompleteTasksStringFormatted = [[NSMutableAttributedString alloc] initWithString:totalNumberOfIncompleteTasksString];
[totalNumberOfIncompleteTasksStringFormatted addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Avenir-Heavy" size:16.0] range:rangeForTotalNumberOfIncompleteTasks];
self.totalNumberOfIncompleteTasksLabel.attributedText = totalNumberOfIncompleteTasksStringFormatted;
//averageNumberOfTasksPerDay
NSString *averageNumberOfTasksPerDayString = [NSString stringWithFormat:@"Average number of tasks per day: %.2f", averageNumberOfTasksPerDay.floatValue];
rangeForColon = [averageNumberOfTasksPerDayString rangeOfString:@":"];
length = [averageNumberOfTasksPerDay.stringValue length];
NSRange rangeForAverageNumberOfTasksPerDay;
if (averageNumberOfTasksPerDay.floatValue < 9.9999999999999) {
rangeForAverageNumberOfTasksPerDay = NSMakeRange(rangeForColon.location + 2, 4);
} else if(averageNumberOfTasksPerDay.floatValue < 99.99999999999) {
rangeForAverageNumberOfTasksPerDay = NSMakeRange(rangeForColon.location + 2, 5);
} else if (averageNumberOfTasksPerDay.floatValue < 999.9999999999999) {
rangeForAverageNumberOfTasksPerDay = NSMakeRange(rangeForColon.location + 2, 6);
}else {
rangeForAverageNumberOfTasksPerDay = NSMakeRange(rangeForColon.location + 2, 3);
}
NSMutableAttributedString *averageNumberOfTasksPerDayStringFormatted = [[NSMutableAttributedString alloc] initWithString:averageNumberOfTasksPerDayString];
[averageNumberOfTasksPerDayStringFormatted addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Avenir-Heavy" size:16.0] range:rangeForAverageNumberOfTasksPerDay];
self.averageNumberOfTasksPerDayLabel.attributedText = averageNumberOfTasksPerDayStringFormatted;
//favoriteCategory
if ([favoriteCategory valueForKey:@"name"]) {
NSString *favoriteCategoryString = [NSString stringWithFormat:@"Favorite Category: %@", [favoriteCategory valueForKey:@"name"]];
rangeForColon = [favoriteCategoryString rangeOfString:@":"];
length = [[favoriteCategory valueForKey:@"name"] length];
NSRange rangeForFavoriteCategory = NSMakeRange(rangeForColon.location + 2, length);
NSMutableAttributedString *favoriteCategoryStringFormatted = [[NSMutableAttributedString alloc] initWithString:favoriteCategoryString];
[favoriteCategoryStringFormatted addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Avenir-Heavy" size:16.0] range:rangeForFavoriteCategory];
self.favoriteCategoryLabel.attributedText = favoriteCategoryStringFormatted;
} else {
self.favoriteCategoryLabel.text = @"No Favorite Category.";
}
}
- (void)showAllTasks
{
ZSWSummaryTableViewController *tbct = [[ZSWSummaryTableViewController alloc] init];
tbct.segIndex = 0;
[self.navigationController pushViewController:tbct animated:YES];
}
- (void)showAllCompleteTasks
{
ZSWSummaryTableViewController *tbct = [[ZSWSummaryTableViewController alloc] init];
tbct.segIndex = 1;
[self.navigationController pushViewController:tbct animated:YES];
}
- (void)showAllIncompleteTasks
{
ZSWSummaryTableViewController *tbct = [[ZSWSummaryTableViewController alloc] init];
tbct.segIndex = 2;
[self.navigationController pushViewController:tbct animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end