123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- //
- // CommentMSGViewController.m
- // FirstLink
- //
- // Created by Lemon on 15/1/10.
- // Copyright (c) 2015年 FirstLink. All rights reserved.
- //
- #import "CommentMessageController.h"
- #import "CommentMessageCell.h"
- #import <SDWebImage/UIImageView+WebCache.h>
- #import "MessageViewModel.h"
- #import "FKAppMessage.h"
- #import "SchemaManager.h"
- #import "ChatListViewController.h"
- #import "PindanDetailController.h"
- #import "FirstLinkAppDelegate.h"
- static NSString *CommentMessageCellIdentifier = @"CommentMessageCellIdentifier";
- @interface CommentMessageController ()
- <UITableViewDataSource, UITableViewDelegate, RefreshControlDelegate>
- @property (strong, nonatomic) UITableView *tableView;
- @property (nonatomic, strong) MessageViewModel *viewModel;
- @end
- @implementation CommentMessageController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- self.view.backgroundColor = UIColorFromRGB(0xeeeeee);
- [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:[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);
- }
- #pragma mark - FLTableView Init
- - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
- __weak CommentMessageController *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 removeAllMessages];
- [weakSelf.viewModel addMessages:responseObject];
-
- [weakSelf.statusView removeFromSuperview];
- [weakSelf.tableView reloadData];
-
- if (weakSelf.viewModel.appMessages.count == 0) {
- [weakSelf showStatusTipInView:weakSelf.view
- image:[UIImage imageNamed:@"StatusNoCommentIcon"]
- title:NoCommentTip];
- 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 addMessages:responseObject];
-
- if ([responseObject isKindOfClass:[NSArray class]]
- && [(NSArray*)responseObject count] > 0) {
- [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:@"3"
- 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 heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- FKAppMessage *message = [self itemAtIndex:indexPath.section];
- if (message) {
- return [CommentMessageCell cellHeightWith:message.content];
- }
- return 44;
- }
- - (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 {
- CommentMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CommentMessageCellIdentifier];
- FKAppMessage *message = [self itemAtIndex:indexPath.section];
- if (message) {
- NSString *urlString = [NSString stringWithFormat:@"%@%@", message.headURL,
- [FLStringHelper cdnParamaterString:32
- height:32]];
- cell.headView.image = nil;
- [cell.headView sd_setImageWithURL:[NSURL URLWithString:urlString]];
-
- urlString = [NSString stringWithFormat:@"%@%@", message.firstPicURL,
- [FLStringHelper cdnParamaterString:40
- height:40]];
- [cell.photoView sd_setImageWithURL:[NSURL URLWithString:urlString]];
-
- cell.nickLabel.text = [NSString stringWithFormat:@"%@ 评论了你", message.nickname];
- cell.timeLabel.text = [FLStringHelper convertStringToTipTime:message.createTime];
- cell.contentLabel.text = message.content;
- }
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
-
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- FKAppMessage *item = [self itemAtIndex:indexPath.section];
- [[SchemaManager sharedManager] parserURL:[NSURL URLWithString:item.targetURL] shouldCache:NO];
- }
- #pragma mark - Configure
- - (FKAppMessage*)itemAtIndex:(NSInteger)index {
- if (index < self.viewModel.appMessages.count) {
- return self.viewModel.appMessages[index];
- }
- return nil;
- }
- - (NSString *)typeOfRequestNewMessage {
- return @"3";
- }
- @end
|