-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWNewPostController.m
201 lines (118 loc) · 5.87 KB
/
SWNewPostController.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
//
// SWNewPostController.m
// StarWorld
//
// Created by Aaron Baker on 9/6/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "SWNewPostController.h"
#import "PRPFormEncodedPOSTRequest.h"
static NSString* kSWNewPostURL = @"http://pandora.starworlddata.com/posts/add";
@interface SWNewPostController (hidden)
-(void)checkCount;
@end
@implementation SWNewPostController
@synthesize countLabel;
- (id)initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query {
if ((self = [super init])) {
self.delegate = self;
currentUser = [SWCurrentUser currentUserInstance];
if (!currentUser.authenticated) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"You need to log in before you can post."
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
CGFloat left = 40.0;
CGFloat top = 40.0;
CGRect countFrame = CGRectMake(left, top, 30.0, 20.0);
countLabel = [[UILabel alloc] initWithFrame:countFrame];
countLabel.text = @"140";
countLabel.textColor = RGBCOLOR(79, 89, 105);
countLabel.alpha = 0;
countLabel.backgroundColor = [UIColor clearColor];
countLabel.tag = 52;
[self.textView addSubview:countLabel];
//FIX THIS THING.
//[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkCount) userInfo:nil repeats:YES];
NSLog(@"COUNT TIMER ALLOCATED");
countTimer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:0.1 target:self selector:@selector(checkCount) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:countTimer forMode:NSDefaultRunLoopMode];
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)showInView:(UIView*)view animated:(BOOL)animated {
[super showInView:view animated:animated];
CGFloat left = self.textView.frame.size.width - 40.0;
CGFloat top = self.textView.frame.size.height - 30.0;
[countLabel setFrame:CGRectMake(left, top, 30.0, 20.0)];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)showAnimationDidStop {
[super showAnimationDidStop];
countLabel.alpha = 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void) viewDidDisappear:(BOOL)animated {
[countTimer invalidate];
[countTimer release];
[super viewDidDisappear:animated];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:fromInterfaceOrientation duration:duration];
CGFloat left = self.textView.frame.size.width - 20.0;
CGFloat top = self.textView.frame.size.width - 20.0;
//CGRect countFrame = CGRectMake(left, top, 20.0, 20.0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
-(void)checkCount {
NSUInteger charCount = 140 - self.textView.text.length;
NSString *charString = [[NSString alloc] initWithFormat:@"%d",charCount];
countLabel.text = charString;
[charString release];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString*)titleForActivity {
return @"SOMETHING's HAPPENING!";
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)postController:(TTPostController *)postController
didPostText:(NSString *)text
withResult:(id)result {
NSString *xString = [NSString stringWithFormat:@"%f",currentUser.x];
NSString *yString = [NSString stringWithFormat:@"%f",currentUser.y];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setObject:text forKey:@"data[Post][body]"];
[params setObject:xString forKey:@"data[Post][x]"];
[params setObject:yString forKey:@"data[Post][y]"];
NSURL *postURL = [NSURL URLWithString:kSWNewPostURL];
currentUser.request = [PRPFormEncodedPOSTRequest requestWithURL:postURL
formParameters:params];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:currentUser.request
returningResponse:&response
error:&error];
NSLog(@"****POSTED CORDS***** X: %@ Y: %@",xString,yString);
[TestFlight passCheckpoint:@"New Post!"];
if (responseData) {
NSRange range = [(NSString*)[[response URL] absoluteString] rangeOfString:@"success" options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
} else {
NSLog(@"Error posting to %@ (%@)", kSWNewPostURL, error);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc {
TT_RELEASE_SAFELY(countLabel);
[super dealloc];
}
@end