T1HomeTimelineItemsViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // T1HomeTimelineItemsViewController.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/10/9.
  6. // Copyright (C) 2015 ibireme. All rights reserved.
  7. //
  8. #import "T1HomeTimelineItemsViewController.h"
  9. #import "T1StatusLayout.h"
  10. #import "T1StatusCell.h"
  11. #import "YYTableView.h"
  12. #import "YYFPSLabel.h"
  13. #import "YYPhotoGroupView.h"
  14. #import "YYSimpleWebViewController.h"
  15. @interface T1HomeTimelineItemsViewController() <UITableViewDelegate, UITableViewDataSource, T1StatusCellDelegate>
  16. @property (nonatomic, strong) NSMutableArray *layouts;
  17. @property (nonatomic, strong) UITableView *tableView;
  18. @property (nonatomic, strong) YYFPSLabel *fpsLabel;
  19. @end
  20. @implementation T1HomeTimelineItemsViewController
  21. - (instancetype)init {
  22. self = [super init];
  23. _tableView = [YYTableView new];
  24. _tableView.delegate = self;
  25. _tableView.dataSource = self;
  26. return self;
  27. }
  28. - (void)viewWillAppear:(BOOL)animated {
  29. [super viewWillAppear:animated];
  30. self.view.backgroundColor = [UIColor whiteColor];
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. self.view.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.919];
  35. if ([self respondsToSelector:@selector( setAutomaticallyAdjustsScrollViewInsets:)]) {
  36. self.automaticallyAdjustsScrollViewInsets = NO;
  37. }
  38. _tableView.frame = self.view.bounds;
  39. _tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
  40. _tableView.scrollIndicatorInsets = _tableView.contentInset;
  41. _tableView.backgroundColor = [UIColor clearColor];
  42. _tableView.backgroundView.backgroundColor = [UIColor clearColor];
  43. [self.view addSubview:_tableView];
  44. _fpsLabel = [YYFPSLabel new];
  45. [_fpsLabel sizeToFit];
  46. _fpsLabel.bottom = self.view.height - kT1CellPadding;
  47. _fpsLabel.left = kT1CellPadding;
  48. _fpsLabel.alpha = 0;
  49. [self.view addSubview:_fpsLabel];
  50. if (kSystemVersion < 7) {
  51. _fpsLabel.top -= 44;
  52. _tableView.top -= 64;
  53. _tableView.height += 20;
  54. }
  55. self.navigationController.view.userInteractionEnabled = NO;
  56. UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  57. indicator.size = CGSizeMake(80, 80);
  58. indicator.center = CGPointMake(self.view.width / 2, self.view.height / 2);
  59. indicator.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.670];
  60. indicator.clipsToBounds = YES;
  61. indicator.layer.cornerRadius = 6;
  62. [indicator startAnimating];
  63. [self.view addSubview:indicator];
  64. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  65. NSMutableArray *layouts = [NSMutableArray new];
  66. for (int i = 0; i <= 3; i++) {
  67. NSData *data = [NSData dataNamed:[NSString stringWithFormat:@"twitter_%d.json",i]];
  68. T1APIRespose *response = [T1APIRespose modelWithJSON:data];
  69. for (id item in response.timelineItmes) {
  70. if ([item isKindOfClass:[T1Tweet class]]) {
  71. T1Tweet *tweet = item;
  72. T1StatusLayout *layout = [T1StatusLayout new];
  73. layout.tweet = tweet;
  74. [layouts addObject:layout];
  75. } else if ([item isKindOfClass:[T1Conversation class]]) {
  76. T1Conversation *conv = item;
  77. NSMutableArray *convLayouts = [NSMutableArray new];
  78. for (T1Tweet *tweet in conv.tweets) {
  79. T1StatusLayout *layout = [T1StatusLayout new];
  80. layout.conversation = conv;
  81. layout.tweet = tweet;
  82. [convLayouts addObject:layout];
  83. }
  84. if (conv.targetCount > 0 && convLayouts.count >= 2) {
  85. T1StatusLayout *split = [T1StatusLayout new];
  86. split.conversation = conv;
  87. [split layout];
  88. [convLayouts insertObject:split atIndex:1];
  89. }
  90. [layouts addObjectsFromArray:convLayouts];
  91. }
  92. }
  93. }
  94. dispatch_async(dispatch_get_main_queue(), ^{
  95. self.title = [NSString stringWithFormat:@"Twitter (loaded:%d)", (int)layouts.count];
  96. [indicator removeFromSuperview];
  97. self.navigationController.view.userInteractionEnabled = YES;
  98. self.layouts = layouts;
  99. [_tableView reloadData];
  100. });
  101. });
  102. }
  103. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  104. if (_fpsLabel.alpha == 0) {
  105. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
  106. _fpsLabel.alpha = 1;
  107. } completion:NULL];
  108. }
  109. }
  110. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  111. if (!decelerate) {
  112. if (_fpsLabel.alpha != 0) {
  113. [UIView animateWithDuration:1 delay:2 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
  114. _fpsLabel.alpha = 0;
  115. } completion:NULL];
  116. }
  117. }
  118. }
  119. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  120. if (_fpsLabel.alpha != 0) {
  121. [UIView animateWithDuration:1 delay:2 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
  122. _fpsLabel.alpha = 0;
  123. } completion:NULL];
  124. }
  125. }
  126. - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
  127. if (_fpsLabel.alpha == 0) {
  128. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
  129. _fpsLabel.alpha = 1;
  130. } completion:^(BOOL finished) {
  131. }];
  132. }
  133. }
  134. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  135. return _layouts.count;
  136. }
  137. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  138. NSString *cellID = @"cell";
  139. T1StatusCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
  140. if (!cell) {
  141. cell = [[T1StatusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
  142. cell.delegate = self;
  143. }
  144. [cell setLayout:_layouts[indexPath.row]];
  145. return cell;
  146. }
  147. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  148. return ((T1StatusLayout *)_layouts[indexPath.row]).height;
  149. }
  150. - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
  151. return NO;
  152. }
  153. #pragma mark - T1StatusCellDelegate
  154. - (void)cell:(T1StatusCell *)cell didClickInLabel:(YYLabel *)label textRange:(NSRange)textRange {
  155. YYTextHighlight *highlight = [label.textLayout.text attribute:YYTextHighlightAttributeName atIndex:textRange.location];
  156. NSDictionary *info = highlight.userInfo;
  157. NSURL *link = nil;
  158. NSString *linkTitle = nil;
  159. if (info[@"T1URL"]) {
  160. T1URL *url = info[@"T1URL"];
  161. if (url.expandedURL.length) {
  162. link = [NSURL URLWithString:url.expandedURL];
  163. linkTitle = url.displayURL;
  164. }
  165. } else if (info[@"T1Media"]) {
  166. T1Media *media = info[@"T1Media"];
  167. if (media.expandedURL.length) {
  168. link = [NSURL URLWithString:media.expandedURL];
  169. linkTitle = media.displayURL;
  170. }
  171. }
  172. if (link) {
  173. YYSimpleWebViewController *vc = [[YYSimpleWebViewController alloc] initWithURL:link];
  174. vc.title = linkTitle;
  175. [self.navigationController pushViewController:vc animated:YES];
  176. }
  177. }
  178. - (void)cell:(T1StatusCell *)cell didClickImageAtIndex:(NSUInteger)index withLongPress:(BOOL)longPress {
  179. if (longPress) {
  180. // show alert
  181. return;
  182. }
  183. UIImageView *fromView = nil;
  184. NSMutableArray *items = [NSMutableArray new];
  185. NSArray<T1Media *> *images = cell.layout.images;
  186. for (NSUInteger i = 0, max = images.count; i < max; i++) {
  187. UIImageView *imgView = cell.statusView.mediaView.imageViews[i];
  188. T1Media *img = images[i];
  189. YYPhotoGroupItem *item = [YYPhotoGroupItem new];
  190. item.thumbView = imgView;
  191. item.largeImageURL = img.mediaLarge.url;
  192. item.largeImageSize = img.mediaLarge.size;
  193. [items addObject:item];
  194. if (i == index) {
  195. fromView = imgView;
  196. }
  197. }
  198. YYPhotoGroupView *v = [[YYPhotoGroupView alloc] initWithGroupItems:items];
  199. [v presentFromImageView:fromView toContainer:self.navigationController.view animated:YES completion:nil];
  200. }
  201. - (void)cell:(T1StatusCell *)cell didClickQuoteWithLongPress:(BOOL)longPress {
  202. }
  203. - (void)cell:(T1StatusCell *)cell didClickAvatarWithLongPress:(BOOL)longPress {
  204. }
  205. - (void)cell:(T1StatusCell *)cell didClickContentWithLongPress:(BOOL)longPress {
  206. }
  207. - (void)cellDidClickReply:(T1StatusCell *)cell {
  208. }
  209. - (void)cellDidClickRetweet:(T1StatusCell *)cell {
  210. T1StatusLayout *layout = cell.layout;
  211. T1Tweet *tweet = layout.displayedTweet;
  212. if (tweet.retweeted) {
  213. tweet.retweeted = NO;
  214. if (tweet.retweetCount > 0) tweet.retweetCount--;
  215. layout.retweetCountTextLayout = [layout retweetCountTextLayoutForTweet:tweet];
  216. } else {
  217. tweet.retweeted = YES;
  218. tweet.retweetCount++;
  219. layout.retweetCountTextLayout = [layout retweetCountTextLayoutForTweet:tweet];
  220. }
  221. [cell.statusView.inlineActionsView updateRetweetWithAnimation];
  222. }
  223. - (void)cellDidClickFavorite:(T1StatusCell *)cell {
  224. T1StatusLayout *layout = cell.layout;
  225. T1Tweet *tweet = layout.displayedTweet;
  226. if (tweet.favorited) {
  227. tweet.favorited = NO;
  228. if (tweet.favoriteCount > 0) tweet.favoriteCount--;
  229. layout.favoriteCountTextLayout = [layout favoriteCountTextLayoutForTweet:tweet];
  230. } else {
  231. tweet.favorited = YES;
  232. tweet.favoriteCount++;
  233. layout.favoriteCountTextLayout = [layout favoriteCountTextLayoutForTweet:tweet];
  234. }
  235. [cell.statusView.inlineActionsView updateFavouriteWithAnimation];
  236. }
  237. - (void)cellDidClickFollow:(T1StatusCell *)cell {
  238. T1StatusLayout *layout = cell.layout;
  239. T1Tweet *tweet = layout.displayedTweet;
  240. tweet.user.following = !tweet.user.following;
  241. [cell.statusView.inlineActionsView updateFollowWithAnimation];
  242. }
  243. @end