ForumItemListViewController.m 8.12 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
@interface ForumItemListViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate,TapClickImageViewDelegate,MWPhotoBrowserDelegate>
16

17 18
@property (nonatomic,strong) ForumTopicCondition *condition;
@property (nonatomic,strong) NSMutableArray *datasArray;
19 20 21 22 23

/**
 图片数组
 */
@property (nonatomic,strong) NSMutableArray *selecteImageArray;
24 25 26 27
@end

@implementation ForumItemListViewController

28 29 30 31 32 33 34 35
#pragma mark - lazy
- (ForumTopicCondition *)condition
{
    if (!_condition) {
        _condition = [[ForumTopicCondition alloc] init];
        DataPage *page = [[DataPage alloc] init];
        page.page = ZERO;
        page.rows = KROWS;
36
        _condition.categoryEquals = self.category.fid;
37 38 39 40 41 42 43 44 45 46 47 48 49
        _condition.page = page;
    }
    return _condition;
}

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

50 51 52 53 54 55 56 57
- (NSMutableArray *)selecteImageArray
{
    if (!_selecteImageArray) {
        _selecteImageArray = [NSMutableArray array];
    }
    return _selecteImageArray;
}

58 59 60
- (void)viewDidLoad {
    [super viewDidLoad];
    
61
    [self setUpRefreshAction];
62
    [self uiConfigAction];
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
#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;
}

89 90 91 92 93
#pragma mark - UI
- (void)uiConfigAction
{
    self.classifyTitleLabel.text = self.category.name;
    [self.classifyImageView sd_setImageWithURL:[NSURL URLWithString:self.category.attachment.fileUrl] placeholderImage:REPLACEIMAGE];
94
    self.classifyListCountLabel.text = [NSString stringWithFormat:@"%ld",self.category.topicCount];
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
#pragma mark - 获取分类帖子列表
- (void)getClassificationList:(ForumTopicCondition *)condtion isRemoveArray:(BOOL)remove
{
    [XBLoadingView showHUDViewWithDefault];
    WS(weakSelf);
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(QUERTFORUMLIST) WithRequestType:ZERO WithParameter:condtion WithReturnValueBlock:^(id returnValue) {
        
        [XBLoadingView hideHUDViewWithDefault];
        [weakSelf endRefreshingForTableView:weakSelf.classificationListTableView];
        weakSelf.classificationListTableView.emptyDataSetSource = weakSelf;
        weakSelf.classificationListTableView.emptyDataSetDelegate = weakSelf;
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            if (remove) {
                [weakSelf.datasArray removeAllObjects];
            }
            ForumTopicResponse *result = [[ForumTopicResponse alloc] initWithDictionary:returnValue[@"data"] error:nil];
            [weakSelf.datasArray addObjectsFromArray:result.forumTopicEntity];
        }else {
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
        }
        [weakSelf.classificationListTableView reloadData];
    } WithFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark - 发帖
- (IBAction)releaseButtonClickAction:(UIButton *)sender {
    
    ReleasePostViewController *releaseVc = [[[self class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"ReleasePostViewController"];
    releaseVc.category = self.category;
128 129 130 131
    WS(weakSelf);
    [releaseVc setFinishBlock:^{
        [weakSelf.classificationListTableView.mj_header beginRefreshing];
    }];
132 133
    [self.navigationController pushViewController:releaseVc animated:YES];
}
134

曹云霄's avatar
曹云霄 committed
135 136 137
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ForumItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ForumItemTableViewCell" forIndexPath:indexPath];
138
    cell.indexPath = indexPath;
139
    cell.topicEntity = self.datasArray[indexPath.row];
140
    cell.imageBackView.delegate = self;
141 142 143
    return cell;
}

曹云霄's avatar
曹云霄 committed
144 145
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
146
    return self.datasArray.count;
147 148
}

曹云霄's avatar
曹云霄 committed
149 150
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
151
    CustomTOForumTopicEntity *entity = self.datasArray[indexPath.row];
152
    //10 表示控件间隙、60 表示content  Y  150 表示图片背景框高度
153 154
    CGFloat height = entity.contentHeight + 60 + 20;
    if (entity.attachments.count) {
155
        height += 150+10;
156 157 158 159 160 161 162
    }
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ForumItemDetailViewController *postDetail = [[[self class] getLearningCenterStoryboardClass] instantiateViewControllerWithIdentifier:@"ForumItemDetailViewController"];
163 164 165 166 167 168 169 170 171 172 173 174 175 176
    postDetail.topicDetail = self.datasArray[indexPath.row];
    WS(weakSelf);
    //删除帖子
    [postDetail setDelectBlock:^(NSString *topicId) {
        for (int i=0; i<weakSelf.datasArray.count; i++) {
            CustomTOForumTopicEntity *entity = self.datasArray[i];
            if ([entity.fid isEqualToString:topicId]) {
                [weakSelf.classificationListTableView beginUpdates];
                [weakSelf.datasArray removeObjectAtIndex:i];
                [weakSelf.classificationListTableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:ZERO]] withRowAnimation:UITableViewRowAnimationLeft];
                [weakSelf.classificationListTableView endUpdates];
            }
        }
    }];
177 178 179
    [self.navigationController pushViewController:postDetail animated:YES];
}

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
#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;
}


207 208 209 210 211 212 213 214 215 216 217 218 219 220
#pragma mark -友好界面
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return kNoDataImage;
}

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

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

@end