forked from Awful/Awful.app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAwfulReplyComposeViewController.m
More file actions
157 lines (139 loc) · 5.14 KB
/
AwfulReplyComposeViewController.m
File metadata and controls
157 lines (139 loc) · 5.14 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
//
// AwfulReplyComposeViewController.m
// Awful
//
// Created by Sean Berry on 11/21/10.
// Copyright 2010 Regular Berry Software LLC. All rights reserved.
//
#import "AwfulReplyComposeViewController.h"
#import "AwfulComposeViewControllerSubclass.h"
#import "AwfulAlertView.h"
#import "AwfulHTTPClient.h"
#import "AwfulKeyboardBar.h"
#import "AwfulModels.h"
#import "AwfulSettings.h"
#import "AwfulTextView.h"
#import "ImgurHTTPClient.h"
#import "NSString+CollapseWhitespace.h"
#import "SVProgressHUD.h"
#import "UINavigationItem+TwoLineTitle.h"
@interface AwfulReplyComposeViewController () <UIImagePickerControllerDelegate,
UINavigationControllerDelegate,
UIPopoverControllerDelegate>
@property (weak, nonatomic) NSOperation *networkOperation;
@property (nonatomic) AwfulThread *thread;
@property (nonatomic) AwfulPost *post;
@end
@implementation AwfulReplyComposeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (!(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) return nil;
self.sendButton.target = self;
self.sendButton.action = @selector(didTapSend);
self.cancelButton.target = self;
self.cancelButton.action = @selector(cancel);
return self;
}
- (void)editPost:(AwfulPost *)post text:(NSString *)text
{
self.post = post;
self.thread = nil;
self.textView.text = text;
self.title = [post.thread.title stringByCollapsingWhitespace];
self.navigationItem.titleLabel.text = self.title;
self.sendButton.title = @"Save";
}
- (void)replyToThread:(AwfulThread *)thread withInitialContents:(NSString *)contents
{
self.thread = thread;
self.post = nil;
self.textView.text = contents;
self.title = [thread.title stringByCollapsingWhitespace];
self.navigationItem.titleLabel.text = self.title;
self.sendButton.title = @"Reply";
}
- (void)didTapSend
{
if (self.state != AwfulComposeViewControllerStateReady) return;
[self.networkOperation cancel];
[self.textView resignFirstResponder];
self.textView.userInteractionEnabled = NO;
if ([AwfulSettings settings].confirmBeforeReplying) {
AwfulAlertView *alert = [AwfulAlertView new];
alert.title = @"Incoming Forums Superstar";
alert.message = @"Does my reply offer any significant advice or help "
"contribute to the conversation in any fashion?";
[alert addCancelButtonWithTitle:@"Nope" block:^{
self.textView.userInteractionEnabled = YES;
[self.textView becomeFirstResponder];
}];
[alert addButtonWithTitle:self.sendButton.title block:^{ [self prepareToSendMessage]; }];
[alert show];
} else {
[self prepareToSendMessage];
}
}
- (void)willTransitionToState:(AwfulComposeViewControllerState)state
{
if (state == AwfulComposeViewControllerStateReady) {
self.textView.userInteractionEnabled = YES;
[self.textView becomeFirstResponder];
} else {
self.textView.userInteractionEnabled = NO;
[self.textView resignFirstResponder];
}
if (state == AwfulComposeViewControllerStateUploadingImages) {
[SVProgressHUD showWithStatus:@"Uploading images…"];
} else if (state == AwfulComposeViewControllerStateSending) {
[SVProgressHUD showWithStatus:self.thread ? @"Replying…" : @"Editing…"
maskType:SVProgressHUDMaskTypeClear];
} else if (state == AwfulComposeViewControllerStateError) {
[SVProgressHUD dismiss];
}
}
- (void)send:(NSString *)messageBody
{
NSOperation *op;
void (^errorHandler)(NSError*) = ^(NSError *error){
[SVProgressHUD dismiss];
[AwfulAlertView showWithTitle:@"Network Error" error:error buttonTitle:@"OK" completion:^{
self.textView.userInteractionEnabled = YES;
}];
};
if (self.thread) {
op = [[AwfulHTTPClient client] replyToThreadWithID:self.thread.threadID text:messageBody
andThen:^(NSError *error, NSString *postID)
{
if (error) return errorHandler(error);
[SVProgressHUD showSuccessWithStatus:@"Replied"];
[self.delegate replyComposeController:self didReplyToThread:self.thread];
}];
} else {
op = [[AwfulHTTPClient client] editPostWithID:self.post.postID text:messageBody
andThen:^(NSError *error)
{
if (error) return errorHandler(error);
[SVProgressHUD showSuccessWithStatus:@"Edited"];
[self.delegate replyComposeController:self didEditPost:self.post];
}];
}
self.networkOperation = op;
}
- (void)cancel
{
[super cancel];
[self.networkOperation cancel];
if ([SVProgressHUD isVisible]) {
[SVProgressHUD dismiss];
self.textView.userInteractionEnabled = YES;
[self.textView becomeFirstResponder];
} else {
[self.delegate replyComposeControllerDidCancel:self];
}
}
#pragma mark - UIViewController
- (void)loadView
{
self.view = self.textView;
}
@end