GlobalTimelineViewController.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 "UIRefreshControl+AFNetworking.h"
  26. #import "UIAlertView+AFNetworking.h"
  27. @interface GlobalTimelineViewController ()
  28. @property (readwrite, nonatomic, strong) NSArray *posts;
  29. @end
  30. @implementation GlobalTimelineViewController
  31. - (void)reload:(__unused id)sender {
  32. self.navigationItem.rightBarButtonItem.enabled = NO;
  33. NSURLSessionTask *task = [Post globalTimelinePostsWithBlock:^(NSArray *posts, NSError *error) {
  34. if (!error) {
  35. self.posts = posts;
  36. [self.tableView reloadData];
  37. }
  38. }];
  39. [UIAlertView showAlertViewForTaskWithErrorOnCompletion:task delegate:nil];
  40. [self.refreshControl setRefreshingWithStateOfTask:task];
  41. }
  42. #pragma mark - UIViewController
  43. - (void)viewDidLoad {
  44. [super viewDidLoad];
  45. self.title = NSLocalizedString(@"AFNetworking", nil);
  46. self.refreshControl = [[UIRefreshControl alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.frame.size.width, 100.0f)];
  47. [self.refreshControl addTarget:self action:@selector(reload:) forControlEvents:UIControlEventValueChanged];
  48. [self.tableView.tableHeaderView addSubview:self.refreshControl];
  49. self.tableView.rowHeight = 70.0f;
  50. [self reload:nil];
  51. }
  52. #pragma mark - UITableViewDataSource
  53. - (NSInteger)tableView:(__unused UITableView *)tableView
  54. numberOfRowsInSection:(__unused NSInteger)section
  55. {
  56. return (NSInteger)[self.posts count];
  57. }
  58. - (UITableViewCell *)tableView:(UITableView *)tableView
  59. cellForRowAtIndexPath:(NSIndexPath *)indexPath
  60. {
  61. static NSString *CellIdentifier = @"Cell";
  62. PostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  63. if (!cell) {
  64. cell = [[PostTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  65. }
  66. cell.post = self.posts[(NSUInteger)indexPath.row];
  67. return cell;
  68. }
  69. #pragma mark - UITableViewDelegate
  70. - (CGFloat)tableView:(__unused UITableView *)tableView
  71. heightForRowAtIndexPath:(NSIndexPath *)indexPath
  72. {
  73. return [PostTableViewCell heightForCellWithPost:self.posts[(NSUInteger)indexPath.row]];
  74. }
  75. - (void)tableView:(UITableView *)tableView
  76. didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  77. {
  78. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  79. }
  80. @end