forked from soffes/cheddar-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDIAddTaskView.m
323 lines (257 loc) · 11.3 KB
/
CDIAddTaskView.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
//
// CDIAddTaskView.m
// Cheddar for iOS
//
// Created by Sam Soffes on 4/16/12.
// Copyright (c) 2012 Nothing Magical. All rights reserved.
//
#import "CDIAddTaskView.h"
#import "CDITableViewCell.h"
#import "CDITagView.h"
#import "CDIKeyboardBar.h"
#import "UIColor+CheddariOSAdditions.h"
#import "UIFont+CheddariOSAdditions.h"
#import "UIButton+CheddariOSAdditions.h"
#import "SMTEDelegateController.h"
@interface CDIAddTaskView () <UITextFieldDelegate>
@property (nonatomic, strong) SMTEDelegateController *textExpander;
- (void)_closeTags;
@end
@implementation CDIAddTaskView {
UIView *_tagContainer;
UIView *_tagViewShadow;
CDITagView *_tagView;
}
@synthesize textExpander = _textExpander;
@synthesize delegate = _delegate;
@synthesize textField = _textField;
@synthesize shadowView = _shadowView;
@synthesize editing = _editing;
@synthesize renameListButton = _renameListButton;
@synthesize archiveTasksButton = _archiveTasksButton;
@synthesize archiveAllTasksButton = _archiveAllTasksButton;
@synthesize archiveCompletedTasksButton = _archiveCompletedTasksButton;
#pragma mark - Accessors
- (void)setEditing:(BOOL)editing {
[self setEditing:editing animated:YES];
}
#pragma mark - Class Methods
+ (CGFloat)height {
return [CDITableViewCell cellHeight];
}
+ (CGFloat)margin {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return 7.0f;
}
return 5.0f;
}
#pragma mark - NSObject
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self.textExpander];
}
#pragma mark - UIView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backgroundColor = [UIColor redColor];
CGSize size = self.bounds.size;
SSGradientView *backgroundView = [[SSGradientView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, size.width, [[self class] height])];
backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
backgroundView.colors = [NSArray arrayWithObjects:
[UIColor colorWithRed:0.941f green:0.941f blue:0.941f alpha:1.0f],
[UIColor colorWithRed:0.890f green:0.890f blue:0.890f alpha:1.0f],
nil];
backgroundView.bottomInsetColor = [UIColor colorWithWhite:0.914f alpha:1.0f];
backgroundView.bottomBorderColor = [UIColor colorWithWhite:0.706f alpha:1.0f];
[self addSubview:backgroundView];
_renameListButton = [UIButton cheddarBigButton];
[_renameListButton setTitle:@"Rename List" forState:UIControlStateNormal];
_renameListButton.alpha = 0.0f;
[self addSubview:_renameListButton];
_archiveTasksButton = [UIButton cheddarBigButton];
[_archiveTasksButton setTitle:@"Archive Tasks" forState:UIControlStateNormal];
_archiveTasksButton.alpha = 0.0f;
[self addSubview:_archiveTasksButton];
_archiveAllTasksButton = [UIButton cheddarBigButton];
[_archiveAllTasksButton setTitle:@"Archive All" forState:UIControlStateNormal];
_archiveAllTasksButton.alpha = 0.0f;
[self addSubview:_archiveAllTasksButton];
_archiveCompletedTasksButton = [UIButton cheddarBigButton];
[_archiveCompletedTasksButton setTitle:@"Archive Completed" forState:UIControlStateNormal];
_archiveCompletedTasksButton.alpha = 0.0f;
[self addSubview:_archiveCompletedTasksButton];
_textField = [[SSTextField alloc] initWithFrame:CGRectZero];
_textField.delegate = self;
_textField.background = [[UIImage imageNamed:@"textfield"] stretchableImageWithLeftCapWidth:8 topCapHeight:0];
_textField.textEdgeInsets = UIEdgeInsetsMake(0.0f, 13.0f, 0.0f, 13.0f);
_textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
_textField.autocorrectionType = UITextAutocorrectionTypeYes;
_textField.textColor = [UIColor cheddarTextColor];
_textField.placeholderTextColor = [UIColor cheddarLightTextColor];
_textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_textField.placeholder = @"What do you have to do?";
_textField.returnKeyType = UIReturnKeyGo;
_textField.font = [UIFont cheddarFontOfSize:18.0f];
CDIKeyboardBar *keyboardBar = [[CDIKeyboardBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, size.width, UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? 54.0f : 36.0f)];
keyboardBar.keyInputView = _textField;
_textField.inputAccessoryView = keyboardBar;
[self addSubview:_textField];
_shadowView = [[SSGradientView alloc] initWithFrame:CGRectMake(0.0f, [[self class] height], size.width, 3.0f)];
_shadowView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_shadowView.backgroundColor = [UIColor clearColor];
_shadowView.colors = [NSArray arrayWithObjects:
[UIColor colorWithWhite:0.0f alpha:0.1f],
[UIColor colorWithWhite:0.0f alpha:0.0f],
nil];
_shadowView.alpha = 0.0f;
[self addSubview:_shadowView];
if ([SMTEDelegateController isTextExpanderTouchInstalled]) {
self.textExpander = [[SMTEDelegateController alloc] init];
self.textExpander.nextDelegate = self;
_textField.delegate = self.textExpander;
[[NSNotificationCenter defaultCenter] addObserver:self.textExpander selector:@selector(willEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
}
return self;
}
- (void)layoutSubviews {
CGFloat margin = [[self class] margin];
CGSize size = self.bounds.size;
_textField.frame = CGRectMake(margin - 3.0f, 2.0f, size.width - margin - margin + 6.0f, 46.0f);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
CGFloat spacing = 6.0f;
if (size.width < 500.0f) {
CGFloat width = roundf((size.width - margin - margin + 2.0f - spacing) / 2.0f);
_renameListButton.frame = CGRectMake(margin - 1.0f, 5.0f, width, 42.0f);
_archiveTasksButton.frame = CGRectMake(margin - 1.0f + spacing + width, 5.0f, width, 42.0f);
_archiveAllTasksButton.frame = _archiveTasksButton.frame;
_archiveCompletedTasksButton.frame = _archiveTasksButton.frame;
} else {
CGFloat width = roundf((size.width - margin - margin + 2.0f - spacing - spacing) / 3.0f);
_renameListButton.frame = CGRectMake(margin - 1.0f, 5.0f, width, 42.0f);
_archiveTasksButton.frame = CGRectMake(margin - 1.0f + spacing + width, 5.0f, width, 42.0f);
_archiveAllTasksButton.frame = CGRectMake(margin - 1.0f + spacing + width, 5.0f, width, 42.0f);
_archiveCompletedTasksButton.frame = CGRectMake(margin - 1.0f + spacing + width + spacing + width, 5.0f, width, 42.0f);
}
} else {
CGFloat buttonWidth = roundf((size.width - margin - margin - margin) / 2.0f);
_renameListButton.frame = CGRectMake(margin, 5.0f, buttonWidth, 42.0f);
_archiveTasksButton.frame = CGRectMake(size.width - buttonWidth - margin, 5.0f, buttonWidth, 42.0f);
}
}
- (void)setFrame:(CGRect)frame {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (frame.size.width < 500.0f && _archiveAllTasksButton.alpha > 0.0f) {
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
_archiveTasksButton.alpha = 1.0f;
_archiveAllTasksButton.alpha = 0.0f;
_archiveCompletedTasksButton.alpha = 0.0f;
} completion:nil];
} else if (frame.size.width >= 500.0f && _archiveTasksButton.alpha > 0.0f) {
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
_archiveTasksButton.alpha = 0.0f;
_archiveAllTasksButton.alpha = 1.0f;
_archiveCompletedTasksButton.alpha = 1.0f;
} completion:nil];
}
}
[super setFrame:frame];
}
#pragma mark - Editing
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
_editing = editing;
self.textField.enabled = !_editing;
void (^change)(void) = ^{
_textField.alpha = _editing ? 0.0f : 1.0f;
_renameListButton.alpha = _editing ? 1.0f : 0.0f;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (self.frame.size.width < 500.0f) {
_archiveTasksButton.alpha = _renameListButton.alpha;
_archiveAllTasksButton.alpha = 0.0f;
_archiveCompletedTasksButton.alpha = 0.0f;
} else {
_archiveTasksButton.alpha = 0.0f;
_archiveAllTasksButton.alpha = _renameListButton.alpha;
_archiveCompletedTasksButton.alpha = _renameListButton.alpha;
}
} else {
_archiveTasksButton.alpha = _renameListButton.alpha;
}
};
if (animated) {
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:change completion:nil];
} else {
change();
}
}
#pragma mark - Tag
- (void)showTags:(NSArray *)tags {
_textField.enabled = NO;
NSString *text = [[NSString stringWithFormat:@"#%@", [[tags valueForKey:@"name"] componentsJoinedByString:@" #"]] lowercaseString];
if (!_tagContainer) {
_tagContainer = [[UIView alloc] initWithFrame:self.bounds];
_tagContainer.backgroundColor = [UIColor clearColor];
_tagContainer.clipsToBounds = YES;
[self addSubview:_tagContainer];
CGSize size = self.bounds.size;
_tagViewShadow = [[UIView alloc] initWithFrame:self.bounds];
_tagViewShadow.backgroundColor = [UIColor blackColor];
_tagViewShadow.alpha = 0.0f;
[_tagContainer addSubview:_tagViewShadow];
_tagView = [[CDITagView alloc] initWithFrame:CGRectMake(0.0f, size.height, size.width, size.height)];
_tagView.textLabel.text = text;
[_tagContainer addSubview:_tagView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_closeTags)];
[_tagView addGestureRecognizer:tap];
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{
_tagView.frame = CGRectMake(0.0f, 0.0f, size.width, size.height);
_tagViewShadow.alpha = 0.55f;
} completion:nil];
} else {
_tagView.textLabel.text = text;
}
}
- (void)closeTags {
_textField.enabled = YES;
CGSize size = self.bounds.size;
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{
_tagView.frame = CGRectMake(0.0f, size.height, size.width, size.height);
_tagViewShadow.alpha = 0.0f;
} completion:^(BOOL finished) {
[_tagView removeFromSuperview];
_tagView = nil;
[_tagViewShadow removeFromSuperview];
_tagViewShadow = nil;
[_tagContainer removeFromSuperview];
_tagContainer = nil;
}];
}
- (void)_closeTags {
if ([self.delegate respondsToSelector:@selector(addTaskViewShouldCloseTags:)]) {
[self.delegate addTaskViewShouldCloseTags:self];
}
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
_textField.background = [[UIImage imageNamed:@"textfield-focused"] stretchableImageWithLeftCapWidth:8 topCapHeight:0];
if ([self.delegate respondsToSelector:@selector(addTaskViewDidBeginEditing:)]) {
[self.delegate addTaskViewDidBeginEditing:self];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
_textField.background = [[UIImage imageNamed:@"textfield"] stretchableImageWithLeftCapWidth:8 topCapHeight:0];
if ([self.delegate respondsToSelector:@selector(addTaskViewDidEndEditing:)]) {
[self.delegate addTaskViewDidEndEditing:self];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField.text.length == 0) {
[textField resignFirstResponder];
return NO;
}
NSString *title = textField.text;
textField.text = nil;
[self.delegate addTaskView:self didReturnWithTitle:title];
return NO;
}
@end