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

#import "AnnouncementViewController.h"
10 11
#import "AnnouncementTableViewCell.h"
#import "ScreeningButton.h"
曹云霄's avatar
曹云霄 committed
12
#import "AnnountcementDetailViewController.h"
曹云霄's avatar
曹云霄 committed
13

14
@interface AnnouncementViewController ()<UITableViewDelegate,UITableViewDataSource,ReturnTableviewcellIndexpathdelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
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

/**
 公告请求
 */
@property (nonatomic,strong) AfficheCondition *afficheModel;

/**
 公告结果
 */
@property (nonatomic,strong) NSMutableArray *afficheArray;

/**
 总页数
 */
@property (nonatomic,assign) NSInteger totalPage;

/**
 时间
 */
@property (nonatomic,strong) NSMutableArray *timeArray;

/**
 类别
 */
@property (nonatomic,strong) NSMutableArray *typeArray;

@property (nonatomic,strong) ScreeningButton *dateButton;
@property (nonatomic,strong) ScreeningButton *typeButton;
曹云霄's avatar
曹云霄 committed
43 44 45 46 47 48 49 50

@end

@implementation AnnouncementViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    [self setUpTableView];
    [self setUpChooseView];
    [self setUpRefreshAction];
}

#pragma mark - UITableView
- (void)setUpTableView
{
    self.announcementTableView.tableFooterView = [UIView new];
    self.announcementTableView.rowHeight = 80;
}

#pragma mark - 筛选
- (void)setUpChooseView
{
    self.dateButton = [ScreeningButton buttonWithType:UIButtonTypeCustom];
67
    self.dateButton.frame = CGRectMake(ScreenWidth-400, 15, 150, 30);
68 69 70
    [self.dateButton setTitle:@"时间" forState:UIControlStateNormal];
    [self.dateButton addTarget:self action:@selector(dateChooseButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.typeButton = [ScreeningButton buttonWithType:UIButtonTypeCustom];
71
    self.typeButton.frame = CGRectMake(ScreenWidth-200, 15, 150, 30);
72 73 74 75 76 77
    [self.typeButton setTitle:@"类别" forState:UIControlStateNormal];
    [self.typeButton addTarget:self action:@selector(typeChooseButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.chooseBackgroundView addSubview:self.dateButton];
    [self.chooseBackgroundView addSubview:self.typeButton];
}

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
#pragma mark - 时间选择
- (void)dateChooseButtonClick:(UIButton *)sender
{
    [self showPopoverViewController:CGSizeMake(150, 200) withdatas:self.timeArray showButton:sender buttonCode:0];
}

#pragma mark - 类别选择
- (void)typeChooseButtonClick:(UIButton *)sender
{
    [self showPopoverViewController:CGSizeMake(150, 200) withdatas:self.typeArray showButton:sender buttonCode:1];
}

#pragma mark - 时间排序
- (IBAction)dateSortButtonClickAction:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    self.afficheModel.order = sender.selected?SORTDIRECTION_ASC:SORTDIRECTION_DESC;
    [self.announcementTableView.mj_header beginRefreshing];
}

#pragma mark -弹出popover视图控制器
- (void)showPopoverViewController:(CGSize)size withdatas:(NSArray *)datasArray showButton:(UIButton *)button buttonCode:(NSInteger)code
{
    PopoverViewController *popover = [[PopoverViewController alloc]init];
    popover.datasArray = datasArray;
    popover.delegate = self;
    popover.contentSize = size;
    popover.isString = YES;
107
    popover.selectedindex = code;
108 109 110 111 112 113 114 115 116 117
    popover.preferredContentSize = size;
    popover.modalPresentationStyle = UIModalPresentationPopover;
    UIPopoverPresentationController *pop = popover.popoverPresentationController;
    pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
    pop.sourceView = popover.view;
    pop.barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    [self presentViewController:popover animated:YES completion:nil];
}

#pragma mark -选中筛选条件回传typecode
118
- (void)returnCellindexpathwithrow:(NSString *)type withCellTitle:(NSString *)title withSelected:(NSInteger)selected
119 120
{
    //时间
121
    if (selected == 0) {
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        self.afficheModel.dayCountEquals = [self timeString:title];
        [self.dateButton setTitle:title forState:UIControlStateNormal];
    }else
    {//类型
        self.afficheModel.afficheTypeEuals = [title isEqualToString:@"全部"]?nil:title;
        [self.typeButton setTitle:title forState:UIControlStateNormal];
    }
    [self.announcementTableView.mj_header beginRefreshing];
}

#pragma mark - 时间转换
- (NSInteger)timeString:(NSString *)string
{
    NSInteger dayCount = 0;
    if ([string isEqualToString:@"全部"]) {
        dayCount = 0;
    }
    if ([string isEqualToString:@"近三天"]) {
        dayCount = 3;;
    }
    if ([string isEqualToString:@"近一周"]) {
        dayCount = 7;
    }
    if ([string isEqualToString:@"近一个月"]) {
        dayCount = 30;
    }
    return dayCount;
}

#pragma mark - Refresh
- (void)setUpRefreshAction
{
    WS(weakSelf);
    MjRefreshHeaderCustom *headerRefresh = [MjRefreshHeaderCustom headerWithRefreshingBlock:^{
        [weakSelf.announcementTableView.mj_footer resetNoMoreData];
        weakSelf.afficheModel.page.page = ONE;
        [weakSelf getAnnouncementDatas:YES];
    }];
    headerRefresh.stateLabel.hidden = YES;
    headerRefresh.lastUpdatedTimeLabel.hidden = YES;
    [headerRefresh beginRefreshing];
    self.announcementTableView.mj_header = headerRefresh;
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
曹云霄's avatar
曹云霄 committed
165
        if (++ weakSelf.afficheModel.page.page > weakSelf.totalPage) {
166 167 168 169 170 171 172 173 174 175 176 177 178
            [weakSelf.announcementTableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [weakSelf getAnnouncementDatas:NO];
        }
    }];
    footer.automaticallyHidden = YES;
    self.announcementTableView.mj_footer = footer;
}

#pragma mark - 公告数据
- (void)getAnnouncementDatas:(BOOL)isRemove
{
    WS(weakSelf);
179
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
180
    [HTTP networkRequestWithURL:SERVERREQUESTURL(AFFICHELIST) withRequestType:ZERO withParameter:self.afficheModel withReturnValueBlock:^(id returnValue) {
181
        
182
        [XBLoadingView hideHUDViewWithDefault];
183
        [weakSelf endRefreshingForTableView:weakSelf.announcementTableView];
184 185
        weakSelf.announcementTableView.emptyDataSetSource = weakSelf;
        weakSelf.announcementTableView.emptyDataSetDelegate = weakSelf;
曹云霄's avatar
曹云霄 committed
186
        if (RESULT(returnValue)) {
187 188 189
            if (isRemove) {
                [weakSelf.afficheArray removeAllObjects];
            }
曹云霄's avatar
曹云霄 committed
190
            AfficheResponse *respone = [[AfficheResponse alloc]initWithDictionary:RESPONSE(returnValue) error:nil];
191 192 193
            weakSelf.totalPage = respone.totalpages;
            [weakSelf.afficheArray addObjectsFromArray:respone.afficheEntity];
        }else {
194
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
195 196 197
        }
        [weakSelf.announcementTableView reloadData];
        
198
    } withFailureBlock:^(NSError *error) {
199
        [weakSelf endRefreshingForTableView:weakSelf.announcementTableView];
200
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
201 202 203
    }];
}

204 205 206 207 208 209
#pragma mark -设置已读,未读个数减一
- (void)cornerMarkIsShow
{
    [Notification postNotificationName:NOTREADANNOUNCEMENT object:@1];
}

210 211 212 213
#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AnnouncementTableViewCell *announcementCell = [tableView dequeueReusableCellWithIdentifier:@"AnnouncementTableViewCell" forIndexPath:indexPath];
214 215 216
    TOAfficheEntity *entity = self.afficheArray[indexPath.row];
    announcementCell.afficheEntity = entity;
    announcementCell.isReadImageview.hidden = entity.readed;
217 218 219 220 221 222 223 224
    return announcementCell;
}

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

曹云霄's avatar
曹云霄 committed
225 226
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
227
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
曹云霄's avatar
曹云霄 committed
228
    AnnountcementDetailViewController *detail = [[[self class] getAnnouncementStoryboardClass]instantiateViewControllerWithIdentifier:@"AnnountcementDetailViewController"];
229 230 231 232 233
    detail.announcementEntity = self.afficheArray[indexPath.row];
    WS(weakSelf);
    [detail setReadBlock:^{
        TOAfficheEntity *entity = weakSelf.afficheArray[indexPath.row];
        entity.readed = YES;
234
        [weakSelf cornerMarkIsShow];
235 236
        [weakSelf.announcementTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }];
曹云霄's avatar
曹云霄 committed
237 238 239
    [self.navigationController pushViewController:detail animated:YES];
}

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
#pragma mark -友好界面
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return kNoDataImage;
}

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

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

256 257 258 259 260 261 262 263 264
#pragma mark - lazy
- (AfficheCondition *)afficheModel
{
    if (!_afficheModel) {
        _afficheModel = [[AfficheCondition alloc]init];
        DataPage *page = [[DataPage alloc]init];
        page.page = ONE;
        page.rows = KROWS;
        _afficheModel.page = page;
265
        _afficheModel.sort = @"createDate";
266
        _afficheModel.order = SORTDIRECTION_DESC;
267 268
        _afficheModel.departIdEquals = [Shoppersmanager manager].shoppers.employee.departid;
        _afficheModel.employeeId = [Shoppersmanager manager].shoppers.employee.fid;
269 270
    }
    return _afficheModel;
曹云霄's avatar
曹云霄 committed
271 272
}

273 274 275 276 277 278
- (NSMutableArray *)afficheArray
{
    if (!_afficheArray) {
        _afficheArray = [NSMutableArray array];
    }
    return _afficheArray;
曹云霄's avatar
曹云霄 committed
279 280
}

281 282 283 284 285 286 287 288
- (NSMutableArray *)timeArray
{
    if (!_timeArray) {
        _timeArray = [NSMutableArray arrayWithObjects:@"全部",@"近三天",@"近一周",@"近一个月", nil];
        
    }
    return _timeArray;
}
曹云霄's avatar
曹云霄 committed
289

290 291 292 293 294 295
- (NSMutableArray *)typeArray
{
    if (!_typeArray) {
        _typeArray = [NSMutableArray arrayWithObjects:@"全部",@"品牌资讯",@"行业动态",@"活动公告", nil];
    }
    return _typeArray;
曹云霄's avatar
曹云霄 committed
296
}
297 298 299 300 301 302 303 304 305 306










曹云霄's avatar
曹云霄 committed
307 308

@end