-
Notifications
You must be signed in to change notification settings - Fork 3
/
FJSTweetViewController.m
265 lines (169 loc) · 6.21 KB
/
FJSTweetViewController.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
//
// FJSTweetViewController.m
// BuyingGuide
//
// Created by Corey Floyd on 1/21/10.
// Copyright 2010 Flying Jalapeño Software. All rights reserved.
//
#import "FJSTweetViewController.h"
#import "SA_OAuthTwitterEngine.h"
#import "NSError+Alertview.h"
#import "LoadingView.h"
#import "SDNextRunloopProxy.h"
#import "SA_OAuthTwitterController.h"
#define kOAuthConsumerKey @" "
#define kOAuthConsumerSecret @" "
NSString* const FJSTwitterUsernameKey = @"FJSTwitterUsername";
NSString* const FJSTwitterPasswordKey = @"FJSTwitterPassord";
@interface FJSTweetViewController()
@property(nonatomic,retain)SA_OAuthTwitterEngine *twitterEngine;
@property(nonatomic,copy)NSString *prefilledText;
- (void)launchLoginViewForce:(BOOL)flag;
@end
@implementation FJSTweetViewController
@synthesize tweetTextView;
@synthesize charCount;
@synthesize twitterEngine;
@synthesize prefilledText;
@synthesize accountName;
#pragma mark -
#pragma mark NSObject
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.accountName = nil;
self.prefilledText = nil;
self.tweetTextView = nil;
self.charCount = nil;
self.twitterEngine = nil;
[super dealloc];
}
- (id)initWithText:(NSString*)someText{
self = [super initWithNibName:@"FJSTweetViewController" bundle:nil];
if (self != nil) {
self.prefilledText = someText;
}
return self;
}
#pragma mark -
#pragma mark UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tweetTextView.font = [UIFont boldSystemFontOfSize:14];
self.twitterEngine = [SA_OAuthTwitterEngine OAuthTwitterEngineWithDelegate:self];
twitterEngine.consumerKey = kOAuthConsumerKey;
twitterEngine.consumerSecret = kOAuthConsumerSecret;
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
[twitterEngine requestRequestToken];
//this is sloppy, but UIKit dreads popping up 2 modal views in quick succession
[[self nextRunloopProxy] launchLoginViewForce:NO];
}
#pragma mark -
#pragma mark IBActions
- (IBAction)tweet{
int maxChars = 140;
if(self.tweetTextView.text.length > maxChars){
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"No more characters"
message:[NSString stringWithFormat:@"You have reached the character limit of %d.",maxChars]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
return;
}
CGRect rect = self.view.bounds;
rect.size.height = rect.size.height - 215;
[[LoadingView loadingViewInView:self.view frame:rect] setDelegate:self];
[self.twitterEngine sendUpdate:self.tweetTextView.text];
}
- (IBAction)cancel{
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)loginManually{
[self launchLoginViewForce:YES];
}
#pragma mark -
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[self tweet];
return NO;
}
- (void)textViewDidChange:(UITextView *)textView{
int maxChars = 140;
int charsLeft = maxChars - [textView.text length];
self.charCount.text = [NSString stringWithFormat:@"%d",charsLeft];
}
#pragma mark -
#pragma mark FJSTwitterLoginController
- (void)launchLoginViewForce:(BOOL)force{
if(force){
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: nil forKey: @"authData"];
[defaults setObject: nil forKey:FJSTwitterUsernameKey];
[defaults synchronize];
self.twitterEngine = [SA_OAuthTwitterEngine OAuthTwitterEngineWithDelegate:self];
twitterEngine.consumerKey = kOAuthConsumerKey;
twitterEngine.consumerSecret = kOAuthConsumerSecret;
[twitterEngine requestRequestToken];
}
UIViewController *controller =
[SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: self.twitterEngine
delegate: self];
if (controller)
[self presentModalViewController: controller animated: YES];
else {
self.tweetTextView.text = self.prefilledText;
[self textViewDidChange:self.tweetTextView];
NSString* username = [self.twitterEngine username];
[self.accountName setTitle:username forState:UIControlStateNormal];
//get the keyboard up for ui purposes
[self.tweetTextView becomeFirstResponder];
}
}
//=============================================================================================================================
#pragma mark SA_OAuthTwitterEngineDelegate
- (void) storeCachedTwitterOAuthData: (NSString *) data forUsername: (NSString *) username {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: data forKey: @"authData"];
[defaults setObject: username forKey:FJSTwitterUsernameKey];
[defaults synchronize];
}
- (NSString *) cachedTwitterOAuthDataForUsername: (NSString *) username {
return [[NSUserDefaults standardUserDefaults] objectForKey: @"authData"];
}
//=============================================================================================================================
#pragma mark SA_OAuthTwitterControllerDelegate
- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {
NSLog(@"Authenicated for %@", username);
}
- (void) OAuthTwitterControllerFailed: (SA_OAuthTwitterController *) controller {
NSLog(@"Authentication Failed!");
}
- (void) OAuthTwitterControllerCanceled: (SA_OAuthTwitterController *) controller {
NSLog(@"Authentication Canceled.");
[self.navigationController.parentViewController dismissModalViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark MGTwitterEngineDelegate
- (void)requestSucceeded:(NSString *)connectionIdentifier{
for(UIView* aView in self.view.subviews){
if([aView isKindOfClass:[LoadingView class]])
[(LoadingView*)aView updateTextAndRemoveView:@"Tweet Sent!"];
}
}
- (void)requestFailed:(NSString *)connectionIdentifier withError:(NSError *)error{
for(UIView* aView in self.view.subviews){
if([aView isKindOfClass:[LoadingView class]])
[(LoadingView*)aView removeView];
}
[error presentAlertViewWithDelegate:nil];
}
#pragma mark LoadingViewDelegate
- (void)loadingViewDidClose:(LoadingView*)loadingView{
[self dismissModalViewControllerAnimated:YES];
}
@end