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

#import "ForumItemListViewController.h"
曹云霄's avatar
曹云霄 committed
10
#import "ForumItemTableViewCell.h"
11 12
#import "ReleasePostViewController.h"
#import "ForumItemDetailViewController.h"
13
#import "MWPhotoBrowser.h"
14 15
#import "UITableView+FDTemplateLayoutCell.h"
#import "CommentTagTableViewCell.h"
16
#import "UIButton+Title_Image.h"
17 18 19 20

static NSString *CELL_IDENTIFIER = @"ForumItemTableViewCell";
static NSString *TAGCELL_IDENTIFIER = @"CommentTagTableViewCell";

21

22
@interface ForumItemListViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate,TapClickImageViewDelegate,MWPhotoBrowserDelegate>
23

24 25
@property (nonatomic,strong) ForumTopicCondition *condition;
@property (nonatomic,strong) NSMutableArray *datasArray;
26 27 28 29 30

/**
 图片数组
 */
@property (nonatomic,strong) NSMutableArray *selecteImageArray;
31 32 33 34
@end

@implementation ForumItemListViewController

35 36 37 38 39 40 41 42
#pragma mark - lazy
- (ForumTopicCondition *)condition
{
    if (!_condition) {
        _condition = [[ForumTopicCondition alloc] init];
        DataPage *page = [[DataPage alloc] init];
        page.page = ZERO;
        page.rows = KROWS;
43
        _condition.categoryEquals = self.category.fid;
44 45 46 47 48 49 50 51 52 53 54 55 56
        _condition.page = page;
    }
    return _condition;
}

- (NSMutableArray *)datasArray
{
    if (!_datasArray) {
        _datasArray = [NSMutableArray array];
    }
    return _datasArray;
}

57 58 59 60 61 62 63 64
- (NSMutableArray *)selecteImageArray
{
    if (!_selecteImageArray) {
        _selecteImageArray = [NSMutableArray array];
    }
    return _selecteImageArray;
}

65 66 67
- (void)viewDidLoad {
    [super viewDidLoad];
    
68
    [self setUpRefreshAction];
69
    [self uiConfigAction];
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
#pragma mark - 设置刷新
- (void)setUpRefreshAction
{
    WS(weakSelf);
    MjRefreshHeaderCustom *headerRefresh = [MjRefreshHeaderCustom headerWithRefreshingBlock:^{
        weakSelf.condition.page.page = ONE;
        [weakSelf.classificationListTableView.mj_footer resetNoMoreData];
        [weakSelf getClassificationList:weakSelf.condition isRemoveArray:YES];
    }];
    headerRefresh.stateLabel.hidden = YES;
    headerRefresh.lastUpdatedTimeLabel.hidden = YES;
    self.classificationListTableView.mj_header =headerRefresh;
    [self.classificationListTableView.mj_header beginRefreshing];
    self.classificationListTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (++ weakSelf.condition.page.page > weakSelf.condition.page.total) {
            [weakSelf.classificationListTableView.mj_footer endRefreshingWithNoMoreData];
        }else
        {
            [weakSelf getClassificationList:weakSelf.condition isRemoveArray:NO];
        }
    }];
    self.classificationListTableView.mj_footer.automaticallyHidden = YES;
}

96 97 98
#pragma mark - UI
- (void)uiConfigAction
{
曹云霄's avatar
曹云霄 committed
99
    self.classificationListTableView.backgroundColor = RGB(237, 238, 239, 1);
100
    [self.postingButton horizontalCenterImageAndTitle:5];
101
    self.classifyTitleLabel.text = self.category.name;
曹云霄's avatar
曹云霄 committed
102 103
    [self.classifyImageView sd_setImageWithURL:[NSURL URLWithString:self.category.attachmentBg.fileUrl] placeholderImage:REPLACEIMAGE];
    [self.classIconImageView sd_setImageWithURL:[NSURL URLWithString:self.category.attachment.fileUrl] placeholderImage:REPLACEIMAGE];
104 105 106
    if (self.isPosting) {
        [self.postingButton setTitle:(self.isPosting == 1)?@"我要发帖":@"我要提问" forState:UIControlStateNormal];
    }else {
107 108
        self.postingButton.hidden = YES;
    }
109 110
}

111 112 113 114 115
#pragma mark - 获取分类帖子列表
- (void)getClassificationList:(ForumTopicCondition *)condtion isRemoveArray:(BOOL)remove
{
    [XBLoadingView showHUDViewWithDefault];
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
116
    [HTTP networkRequestWithURL:SERVERREQUESTURL(QUERTFORUMLIST) withRequestType:ZERO withParameter:condtion withReturnValueBlock:^(id returnValue) {
117 118 119 120 121
        
        [XBLoadingView hideHUDViewWithDefault];
        [weakSelf endRefreshingForTableView:weakSelf.classificationListTableView];
        weakSelf.classificationListTableView.emptyDataSetSource = weakSelf;
        weakSelf.classificationListTableView.emptyDataSetDelegate = weakSelf;
曹云霄's avatar
曹云霄 committed
122
        if (RESULT(returnValue)) {
123 124 125
            if (remove) {
                [weakSelf.datasArray removeAllObjects];
            }
曹云霄's avatar
曹云霄 committed
126
            ForumTopicResponse *result = [[ForumTopicResponse alloc] initWithDictionary:RESPONSE(returnValue) error:nil];
127
            [weakSelf.datasArray addObjectsFromArray:result.forumTopicEntity];
128
            weakSelf.classifyListCountLabel.text = [NSString stringWithFormat:@"%ld",result.total];
129
        }else {
130
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
131 132
        }
        [weakSelf.classificationListTableView reloadData];
133
    } withFailureBlock:^(NSError *error) {
134 135 136 137 138 139 140 141 142
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark - 发帖
- (IBAction)releaseButtonClickAction:(UIButton *)sender {
    
    ReleasePostViewController *releaseVc = [[[self class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"ReleasePostViewController"];
    releaseVc.category = self.category;
143 144 145 146
    WS(weakSelf);
    [releaseVc setFinishBlock:^{
        [weakSelf.classificationListTableView.mj_header beginRefreshing];
    }];
147 148
    [self.navigationController pushViewController:releaseVc animated:YES];
}
149

曹云霄's avatar
曹云霄 committed
150 151
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
152 153 154 155 156 157 158 159 160
    CustomTOForumTopicEntity *entity = self.datasArray[indexPath.row];
    if (entity.backEnd || entity.bestTopic || ![[self class] isBlankString:entity.top]) {
        CommentTagTableViewCell *tagCell = [tableView dequeueReusableCellWithIdentifier:TAGCELL_IDENTIFIER forIndexPath:indexPath];
        [self configTagCellAction:tagCell withIndexPath:indexPath];
        return tagCell;
    }
    ForumItemTableViewCell *commonCell = [tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER forIndexPath:indexPath];
    [self configCommonCellAction:commonCell withIndexPath:indexPath];
    return commonCell;
161 162
}

曹云霄's avatar
曹云霄 committed
163 164
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
165
    return self.datasArray.count;
166 167
}

曹云霄's avatar
曹云霄 committed
168 169
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
170
    CustomTOForumTopicEntity *entity = self.datasArray[indexPath.row];
171 172 173
    if (entity.backEnd || entity.bestTopic || ![[self class] isBlankString:entity.top]) {
        return 60;
    }
174
    //10 表示控件间隙、60 表示content  Y  150 表示图片背景框高度
175
    CGFloat height = entity.contentHeight + entity.titleHeight + 80 + 10;
176
    if (entity.attachments.count) {
177
        height += 150+10;
178 179 180 181 182 183 184
    }
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ForumItemDetailViewController *postDetail = [[[self class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"ForumItemDetailViewController"];
185 186
    CustomTOForumTopicEntity *entity = self.datasArray[indexPath.row];
    postDetail.topicDetail = entity;
187
    postDetail.category = self.category;
188
    postDetail.indexPath = indexPath;
189 190
    entity.isRead = YES;
    [self.classificationListTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
191
    WS(weakSelf);
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    //更新点赞数、评论数
    [postDetail setRefreshBlock:^(NSInteger count,COMMENT_PRAISE type,NSIndexPath *indexPath){
        switch (type) {
            case Comment:
            {
                entity.replyCount += count;
            }
                break;
            case Praise:
            {
                entity.likeCount += count;
                entity.canLike = (count == 1)?NO:YES;
            }
                break;
                
            default:
                break;
        }
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
211
    }];
212 213 214 215 216
    //删除帖子
    [postDetail setDelectBlock:^(NSString *topicId) {
        for (int i=0; i<weakSelf.datasArray.count; i++) {
            CustomTOForumTopicEntity *entity = self.datasArray[i];
            if ([entity.fid isEqualToString:topicId]) {
217
                [weakSelf.classificationListTableView.mj_header beginRefreshing];
218 219 220
            }
        }
    }];
221 222 223
    [self.navigationController pushViewController:postDetail animated:YES];
}

224 225 226 227 228 229 230 231 232 233 234 235
#pragma mark -设置普通帖子
- (void)configCommonCellAction:(ForumItemTableViewCell *)cell withIndexPath:(NSIndexPath *)indexPath
{
    [cell refreshCell:indexPath withTopicEntity:self.datasArray[indexPath.row] withDelegate:self];
}

#pragma mark 设置带标签的帖子
- (void)configTagCellAction:(CommentTagTableViewCell *)cell withIndexPath:(NSIndexPath *)indexPath
{
    cell.topicModel = self.datasArray[indexPath.item];
}

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
#pragma mark - 单击查看大图<TapClickImageViewDelegate>
- (void)tapImageViewWithIndex:(NSInteger)index withCellIndex:(NSIndexPath *)indexPath
{
    [self.selecteImageArray removeAllObjects];
    CustomTOForumTopicEntity *topicEntity = self.datasArray[indexPath.row];
    for (TOAttachmentEntity *entity in topicEntity.attachments) {
        MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:entity.fileUrl]];
        [self.selecteImageArray addObject:photo];
    }
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    [browser setCurrentPhotoIndex:index];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:browser];
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}

#pragma mark - <MWPhotoBrowserDelegate>
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.selecteImageArray.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < self.selecteImageArray.count)
        return [self.selecteImageArray objectAtIndex:index];
    return nil;
}

262 263 264 265 266 267 268 269 270 271 272 273 274 275
#pragma mark -友好界面
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return kNoDataImage;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    return [[NSAttributedString alloc]initWithString:@"暂无数据" attributes:nil];
276 277 278
}

@end