PublicTimelineViewController.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // PublicTimelineViewController.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 "PublicTimelineViewController.h"
  23. #import "Tweet.h"
  24. #import "TweetTableViewCell.h"
  25. @interface PublicTimelineViewController ()
  26. - (void)reload:(id)sender;
  27. @end
  28. @implementation PublicTimelineViewController {
  29. @private
  30. NSArray *_tweets;
  31. __strong UIActivityIndicatorView *_activityIndicatorView;
  32. }
  33. - (void)reload:(id)sender {
  34. [_activityIndicatorView startAnimating];
  35. self.navigationItem.rightBarButtonItem.enabled = NO;
  36. [Tweet publicTimelineTweetsWithBlock:^(NSArray *tweets) {
  37. if (tweets) {
  38. _tweets = tweets;
  39. [self.tableView reloadData];
  40. }
  41. [_activityIndicatorView stopAnimating];
  42. self.navigationItem.rightBarButtonItem.enabled = YES;
  43. }];
  44. }
  45. #pragma mark - UIViewController
  46. - (void)loadView {
  47. [super loadView];
  48. _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  49. _activityIndicatorView.hidesWhenStopped = YES;
  50. }
  51. - (void)viewDidLoad {
  52. [super viewDidLoad];
  53. self.title = NSLocalizedString(@"AFNetworking", nil);
  54. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_activityIndicatorView];
  55. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reload:)];
  56. self.tableView.rowHeight = 70.0f;
  57. [self reload:nil];
  58. }
  59. - (void)viewDidUnload {
  60. _activityIndicatorView = nil;
  61. [super viewDidUnload];
  62. }
  63. - (void)viewWillAppear:(BOOL)animated {
  64. [super viewWillAppear:animated];
  65. }
  66. - (void)viewDidAppear:(BOOL)animated {
  67. [super viewDidAppear:animated];
  68. }
  69. - (void)viewWillDisappear:(BOOL)animated {
  70. [super viewWillDisappear:animated];
  71. }
  72. - (void)viewDidDisappear:(BOOL)animated {
  73. [super viewDidDisappear:animated];
  74. }
  75. #pragma mark - UITableViewDataSource
  76. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  77. return [_tweets count];
  78. }
  79. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  80. static NSString *CellIdentifier = @"Cell";
  81. TweetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  82. if (!cell) {
  83. cell = [[TweetTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  84. }
  85. cell.tweet = [_tweets objectAtIndex:indexPath.row];
  86. return cell;
  87. }
  88. #pragma mark - UITableViewDelegate
  89. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  90. return [TweetTableViewCell heightForCellWithTweet:[_tweets objectAtIndex:indexPath.row]];
  91. }
  92. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  93. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  94. }
  95. @end