-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTORatioVC.m
More file actions
348 lines (291 loc) · 15.4 KB
/
RTORatioVC.m
File metadata and controls
348 lines (291 loc) · 15.4 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
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
342
343
344
345
346
347
348
//
// RTORatioVC.m
// Ratio
//
// Created by Matthew Hillman on 9/21/13.
// Copyright (c) 2013 Matthew Hillman. All rights reserved.
//
#import "RTORatioVC.h"
#import "RTORatioCVC.h"
#import "RTOListCVC.h"
#import "RTOCacluationCell.h"
#import "RTOUnitConverter.h"
#import "UIColor+RGB.h"
@interface RTORatioVC () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate, RTOCalculationDelegate, UITextFieldDelegate>
@property (nonatomic, strong) UIActionSheet *unitsSheet;
@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipe;
@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipe;
@property (weak, nonatomic) IBOutlet UIButton *amountButton;
@property (nonatomic, strong) UIView *changeView;
@end
@implementation RTORatioVC
- (IBAction)changeRatioView
{
[self changeRatioViewWithIndex:self.viewSelectSegmentedControl.selectedSegmentIndex animated:YES];
}
#define ANIMATION_DURATION 0.25
- (void)changeRatioViewWithIndex:(NSInteger)index animated:(BOOL)animated
{
CGFloat dx = -1 * index * self.view.bounds.size.width;
CGFloat dy = 0;
if (animated) {
CABasicAnimation *ra = [CABasicAnimation animationWithKeyPath:@"transform"];
ra.autoreverses = NO;
ra.duration = ANIMATION_DURATION;
ra.fromValue = [NSValue valueWithCATransform3D:self.ratioCollectionView.layer.transform];
ra.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(self.ratioCollectionView.layer.bounds.origin.x + dx, dy, 0)];
CABasicAnimation *ta = [CABasicAnimation animationWithKeyPath:@"transform"];
ta.autoreverses = NO;
ta.duration = ANIMATION_DURATION;
ta.fromValue = [NSValue valueWithCATransform3D:self.calculateView.layer.transform];
ta.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(self.calculateView.layer.bounds.origin.x + dx, dy, 0)];
CABasicAnimation *txta = [CABasicAnimation animationWithKeyPath:@"transform"];
txta.autoreverses = NO;
txta.duration = ANIMATION_DURATION;
txta.fromValue = [NSValue valueWithCATransform3D:self.instructionsTextView.layer.transform];
txta.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(self.instructionsTextView.layer.bounds.origin.x + dx, dy, 0)];
[self.ratioCollectionView.layer addAnimation:ra forKey:nil];
[self.calculateView.layer addAnimation:ta forKey:nil];
[self.instructionsTextView.layer addAnimation:txta forKey:nil];
}
self.ratioCollectionView.layer.transform = CATransform3DMakeTranslation(self.ratioCollectionView.layer.bounds.origin.x + dx, 0, 0);
self.calculateView.layer.transform = CATransform3DMakeTranslation(self.calculateView.layer.bounds.origin.x + dx, 0, 0);
self.instructionsTextView.layer.transform = CATransform3DMakeTranslation(self.instructionsTextView.layer.bounds.origin.x + dx, 0, 0);
}
- (void)changeViewWithSwipe:(UISwipeGestureRecognizer *)sender
{
NSInteger starting = self.viewSelectSegmentedControl.selectedSegmentIndex;
switch (sender.direction) {
case UISwipeGestureRecognizerDirectionLeft:
if (self.viewSelectSegmentedControl.selectedSegmentIndex < 2) self.viewSelectSegmentedControl.selectedSegmentIndex++;
break;
case UISwipeGestureRecognizerDirectionRight:
if (self.viewSelectSegmentedControl.selectedSegmentIndex > 0) self.viewSelectSegmentedControl.selectedSegmentIndex--;
break;
default:
break;
}
if (starting != self.viewSelectSegmentedControl.selectedSegmentIndex) {
[self changeRatioView];
}
}
#pragma mark Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.ratio.ingredients count] + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier;
if (indexPath.section == 0) {
if (indexPath.item == 0) {
cellIdentifier = @"calcHeading";
} else if (indexPath.item > [self.ratio.ingredients count]) {
cellIdentifier = @"saveRow";
} else {
cellIdentifier = @"calcRow";
}
} else {
cellIdentifier = @"variationRow";
}
UITableViewCell *cell = [self.calculateTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if ([cell isKindOfClass:[RTOCacluationCell class]]) {
RTOCacluationCell *rcc = (RTOCacluationCell *)cell;
RTOIngredient *ingredient = self.ratio.ingredients[indexPath.item-1];
rcc.quantity.text = [ingredient.amountInRecipe quantityAsString];
rcc.quantity.placeholder = rcc.quantity.text;
rcc.quantity.inputAccessoryView = [self inputAccessoryView:rcc.quantity];
rcc.quantity.delegate = self;
[rcc.unitButton setTitle:[ingredient.amountInRecipe.unit capitalizedString] forState:UIControlStateNormal];
rcc.ingredientLabel.text = [ingredient.name capitalizedString];
rcc.ingredientLabel.alpha = [[ingredient.amountInRecipe.unit capitalizedString] isEqualToString:rcc.ingredientLabel.text] ? 0 : 1;
rcc.ratioPieView.ratio = self.ratio;
rcc.ratioPieView.indexPath = indexPath;
rcc.delegate = self;
if (indexPath.item == [self.ratio.ingredients count]) {
rcc.separatorInset = UIEdgeInsetsZero;
rcc.indentationLevel = 0;
}
}
return cell;
}
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//
//}
- (UIView *)inputAccessoryView:(UITextField *)sender {
CGRect accessFrame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 44.0);
UIView *inputAccessoryView = [[UIView alloc] initWithFrame:accessFrame];
inputAccessoryView.backgroundColor = [UIColor groupTableViewBackgroundColor];
UIButton *compButton = [UIButton buttonWithType:UIButtonTypeSystem];
compButton.frame = CGRectMake(self.view.bounds.size.width - 88.0, 5.0, 70.0, 34.0);
[compButton setTitle: @"Done" forState:UIControlStateNormal];
[compButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
[compButton setTitleColor:self.view.tintColor forState:UIControlStateNormal];
[compButton addTarget:sender action:@selector(resignFirstResponder) forControlEvents:UIControlEventTouchUpInside];
[inputAccessoryView addSubview:compButton];
return inputAccessoryView;
}
- (void)calculationRow:(RTOCacluationCell *)sender updatedUnitTo:(NSString *)unit
{
NSIndexPath *indexPath = [self.calculateTableView indexPathForCell:sender];
[self.ratio.ingredients[indexPath.item-1] setRecipeUnits:unit];
[self.calculateTableView reloadData];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
UIView *parentCell = textField.superview;
while (parentCell.superview && ![parentCell isKindOfClass:[RTOCacluationCell class]]) {
parentCell = parentCell.superview;
}
if (parentCell.superview) {
NSIndexPath *indexPath = [self.calculateTableView indexPathForCell:(RTOCacluationCell *)parentCell];
RTOIngredient *updated = self.ratio.ingredients[indexPath.item-1];
CGFloat ratio = [textField.text floatValue] / [updated.amountInRecipe.quantity floatValue];
for (RTOIngredient *i in self.ratio.ingredients) {
i.amountInRecipe.quantity = [NSNumber numberWithFloat:[i.amountInRecipe.quantity floatValue] * ratio];
}
[self reloadData];
}
}
- (void)reloadData
{
[self.calculateTableView reloadData];
[self.amountButton setTitle:[[self.ratio totalAsString] uppercaseString] forState:UIControlStateNormal];
}
- (void)changeTotal:(UIStepper *)stepper
{
self.ratio.totalQuantity = stepper.value;
[self reloadData];
}
- (IBAction)togggleShowChangeAmount
{
if (self.ratio.canSetAmountByTotal) {
BOOL show = self.changeView == nil;
CATransform3D t1 = CATransform3DIdentity;
CATransform3D v1 = CATransform3DIdentity;
if (show) {
self.changeView = [[UIView alloc] initWithFrame:self.amountButton.bounds];
self.changeView.backgroundColor = [UIColor colorForR:248 G:248 B:248 A:1];
// CGRect shadowRect = self.changeView.bounds;
// shadowRect.origin.x -= 10;
// shadowRect.size.width += 20;
// UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:shadowRect];
//// animating.layer.masksToBounds = NO;
// self.changeView.layer.shadowColor = [UIColor blackColor].CGColor;
// self.changeView.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
// self.changeView.layer.shadowOpacity = 0.5f;
// self.changeView.layer.shadowPath = shadowPath.CGPath;
[self.calculateView insertSubview:self.changeView aboveSubview:self.calculateTableView];
UIStepper *stepper = [[UIStepper alloc] init];
stepper.layer.transform = CATransform3DMakeTranslation(20, 8, 0);
stepper.autorepeat = NO;
stepper.stepValue = [self.ratio step];
stepper.minimumValue = 1;
stepper.maximumValue = 100 * stepper.stepValue;
stepper.value = self.ratio.totalQuantity - fmodf(self.ratio.totalQuantity, [self.ratio step]);
[stepper addTarget:self action:@selector(changeTotal:) forControlEvents:UIControlEventValueChanged];
[self.changeView addSubview:stepper];
t1 = CATransform3DMakeTranslation(0, CGRectGetHeight(self.amountButton.bounds), 0);
v1 = CATransform3DMakeTranslation(0, CGRectGetHeight(self.amountButton.bounds)+20, 0);
}
CABasicAnimation *ra = [CABasicAnimation animationWithKeyPath:@"transform"];
ra.autoreverses = NO;
ra.duration = ANIMATION_DURATION;
ra.fromValue = [NSValue valueWithCATransform3D:self.calculateTableView.layer.transform];
ra.toValue = [NSValue valueWithCATransform3D:t1];
CABasicAnimation *na = [ra mutableCopy];
na.fromValue = [NSValue valueWithCATransform3D:self.changeView.layer.transform];
na.toValue = [NSValue valueWithCATransform3D:v1];
[self.calculateTableView.layer addAnimation:ra forKey:nil];
[self.changeView.layer addAnimation:na forKey:nil];
self.calculateTableView.layer.transform = t1;
self.changeView.layer.transform = v1;
if (show) {
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(togggleShowChangeAmount)];
[self.changeView addGestureRecognizer:tgr];
} else {
[self.changeView removeFromSuperview];
self.changeView = nil;
}
}
}
#pragma mark Collection View
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.ratioCollectionView dequeueReusableCellWithReuseIdentifier:indexPath.item == 0 ? @"Ratio" : @"List" forIndexPath:indexPath];
if ([cell isKindOfClass:[RTORatioCVC class]]) {
RTORatioCVC *rcvc = (RTORatioCVC *)cell;
rcvc.ratioPieView.ratio = self.ratio;
} else if ([cell isKindOfClass:[RTOListCVC class]]) {
RTOListCVC *lcvc = (RTOListCVC *)cell;
lcvc.ratioPieView.ratio = self.ratio;
lcvc.ratioPieView.indexPath = indexPath;
lcvc.ingredientLabel.text = [[((RTOIngredient *)self.ratio.ingredients[indexPath.item-1]) description] capitalizedString];
}
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat w = self.view.bounds.size.width, h = indexPath.item == 0 ? 220 : 30;
return CGSizeMake(w, h);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.ratio.ingredients count] + 1;
}
#pragma mark View Life Cycle
- (void)setup
{
// TODO move to view controller for UITextView
NSMutableParagraphStyle *head = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
head.alignment = NSTextAlignmentCenter;
head.paragraphSpacing = 14;
NSMutableParagraphStyle *body = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
body.paragraphSpacing = 14;
body.lineSpacing = 3.5;
NSDictionary *headStyle = @{NSFontAttributeName: [UIFont fontWithName:@"Avenir Black Oblique" size:15],
NSParagraphStyleAttributeName: head,
NSKernAttributeName: [NSNumber numberWithInt:1],
NSForegroundColorAttributeName: [UIColor colorForR:255 G:27 B:28 A:1]};
NSDictionary *bodyStyle = @{NSParagraphStyleAttributeName : body,
NSFontAttributeName: [UIFont fontWithName:@"Avenir Light" size:14]};
NSMutableAttributedString *instructionsTxt = [[NSMutableAttributedString alloc] initWithString:@"INSTRUCTIONS\n" attributes:headStyle];
NSArray *paragraphs = [self.ratio.instructions componentsSeparatedByString:@"\n"];
[instructionsTxt appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"• %@", [paragraphs componentsJoinedByString:@"\n• "]] attributes:bodyStyle]];
[instructionsTxt appendAttributedString:[[NSAttributedString alloc] initWithString:@"\nVARIATIONS\n" attributes:headStyle]];
paragraphs = [self.ratio.variations componentsSeparatedByString:@"\n"];
[instructionsTxt appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"• %@", [paragraphs componentsJoinedByString:@"\n• "]] attributes:bodyStyle]];
self.instructionsTextView.attributedText = instructionsTxt;
// TODO move to view controller for UITableView
[self.amountButton setTitle:[[self.ratio totalAsString] uppercaseString] forState:UIControlStateNormal];
self.amountButton.enabled = [self.ratio canSetAmountByTotal];
// This has to stay at this level
self.rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeViewWithSwipe:)];
self.rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
self.leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeViewWithSwipe:)];
self.leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:self.rightSwipe];
[self.view addGestureRecognizer:self.leftSwipe];
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.calculateView.layer.position = CGPointMake(self.calculateView.layer.position.x + self.view.bounds.size.width, self.calculateView.layer.position.y);
self.instructionsTextView.layer.position = CGPointMake(self.instructionsTextView.layer.position.x + 2 * self.view.bounds.size.width, self.instructionsTextView.layer.position.y);
[self changeRatioViewWithIndex:self.viewSelectSegmentedControl.selectedSegmentIndex animated:NO];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.calculateView.alpha = 1;
self.instructionsTextView.alpha = 1;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setup];
self.calculateView.alpha = 0;
self.instructionsTextView.alpha = 0;
}
@end