GlobalTimelineViewController.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // GlobalTimelineViewController.m
  2. //
  3. // Copyright (c) 2012 Mattt Thompson (http://mattt.me/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #import "GlobalTimelineViewController.h"
  23. #import "Post.h"
  24. #import "PostTableViewCell.h"
  25. #import "UIActivityIndicatorView+AFNetworking.h"
  26. #import "UIAlertView+AFNetworking.h"
  27. @interface GlobalTimelineViewController ()
  28. - (void)reload:(id)sender;
  29. @end
  30. @implementation GlobalTimelineViewController {
  31. @private
  32. NSArray *_posts;
  33. }
  34. - (void)reload:(id)sender {
  35. self.navigationItem.rightBarButtonItem.enabled = NO;
  36. NSURLSessionTask *task = [Post globalTimelinePostsWithBlock:^(NSArray *posts, NSError *error) {
  37. if (!error) {
  38. _posts = posts;
  39. [self.tableView reloadData];
  40. }
  41. self.navigationItem.rightBarButtonItem.enabled = YES;
  42. }];
  43. [UIAlertView showAlertViewForTaskWithErrorOnCompletion:task delegate:nil];
  44. UIActivityIndicatorView *activityIndicatorView = (UIActivityIndicatorView *)self.navigationItem.leftBarButtonItem.customView;
  45. [activityIndicatorView setAnimatingWithStateOfTask:task];
  46. }
  47. #pragma mark - UIViewController
  48. - (void)viewDidLoad {
  49. [super viewDidLoad];
  50. self.title = NSLocalizedString(@"AFNetworking", nil);
  51. UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  52. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:activityIndicatorView];
  53. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reload:)];
  54. self.tableView.rowHeight = 70.0f;
  55. [self reload:nil];
  56. }
  57. #pragma mark - UITableViewDataSource
  58. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  59. return [_posts count];
  60. }
  61. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  62. static NSString *CellIdentifier = @"Cell";
  63. PostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  64. if (!cell) {
  65. cell = [[PostTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  66. }
  67. cell.post = [_posts objectAtIndex:indexPath.row];
  68. return cell;
  69. }
  70. #pragma mark - UITableViewDelegate
  71. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  72. return [PostTableViewCell heightForCellWithPost:[_posts objectAtIndex:indexPath.row]];
  73. }
  74. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  75. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  76. }
  77. @end