ForumItemDetailViewController.m 14.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
//
//  ForumItemDetailViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2016/12/12.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "ForumItemDetailViewController.h"
10 11
#import "CommentListTableViewCell.h"
#import "PostPhotoManagerViewController.h"
12
#import "ShareGoodsViewController.h"
13 14 15
#import "WkWebViewViewController.h"
#import "AnnouncementContentTableViewCell.h"
#import "ForumDetailPhotoTableViewCell.h"
16

17
@interface ForumItemDetailViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,TapClickDelegate,ReturnTableviewcellIndexpathdelegate>
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


@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;

/**
48
 分享
49
 */
50 51 52 53 54 55
@property (weak, nonatomic) IBOutlet UIButton *shareButton;

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

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

62 63 64 65
/**
 图片附件高度
 */
@property (nonatomic,assign) CGFloat attachmentHeight;
66

67 68 69 70
/**
 获取回复列表
 */
@property (nonatomic,strong) ForumReplyCondition *condition;
71 72 73 74 75 76


@end

@implementation ForumItemDetailViewController

77 78 79 80
#pragma mark - lazy
- (NSMutableArray *)commentsArray
{
    if (!_commentsArray) {
81
        _commentsArray = [NSMutableArray arrayWithObjects:@"",@"", nil];
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    }
    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;
}

99 100
- (void)viewDidLoad {
    [super viewDidLoad];
101 102
    
    [self uiConfigAction];
103
    [self addChildWebViewController];
104
    [self getPostDetailAction:NO];
105 106 107 108 109 110 111 112 113 114
}

#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];
115
    BOOL boolValue = ![self.topicDetail.posterId isEqualToString:[Shoppersmanager manager].shoppers.employee.fid];
116
    self.deleteButton.hidden = boolValue;
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
    [self.forumDetailTableView registerClass:[AnnouncementContentTableViewCell class] forCellReuseIdentifier:@"AnnouncementContentTableViewCell"];
    [self.forumDetailTableView registerClass:[ForumDetailPhotoTableViewCell class] forCellReuseIdentifier:@"ForumDetailPhotoTableViewCell"];
    [self setUpHeaderView];
    self.forumDetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#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];
    //图片
    PostPhotoManagerViewController *photoManager = [[[self class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"PostPhotoManagerViewController"];
    [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;
144 145 146
}

#pragma mark - 获取回复列表
147
- (void)getPostDetailAction:(BOOL)isRemove
148 149 150
{
    WS(weakSelf);
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
151
    [HTTP networkRequestWithURL:SERVERREQUESTURL(POSTDETAIL) withRequestType:ZERO withParameter:self.condition withReturnValueBlock:^(id returnValue) {
152 153
        
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
154
        if (RESULT(returnValue)) {
155 156
            if (isRemove) {
                [weakSelf.commentsArray removeAllObjects];
157
                weakSelf.commentsArray = nil;
158
            }
曹云霄's avatar
曹云霄 committed
159
            weakSelf.topicReply = [[ForumReplyResponse alloc] initWithDictionary:RESPONSE(returnValue) error:nil];
160 161 162 163
            [weakSelf.commentsArray addObjectsFromArray:weakSelf.topicReply.forumReplyEntity];
            [weakSelf setUpCommentNumber];
            [weakSelf.forumDetailTableView reloadData];
        }else {
164
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
165
        }
166
    } withFailureBlock:^(NSError *error) {
167 168
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
169 170
}

171 172 173 174
#pragma mark - 设置HeaderView
- (void)setUpHeaderView
{
    self.postTitleLabel.text = self.topicDetail.title;
曹云霄's avatar
曹云霄 committed
175
        //系统管理员
曹云霄's avatar
曹云霄 committed
176
    if (self.topicDetail.backEnd) {
177
        self.issuerImageView.image = GuideReplaceImage;
曹云霄's avatar
曹云霄 committed
178
        self.issuerNameAndDateLabel.text = [NSString stringWithFormat:@"%@  %@",self.topicDetail.posterRealName,[BaseViewController formateDate:self.topicDetail.postTime]];
曹云霄's avatar
曹云霄 committed
179
    }else {
曹云霄's avatar
曹云霄 committed
180 181 182
        //普通用户
        [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]];
曹云霄's avatar
曹云霄 committed
183
    }
184
    self.tableViewHeaderView.height = 80;
185
    self.forumDetailTableView.tableHeaderView = self.tableViewHeaderView;
186 187
}

188 189 190
#pragma mark - 设置评论个数
- (void)setUpCommentNumber
{
191
    self.commentNumberLabel.text = [NSString stringWithFormat:@"已有%ld条评论",(unsigned long)self.commentsArray.count];
192
    self.praiseButton.selected = !self.topicDetail.canLike;
193 194
    WkWebViewViewController *webView = [self.childViewControllers firstObject];
    webView.htmlString = self.topicDetail.content;
195 196 197 198 199
}

#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    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;

    }
217 218
    CommentListTableViewCell *commentCell = [tableView dequeueReusableCellWithIdentifier:@"CommentListTableViewCell" forIndexPath:indexPath];
    commentCell.bestView.delegate = self;
219
    commentCell.indexPath = indexPath;
220
    commentCell.replyEntity = self.commentsArray[indexPath.row];
221
    BOOL boolValue = ([self.topicDetail.posterId isEqualToString:[Shoppersmanager manager].shoppers.employee.fid] && [self.category.name isEqualToString:OPPLE_ASK]);
222
    commentCell.optionButton.hidden = !boolValue;
223 224 225 226 227 228 229
    return commentCell;
}

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

231 232
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
233 234 235 236 237 238 239 240 241 242 243 244 245
    switch (indexPath.row) {
            case 0:
        {
            return self.contentHeigt;

        }
            break;
            case 1:
        {
            return self.attachmentHeight;
        }
            break;
    }
246
    CustomTOForumReplyEntity *replyEntity = self.commentsArray[indexPath.row];
247 248 249 250 251 252
    //68为上边距   10为下边距
    return replyEntity.replyContentHeight + 68 + 10;
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
253 254 255 256 257
    switch (indexPath.row) {
            case 0:
            case 1:
            return;
    }
258
    CommentListTableViewCell *commentCell = (CommentListTableViewCell *)cell;
259 260
    commentCell.bestView.width = ZERO;
    commentCell.bestView.x = commentCell.optionButton.x;
261
}
262 263 264 265

#pragma mark - 计算选中图片显示所需高度
- (CGFloat)calculateImageHeight
{
266 267
    //默认每行显示3张图片
    CGFloat height = (ScreenWidth-60-2*10)/3.0;
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
    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
{
    if ([[self class] isBlankString:textField.text]) {
287 288 289
        [XBLoadingView showHUDViewWithText:@"评论内容不能为空"];
    }else {
        [self replyPostRequest];
290 291 292 293 294 295 296
    }
    return YES;
}

#pragma mark - 回复帖子
- (void)replyPostRequest
{
297
    [self.view endEditing:YES];
298 299 300
    TOForumReplyEntity *reply = [[TOForumReplyEntity alloc] init];
    reply.topicId = self.topicDetail.fid;
    reply.replyContent = self.commentInputTextFieldView.text;
301 302 303
    reply.replyerName = [Shoppersmanager manager].shoppers.employee.userName;
    reply.replyerRealName = [Shoppersmanager manager].shoppers.employee.realName;
    reply.replyerPosition = [Shoppersmanager manager].shoppers.employee.positionsName;
304
    reply.replyTime = [[self class] getTimeby:0];
305
    reply.replyerId = [Shoppersmanager manager].shoppers.employee.fid;
306
    [XBLoadingView showHUDViewWithDefault];
307
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
308
    [HTTP networkRequestWithURL:SERVERREQUESTURL(REPLYPOST) withRequestType:ZERO withParameter:reply withReturnValueBlock:^(id returnValue) {
309 310
        
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
311
        if (RESULT(returnValue)) {
312
            weakSelf.commentInputTextFieldView.text = nil;
313
            [XBLoadingView showHUDViewWithSuccessText:@"评论成功" completeBlock:^{
314
                if (weakSelf.refreshBlock) {
315
                    weakSelf.refreshBlock(ONE,Comment,weakSelf.indexPath);
316
                }
317 318
                [weakSelf getPostDetailAction:YES];
            }];
319
        }else {
320
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
321 322
        }
        
323
    } withFailureBlock:^(NSError *error) {
324 325 326 327
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

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

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

338
#pragma mark - 删除帖子
339
- (void)delecteClickPostAction
340
{
曹云霄's avatar
曹云霄 committed
341
    ShowDefaultAlertView(self, nil, @"确认删除此贴吗?", UIAlertControllerStyleAlert, ^{
342 343
        [XBLoadingView showHUDViewWithDefault];
        WS(weakSelf);
曹云霄's avatar
曹云霄 committed
344
        [HTTP networkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(DELETEPOST),self.topicDetail.fid] withRequestType:NetworkRequestWithDELETE withParameter:nil withReturnValueBlock:^(id returnValue) {
345
            [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
346
            if (RESULT(returnValue)) {
347 348 349 350 351 352 353
                [XBLoadingView showHUDViewWithSuccessText:@"删除成功" completeBlock:^{
                    if (weakSelf.delectBlock) {
                        weakSelf.delectBlock(weakSelf.topicDetail.fid);
                    }
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                }];
            }else {
354
                [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
355 356
            }
            
357
        } withFailureBlock:^(NSError *error) {
358 359 360
            [XBLoadingView hideHUDViewWithDefault];
            [XBLoadingView showHUDViewWithText:error.localizedDescription];
        }];
361
    }, nil);
362 363
}

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


375 376
#pragma mark - <TapClickDelegate>
#pragma mark -最佳回复
377
- (void)tapClickAction:(BOOL)isBest withReplyId:(NSString *)replyId withIndexPath:(NSIndexPath *)indexPath
378
{
379
    WS(weakSelf);
380
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
381
    [HTTP networkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(BASEREPLY),self.topicDetail.fid,replyId,isBest?@"true":@"false"] withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
382
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
383
        if (RESULT(returnValue)) {
384
            [weakSelf getPostDetailAction:YES];
385
        }else {
386
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
387 388
        }
        
389
    } withFailureBlock:^(NSError *error) {
390 391 392 393 394 395 396 397
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark - 点赞
- (IBAction)praiseButtonClickAction:(UIButton *)sender {
    
    [XBLoadingView showHUDViewWithDefault];
398
    NSString *url = [NSString stringWithFormat:SERVERREQUESTURL(PRAISE),self.topicDetail.fid,!sender.selected?@"true":@"false"];
399
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
400
    [HTTP networkWithDictionaryRequestWithURL:url withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
401
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
402
        if (RESULT(returnValue)) {
403
            sender.selected = !sender.selected;
404 405 406 407
            //刷新列表
            if (weakSelf.refreshBlock) {
                weakSelf.refreshBlock(sender.selected?ONE:-ONE,Praise,weakSelf.indexPath);
            }
408
        }else {
409
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
410 411
        }
        
412
    } withFailureBlock:^(NSError *error) {
413 414 415 416
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

417 418

@end