Squeegy / latest-chatty

OLD CODE - DO NOT USE

This URL has Read+Write access

commit  0743216a0856ee7ace8560ffb036322effd8ee57
tree    bf04b8e87c9fd1f4dc959db75278582c0b1f7fda
parent  f851468cdf28b00f7a56c6ffc4682bc9d5cfca19
latest-chatty / Classes / DetailViewController.m
100644 297 lines (232 sloc) 9.684 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
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
286
287
288
289
290
291
292
293
294
295
296
297
//
// LatestChattyAppDelegate.m
// LatestChatty
//
// Created by Alex Wayne on 7/20/08.
// Copyright __MyCompanyName__ 2008. All rights reserved.
//
 
#import "DetailViewController.h"
 
@implementation DetailViewController
 
- (id)initWithStoryId:(int)aStoryId rootPost:(Post *)post; {
  [self initWithNibName:@"DetailViewController" bundle:nil];
  storyId = aStoryId;
  currentRoot = post;
  currentPost = currentRoot;
  currentPostIndex = 0;
  tableIsVisible = YES;
  [self updateViews];
  
  self.title = @"Thread";
  
  // Reply button
UIBarButtonItem *replyItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply
                                                                              target:self
                                                                              action:@selector(reply:)];
self.navigationItem.rightBarButtonItem = replyItem;
[replyItem release];
   
  return self;
}
 
- (void)viewDidLoad {
  [self showCurrentThread];
}
 
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
 
 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  return (interfaceOrientation == UIInterfaceOrientationPortrait);
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
 
 
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
 
 
- (void)dealloc {
  [postView release];
  [currentRoot release];
[super dealloc];
}
 
 
 
// UITableViewDelegate methods
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
  return [currentRoot replyCount] + 1;
}
 
- (NSInteger)tableView:(UITableView *)aTableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
  return [currentRoot postAtIndex:indexPath.row].depth - currentRoot.depth;
}
 
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [[UITableViewCell alloc] init];
  
  Post *post = [currentRoot postAtIndex:indexPath.row];
  
  // Most recent 10 posts are whiter than the rest
  float alpha = 0.6;
  if (post.recentIndex < 10) {
    alpha = alpha + ((1.0 - alpha) / 10) * (float)(10 - (post.recentIndex));
  }
  cell.textColor = [UIColor colorWithWhite:1.0 alpha:alpha];
  
  NSLog(@"alpha:%f index:%d", alpha, post.recentIndex);
  
  // the latest post is bold
  if (post.recentIndex == 0) {
    cell.font = [UIFont boldSystemFontOfSize:14.0];
  } else {
    cell.font = [UIFont systemFontOfSize:14.0];
  }
  
  
  
  // Set preview text
  cell.text = [post preview];
  
  return cell;
}
 
 
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  currentPostIndex = indexPath.row;
  [self showPost:[currentRoot postAtIndex:currentPostIndex]];
}
 
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  if (scrollView == tableView) {
    [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:currentPostIndex inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
  }
}
// End UITableViewDelegate methods
 
 
// UIWebViewDelegate methods
- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
  if (navigationType == UIWebViewNavigationTypeLinkClicked) {
    ExternalWebViewController *controller = [[ExternalWebViewController alloc] initWithRequest:request];
    [[self navigationController] pushViewController:controller animated:YES];
    return NO;
  }
  
  return YES;
}
// End UIWebViewDelegate methods
 
 
- (void)showCurrentThread {
  [self showPost:currentRoot];
  [tableView reloadData];
  [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
                         animated:NO
                   scrollPosition:UITableViewScrollPositionTop];
}
 
- (void)showPost:(Post *)post {
  currentPost = post;
  [postView loadHTMLString:[[currentPost html] stringByReplacingOccurrencesOfString:@"target=\"_blank\"" withString:@""]
                   baseURL:[NSURL URLWithString:@"http://thread-detail.shacknews.com/"]];
}
 
- (void)updateViews {
  int height;
  int width;
  
  if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
    height = 372;
    width = 320;
  } else {
    height = 224;
    width = 480;
  }
  
  if (tableIsVisible) {
    tableView.frame = CGRectMake(0, height/2, width, height/2);
    postView.frame = CGRectMake(0, 0, width, height/2);
  } else {
    tableView.frame = CGRectMake(0, height , width, 0);
    postView.frame = CGRectMake(0, 0, width, height);
  }
  
  toolbarView.frame = CGRectMake(toolbarView.frame.origin.x, height, width, toolbarView.frame.size.height);
}
 
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
  [self updateViews];
}
 
- (IBAction)toggleTable:(id)sender {
  tableIsVisible = !tableIsVisible;
  
  [UIView beginAnimations:@"toggle" context:NULL];
  [self updateViews];
  [UIView commitAnimations];
}
 
- (IBAction)prevReply:(id)sender {
  if (currentPostIndex > 0) {
    currentPostIndex--;
    [self showRow:currentPostIndex];
  }
}
 
- (IBAction)nextReply:(id)sender {
  if (currentPostIndex < [currentRoot replyCount]) {
    currentPostIndex++;
    [self showRow:currentPostIndex];
  }
}
 
- (void)showRow:(int)row {
  currentPost = [currentRoot postAtIndex:row];
  [self showPost:currentPost];
  
  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:currentPostIndex inSection:0];
  [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:currentPostIndex inSection:0]
                         animated:NO
                   scrollPosition:UITableViewScrollPositionNone];
  [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
 
 
- (IBAction)refresh:(id)sender {
  if (!loading) {
    NSLog(@"Refreshing...");
    loading = YES;
    currentRoot = [[Post alloc] initWithThreadId:currentPost.postId delegate:self];
    
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  }
}
 
- (void)refreshAndPop {
  [self refresh:nil];
  popAfterLoad = YES;
}
 
- (void)didFinishLoadingThread:(Post *)post {
  currentPost = currentRoot;
  currentPostIndex = 0;
  [self showPost:currentRoot];
  [self updateViews];
  [tableView reloadData];
  
  [refreshButtonLoading stopAnimating];
  
  [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  NSLog(@"Done refreshing...");
  loading = NO;
  
  if (popAfterLoad) {
    [self.navigationController popViewControllerAnimated:YES];
  }
  
  popAfterLoad = NO;
}
 
- (IBAction)reply:(id)sender {
  ComposeViewController *composeViewController = [[ComposeViewController alloc] initWithStoryId:storyId parentPost:currentPost];
  [[self navigationController] pushViewController:composeViewController animated:YES];
  [composeViewController release];
}
 
- (IBAction)tag:(id)sender {
UIActionSheet *dialog = [[UIActionSheet alloc] initWithTitle:@"Tag Post"
                                                      delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil
                                             otherButtonTitles:@"LOL", @"INF", @"Shackmark", nil];
dialog.actionSheetStyle = UIBarStyleBlackTranslucent;
dialog.destructiveButtonIndex = -1;
[dialog showInView:self.view];
[dialog release];
}
 
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"username_preference"] length] == 0) {
    //Should probably make some uniform function to display errors, no?
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Username"
                                                    message:@"It appears you credentials aren't right. Go your device settings and set your username and password for the \"LatestChatty\" application."
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    
  } else {
    switch (buttonIndex) {
      case 0:
        [self tagWithTag:@"lol"]; break;
      case 1:
        [self tagWithTag:@"inf"]; break;
      case 2:
        [self tagWithTag:@"mark"]; break;
    }
  }
}
 
- (void)tagWithTag:(NSString *)tag {
  NSURL *url = nil;
  //I dunno if this is exactly a great way to do this. But really, this is probably the last thing that's going to get tagged, so it's probably fine.
  if (tag == @"mark") {
    url = [NSURL URLWithString:[NSString stringWithFormat:@"http://socksandthecity.net/shackmarks/shackmark.php?user=%@&id=%d&version=20080528",
                                [[NSUserDefaults standardUserDefaults] stringForKey:@"username_preference"], currentPost.postId]];
  } else {
    url = [NSURL URLWithString:[NSString stringWithFormat:@"http://lmnopc.com/greasemonkey/shacklol/report.php?who=%@&what=%d&tag=%@&version=-1",
                                [[NSUserDefaults standardUserDefaults] stringForKey:@"username_preference"], currentPost.postId, tag]];
  }
  
  if (url) {
    NSLog(@"Tagged URL: %@", [url relativeString]);
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection connectionWithRequest:request delegate:nil];
  }
}
 
@end