forked from soffes/cheddar-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDIEditTaskViewController.m
168 lines (123 loc) · 5.17 KB
/
CDIEditTaskViewController.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
//
// CDIEditTaskViewController.m
// Cheddar for iOS
//
// Created by Sam Soffes on 5/30/12.
// Copyright (c) 2012 Nothing Magical. All rights reserved.
//
#import "CDIEditTaskViewController.h"
#import "CDIMoveTaskView.h"
#import "CDIKeyboardBar.h"
#import "UIColor+CheddariOSAdditions.h"
#import "UIFont+CheddariOSAdditions.h"
@interface CDIEditTaskViewController () <UITextViewDelegate>
- (void)_keyboardDidShow:(NSNotification *)notification;
- (void)_layout:(NSTimeInterval)duration;
@end
@implementation CDIEditTaskViewController {
CGRect _keyboardRect;
BOOL _shown;
CDIMoveTaskView *_moveTaskView;
}
@synthesize task = _task;
@synthesize textView = _textView;
#pragma mark - NSObject
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Edit Task";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save:)];
self.view.backgroundColor = [UIColor whiteColor];
_textView = [[SSTextView alloc] init];
_textView.delegate = self;
_textView.autocapitalizationType = UITextAutocapitalizationTypeSentences;
_textView.autocorrectionType = UITextAutocorrectionTypeYes;
_textView.textColor = [UIColor cheddarTextColor];
_textView.placeholderTextColor = [UIColor cheddarLightTextColor];
_textView.placeholder = @"What do you have to do?";
_textView.returnKeyType = UIReturnKeyGo;
_textView.font = [UIFont cheddarFontOfSize:18.0f];
_textView.text = self.task.text;
CDIKeyboardBar *keyboardBar = [[CDIKeyboardBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? 54.0f : 36.0f)];
keyboardBar.keyInputView = _textView;
_textView.inputAccessoryView = keyboardBar;
[self.view addSubview:_textView];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
_moveTaskView = [[CDIMoveTaskView alloc] init];
_moveTaskView.editViewController = self;
[_moveTaskView.moveButton addTarget:self action:@selector(moveTask:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_moveTaskView];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Default to avoid flash
CGSize size = self.view.frame.size;
_keyboardRect = CGRectMake(0.0f, size.height - 216.0f, size.width, 216.0f);
[self _layout:0.0];
[_textView becomeFirstResponder];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
[self _layout:duration];
} completion:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES;
}
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
#pragma mark - Actions
- (void)save:(id)sender {
self.task.text = self.textView.text;
[self.task save];
[self.task update];
[self.navigationController dismissModalViewControllerAnimated:YES];
}
- (void)cancel:(id)sender {
[self.navigationController dismissModalViewControllerAnimated:YES];
}
- (void)moveTask:(id)sender {
if ([_textView isFirstResponder]) {
[_textView resignFirstResponder];
[_moveTaskView.tableView flashScrollIndicators];
} else {
[_textView becomeFirstResponder];
}
}
- (void)moveTaskToList:(CDKList *)newList {
[self.task moveToList:newList];
[self.navigationController dismissModalViewControllerAnimated:YES];
}
#pragma mark - Private
- (void)_layout:(NSTimeInterval)duration {
CGSize size = self.view.bounds.size;
CGFloat heightAdjust = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? 0.0f : _keyboardRect.size.height;
CGFloat textViewHeight = size.height - heightAdjust - 32.0f;
[UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseInOut animations:^{
_textView.frame = CGRectMake(0.0f, 0.0f, size.width, textViewHeight);
_moveTaskView.frame = CGRectMake(0.0f, textViewHeight, size.width, size.height - textViewHeight);
} completion:nil];
}
- (void)_keyboardDidShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
_keyboardRect = [self.view convertRect:[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil];
CGFloat duration = _shown ? [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue] : 0.0f;
[self _layout:duration];
_shown = YES;
}
#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqual:@"\n"]) {
[self save:textView];
return NO;
}
return YES;
}
@end