123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- // GlobalTimelineViewController.m
- //
- // Copyright (c) 2012 Mattt Thompson (http://mattt.me/)
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- #import "GlobalTimelineViewController.h"
- #import "Post.h"
- #import "PostTableViewCell.h"
- #import "UIActivityIndicatorView+AFNetworking.h"
- #import "UIAlertView+AFNetworking.h"
- @interface GlobalTimelineViewController ()
- @property (readwrite, nonatomic, strong) NSArray *posts;
- - (void)reload:(id)sender;
- @end
- @implementation GlobalTimelineViewController
- - (void)reload:(__unused id)sender {
- self.navigationItem.rightBarButtonItem.enabled = NO;
- NSURLSessionTask *task = [Post globalTimelinePostsWithBlock:^(NSArray *posts, NSError *error) {
- if (!error) {
- self.posts = posts;
- [self.tableView reloadData];
- }
-
- self.navigationItem.rightBarButtonItem.enabled = YES;
- }];
- [UIAlertView showAlertViewForTaskWithErrorOnCompletion:task delegate:nil];
- UIActivityIndicatorView *activityIndicatorView = (UIActivityIndicatorView *)self.navigationItem.leftBarButtonItem.customView;
- [activityIndicatorView setAnimatingWithStateOfTask:task];
- }
- #pragma mark - UIViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- self.title = NSLocalizedString(@"AFNetworking", nil);
-
- UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
- self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:activityIndicatorView];
- self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reload:)];
-
- self.tableView.rowHeight = 70.0f;
-
- [self reload:nil];
- }
- #pragma mark - UITableViewDataSource
- - (NSInteger)tableView:(__unused UITableView *)tableView
- numberOfRowsInSection:(__unused NSInteger)section
- {
- return (NSInteger)[self.posts count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView
- cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
-
- PostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (!cell) {
- cell = [[PostTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
- }
-
- cell.post = [self.posts objectAtIndex:(NSUInteger)indexPath.row];
-
- return cell;
- }
- #pragma mark - UITableViewDelegate
- - (CGFloat)tableView:(__unused UITableView *)tableView
- heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return [PostTableViewCell heightForCellWithPost:[self.posts objectAtIndex:(NSUInteger)indexPath.row]];
- }
- - (void)tableView:(UITableView *)tableView
- didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- }
- @end
|