GlobalTimelineViewController.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. @property (readwrite, nonatomic, strong) NSArray *posts;
  29. - (void)reload:(id)sender;
  30. @end
  31. @implementation GlobalTimelineViewController
  32. - (void)reload:(__unused id)sender {
  33. self.navigationItem.rightBarButtonItem.enabled = NO;
  34. NSURLSessionTask *task = [Post globalTimelinePostsWithBlock:^(NSArray *posts, NSError *error) {
  35. if (!error) {
  36. self.posts = posts;
  37. [self.tableView reloadData];
  38. }
  39. self.navigationItem.rightBarButtonItem.enabled = YES;
  40. }];
  41. [UIAlertView showAlertViewForTaskWithErrorOnCompletion:task delegate:nil];
  42. UIActivityIndicatorView *activityIndicatorView = (UIActivityIndicatorView *)self.navigationItem.leftBarButtonItem.customView;
  43. [activityIndicatorView setAnimatingWithStateOfTask:task];
  44. }
  45. #pragma mark - UIViewController
  46. - (void)viewDidLoad {
  47. [super viewDidLoad];
  48. self.title = NSLocalizedString(@"AFNetworking", nil);
  49. UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  50. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:activityIndicatorView];
  51. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reload:)];
  52. self.tableView.rowHeight = 70.0f;
  53. [self reload:nil];
  54. }
  55. #pragma mark - UITableViewDataSource
  56. - (NSInteger)tableView:(__unused UITableView *)tableView
  57. numberOfRowsInSection:(__unused NSInteger)section
  58. {
  59. return (NSInteger)[self.posts count];
  60. }
  61. - (UITableViewCell *)tableView:(UITableView *)tableView
  62. cellForRowAtIndexPath:(NSIndexPath *)indexPath
  63. {
  64. static NSString *CellIdentifier = @"Cell";
  65. PostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  66. if (!cell) {
  67. cell = [[PostTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  68. }
  69. cell.post = [self.posts objectAtIndex:(NSUInteger)indexPath.row];
  70. return cell;
  71. }
  72. #pragma mark - UITableViewDelegate
  73. - (CGFloat)tableView:(__unused UITableView *)tableView
  74. heightForRowAtIndexPath:(NSIndexPath *)indexPath
  75. {
  76. return [PostTableViewCell heightForCellWithPost:[self.posts objectAtIndex:(NSUInteger)indexPath.row]];
  77. }
  78. - (void)tableView:(UITableView *)tableView
  79. didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  80. {
  81. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  82. }
  83. @end