暂无描述

SysMessageController.m 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //
  2. // SystemViewController.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 14/12/25.
  6. // Copyright (c) 2014年 FirstLink. All rights reserved.
  7. //
  8. #import "SysMessageController.h"
  9. #import "MessageViewModel.h"
  10. #import "FKAppMessage.h"
  11. #import <SDWebImage/UIImageView+WebCache.h>
  12. #import "SysMessageCell.h"
  13. #import "CommentMessageCell.h"
  14. #import "SchemaManager.h"
  15. #import "ChatListViewController.h"
  16. #import "WebViewController.h"
  17. #import "FKProDetailController.h"
  18. #import "FKProductAdviseController.h"
  19. @interface SysMessageController ()
  20. <UITableViewDataSource, UITableViewDelegate, RefreshControlDelegate>
  21. @property (nonatomic, strong) UITableView *tableView;
  22. @property (nonatomic, strong) MessageViewModel *viewModel;
  23. @end
  24. static NSString *SysMessageCellIdentifier = @"SysMessageCellIdentifier";
  25. static NSString *CommentMessageCellIdentifier = @"CommentMessageCellIdentifier";
  26. @implementation SysMessageController
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. [self.view addSubview:self.tableView];
  30. if (self.viewModel.appMessages.count <= 0) {
  31. [self autoRefresh];
  32. [self markMessageAsRead];
  33. }
  34. }
  35. - (void)viewDidAppear:(BOOL)animated {
  36. [super viewDidAppear:animated];
  37. }
  38. - (void)viewWillAppear:(BOOL)animated {
  39. [super viewWillAppear:animated];
  40. self.navigationItem.title = @"系统通知";
  41. [self.navigationController.tabBarController.tabBar setHidden:YES];
  42. [self.navigationController setNavigationBarHidden:NO animated:YES];
  43. [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
  44. }
  45. - (void)viewWillDisappear:(BOOL)animated {
  46. [super viewWillDisappear:animated];
  47. }
  48. #pragma mark - Generate Property
  49. - (MessageViewModel*)viewModel {
  50. if (!_viewModel) {
  51. _viewModel = [[MessageViewModel alloc] init];
  52. }
  53. return _viewModel;
  54. }
  55. - (UITableView*)tableView {
  56. if (!_tableView) {
  57. _tableView = [[UITableView alloc] initWithFrame:[self contentFrame] style:UITableViewStyleGrouped];
  58. _tableView.delegate = self;
  59. _tableView.dataSource = self;
  60. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  61. _tableView.backgroundColor = UIColorFromRGB(0xeeeeee);
  62. [_tableView registerClass:[SysMessageCell class] forCellReuseIdentifier:SysMessageCellIdentifier];
  63. [_tableView registerClass:[CommentMessageCell class] forCellReuseIdentifier:CommentMessageCellIdentifier];
  64. [self initRefreshControlWithTableView:_tableView];
  65. if (@available(iOS 11.0, *)) {
  66. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  67. }
  68. }
  69. return _tableView;
  70. }
  71. - (CGRect)contentFrame {
  72. float width = self.view.frame.size.width;
  73. float height = [UIScreen mainScreen].bounds.size.height
  74. - [UIApplication sharedApplication].statusBarFrame.size.height
  75. - self.navigationController.navigationBar.frame.size.height;
  76. return CGRectMake(0, 0, width, height);
  77. }
  78. - (NSMutableDictionary*)parserPara:(NSString*)query {
  79. query = [query stringByRemovingPercentEncoding];
  80. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  81. for (NSString *param in [query componentsSeparatedByString:@"&"]) {
  82. NSArray *elts = [param componentsSeparatedByString:@"="];
  83. if([elts count] < 2) continue;
  84. [params setObject:[elts objectAtIndex:1] forKey:[elts objectAtIndex:0]];
  85. }
  86. return params;
  87. }
  88. #pragma mark - FLTableView Init
  89. - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
  90. WeakSelf(weakSelf);
  91. if (direction==RefreshDirectionTop) {
  92. PageHeader *header = self.viewModel.pageHeader;
  93. [self.viewModel requestMessageWithIndex:@"0"
  94. AnchorID:header.anchorID
  95. startRow:@"0"
  96. pageSize:[NSString stringWithFormat:@"%d", PAGE_RECORD_COUNT]
  97. type:@"1"
  98. success:^(MSGHeader *header, id responseObject, NSString *serverTime)
  99. {
  100. [weakSelf finishLoadingData];
  101. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  102. [weakSelf.viewModel removeAllMessages];
  103. [weakSelf.viewModel addMessages:responseObject];
  104. [weakSelf.statusView removeFromSuperview];
  105. [weakSelf.tableView reloadData];
  106. if (weakSelf.viewModel.appMessages.count == 0) {
  107. [weakSelf showStatusTipInView:weakSelf.view
  108. image:[UIImage imageNamed:@"StatusNoMessageIcon"]
  109. title:NoMessageTip];
  110. return ;
  111. }
  112. } else {
  113. [FLProgressHUDHelper showText:responseObject inView:weakSelf.view];
  114. }
  115. } failure:^(MSGHeader *header, NSError *error) {
  116. [weakSelf finishLoadingData];
  117. [weakSelf.tableView reloadData];
  118. [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view];
  119. }];
  120. } else if (direction==RefreshDirectionBottom) {
  121. PageHeader *header = self.viewModel.pageHeader;
  122. [self.viewModel requestMessageWithIndex:header.index
  123. AnchorID:header.anchorID
  124. startRow:[NSString stringWithFormat:@"%lu", (unsigned long)(header.startRow.intValue + PAGE_RECORD_COUNT)]
  125. pageSize:[NSString stringWithFormat:@"%d", PAGE_RECORD_COUNT]
  126. type:@"1"
  127. success:^(MSGHeader *header, id responseObject, NSString *serverTime)
  128. {
  129. [weakSelf finishLoadingData];
  130. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  131. [weakSelf.viewModel addMessages:responseObject];
  132. [weakSelf.tableView reloadData];
  133. } else {
  134. [FLProgressHUDHelper showText:responseObject inView:weakSelf.view];
  135. }
  136. } failure:^(MSGHeader *header, NSError *error) {
  137. [weakSelf finishLoadingData];
  138. [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view];
  139. }];
  140. }
  141. }
  142. - (void)forceRefresh {
  143. [self autoRefresh];
  144. }
  145. - (void)autoRefresh {
  146. [self.refreshControl startRefreshingDirection:RefreshDirectionTop];
  147. }
  148. - (void)markMessageAsRead {
  149. [MessageViewModel markMessageRead:nil
  150. msgType:@"1"
  151. success:^(MSGHeader *header, id responseObject)
  152. {
  153. } failure:^(MSGHeader *header, NSError *error) {
  154. }];
  155. }
  156. #pragma mark - TableView Delegate Method
  157. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  158. return self.viewModel.appMessages.count;
  159. }
  160. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  161. return 1;
  162. }
  163. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  164. if (section == 0) {
  165. return 10;
  166. }
  167. return 0.1;
  168. }
  169. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  170. return 10;
  171. }
  172. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  173. FKAppMessage *item = [self itemAtIndex:indexPath.section];
  174. if (item) {
  175. if ([item.itemType isEqualToString:@"11"]) {
  176. return [CommentMessageCell cellHeightWith:item.content];
  177. } else {
  178. return [SysMessageCell cellHeightWith:item];
  179. }
  180. }
  181. return 80;
  182. }
  183. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  184. UITableViewCell *cell;
  185. FKAppMessage *item = [self itemAtIndex:indexPath.section];
  186. if (item) {
  187. if ([item.itemType isEqualToString:@"11"]) {
  188. CommentMessageCell *commentCell = [tableView dequeueReusableCellWithIdentifier:CommentMessageCellIdentifier];
  189. NSString *urlString = [NSString stringWithFormat:@"%@%@", item.headURL,
  190. [FLStringHelper cdnParamaterString:32
  191. height:32]];
  192. [commentCell.headView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:nil];
  193. urlString = [NSString stringWithFormat:@"%@%@", item.firstPicURL,
  194. [FLStringHelper cdnParamaterString:40
  195. height:40]];
  196. [commentCell.photoView sd_setImageWithURL:[NSURL URLWithString:urlString]];
  197. commentCell.nickLabel.text = [NSString stringWithFormat:@"%@ 回复了你", [FLStringHelper replaceNilWithEmpty:item.nickname]];
  198. commentCell.timeLabel.text = [FLStringHelper convertStringToTipTime:item.createTime];
  199. commentCell.contentLabel.text = item.content;
  200. cell = commentCell;
  201. } else {
  202. SysMessageCell *sysCell = [tableView dequeueReusableCellWithIdentifier:SysMessageCellIdentifier];
  203. sysCell.descLabel.text = item.content;
  204. sysCell.timeLabel.text = [FLStringHelper convertStringToTipTime:item.createTime];
  205. if (item.targetURL.length > 0) {
  206. sysCell.descLabel.numberOfLines = 0;
  207. sysCell.detailButton.hidden = NO;
  208. } else {
  209. sysCell.descLabel.numberOfLines = 0;
  210. sysCell.detailButton.hidden = YES;
  211. }
  212. cell = sysCell;
  213. }
  214. }
  215. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  216. return cell;
  217. }
  218. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  219. if (indexPath.section < self.viewModel.appMessages.count) {
  220. FKAppMessage *message = self.viewModel.appMessages[indexPath.section];
  221. if ([message.itemType isEqualToString:@"11"]) {
  222. if (message.targetURL.length > 0) {
  223. NSURL *url = [NSURL URLWithString:message.targetURL];
  224. NSDictionary *para = [self parserPara:url.query];
  225. NSString *productID = [para objectForKey:@"id"];
  226. if (productID.length > 0) {
  227. FKProDetailController *productController = [[FKProDetailController alloc] initWithProductID:productID];
  228. FKProductAdviseController *adviseController = [[FKProductAdviseController alloc] initWithItemID:productID];
  229. NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
  230. [viewControllers addObjectsFromArray:@[productController, adviseController]];
  231. [self.navigationController setViewControllers:viewControllers animated:YES];
  232. }
  233. }
  234. } else {
  235. if (message.targetURL.length > 0) {
  236. NSURL *url = [NSURL URLWithString:message.targetURL];
  237. if ([url.scheme rangeOfString:@"HTTP" options:NSCaseInsensitiveSearch].length > 0) {
  238. WebViewController *newViewController = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:@"WebViewController"];
  239. newViewController.url = message.targetURL;
  240. [self.navigationController pushViewController:newViewController animated:YES];
  241. } else {
  242. [[SchemaManager sharedManager] parserURL:url shouldCache:NO];
  243. }
  244. }
  245. }
  246. }
  247. }
  248. - (FKAppMessage*)itemAtIndex:(NSInteger)index {
  249. if (index < self.viewModel.appMessages.count) {
  250. return self.viewModel.appMessages[index];
  251. }
  252. return nil;
  253. }
  254. @end