PublicTimelineViewController.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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, NSError *error) {
  37. if (error) {
  38. [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"OK", nil), nil] show];
  39. } else {
  40. _tweets = tweets;
  41. [self.tableView reloadData];
  42. }
  43. [_activityIndicatorView stopAnimating];
  44. self.navigationItem.rightBarButtonItem.enabled = YES;
  45. }];
  46. }
  47. #pragma mark - UIViewController
  48. - (void)loadView {
  49. [super loadView];
  50. _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  51. _activityIndicatorView.hidesWhenStopped = YES;
  52. }
  53. - (void)viewDidLoad {
  54. [super viewDidLoad];
  55. self.title = NSLocalizedString(@"AFNetworking", nil);
  56. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_activityIndicatorView];
  57. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reload:)];
  58. self.tableView.rowHeight = 70.0f;
  59. [self reload:nil];
  60. }
  61. - (void)viewDidUnload {
  62. _activityIndicatorView = nil;
  63. [super viewDidUnload];
  64. }
  65. - (void)viewWillAppear:(BOOL)animated {
  66. [super viewWillAppear:animated];
  67. }
  68. - (void)viewDidAppear:(BOOL)animated {
  69. [super viewDidAppear:animated];
  70. }
  71. - (void)viewWillDisappear:(BOOL)animated {
  72. [super viewWillDisappear:animated];
  73. }
  74. - (void)viewDidDisappear:(BOOL)animated {
  75. [super viewDidDisappear:animated];
  76. }
  77. #pragma mark - UITableViewDataSource
  78. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  79. return [_tweets count];
  80. }
  81. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  82. static NSString *CellIdentifier = @"Cell";
  83. TweetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  84. if (!cell) {
  85. cell = [[TweetTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  86. }
  87. cell.tweet = [_tweets objectAtIndex:indexPath.row];
  88. return cell;
  89. }
  90. #pragma mark - UITableViewDelegate
  91. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  92. return [TweetTableViewCell heightForCellWithTweet:[_tweets objectAtIndex:indexPath.row]];
  93. }
  94. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  95. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  96. }
  97. @end