public
Description:
Homepage:
Clone URL: git://github.com/Squeegy/latest-chatty.git
latest-chatty / Classes / RootViewController.m
100644 208 lines (150 sloc) 6.493 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
//
// RootViewController.m
// LatestChatty
//
// Created by Alex Wayne on 7/31/08.
// Copyright __MyCompanyName__ 2008. All rights reserved.
//
 
#import "RootViewController.h"
#import "LatestChattyAppDelegate.h"
 
 
@implementation RootViewController
 
 
- (void)viewDidLoad {
  // Refresh button
  UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                                                                                 target:self
                                                                                 action:@selector(refresh:)];
  self.navigationItem.leftBarButtonItem = refreshButton;
  
  
  // Compose button
  UIBarButtonItem *composeItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
                                                                               target:self
                                                                               action:@selector(compose:)];
  self.navigationItem.rightBarButtonItem = composeItem;
  [composeItem release];
  
  // Fetch feed
  [self refresh:nil];
}
 
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
 
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  if ([feed hasMorePages]) {
    return [[feed posts] count] + 1;
  } else {
    return [[feed posts] count];
  }
}
 
 
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"asked for cell!");
  RootPostCellView *cell;
if (indexPath.row < [[feed posts] count]) {
    Post *post = [[feed posts] objectAtIndex:indexPath.row];
    
    cell = (RootPostCellView *)[tableView dequeueReusableCellWithIdentifier:@"rootPostCell"];
    if (cell == nil) {
      cell = [[RootPostCellView alloc] initForPost];
    }
    
    [cell updateWithPost:post];
  } else {
    cell = [[RootPostCellView alloc] initLoadMore];
  }
 
  cell.striped = (indexPath.row % 2 == 1);
  return (UITableViewCell *)cell;
}
 
 
 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  // Tapped a post cell
  if (indexPath.row < [[feed posts] count]) {
    RootPostCellView *cell = (RootPostCellView *)[tableView cellForRowAtIndexPath:indexPath];
    [cell setLoading:YES];
    Post *rootPost = [[feed posts] objectAtIndex:indexPath.row];
    [[Post alloc] initWithThreadId:rootPost.postId delegate:self];
  
  // Tapped the load more cell
  } else {
    RootPostCellView *cell = (RootPostCellView *)[tableView cellForRowAtIndexPath:indexPath];
    [cell setLoading:YES];
    [feed loadNextPage];
  }
  
  [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
 
- (void)feedDidFinishLoading {
  [tableView reloadData];
  if (feed.lastPageLoaded == 1) {
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
  }
  [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
  
  self.title = [feed storyName];
  
  [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
 
- (void)didFinishLoadingThread:(Post *)post {
  // create the detail view controller
  DetailViewController *detailViewController = [[DetailViewController alloc] initWithStoryId:feed.storyId rootPost:post];
  
  // Push the detail view controller onto the navigation stack
  [[self navigationController] pushViewController:detailViewController animated:YES];
  [detailViewController release];
  
  // Remove loading status from tapped cell
  [(RootPostCellView *)[tableView cellForRowAtIndexPath:[tableView indexPathForSelectedRow]] setLoading:NO];
  
  [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
 
/*
Override if you support editing the list
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
 
 
/*
Override if you support conditional editing of the list
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
 
 
/*
Override if you support rearranging the list
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
 
 
/*
Override if you support conditional rearranging of the list
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
 
 
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
  [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
}
 
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
 
- (void)viewWillDisappear:(BOOL)animated {
}
 
- (void)viewDidDisappear:(BOOL)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 {
  [feed release];
[super dealloc];
}
 
 
 
 
 
 
- (void)refresh:(id)sender {
  [feed release];
  feed = [[Feed alloc] initWithLatestChattyAndDelegate:self];
  [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
 
- (IBAction)compose:(id)sender {
  ComposeViewController *composeViewController = [[ComposeViewController alloc] initWithStoryId:feed.storyId];
  [[self navigationController] pushViewController:composeViewController animated:YES];
  [composeViewController release];
}
 
 
@end