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

#import "MessageViewController.h"
#import "MessageTableViewCell.h"
#import "UITableView+FDTemplateLayoutCell.h"

13
@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37


/**
 查询对象
 */
@property (nonatomic,strong) NoticeCondition *messageModel;

/**
 结果对象
 */
@property (nonatomic,strong) NoticeResponse *resultModel;

/**
 数据源
 */
@property (nonatomic,strong) NSMutableArray *messageArray;

@end

@implementation MessageViewController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
曹云霄's avatar
曹云霄 committed
38 39 40 41
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
42 43 44
    [self.meesageTableView.mj_header beginRefreshing];
}

曹云霄's avatar
曹云霄 committed
45 46 47 48 49 50 51 52 53 54
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = YES;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}


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
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self uiConfigAction];
    [self setUpRefreshAction];
}

#pragma mark -UI
- (void)uiConfigAction
{
    self.meesageTableView.tableFooterView = [UIView new];
    [self.timeOrderButton addTarget:self action:@selector(timeOrderClickAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - Refresh
- (void)setUpRefreshAction
{
    WS(weakSelf);
    MjRefreshHeaderCustom *headerRefresh = [MjRefreshHeaderCustom headerWithRefreshingBlock:^{
        [weakSelf.meesageTableView.mj_footer resetNoMoreData];
        weakSelf.messageModel.page.page = ONE;
        [weakSelf getAllMessageDatas:YES];
    }];
    headerRefresh.stateLabel.hidden = YES;
    headerRefresh.lastUpdatedTimeLabel.hidden = YES;
    self.meesageTableView.mj_header = headerRefresh;
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        if (++ weakSelf.messageModel.page.page > weakSelf.resultModel.totalpages) {
            [weakSelf.meesageTableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [weakSelf getAllMessageDatas:NO];
        }
    }];
    footer.automaticallyHidden = YES;
    self.meesageTableView.mj_footer = footer;
}

#pragma mark -获取数据
- (void)getAllMessageDatas:(BOOL)isRemove
{
    WS(weakSelf);
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
98
   [HTTP networkRequestWithURL:SERVERREQUESTURL(MESSAGELIST) withRequestType:0 withParameter:self.messageModel withReturnValueBlock:^(id returnValue) {
99 100
       
       [XBLoadingView hideHUDViewWithDefault];
101 102
       weakSelf.meesageTableView.emptyDataSetSource = weakSelf;
       weakSelf.meesageTableView.emptyDataSetDelegate = weakSelf;
103
       [weakSelf endRefreshingForTableView:weakSelf.meesageTableView];
曹云霄's avatar
曹云霄 committed
104
       if (RESULT(returnValue)) {
105 106 107
           if (isRemove) {
               [weakSelf.messageArray removeAllObjects];
           }
曹云霄's avatar
曹云霄 committed
108
           weakSelf.resultModel = [[NoticeResponse alloc] initWithDictionary:RESPONSE(returnValue) error:nil];
109 110
           [weakSelf.messageArray addObjectsFromArray:weakSelf.resultModel.noticeEntity];
           [weakSelf.meesageTableView reloadData];
111
       }else {
112
           [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
113 114 115 116 117 118 119
       }
       
   } withFailureBlock:^(NSError *error) {
       [XBLoadingView showHUDViewWithText:error.localizedDescription];
   }];
}

曹云霄's avatar
曹云霄 committed
120 121 122 123 124
#pragma mark -设为已读
- (void)readedMessage:(NSString *)messageId withIndexPath:(NSIndexPath *)indexPath
{
    WS(weakSelf);
    [XBLoadingView showHUDViewWithDefault];
125
    [HTTP networkRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(MESSAGE_READ),messageId,[Shoppersmanager manager].shoppers.employee.fid] withRequestType:0 withParameter:nil withReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
126 127 128 129
        
        [XBLoadingView hideHUDViewWithDefault];
        if (RESULT(returnValue)) {
            TONoticeEntity *entity = weakSelf.messageArray[indexPath.row];
曹云霄's avatar
曹云霄 committed
130
            entity.readed = YES;
131 132
            [weakSelf cornerMarkIsShow];
            [weakSelf configCell:[weakSelf.meesageTableView cellForRowAtIndexPath:indexPath] withIndexPath:indexPath];
曹云霄's avatar
曹云霄 committed
133 134 135 136 137 138 139
            [weakSelf.meesageTableView reloadData];
        }
        
    } withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

#pragma mark -UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageTableViewCell *messageCell = [tableView dequeueReusableCellWithIdentifier:@"MessageTableViewCell" forIndexPath:indexPath];
    [self configCell:messageCell withIndexPath:indexPath];
    return messageCell;
}

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
曹云霄's avatar
曹云霄 committed
157
    WS(weakSelf);
158 159
    TONoticeEntity *entity = self.messageArray[indexPath.row];
    if (!entity.readed) {
160 161 162 163
        ShowAlertView(@"提示", @"是否设置为已读", @[@"确认",@"取消"], UIAlertControllerStyleAlert, ^(NSInteger index) {
            if (index == ONE) {
                return;
            }
164 165
            TONoticeEntity *entity = weakSelf.messageArray[indexPath.row];
            [weakSelf readedMessage:entity.fid withIndexPath:indexPath];
166
        });
167
    }
168 169 170 171 172 173 174 175 176 177 178 179 180 181
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return [tableView fd_heightForCellWithIdentifier:@"MessageTableViewCell" cacheByIndexPath:indexPath configuration:^(id cell) {
        [self configCell:cell withIndexPath:indexPath];
    }];
}


#pragma mark -cell赋值
- (void)configCell:(MessageTableViewCell *)cell withIndexPath:(NSIndexPath *)indexPath
{
    TONoticeEntity *entity = self.messageArray[indexPath.row];
182
    cell.isReadedImageView.hidden = entity.readed;
曹云霄's avatar
曹云霄 committed
183
    cell.messageLabel.text = entity.message;
184 185 186
    cell.releaseTimeLabel.text = entity.createDate;
}

187 188 189 190 191 192 193
#pragma mark --设置已读,未读个数减一
- (void)cornerMarkIsShow
{
    [Notification postNotificationName:MESSAGE_COUNT object:@1];
}


194 195 196 197
#pragma mark -时间排序
- (void)timeOrderClickAction:(UIButton *)sender
{
    sender.selected = !sender.selected;
198
    self.messageModel.order = sender.selected?SORTDIRECTION_ASC:SORTDIRECTION_DESC;
199 200 201
    [self.meesageTableView.mj_header beginRefreshing];
}

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

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

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

218 219 220 221 222 223 224 225
#pragma mark -lazy
- (NoticeCondition *)messageModel
{
    if (!_messageModel) {
        _messageModel = [[NoticeCondition alloc] init];
        DataPage *page = [[DataPage alloc] init];
        page.page = ZERO;
        page.rows = KROWS;
226 227
        _messageModel.sort = @"createDate";
        _messageModel.order = SORTDIRECTION_DESC;
228
        _messageModel.page = page;
229
        _messageModel.employeeId = [Shoppersmanager manager].shoppers.employee.fid;
230 231 232 233 234 235 236 237 238 239 240 241 242
    }
    return _messageModel;
}

- (NSMutableArray *)messageArray
{
    if (!_messageArray) {
        _messageArray = [NSMutableArray array];
    }
    return _messageArray;
}

@end