ForumItemDetailViewController.m 18.9 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
//
//  ForumItemDetailViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2016/12/12.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "ForumItemDetailViewController.h"
#import "CommentListTableViewCell.h"
#import "PostPhotoManagerViewController.h"
#import "ShareGoodsViewController.h"
#import "WkWebViewViewController.h"
#import "AnnouncementContentTableViewCell.h"
#import "ForumDetailPhotoTableViewCell.h"
#import "ZJKeyBoardAccessoryView.h"
#import "ForumDetailBottomView.h"
#import "OSSHelper.h"
#import "ForumCommentListViewModel.h"
@interface ForumItemDetailViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,TapClickDelegate,ReturnTableviewcellIndexpathdelegate>


@property (nonatomic,strong) ForumReplyResponse *topicReply;

/**
 评论列表
 */
@property (nonatomic,strong) NSMutableArray *commentsArray;

/**
 headerView
 */
@property (weak, nonatomic) IBOutlet UIView *tableViewHeaderView;

/**
 发贴人头像
 */
@property (weak, nonatomic) IBOutlet UIImageView *issuerImageView;

/**
 贴心标题
 */
@property (weak, nonatomic) IBOutlet UILabel *postTitleLabel;

/**
 发帖人姓名、时间
 */
@property (weak, nonatomic) IBOutlet UILabel *issuerNameAndDateLabel;

/**
 分享
 */
@property (weak, nonatomic) IBOutlet UIButton *shareButton;

/**
 删除
 */
@property (weak, nonatomic) IBOutlet UIButton *deleteButton;

/**
 内容高度
 */
@property (nonatomic,assign) CGFloat contentHeigt;

/**
 图片附件高度
 */
@property (nonatomic,assign) CGFloat attachmentHeight;

/**
 获取回复列表
 */
@property (nonatomic,strong) ForumReplyCondition *condition;


/**
 底部的view
 */
@property (weak, nonatomic) IBOutlet ForumDetailBottomView *bottomView;



@end

@implementation ForumItemDetailViewController

#pragma mark - lazy
- (NSMutableArray *)commentsArray
{
    if (!_commentsArray) {
        _commentsArray = [NSMutableArray arrayWithObjects:@"",@"", nil];
    }
    return _commentsArray;
}

- (ForumReplyCondition *)condition
{
    if (!_condition) {
        _condition = [[ForumReplyCondition alloc] init];
        DataPage *page = [[DataPage alloc] init];
        page.page = ONE;
        page.rows = KROWS;
        _condition.page = page;
        _condition.topicIdEquals = self.topicDetail.fid;
    }
    return _condition;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self uiConfigAction];
    [self addChildWebViewController];
    [self getPostDetailAction:NO];
}

#pragma mark - UI
- (void)uiConfigAction
{
    self.commentView.layer.borderWidth = 1.0f;
    self.commentView.layer.borderColor = RGB(237, 238, 239, 1).CGColor;
    self.commentView.layer.cornerRadius = 4;
    self.commentView.layer.masksToBounds = YES;
    self.forumDetailTableView.tableFooterView = [UIView new];
    BOOL boolValue = ![self.topicDetail.posterId isEqualToString:[Shoppersmanager manager].shoppers.employee.fid];
    self.deleteButton.hidden = boolValue;
    [self.forumDetailTableView registerClass:[AnnouncementContentTableViewCell class] forCellReuseIdentifier:@"AnnouncementContentTableViewCell"];
    [self.forumDetailTableView registerClass:[ForumDetailPhotoTableViewCell class] forCellReuseIdentifier:@"ForumDetailPhotoTableViewCell"];
    [self setUpHeaderView];
    self.forumDetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.commentInputTextFieldView.inputAccessoryView = self.bottomView.toolView;
}

#pragma mark - WKWebView/photoManager
- (void)addChildWebViewController
{
    WS(weakSelf);
    WkWebViewViewController *webView = [[WkWebViewViewController alloc]initWithReturnContentSize:^(CGFloat contentHeight) {
        weakSelf.contentHeigt = contentHeight;
        [weakSelf.forumDetailTableView reloadData];
    }];
    self.contentHeigt = 44;//默认值
    webView.view.frame = CGRectMake(0, 0, ScreenWidth, self.contentHeigt);
    [self addChildViewController:webView];
    //图片
146
    PostPhotoManagerViewController *photoManager = [PostPhotoManagerViewController viewControllerWithStoryBoardType:STORYBOARD_TYPE_LEARNINGCENTER];
曹云霄's avatar
曹云霄 committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    [self addChildViewController:photoManager];
    NSMutableArray *array = [NSMutableArray array];
    for (TOAttachmentEntity *entity in self.topicDetail.attachments) {
        [array addObject:entity.fileUrl];
    }
    self.attachmentHeight = [self calculateImageHeight];
    photoManager.view.height = self.attachmentHeight;
    photoManager.imageArray = array;
}

#pragma mark - 获取回复列表
- (void)getPostDetailAction:(BOOL)isRemove
{
    WS(weakSelf);
    [XBLoadingView showHUDViewWithDefault];
    [HTTP networkRequestWithURL:SERVERREQUESTURL(POSTDETAIL) withRequestType:ZERO withParameter:self.condition withReturnValueBlock:^(id returnValue) {
        
        [XBLoadingView hideHUDViewWithDefault];
        if (RESULT(returnValue)) {
            if (isRemove) {
                [weakSelf.commentsArray removeAllObjects];
                weakSelf.commentsArray = nil;
            }
            weakSelf.topicReply = [[ForumReplyResponse alloc] initWithDictionary:RESPONSE(returnValue) error:nil];
            for (CustomTOForumReplyEntity *entity in weakSelf.topicReply.forumReplyEntity) {
                ForumCommentListViewModel *model = [ForumCommentListViewModel new];
                model.replyEntity = entity;
                [weakSelf.commentsArray addObject:model];
    
            }
            [weakSelf setUpCommentNumber];
            [weakSelf.forumDetailTableView reloadData];
        }else {
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    } withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark - 设置HeaderView
- (void)setUpHeaderView
{
    self.postTitleLabel.text = self.topicDetail.title;
        //系统管理员
    if (self.topicDetail.backEnd) {
        self.issuerImageView.image = GuideReplaceImage;
        self.issuerNameAndDateLabel.text = [NSString stringWithFormat:@"%@  %@",self.topicDetail.posterRealName,[BaseViewController formateDate:self.topicDetail.postTime]];
    }else {
        //普通用户
        [self.issuerImageView sd_setImageWithURL:[NSURL URLWithString:self.topicDetail.posterPicture] placeholderImage:CoustomerReplaceImage];
        self.issuerNameAndDateLabel.text = [NSString stringWithFormat:@"%@:  %@  %@",self.topicDetail.posterPosition,self.topicDetail.posterRealName,[BaseViewController formateDate:self.topicDetail.postTime]];
    }
}

- (void)viewDidLayoutSubviews {
    self.tableViewHeaderView.height = 80;
    self.forumDetailTableView.tableHeaderView = self.tableViewHeaderView;
}

#pragma mark - 设置评论个数
- (void)setUpCommentNumber
{
    self.commentNumberLabel.text = [NSString stringWithFormat:@"已有%ld条评论",(unsigned long)self.commentsArray.count - 2];
    self.praiseButton.selected = !self.topicDetail.canLike;
    WkWebViewViewController *webView = [self.childViewControllers firstObject];
    webView.htmlString = self.topicDetail.content;
}

#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
            case 0:
        {
            AnnouncementContentTableViewCell *contentCell = [tableView dequeueReusableCellWithIdentifier:@"AnnouncementContentTableViewCell" forIndexPath:indexPath];
            [contentCell.contentView addSubview:self.childViewControllers[0].view];
            return contentCell;
        }
            break;
            case 1:
        {
            ForumDetailPhotoTableViewCell *photoCell = [tableView dequeueReusableCellWithIdentifier:@"ForumDetailPhotoTableViewCell" forIndexPath:indexPath];
            [photoCell.contentView addSubview:self.childViewControllers[1].view];
            return photoCell;
        }
            break;

    }
    CommentListTableViewCell *commentCell = [tableView dequeueReusableCellWithIdentifier:@"CommentListTableViewCell" forIndexPath:indexPath];
    commentCell.bestView.delegate = self;
    commentCell.indexPath = indexPath;
    ForumCommentListViewModel *cellModel = self.commentsArray[indexPath.row];
    commentCell.replyEntity = cellModel.replyEntity;
    BOOL boolValue = ([self.topicDetail.posterId isEqualToString:[Shoppersmanager manager].shoppers.employee.fid] && [self.category.name isEqualToString:OPPLE_ASK]);
    commentCell.optionButton.hidden = !boolValue;
    return commentCell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commentsArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
            case 0:
        {
            return self.contentHeigt;

        }
            break;
            case 1:
        {
            return self.attachmentHeight;
        }
            break;
    }
    ForumCommentListViewModel *cellModel = self.commentsArray[indexPath.row];
    //68为上边距   10为下边距
    return cellModel.height;
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
            case 0:
            case 1:
            return;
    }
    CommentListTableViewCell *commentCell = (CommentListTableViewCell *)cell;
    commentCell.bestView.width = ZERO;
    commentCell.bestView.x = commentCell.optionButton.x;
}

#pragma mark - 计算选中图片显示所需高度
- (CGFloat)calculateImageHeight
{
    //默认每行显示3张图片
    CGFloat height = (ScreenWidth-60-2*10)/3.0;
    CGFloat interval = 10;
    CGFloat allHeight = 0;
    NSInteger number = self.topicDetail.attachments.count/3;
    if (self.topicDetail.attachments.count == 0) {
        return allHeight;
    }else if (self.topicDetail.attachments.count < 3) {
        return height;
    }else if (self.topicDetail.attachments.count %3 == 0) {
        allHeight = number*height + (number-1)*interval;
    }else if (number) {
        allHeight = (number+1)*height + number*interval;
    }
    return allHeight;
}

#pragma mark - 确认回复
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];

    if ([[self class] isBlankString:textField.text]) {
        [XBLoadingView showHUDViewWithText:@"评论内容不能为空"];
    }else {
        [self replyPostRequest];
    }
    return YES;
}

#pragma mark - 上传图片附件
- (void)uploadAttachments:(void(^)(NSArray *OSSKeys))finish
{
    if (self.bottomView.toolView.arrSelectedPhotos.count == 0) {
        finish(nil);
        return;
    }
    NSMutableArray *OSSKeyArray = [NSMutableArray array];
    for (int i=0; i<self.bottomView.toolView.arrSelectedPhotos.count; i++) {
        [OSSKeyArray addObject:[OSSHelper getOSSObjectKeyWithtype:@"jpg" index:i]];
    }
    XBLoadingView *loadView = [XBLoadingView showHUDViewProgressLabel:[NSString stringWithFormat:@"图片上传中:1/%ld",self.bottomView.toolView.arrSelectedPhotos.count]];
    static NSInteger number = 0;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        for (int i=0;i<self.bottomView.toolView.arrSelectedPhotos.count;i++) {
            UIImage *image = self.bottomView.toolView.arrSelectedPhotos[i];
            NSString *ossKey = OSSKeyArray[i];
334 335
            UIImage *newSizeImage = [image rescaleImageToSize:ScreenSize];
            NSData *data = UIImagePNGRepresentation(newSizeImage);
曹云霄's avatar
曹云霄 committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
            [OSSHelper uploadImageObjectWithKey:ossKey data:data type:OSSHelperOperationTypeSynchronous progress:^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
                CGFloat totalBytesSentFloat = totalBytesSent;
                CGFloat totalBytesExpectedToSendFloat = totalBytesExpectedToSend;
                CGFloat progress = totalBytesSentFloat / totalBytesExpectedToSendFloat;
                NSLog(@"-->进度%f", progress);
                loadView.progress = progress;
                
            } success:^id(OSSTask *task) {
                
                number ++;
                loadView.labelText = [NSString stringWithFormat:@"图片上传中:%ld/%ld",number,self.self.bottomView.toolView.arrSelectedPhotos.count];
                if (number == self.bottomView.toolView.arrSelectedPhotos.count) {
                    number = 0;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [loadView hide:YES];
                        [XBLoadingView showHUDViewWithSuccessText:@"图片上传成功" completeBlock:^{
                            finish(OSSKeyArray);
                        }];
                    });
                }
                return nil;
            } error:^(NSError *error) {
                [XBLoadingView hideHUDViewWithDefault];
                [XBLoadingView showHUDViewWithText:error.localizedDescription];
            }];
        }
    });
}



#pragma mark - 回复帖子
- (void)replyPostRequest
{
    WS(weakSelf);
    [self uploadAttachments:^(NSArray *OSSKeys) {
        NSLog(@"%@", OSSKeys);
        TOForumReplyEntity *reply = [[TOForumReplyEntity alloc] init];
        reply.topicId = self.topicDetail.fid;
        reply.replyContent = self.commentInputTextFieldView.text;
        reply.replyerName = [Shoppersmanager manager].shoppers.employee.userName;
        reply.replyerRealName = [Shoppersmanager manager].shoppers.employee.realName;
        reply.replyerPosition = [Shoppersmanager manager].shoppers.employee.positionsName;
        reply.replyTime = [[self class] getTimeby:0];
        reply.replyerId = [Shoppersmanager manager].shoppers.employee.fid;
        reply.isAdminReply = [Shoppersmanager manager].shoppers.isAdmin;
        NSMutableArray *attachments = [NSMutableArray array];
        NSString *entityId = [OSSHelper getOSSObjectKey];
        reply.attachmentId = entityId;
        for (NSString *ossKey in OSSKeys) {
            TOAttachmentEntity *entity = [[TOAttachmentEntity alloc] init];
            entity.entityType = @"forumReply";
            entity.entityId = entityId;
            entity.fileUrl = [OSSHelper getCompleteImageURLWithOSSkey:ossKey];
            entity.fileName = ossKey;
            [attachments addObject:entity];
        }
        reply.attachments = (NSArray<TOAttachmentEntity>*)attachments;
        [weakSelf httpSubmit:reply complete:nil];
    }];
}

- (void)httpSubmit:(TOForumReplyEntity *)reply complete:(void (^)(void))complete {
    [XBLoadingView showHUDViewWithDefault];
    WS(weakSelf);
    [HTTP networkRequestWithURL:SERVERREQUESTURL(REPLYPOST) withRequestType:ZERO withParameter:reply withReturnValueBlock:^(id returnValue) {
        
        [XBLoadingView hideHUDViewWithDefault];
        if (RESULT(returnValue)) {
            weakSelf.commentInputTextFieldView.text = nil;
            [weakSelf.bottomView clearPhoto];
            [XBLoadingView showHUDViewWithSuccessText:@"评论成功" completeBlock:^{
                if (weakSelf.refreshBlock) {
                    weakSelf.refreshBlock(ONE,Comment,weakSelf.indexPath);
                }
                [weakSelf getPostDetailAction:YES];
            }];
        }else {
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
        
    } withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark -删除
- (IBAction)deleteButtonClickAction:(UIButton *)sender {
    [self delecteClickPostAction];
}

#pragma mark -分享
- (IBAction)shareButtonClickAction:(UIButton *)sender {
    [self sharePostClickAction];
}

#pragma mark - 删除帖子
- (void)delecteClickPostAction
{
    ShowAlertView(@"提示", @"确认删除此贴吗?", @[@"确认",@"取消"], UIAlertControllerStyleAlert, ^(NSInteger index) {
        if (index == ONE) {
            return;
        }
        [XBLoadingView showHUDViewWithDefault];
        WS(weakSelf);
        [HTTP networkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(DELETEPOST),self.topicDetail.fid] withRequestType:NetworkRequestWithDELETE withParameter:nil withReturnValueBlock:^(id returnValue) {
            [XBLoadingView hideHUDViewWithDefault];
            if (RESULT(returnValue)) {
                [XBLoadingView showHUDViewWithSuccessText:@"删除成功" completeBlock:^{
                    if (weakSelf.delectBlock) {
                        weakSelf.delectBlock(weakSelf.topicDetail.fid);
                    }
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                }];
            }else {
                [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
            }
            
        } withFailureBlock:^(NSError *error) {
            [XBLoadingView hideHUDViewWithDefault];
            [XBLoadingView showHUDViewWithText:error.localizedDescription];
        }];
    });
}

#pragma mark -分享帖子
- (void)sharePostClickAction
{
    ShareGoodsViewController *shareController = [[ShareGoodsViewController alloc]init];
    shareController.type = SHARE_INVITATION;
    shareController.shareID = self.topicDetail.fid;
    shareController.shareImage = [UIImage imageNamed:@"Icon-83.5"];
    [self popoverPresentationController:shareController withPreferredContentSize:CGSizeMake(290, 120) withBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:self.shareButton]];
}


#pragma mark - <TapClickDelegate>
#pragma mark -最佳回复
- (void)tapClickAction:(BOOL)isBest withReplyId:(NSString *)replyId withIndexPath:(NSIndexPath *)indexPath
{
    WS(weakSelf);
    [XBLoadingView showHUDViewWithDefault];
    [HTTP networkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(BASEREPLY),self.topicDetail.fid,replyId,isBest?@"true":@"false"] withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
        [XBLoadingView hideHUDViewWithDefault];
        if (RESULT(returnValue)) {
            [weakSelf getPostDetailAction:YES];
        }else {
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
        
    } withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark - 点赞
- (IBAction)praiseButtonClickAction:(UIButton *)sender {
    
    [XBLoadingView showHUDViewWithDefault];
    NSString *url = [NSString stringWithFormat:SERVERREQUESTURL(PRAISE),self.topicDetail.fid,!sender.selected?@"true":@"false"];
    WS(weakSelf);
    [HTTP networkWithDictionaryRequestWithURL:url withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
        [XBLoadingView hideHUDViewWithDefault];
        if (RESULT(returnValue)) {
            sender.selected = !sender.selected;
            //刷新列表
            if (weakSelf.refreshBlock) {
                weakSelf.refreshBlock(sender.selected?ONE:-ONE,Praise,weakSelf.indexPath);
            }
        }else {
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
        
    } withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}


@end