// // PindanMSGViewController.m // FirstLink // // Created by Lemon on 15/4/27. // Copyright (c) 2015年 FirstLink. All rights reserved. // #import "PindanMessageController.h" #import "PindanMessageCell.h" #import "ChatListViewController.h" #import "MessageViewModel.h" #import "FKAppMessage.h" #import "SchemaManager.h" @interface PindanMessageController () @property (nonatomic, strong) MessageViewModel *viewModel; @property (strong, nonatomic) UITableView *tableView; @end @implementation PindanMessageController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = UIColorFromRGB(0xeeeeee); [self.view addSubview:self.tableView]; if (self.viewModel.appMessages.count <= 0) { [self autoRefresh]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - View Lifecycle - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationItem.title = @"订单消息"; [self.tableView reloadData]; [self.navigationController.tabBarController.tabBar setHidden:YES]; [self.navigationController setNavigationBarHidden:NO animated:YES]; [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [self markAllMessageAsRead]; } #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 = UITableViewCellSeparatorStyleSingleLine; _tableView.backgroundColor = UIColorFromRGB(0xeeeeee); [_tableView registerClass:[PindanMessageCell class] forCellReuseIdentifier:NSStringFromClass([PindanMessageCell class])]; [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])]; [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); } - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction { __weak PindanMessageController *weakSelf = self; 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:[self typeOfRequestNewMessage] success:^(MSGHeader *header, id responseObject, NSString *serverTime) { [weakSelf finishLoadingData]; if ([header.code intValue] == RESPONSE_MSG_NORMAL) { [weakSelf.viewModel removeTimelineMessages]; [weakSelf.viewModel addTimelineMessages:responseObject serverTime:serverTime]; [weakSelf.statusView removeFromSuperview]; [weakSelf.tableView reloadData]; if (![weakSelf.viewModel hasTimelineMessage]) { [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:[self typeOfRequestNewMessage] success:^(MSGHeader *header, id responseObject, NSString *serverTime) { [weakSelf finishLoadingData]; if ([header.code intValue] == RESPONSE_MSG_NORMAL) { [weakSelf.viewModel addTimelineMessages:responseObject serverTime:serverTime]; [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)markAllMessageAsRead { WeakSelf(weakSelf); [MessageViewModel markMessageRead:nil msgType:@"6" success:^(MSGHeader *header, id responseObject) { if ([header.code intValue] == RESPONSE_MSG_NORMAL && weakSelf.readPindanMessageBlock) { weakSelf.readPindanMessageBlock(); } } failure:^(MSGHeader *header, NSError *error) { }]; } #pragma mark - TableView Delegate Method - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self.viewModel numberOfTimelineSectionsInTableView]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.viewModel numberOfRowsInTimelineSection:section]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { return 35; } FKAppMessage *message = [self.viewModel timelineItemAtIndex:indexPath]; return [PindanMessageCell cellHeightWith:message.content isWithTitle:(message.title.length > 0)]; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 10; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; if (indexPath.row == 0) { cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])]; cell.textLabel.text = [self.viewModel timelineHeaderMessageAtIndexPath:indexPath]; cell.textLabel.font = [UIFont systemFontOfSize:13]; cell.textLabel.textColor = UIColorFromRGB(0x666666); } else { FKAppMessage *message = [self.viewModel timelineItemAtIndex:indexPath]; if (message) { PindanMessageCell *messageCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PindanMessageCell class])]; NSString *urlString = [NSString stringWithFormat:@"%@%@", message.firstPicURL, [FLStringHelper cdnParamaterString:75 height:75]]; messageCell.photoView.image = nil; [messageCell.photoView sd_setImageWithURL:[NSURL URLWithString:urlString]]; messageCell.timeLabel.text = [FLStringHelper convertStringToTipTime:message.createTime]; messageCell.titleLabel.text = message.title; messageCell.descLabel.text = message.content; messageCell.descConstraint.constant = [PindanMessageCell titleHeightWith:message.content]; [messageCell layoutWithTitle:(message.title.length > 0)]; [messageCell setUnreadViewWith:message.status]; cell = messageCell; } } cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { // Remove seperator inset if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } // Prevent the cell from inheriting the Table View's margin settings if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) { [cell setPreservesSuperviewLayoutMargins:NO]; } // Explictly set your cell's layout margins if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsMake(0, 15, 0, 0)]; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { FKAppMessage *message = [self.viewModel timelineItemAtIndex:indexPath]; if (message) { if (message.targetURL.length > 0) { [[SchemaManager sharedManager] parserURL:[NSURL URLWithString:message.targetURL] shouldCache:NO]; } message.status = @"2"; [self.tableView reloadData]; } } #pragma mark - - (NSString *)typeOfRequestNewMessage { return @"6"; } @end