-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathComposeViewController.m
285 lines (246 loc) · 10.5 KB
/
ComposeViewController.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
//
// ComposeViewController.m
// Clien
//
// Copyright 2013 Changbeom Ahn
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "ComposeViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "AFHTTPClient.h"
#import "NSScanner+Skip.h"
#import "UIViewController+GAI.h"
@interface ComposeViewController ()
@property (strong, nonatomic) UIImage* image;
@end
@implementation ComposeViewController
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismiss)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"보내기" style:UIBarButtonItemStylePlain target:self action:@selector(submit:)];
[self sendHitWithScreenName:@"Compose"];
}
return self;
}
- (void)submit:(UIBarButtonItem*)sender {
sender.enabled = NO;
AFHTTPClient* httpClient = [AFHTTPClient clientWithBaseURL:_url];
NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
NSArray* array = [_url.baseURL.query componentsSeparatedByString:@"&"];
for (NSString* parameter in array) {
NSArray* nameValue = [parameter componentsSeparatedByString:@"="];
// NSLog(@"%@", nameValue);
parameters[nameValue[0]] = nameValue[1];
}
parameters[@"wr_content"] = _textView.text;
if (_extraParameters) {
[parameters addEntriesFromDictionary:_extraParameters];
}
if (!_isComment) {
parameters[@"wr_subject"] = _titleField.text;
parameters[@"ca_name"] = _categoryField.text;
parameters[@"wr_link1"] = ((UITextField*) _linkFields[0]).text;
parameters[@"wr_link2"] = ((UITextField*) _linkFields[1]).text;
NSMutableURLRequest* request = [httpClient multipartFormRequestWithMethod:@"post" path:@"" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (_image) {
NSData* data = UIImageJPEGRepresentation(_image, 1);
[formData appendPartWithFileData:data name:@"bf_file[]" fileName:@"photo.jpg" mimeType:@"image/jpeg"]; // fixme filename, mime type
}
}];
AFHTTPRequestOperation* operation = [httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self handleResponse:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self handleError:error];
}];
[httpClient enqueueHTTPRequestOperation:operation];
} else {
[httpClient postPath:@"" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self handleResponse:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self handleError:error];
}];
}
}
- (void)handleResponse:(id)responseObject {
// fixme
NSString* response = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@", response);
self.navigationItem.rightBarButtonItem.enabled = YES;
// fixme merge message handling
NSScanner* scanner = [NSScanner scannerWithString:response];
if ([scanner skip:@"alert('"]) {
NSString* message;
[scanner scanUpToString:@"'" intoString:&message];
message = [message stringByReplacingOccurrencesOfString:@"\\n" withString:@" "];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"확인", nil];
[alertView show];
} else {
if (_successBlock) {
_successBlock();
}
}
}
- (void)handleError:(NSError*)error {
NSLog(@"%@", error);
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:error.localizedDescription delegate:nil cancelButtonTitle:nil otherButtonTitles:@"확인", nil];
[alertView show];
}
- (void)dismiss {
[self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.view.superview.layer.cornerRadius = 5;
if (_isComment) {
_imageButton.hidden = YES;
_textViewRightMargin.constant = -70;
_textView.scrollIndicatorInsets = UIEdgeInsetsZero;
}
[_textView becomeFirstResponder];
UIPickerView* pickerView = [[UIPickerView alloc] init];
pickerView.dataSource = self;
pickerView.delegate = self;
_categoryField.inputView = pickerView;
if (_loadBlock) {
_loadBlock(self);
}
[self updateImageButton];
}
- (void)updateImageButton {
[_imageButton setBackgroundImage:_image forState:UIControlStateNormal];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return _categories.count;
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return _categories[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
_categoryField.text = _categories[row];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
if (YES || animated) // fixme
{
CATransition *slide = [CATransition animation];
slide.type = kCATransitionPush;
slide.subtype = kCATransitionFromTop;
slide.duration = 0.4;
slide.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
slide.removedOnCompletion = YES;
[self.navigationController.view.superview.layer addAnimation:slide forKey:@"slidein"];
}
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self shouldShowRowAtIndexPath:indexPath]) {
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
return 0;
}
- (BOOL)shouldShowRowAtIndexPath:(NSIndexPath*)indexPath {
if (_isComment) {
return indexPath.row == 1;
}
return indexPath.row != 2 || _categories.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
cell.hidden = ![self shouldShowRowAtIndexPath:indexPath];
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[_textView becomeFirstResponder];
}
- (void)setCategories:(NSArray*)categories {
_categories = categories;
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)attach:(UIButton*)sender {
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"취소" destructiveButtonTitle:@"삭제" otherButtonTitles:@"기존 항목 선택", nil];
[actionSheet showFromRect:sender.bounds inView:sender animated:YES];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == actionSheet.destructiveButtonIndex) {
self.image = nil;
} else if (buttonIndex == actionSheet.firstOtherButtonIndex) {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:NULL];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)setImage:(UIImage *)image {
_image = image;
[self updateImageButton];
}
@end
@implementation UIViewController (Compose)
- (void)presentComposeViewControllerWithBlock:(void (^)(ComposeViewController *))block {
NSString* identifier;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
identifier = @"ComposePhone";
} else {
identifier = @"ComposePad";
}
UIViewController* container = [[UIStoryboard storyboardWithName:@"SharedStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:identifier];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
} else {
container.modalPresentationStyle = UIModalPresentationFormSheet;
}
[self presentViewController:container animated:YES completion:^{
UINavigationController* nc;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
nc = container.childViewControllers[0];
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
// prevent compose view underlapping status bar
for (NSLayoutConstraint* constraint in container.view.constraints) {
if (constraint.firstAttribute == NSLayoutAttributeTop) {
constraint.constant += 20;
break;
}
}
}
// prevent navigation bar extending under non-existent status bar :-(
nc.view.frame = UIEdgeInsetsInsetRect(nc.view.frame, UIEdgeInsetsMake(1, 0, -1, 0));
} else {
nc = (UINavigationController*) container;
}
ComposeViewController* vc = nc.viewControllers[0];
if (block) {
block(vc);
}
}];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
container.view.superview.bounds = CGRectMake(0, 0, 300, 225);
}
}
@end