// // SystemViewController.m // FirstLink // // Created by ascii on 14/12/25. // Copyright (c) 2014年 FirstLink. All rights reserved. // #import "SysMessageController.h" #import "MessageViewModel.h" #import "FKAppMessage.h" #import #import "SysMessageCell.h" #import "CommentMessageCell.h" #import "SchemaManager.h" #import "ChatListViewController.h" #import "WebViewController.h" #import "FKProDetailController.h" #import "FKProductAdviseController.h" @interface SysMessageController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) MessageViewModel *viewModel; @end static NSString *SysMessageCellIdentifier = @"SysMessageCellIdentifier"; static NSString *CommentMessageCellIdentifier = @"CommentMessageCellIdentifier"; @implementation SysMessageController - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.tableView]; if (self.viewModel.appMessages.count <= 0) { [self autoRefresh]; [self markMessageAsRead]; } } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationItem.title = @"系统通知"; [self.navigationController.tabBarController.tabBar setHidden:YES]; [self.navigationController setNavigationBarHidden:NO animated:YES]; [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } #pragma mark - Generate Property - (MessageViewModel*)viewModel { if (!_viewModel) { _viewModel = [[MessageViewModel alloc] init]; } return _viewModel; } - (UITableView*)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:[self contentFrame] style:UITableViewStyleGrouped]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.backgroundColor = UIColorFromRGB(0xeeeeee); [_tableView registerClass:[SysMessageCell class] forCellReuseIdentifier:SysMessageCellIdentifier]; [_tableView registerClass:[CommentMessageCell class] forCellReuseIdentifier:CommentMessageCellIdentifier]; [self initRefreshControlWithTableView:_tableView]; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } } return _tableView; } - (CGRect)contentFrame { float width = self.view.frame.size.width; float height = [UIScreen mainScreen].bounds.size.height - [UIApplication sharedApplication].statusBarFrame.size.height - self.navigationController.navigationBar.frame.size.height; return CGRectMake(0, 0, width, height); } - (NSMutableDictionary*)parserPara:(NSString*)query { query = [query stringByRemovingPercentEncoding]; NSMutableDictionary *params = [NSMutableDictionary dictionary]; for (NSString *param in [query componentsSeparatedByString:@"&"]) { NSArray *elts = [param componentsSeparatedByString:@"="]; if([elts count] < 2) continue; [params setObject:[elts objectAtIndex:1] forKey:[elts objectAtIndex:0]]; } return params; } #pragma mark - FLTableView Init - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction { WeakSelf(weakSelf); if (direction==RefreshDirectionTop) { PageHeader *header = self.viewModel.pageHeader; [self.viewModel requestMessageWithIndex:@"0" AnchorID:header.anchorID startRow:@"0" pageSize:[NSString stringWithFormat:@"%d", PAGE_RECORD_COUNT] type:@"1" success:^(MSGHeader *header, id responseObject, NSString *serverTime) { [weakSelf finishLoadingData]; if ([header.code intValue] == RESPONSE_MSG_NORMAL) { [weakSelf.viewModel removeAllMessages]; [weakSelf.viewModel addMessages:responseObject]; [weakSelf.statusView removeFromSuperview]; [weakSelf.tableView reloadData]; if (weakSelf.viewModel.appMessages.count == 0) { [weakSelf showStatusTipInView:weakSelf.view image:[UIImage imageNamed:@"StatusNoMessageIcon"] title:NoMessageTip]; return ; } } else { [FLProgressHUDHelper showText:responseObject inView:weakSelf.view]; } } failure:^(MSGHeader *header, NSError *error) { [weakSelf finishLoadingData]; [weakSelf.tableView reloadData]; [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view]; }]; } else if (direction==RefreshDirectionBottom) { PageHeader *header = self.viewModel.pageHeader; [self.viewModel requestMessageWithIndex:header.index AnchorID:header.anchorID startRow:[NSString stringWithFormat:@"%lu", (unsigned long)(header.startRow.intValue + PAGE_RECORD_COUNT)] pageSize:[NSString stringWithFormat:@"%d", PAGE_RECORD_COUNT] type:@"1" success:^(MSGHeader *header, id responseObject, NSString *serverTime) { [weakSelf finishLoadingData]; if ([header.code intValue] == RESPONSE_MSG_NORMAL) { [weakSelf.viewModel addMessages:responseObject]; [weakSelf.tableView reloadData]; } else { [FLProgressHUDHelper showText:responseObject inView:weakSelf.view]; } } failure:^(MSGHeader *header, NSError *error) { [weakSelf finishLoadingData]; [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view]; }]; } } - (void)forceRefresh { [self autoRefresh]; } - (void)autoRefresh { [self.refreshControl startRefreshingDirection:RefreshDirectionTop]; } - (void)markMessageAsRead { [MessageViewModel markMessageRead:nil msgType:@"1" success:^(MSGHeader *header, id responseObject) { } failure:^(MSGHeader *header, NSError *error) { }]; } #pragma mark - TableView Delegate Method - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.viewModel.appMessages.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section == 0) { return 10; } return 0.1; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 10; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { FKAppMessage *item = [self itemAtIndex:indexPath.section]; if (item) { if ([item.itemType isEqualToString:@"11"]) { return [CommentMessageCell cellHeightWith:item.content]; } else { return [SysMessageCell cellHeightWith:item]; } } return 80; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; FKAppMessage *item = [self itemAtIndex:indexPath.section]; if (item) { if ([item.itemType isEqualToString:@"11"]) { CommentMessageCell *commentCell = [tableView dequeueReusableCellWithIdentifier:CommentMessageCellIdentifier]; NSString *urlString = [NSString stringWithFormat:@"%@%@", item.headURL, [FLStringHelper cdnParamaterString:32 height:32]]; [commentCell.headView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:nil]; urlString = [NSString stringWithFormat:@"%@%@", item.firstPicURL, [FLStringHelper cdnParamaterString:40 height:40]]; [commentCell.photoView sd_setImageWithURL:[NSURL URLWithString:urlString]]; commentCell.nickLabel.text = [NSString stringWithFormat:@"%@ 回复了你", [FLStringHelper replaceNilWithEmpty:item.nickname]]; commentCell.timeLabel.text = [FLStringHelper convertStringToTipTime:item.createTime]; commentCell.contentLabel.text = item.content; cell = commentCell; } else { SysMessageCell *sysCell = [tableView dequeueReusableCellWithIdentifier:SysMessageCellIdentifier]; sysCell.descLabel.text = item.content; sysCell.timeLabel.text = [FLStringHelper convertStringToTipTime:item.createTime]; if (item.targetURL.length > 0) { sysCell.descLabel.numberOfLines = 0; sysCell.detailButton.hidden = NO; } else { sysCell.descLabel.numberOfLines = 0; sysCell.detailButton.hidden = YES; } cell = sysCell; } } cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section < self.viewModel.appMessages.count) { FKAppMessage *message = self.viewModel.appMessages[indexPath.section]; if ([message.itemType isEqualToString:@"11"]) { if (message.targetURL.length > 0) { NSURL *url = [NSURL URLWithString:message.targetURL]; NSDictionary *para = [self parserPara:url.query]; NSString *productID = [para objectForKey:@"id"]; if (productID.length > 0) { FKProDetailController *productController = [[FKProDetailController alloc] initWithProductID:productID]; FKProductAdviseController *adviseController = [[FKProductAdviseController alloc] initWithItemID:productID]; NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers]; [viewControllers addObjectsFromArray:@[productController, adviseController]]; [self.navigationController setViewControllers:viewControllers animated:YES]; } } } else { if (message.targetURL.length > 0) { NSURL *url = [NSURL URLWithString:message.targetURL]; if ([url.scheme rangeOfString:@"HTTP" options:NSCaseInsensitiveSearch].length > 0) { WebViewController *newViewController = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:@"WebViewController"]; newViewController.url = message.targetURL; [self.navigationController pushViewController:newViewController animated:YES]; } else { [[SchemaManager sharedManager] parserURL:url shouldCache:NO]; } } } } } - (FKAppMessage*)itemAtIndex:(NSInteger)index { if (index < self.viewModel.appMessages.count) { return self.viewModel.appMessages[index]; } return nil; } @end