GlobalTimelineViewController.m 3.4 KB

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