Keine Beschreibung

PindanMessageController.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // PindanMSGViewController.m
  3. // FirstLink
  4. //
  5. // Created by Lemon on 15/4/27.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "PindanMessageController.h"
  9. #import "PindanMessageCell.h"
  10. #import "ChatListViewController.h"
  11. #import "MessageViewModel.h"
  12. #import "FKAppMessage.h"
  13. #import "SchemaManager.h"
  14. @interface PindanMessageController ()
  15. <UITableViewDataSource, UITableViewDelegate, RefreshControlDelegate>
  16. @property (nonatomic, strong) MessageViewModel *viewModel;
  17. @property (strong, nonatomic) UITableView *tableView;
  18. @end
  19. @implementation PindanMessageController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. // Do any additional setup after loading the view.
  23. self.view.backgroundColor = UIColorFromRGB(0xeeeeee);
  24. [self.view addSubview:self.tableView];
  25. if (self.viewModel.appMessages.count <= 0) {
  26. [self autoRefresh];
  27. }
  28. }
  29. - (void)didReceiveMemoryWarning {
  30. [super didReceiveMemoryWarning];
  31. // Dispose of any resources that can be recreated.
  32. }
  33. #pragma mark - View Lifecycle
  34. - (void)viewDidAppear:(BOOL)animated {
  35. [super viewDidAppear:animated];
  36. }
  37. - (void)viewWillAppear:(BOOL)animated {
  38. [super viewWillAppear:animated];
  39. self.navigationItem.title = @"订单消息";
  40. [self.tableView reloadData];
  41. [self.navigationController.tabBarController.tabBar setHidden:YES];
  42. [self.navigationController setNavigationBarHidden:NO animated:YES];
  43. [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
  44. }
  45. - (void)viewDidDisappear:(BOOL)animated {
  46. [super viewDidDisappear:animated];
  47. [self markAllMessageAsRead];
  48. }
  49. #pragma mark - Generate Property
  50. - (MessageViewModel *)viewModel {
  51. if (!_viewModel) {
  52. _viewModel = [[MessageViewModel alloc] init];
  53. }
  54. return _viewModel;
  55. }
  56. - (UITableView *)tableView {
  57. if (!_tableView) {
  58. _tableView = [[UITableView alloc] initWithFrame:[self contentFrame] style:UITableViewStyleGrouped];
  59. _tableView.delegate = self;
  60. _tableView.dataSource = self;
  61. _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  62. _tableView.backgroundColor = UIColorFromRGB(0xeeeeee);
  63. [_tableView registerClass:[PindanMessageCell class] forCellReuseIdentifier:NSStringFromClass([PindanMessageCell class])];
  64. [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
  65. [self initRefreshControlWithTableView:_tableView];
  66. if (@available(iOS 11.0, *)) {
  67. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  68. }
  69. }
  70. return _tableView;
  71. }
  72. - (CGRect)contentFrame {
  73. float width = self.view.frame.size.width;
  74. float height = [UIScreen mainScreen].bounds.size.height
  75. - [UIApplication sharedApplication].statusBarFrame.size.height
  76. - self.navigationController.navigationBar.frame.size.height;
  77. return CGRectMake(0, 0, width, height);
  78. }
  79. - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
  80. __weak PindanMessageController *weakSelf = self;
  81. if (direction == RefreshDirectionTop) {
  82. PageHeader *header = self.viewModel.pageHeader;
  83. [self.viewModel requestMessageWithIndex:@"0"
  84. AnchorID:header.anchorID
  85. startRow:@"0"
  86. pageSize:[NSString stringWithFormat:@"%d", PAGE_RECORD_COUNT]
  87. type:[self typeOfRequestNewMessage]
  88. success:^(MSGHeader *header, id responseObject, NSString *serverTime)
  89. {
  90. [weakSelf finishLoadingData];
  91. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  92. [weakSelf.viewModel removeTimelineMessages];
  93. [weakSelf.viewModel addTimelineMessages:responseObject serverTime:serverTime];
  94. [weakSelf.statusView removeFromSuperview];
  95. [weakSelf.tableView reloadData];
  96. if (![weakSelf.viewModel hasTimelineMessage]) {
  97. [weakSelf showStatusTipInView:weakSelf.view
  98. image:[UIImage imageNamed:@"StatusNoMessageIcon"]
  99. title:NoMessageTip];
  100. return ;
  101. }
  102. } else {
  103. [FLProgressHUDHelper showText:responseObject inView:weakSelf.view];
  104. }
  105. } failure:^(MSGHeader *header, NSError *error) {
  106. [weakSelf finishLoadingData];
  107. [weakSelf.tableView reloadData];
  108. [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view];
  109. }];
  110. } else if (direction == RefreshDirectionBottom) {
  111. PageHeader *header = self.viewModel.pageHeader;
  112. [self.viewModel requestMessageWithIndex:header.index
  113. AnchorID:header.anchorID
  114. startRow:[NSString stringWithFormat:@"%lu", (unsigned long)(header.startRow.intValue + PAGE_RECORD_COUNT)]
  115. pageSize:[NSString stringWithFormat:@"%d", PAGE_RECORD_COUNT]
  116. type:[self typeOfRequestNewMessage]
  117. success:^(MSGHeader *header, id responseObject, NSString *serverTime)
  118. {
  119. [weakSelf finishLoadingData];
  120. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  121. [weakSelf.viewModel addTimelineMessages:responseObject serverTime:serverTime];
  122. [weakSelf.tableView reloadData];
  123. } else {
  124. [FLProgressHUDHelper showText:responseObject inView:weakSelf.view];
  125. }
  126. } failure:^(MSGHeader *header, NSError *error) {
  127. [weakSelf finishLoadingData];
  128. [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view];
  129. }];
  130. }
  131. }
  132. - (void)forceRefresh {
  133. [self autoRefresh];
  134. }
  135. - (void)autoRefresh {
  136. [self.refreshControl startRefreshingDirection:RefreshDirectionTop];
  137. }
  138. - (void)markAllMessageAsRead {
  139. WeakSelf(weakSelf);
  140. [MessageViewModel markMessageRead:nil
  141. msgType:@"6"
  142. success:^(MSGHeader *header, id responseObject)
  143. {
  144. if ([header.code intValue] == RESPONSE_MSG_NORMAL
  145. && weakSelf.readPindanMessageBlock) {
  146. weakSelf.readPindanMessageBlock();
  147. }
  148. } failure:^(MSGHeader *header, NSError *error) {
  149. }];
  150. }
  151. #pragma mark - TableView Delegate Method
  152. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  153. return [self.viewModel numberOfTimelineSectionsInTableView];
  154. }
  155. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  156. return [self.viewModel numberOfRowsInTimelineSection:section];
  157. }
  158. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  159. if (indexPath.row == 0) {
  160. return 35;
  161. }
  162. FKAppMessage *message = [self.viewModel timelineItemAtIndex:indexPath];
  163. return [PindanMessageCell cellHeightWith:message.content isWithTitle:(message.title.length > 0)];
  164. }
  165. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  166. return 10;
  167. }
  168. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  169. return 0.1;
  170. }
  171. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  172. UITableViewCell *cell;
  173. if (indexPath.row == 0) {
  174. cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
  175. cell.textLabel.text = [self.viewModel timelineHeaderMessageAtIndexPath:indexPath];
  176. cell.textLabel.font = [UIFont systemFontOfSize:13];
  177. cell.textLabel.textColor = UIColorFromRGB(0x666666);
  178. } else {
  179. FKAppMessage *message = [self.viewModel timelineItemAtIndex:indexPath];
  180. if (message) {
  181. PindanMessageCell *messageCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PindanMessageCell class])];
  182. NSString *urlString = [NSString stringWithFormat:@"%@%@", message.firstPicURL, [FLStringHelper cdnParamaterString:75 height:75]];
  183. messageCell.photoView.image = nil;
  184. [messageCell.photoView sd_setImageWithURL:[NSURL URLWithString:urlString]];
  185. messageCell.timeLabel.text = [FLStringHelper convertStringToTipTime:message.createTime];
  186. messageCell.titleLabel.text = message.title;
  187. messageCell.descLabel.text = message.content;
  188. messageCell.descConstraint.constant = [PindanMessageCell titleHeightWith:message.content];
  189. [messageCell layoutWithTitle:(message.title.length > 0)];
  190. [messageCell setUnreadViewWith:message.status];
  191. cell = messageCell;
  192. }
  193. }
  194. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  195. return cell;
  196. }
  197. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  198. // Remove seperator inset
  199. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  200. [cell setSeparatorInset:UIEdgeInsetsZero];
  201. }
  202. // Prevent the cell from inheriting the Table View's margin settings
  203. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  204. [cell setPreservesSuperviewLayoutMargins:NO];
  205. }
  206. // Explictly set your cell's layout margins
  207. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  208. [cell setLayoutMargins:UIEdgeInsetsMake(0, 15, 0, 0)];
  209. }
  210. }
  211. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  212. FKAppMessage *message = [self.viewModel timelineItemAtIndex:indexPath];
  213. if (message) {
  214. if (message.targetURL.length > 0) {
  215. [[SchemaManager sharedManager] parserURL:[NSURL URLWithString:message.targetURL] shouldCache:NO];
  216. }
  217. message.status = @"2";
  218. [self.tableView reloadData];
  219. }
  220. }
  221. #pragma mark -
  222. - (NSString *)typeOfRequestNewMessage {
  223. return @"6";
  224. }
  225. @end