Nessuna descrizione

CommentMessageController.m 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // CommentMSGViewController.m
  3. // FirstLink
  4. //
  5. // Created by Lemon on 15/1/10.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "CommentMessageController.h"
  9. #import "CommentMessageCell.h"
  10. #import <SDWebImage/UIImageView+WebCache.h>
  11. #import "MessageViewModel.h"
  12. #import "FKAppMessage.h"
  13. #import "SchemaManager.h"
  14. #import "ChatListViewController.h"
  15. #import "PindanDetailController.h"
  16. #import "FirstLinkAppDelegate.h"
  17. static NSString *CommentMessageCellIdentifier = @"CommentMessageCellIdentifier";
  18. @interface CommentMessageController ()
  19. <UITableViewDataSource, UITableViewDelegate, RefreshControlDelegate>
  20. @property (strong, nonatomic) UITableView *tableView;
  21. @property (nonatomic, strong) MessageViewModel *viewModel;
  22. @end
  23. @implementation CommentMessageController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. self.view.backgroundColor = UIColorFromRGB(0xeeeeee);
  27. [self.view addSubview:self.tableView];
  28. if (self.viewModel.appMessages.count <= 0) {
  29. [self autoRefresh];
  30. [self markMessageAsRead];
  31. }
  32. }
  33. - (void)viewDidAppear:(BOOL)animated {
  34. [super viewDidAppear:animated];
  35. }
  36. - (void)viewWillAppear:(BOOL)animated {
  37. [super viewWillAppear:animated];
  38. self.navigationItem.title = @"评论";
  39. [self.navigationController.tabBarController.tabBar setHidden:YES];
  40. [self.navigationController setNavigationBarHidden:NO animated:YES];
  41. [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
  42. }
  43. - (void)viewWillDisappear:(BOOL)animated {
  44. [super viewWillDisappear:animated];
  45. }
  46. #pragma mark - Generate Property
  47. - (MessageViewModel *)viewModel {
  48. if (!_viewModel) {
  49. _viewModel = [[MessageViewModel alloc] init];
  50. }
  51. return _viewModel;
  52. }
  53. - (UITableView*)tableView {
  54. if (!_tableView) {
  55. _tableView = [[UITableView alloc] initWithFrame:[self contentFrame] style:UITableViewStyleGrouped];
  56. _tableView.delegate = self;
  57. _tableView.dataSource = self;
  58. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  59. _tableView.backgroundColor = UIColorFromRGB(0xeeeeee);
  60. [_tableView registerClass:[CommentMessageCell class]
  61. forCellReuseIdentifier:CommentMessageCellIdentifier];
  62. [self initRefreshControlWithTableView:_tableView];
  63. if (@available(iOS 11.0, *)) {
  64. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  65. }
  66. }
  67. return _tableView;
  68. }
  69. - (CGRect)contentFrame {
  70. float width = self.view.frame.size.width;
  71. float height = [UIScreen mainScreen].bounds.size.height
  72. - [UIApplication sharedApplication].statusBarFrame.size.height
  73. - self.navigationController.navigationBar.frame.size.height;
  74. return CGRectMake(0, 0, width, height);
  75. }
  76. #pragma mark - FLTableView Init
  77. - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
  78. __weak CommentMessageController *weakSelf = self;
  79. if (direction==RefreshDirectionTop) {
  80. PageHeader *header = self.viewModel.pageHeader;
  81. [self.viewModel requestMessageWithIndex:@"0"
  82. AnchorID:header.anchorID
  83. startRow:@"0"
  84. pageSize:[NSString stringWithFormat:@"%d", PAGE_RECORD_COUNT]
  85. type:[self typeOfRequestNewMessage]
  86. success:^(MSGHeader *header, id responseObject, NSString *serverTime)
  87. {
  88. [weakSelf finishLoadingData];
  89. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  90. [weakSelf.viewModel removeAllMessages];
  91. [weakSelf.viewModel addMessages:responseObject];
  92. [weakSelf.statusView removeFromSuperview];
  93. [weakSelf.tableView reloadData];
  94. if (weakSelf.viewModel.appMessages.count == 0) {
  95. [weakSelf showStatusTipInView:weakSelf.view
  96. image:[UIImage imageNamed:@"StatusNoCommentIcon"]
  97. title:NoCommentTip];
  98. return ;
  99. }
  100. } else {
  101. [FLProgressHUDHelper showText:responseObject inView:weakSelf.view];
  102. }
  103. } failure:^(MSGHeader *header, NSError *error) {
  104. [weakSelf finishLoadingData];
  105. [weakSelf.tableView reloadData];
  106. [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view];
  107. }];
  108. } else if (direction==RefreshDirectionBottom) {
  109. PageHeader *header = self.viewModel.pageHeader;
  110. [self.viewModel requestMessageWithIndex:header.index
  111. AnchorID:header.anchorID
  112. startRow:[NSString stringWithFormat:@"%lu", (unsigned long)(header.startRow.intValue + PAGE_RECORD_COUNT)]
  113. pageSize:[NSString stringWithFormat:@"%d", PAGE_RECORD_COUNT]
  114. type:[self typeOfRequestNewMessage]
  115. success:^(MSGHeader *header, id responseObject, NSString *serverTime)
  116. {
  117. [weakSelf finishLoadingData];
  118. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  119. [weakSelf.viewModel addMessages:responseObject];
  120. if ([responseObject isKindOfClass:[NSArray class]]
  121. && [(NSArray*)responseObject count] > 0) {
  122. [weakSelf.tableView reloadData];
  123. }
  124. } else {
  125. [FLProgressHUDHelper showText:responseObject inView:weakSelf.view];
  126. }
  127. } failure:^(MSGHeader *header, NSError *error) {
  128. [weakSelf finishLoadingData];
  129. [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view];
  130. }];
  131. }
  132. }
  133. - (void)forceRefresh {
  134. [self autoRefresh];
  135. }
  136. - (void)autoRefresh {
  137. [self.refreshControl startRefreshingDirection:RefreshDirectionTop];
  138. }
  139. - (void)markMessageAsRead {
  140. [MessageViewModel markMessageRead:nil
  141. msgType:@"3"
  142. success:^(MSGHeader *header, id responseObject)
  143. {
  144. } failure:^(MSGHeader *header, NSError *error) {
  145. }];
  146. }
  147. #pragma mark - TableView Delegate Method
  148. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  149. return self.viewModel.appMessages.count;
  150. }
  151. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  152. return 1;
  153. }
  154. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  155. FKAppMessage *message = [self itemAtIndex:indexPath.section];
  156. if (message) {
  157. return [CommentMessageCell cellHeightWith:message.content];
  158. }
  159. return 44;
  160. }
  161. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  162. return 10;
  163. }
  164. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  165. return 0.1;
  166. }
  167. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  168. CommentMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CommentMessageCellIdentifier];
  169. FKAppMessage *message = [self itemAtIndex:indexPath.section];
  170. if (message) {
  171. NSString *urlString = [NSString stringWithFormat:@"%@%@", message.headURL,
  172. [FLStringHelper cdnParamaterString:32
  173. height:32]];
  174. cell.headView.image = nil;
  175. [cell.headView sd_setImageWithURL:[NSURL URLWithString:urlString]];
  176. urlString = [NSString stringWithFormat:@"%@%@", message.firstPicURL,
  177. [FLStringHelper cdnParamaterString:40
  178. height:40]];
  179. [cell.photoView sd_setImageWithURL:[NSURL URLWithString:urlString]];
  180. cell.nickLabel.text = [NSString stringWithFormat:@"%@ 评论了你", message.nickname];
  181. cell.timeLabel.text = [FLStringHelper convertStringToTipTime:message.createTime];
  182. cell.contentLabel.text = message.content;
  183. }
  184. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  185. return cell;
  186. }
  187. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  188. FKAppMessage *item = [self itemAtIndex:indexPath.section];
  189. [[SchemaManager sharedManager] parserURL:[NSURL URLWithString:item.targetURL] shouldCache:NO];
  190. }
  191. #pragma mark - Configure
  192. - (FKAppMessage*)itemAtIndex:(NSInteger)index {
  193. if (index < self.viewModel.appMessages.count) {
  194. return self.viewModel.appMessages[index];
  195. }
  196. return nil;
  197. }
  198. - (NSString *)typeOfRequestNewMessage {
  199. return @"3";
  200. }
  201. @end