WBStatusTimelineViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //
  2. // YYWeiboFeedListController.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/9/4.
  6. // Copyright (c) 2015 ibireme. All rights reserved.
  7. //
  8. #import "WBStatusTimelineViewController.h"
  9. #import "YYKit.h"
  10. #import "WBModel.h"
  11. #import "WBStatusLayout.h"
  12. #import "WBStatusCell.h"
  13. #import "YYTableView.h"
  14. #import "YYSimpleWebViewController.h"
  15. #import "WBStatusComposeViewController.h"
  16. #import "YYPhotoGroupView.h"
  17. #import "YYFPSLabel.h"
  18. @interface WBStatusTimelineViewController () <UITableViewDelegate, UITableViewDataSource, WBStatusCellDelegate>
  19. @property (nonatomic, strong) UITableView *tableView;
  20. @property (nonatomic, strong) NSMutableArray *layouts;
  21. @property (nonatomic, strong) YYFPSLabel *fpsLabel;
  22. @end
  23. @implementation WBStatusTimelineViewController
  24. - (instancetype)init {
  25. self = [super init];
  26. _tableView = [YYTableView new];
  27. _tableView.delegate = self;
  28. _tableView.dataSource = self;
  29. _layouts = [NSMutableArray new];
  30. return self;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. if ([self respondsToSelector:@selector( setAutomaticallyAdjustsScrollViewInsets:)]) {
  35. self.automaticallyAdjustsScrollViewInsets = NO;
  36. }
  37. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[WBStatusHelper imageNamed:@"toolbar_compose_highlighted"] style:UIBarButtonItemStylePlain target:self action:@selector(sendStatus)];
  38. rightItem.tintColor = UIColorHex(fd8224);
  39. self.navigationItem.rightBarButtonItem = rightItem;
  40. _tableView.frame = self.view.bounds;
  41. _tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
  42. _tableView.scrollIndicatorInsets = _tableView.contentInset;
  43. _tableView.backgroundColor = [UIColor clearColor];
  44. _tableView.backgroundView.backgroundColor = [UIColor clearColor];
  45. [self.view addSubview:_tableView];
  46. self.view.backgroundColor = kWBCellBackgroundColor;
  47. _fpsLabel = [YYFPSLabel new];
  48. [_fpsLabel sizeToFit];
  49. _fpsLabel.bottom = self.view.height - kWBCellPadding;
  50. _fpsLabel.left = kWBCellPadding;
  51. _fpsLabel.alpha = 0;
  52. [self.view addSubview:_fpsLabel];
  53. if (kSystemVersion < 7) {
  54. _fpsLabel.top -= 44;
  55. _tableView.top -= 64;
  56. _tableView.height += 20;
  57. }
  58. self.navigationController.view.userInteractionEnabled = NO;
  59. UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  60. indicator.size = CGSizeMake(80, 80);
  61. indicator.center = CGPointMake(self.view.width / 2, self.view.height / 2);
  62. indicator.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.670];
  63. indicator.clipsToBounds = YES;
  64. indicator.layer.cornerRadius = 6;
  65. [indicator startAnimating];
  66. [self.view addSubview:indicator];
  67. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  68. for (int i = 0; i <= 7; i++) {
  69. NSData *data = [NSData dataNamed:[NSString stringWithFormat:@"weibo_%d.json",i]];
  70. WBTimelineItem *item = [WBTimelineItem modelWithJSON:data];
  71. for (WBStatus *status in item.statuses) {
  72. WBStatusLayout *layout = [[WBStatusLayout alloc] initWithStatus:status style:WBLayoutStyleTimeline];
  73. // [layout layout];
  74. [_layouts addObject:layout];
  75. }
  76. }
  77. // 复制一下,让列表长一些,不至于滑两下就到底了
  78. [_layouts addObjectsFromArray:_layouts];
  79. dispatch_async(dispatch_get_main_queue(), ^{
  80. self.title = [NSString stringWithFormat:@"Weibo (loaded:%d)", (int)_layouts.count];
  81. [indicator removeFromSuperview];
  82. self.navigationController.view.userInteractionEnabled = YES;
  83. [_tableView reloadData];
  84. });
  85. });
  86. }
  87. - (void)sendStatus {
  88. WBStatusComposeViewController *vc = [WBStatusComposeViewController new];
  89. vc.type = WBStatusComposeViewTypeStatus;
  90. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
  91. @weakify(nav);
  92. vc.dismiss = ^{
  93. @strongify(nav);
  94. [nav dismissViewControllerAnimated:YES completion:NULL];
  95. };
  96. [self presentViewController:nav animated:YES completion:NULL];
  97. }
  98. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  99. if (_fpsLabel.alpha == 0) {
  100. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
  101. _fpsLabel.alpha = 1;
  102. } completion:NULL];
  103. }
  104. }
  105. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  106. if (!decelerate) {
  107. if (_fpsLabel.alpha != 0) {
  108. [UIView animateWithDuration:1 delay:2 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
  109. _fpsLabel.alpha = 0;
  110. } completion:NULL];
  111. }
  112. }
  113. }
  114. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  115. if (_fpsLabel.alpha != 0) {
  116. [UIView animateWithDuration:1 delay:2 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
  117. _fpsLabel.alpha = 0;
  118. } completion:NULL];
  119. }
  120. }
  121. - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
  122. if (_fpsLabel.alpha == 0) {
  123. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
  124. _fpsLabel.alpha = 1;
  125. } completion:^(BOOL finished) {
  126. }];
  127. }
  128. }
  129. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  130. return _layouts.count;
  131. }
  132. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  133. NSString *cellID = @"cell";
  134. WBStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
  135. if (!cell) {
  136. cell = [[WBStatusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
  137. cell.delegate = self;
  138. }
  139. [cell setLayout:_layouts[indexPath.row]];
  140. return cell;
  141. }
  142. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  143. return ((WBStatusLayout *)_layouts[indexPath.row]).height;
  144. }
  145. - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
  146. return NO;
  147. }
  148. #pragma mark - WBStatusCellDelegate
  149. // 此处应该用 Router 之类的东西。。。这里只是个Demo,直接全跳网页吧~
  150. /// 点击了 Cell
  151. - (void)cellDidClick:(WBStatusCell *)cell {
  152. }
  153. /// 点击了 Card
  154. - (void)cellDidClickCard:(WBStatusCell *)cell {
  155. WBPageInfo *pageInfo = cell.statusView.layout.status.pageInfo;
  156. NSString *url = pageInfo.pageURL; // sinaweibo://... 会跳到 Weibo.app 的。。
  157. YYSimpleWebViewController *vc = [[YYSimpleWebViewController alloc] initWithURL:[NSURL URLWithString:url]];
  158. vc.title = pageInfo.pageTitle;
  159. [self.navigationController pushViewController:vc animated:YES];
  160. }
  161. /// 点击了转发内容
  162. - (void)cellDidClickRetweet:(WBStatusCell *)cell {
  163. }
  164. /// 点击了 Cell 菜单
  165. - (void)cellDidClickMenu:(WBStatusCell *)cell {
  166. }
  167. /// 点击了下方 Tag
  168. - (void)cellDidClickTag:(WBStatusCell *)cell {
  169. WBTag *tag = cell.statusView.layout.status.tagStruct.firstObject;
  170. NSString *url = tag.tagScheme; // sinaweibo://... 会跳到 Weibo.app 的。。
  171. YYSimpleWebViewController *vc = [[YYSimpleWebViewController alloc] initWithURL:[NSURL URLWithString:url]];
  172. vc.title = tag.tagName;
  173. [self.navigationController pushViewController:vc animated:YES];
  174. }
  175. /// 点击了关注
  176. - (void)cellDidClickFollow:(WBStatusCell *)cell {
  177. }
  178. /// 点击了转发
  179. - (void)cellDidClickRepost:(WBStatusCell *)cell {
  180. WBStatusComposeViewController *vc = [WBStatusComposeViewController new];
  181. vc.type = WBStatusComposeViewTypeRetweet;
  182. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
  183. @weakify(nav);
  184. vc.dismiss = ^{
  185. @strongify(nav);
  186. [nav dismissViewControllerAnimated:YES completion:NULL];
  187. };
  188. [self presentViewController:nav animated:YES completion:NULL];
  189. }
  190. /// 点击了评论
  191. - (void)cellDidClickComment:(WBStatusCell *)cell {
  192. WBStatusComposeViewController *vc = [WBStatusComposeViewController new];
  193. vc.type = WBStatusComposeViewTypeComment;
  194. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
  195. @weakify(nav);
  196. vc.dismiss = ^{
  197. @strongify(nav);
  198. [nav dismissViewControllerAnimated:YES completion:NULL];
  199. };
  200. [self presentViewController:nav animated:YES completion:NULL];
  201. }
  202. /// 点击了赞
  203. - (void)cellDidClickLike:(WBStatusCell *)cell {
  204. WBStatus *status = cell.statusView.layout.status;
  205. [cell.statusView.toolbarView setLiked:!status.attitudesStatus withAnimation:YES];
  206. }
  207. /// 点击了用户
  208. - (void)cell:(WBStatusCell *)cell didClickUser:(WBUser *)user {
  209. if (user.userID == 0) return;
  210. NSString *url = [NSString stringWithFormat:@"http://m.weibo.cn/u/%lld",user.userID];
  211. YYSimpleWebViewController *vc = [[YYSimpleWebViewController alloc] initWithURL:[NSURL URLWithString:url]];
  212. [self.navigationController pushViewController:vc animated:YES];
  213. }
  214. /// 点击了图片
  215. - (void)cell:(WBStatusCell *)cell didClickImageAtIndex:(NSUInteger)index {
  216. UIView *fromView = nil;
  217. NSMutableArray *items = [NSMutableArray new];
  218. WBStatus *status = cell.statusView.layout.status;
  219. NSArray<WBPicture *> *pics = status.retweetedStatus ? status.retweetedStatus.pics : status.pics;
  220. for (NSUInteger i = 0, max = pics.count; i < max; i++) {
  221. UIView *imgView = cell.statusView.picViews[i];
  222. WBPicture *pic = pics[i];
  223. WBPictureMetadata *meta = pic.largest.badgeType == WBPictureBadgeTypeGIF ? pic.largest : pic.large;
  224. YYPhotoGroupItem *item = [YYPhotoGroupItem new];
  225. item.thumbView = imgView;
  226. item.largeImageURL = meta.url;
  227. item.largeImageSize = CGSizeMake(meta.width, meta.height);
  228. [items addObject:item];
  229. if (i == index) {
  230. fromView = imgView;
  231. }
  232. }
  233. YYPhotoGroupView *v = [[YYPhotoGroupView alloc] initWithGroupItems:items];
  234. [v presentFromImageView:fromView toContainer:self.navigationController.view animated:YES completion:nil];
  235. }
  236. /// 点击了 Label 的链接
  237. - (void)cell:(WBStatusCell *)cell didClickInLabel:(YYLabel *)label textRange:(NSRange)textRange {
  238. NSAttributedString *text = label.textLayout.text;
  239. if (textRange.location >= text.length) return;
  240. YYTextHighlight *highlight = [text attribute:YYTextHighlightAttributeName atIndex:textRange.location];
  241. NSDictionary *info = highlight.userInfo;
  242. if (info.count == 0) return;
  243. if (info[kWBLinkHrefName]) {
  244. NSString *url = info[kWBLinkHrefName];
  245. YYSimpleWebViewController *vc = [[YYSimpleWebViewController alloc] initWithURL:[NSURL URLWithString:url]];
  246. [self.navigationController pushViewController:vc animated:YES];
  247. return;
  248. }
  249. if (info[kWBLinkURLName]) {
  250. WBURL *url = info[kWBLinkURLName];
  251. WBPicture *pic = url.pics.firstObject;
  252. if (pic) {
  253. // 点击了文本中的 "图片链接"
  254. YYTextAttachment *attachment = [label.textLayout.text attribute:YYTextAttachmentAttributeName atIndex:textRange.location];
  255. if ([attachment.content isKindOfClass:[UIView class]]) {
  256. YYPhotoGroupItem *info = [YYPhotoGroupItem new];
  257. info.largeImageURL = pic.large.url;
  258. info.largeImageSize = CGSizeMake(pic.large.width, pic.large.height);
  259. YYPhotoGroupView *v = [[YYPhotoGroupView alloc] initWithGroupItems:@[info]];
  260. [v presentFromImageView:attachment.content toContainer:self.navigationController.view animated:YES completion:nil];
  261. }
  262. } else if (url.oriURL.length){
  263. YYSimpleWebViewController *vc = [[YYSimpleWebViewController alloc] initWithURL:[NSURL URLWithString:url.oriURL]];
  264. [self.navigationController pushViewController:vc animated:YES];
  265. }
  266. return;
  267. }
  268. if (info[kWBLinkTagName]) {
  269. WBTag *tag = info[kWBLinkTagName];
  270. NSLog(@"tag:%@",tag.tagScheme);
  271. return;
  272. }
  273. if (info[kWBLinkTopicName]) {
  274. WBTopic *topic = info[kWBLinkTopicName];
  275. NSString *topicStr = topic.topicTitle;
  276. topicStr = [topicStr stringByURLEncode];
  277. if (topicStr.length) {
  278. NSString *url = [NSString stringWithFormat:@"http://m.weibo.cn/k/%@",topicStr];
  279. YYSimpleWebViewController *vc = [[YYSimpleWebViewController alloc] initWithURL:[NSURL URLWithString:url]];
  280. [self.navigationController pushViewController:vc animated:YES];
  281. }
  282. return;
  283. }
  284. if (info[kWBLinkAtName]) {
  285. NSString *name = info[kWBLinkAtName];
  286. name = [name stringByURLEncode];
  287. if (name.length) {
  288. NSString *url = [NSString stringWithFormat:@"http://m.weibo.cn/n/%@",name];
  289. YYSimpleWebViewController *vc = [[YYSimpleWebViewController alloc] initWithURL:[NSURL URLWithString:url]];
  290. [self.navigationController pushViewController:vc animated:YES];
  291. }
  292. return;
  293. }
  294. }
  295. @end