// // FwzBaseView.m // smartRhino // // Created by tederen on 2019/11/12. // Copyright © 2019 tederen. All rights reserved. // #import "FwzBaseView.h" #import "WorkListCell.h" #import "HomeTableViewCell.h" #import "DocumentModel.h" #import "ChannelModel.h" #import "MyApprovalBCell.h" #import @interface FwzBaseView() @property (nonatomic, assign)NSInteger currentChannel; //暂存id @property (nonatomic, assign)NSInteger page; //暂存id @property (nonatomic, strong) NSMutableArray *dataArr; @property (nonatomic, strong) NSMutableArray *waitArray; @property (nonatomic, strong) ChannelModel *channelModel; @property (nonatomic, strong) UILabel *noDataMessageLbl; @property (nonatomic, assign)NSInteger currentChannelID; // groupId @end @implementation FwzBaseView - (instancetype)initWithFrame:(CGRect)frame channel:(NSInteger)channel ChannelId:(NSInteger)channelId { if (self = [super initWithFrame:frame]) { _currentChannel = channel; _currentChannelID = channelId; self.tableView = [[UITableView alloc] initWithFrame:self.bounds]; self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 67, 0); self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.showsHorizontalScrollIndicator = NO; self.tableView.showsVerticalScrollIndicator = NO; [self addSubview:self.tableView]; self.tableView.delegate = self; self.tableView.dataSource = self; self.page = 1; [self.tableView registerNib:[UINib nibWithNibName:@"MyApprovalBCell" bundle:nil] forCellReuseIdentifier:@"MyApprovalBCell"]; self.tableView.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{ if (self.currentChannel == 1){ [self.waitArray removeAllObjects]; [self updateNewWaitWork]; } else { [self.dataArr removeAllObjects]; [self updateNew]; } }]; self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ if (self.currentChannel == 1){ [self addMoreWaitWork]; }else{ [self addMore]; } }]; } return self; } - (UILabel *)noDataMessageLbl { if (!_noDataMessageLbl) { _noDataMessageLbl = [[UILabel alloc] init]; _noDataMessageLbl.font = [UIFont systemFontOfSize:15]; _noDataMessageLbl.text = @"暂无数据"; _noDataMessageLbl.textColor = [UIColor colorWithHexString:@"BBBBBB"]; _noDataMessageLbl.textAlignment = NSTextAlignmentCenter; _noDataMessageLbl.hidden = YES; } return _noDataMessageLbl; } - (void)addNoDataMessageToViewCenter:(UIView *)contentView message:(NSString *)message { [self.noDataMessageLbl removeFromSuperview]; self.noDataMessageLbl.text = message; [contentView addSubview:self.noDataMessageLbl]; [self.noDataMessageLbl mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(contentView.mas_centerY); make.centerX.equalTo(contentView.mas_centerX); make.height.mas_equalTo(30); }]; } - (void)changeMessageStatusWithNumber:(NSInteger)number { if (number > 0) { self.noDataMessageLbl.hidden = YES; }else { self.noDataMessageLbl.hidden = NO; } } - (void)reloadData { [self.tableView.mj_header beginRefreshing]; } /// 根据channelID刷新对应的tableview - (void)renderUIWithInfoChannelId:(NSInteger)channelId withFrame:(CGRect) frame{ [self requestContent]; // if ([_channelModel.channelId integerValue] == 1) { // [self updateNewWaitWork]; // }else{ // [self updateNew]; // } //[self requestContent]; // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:TABLETOP object:nil]; // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:TABLELEAVETOP object:nil]; } - (void)requestContent { self.dataArr = [NSMutableArray arrayWithArray:self.artModel.Items]; NSLog(@"FwzBaseView - requestContent首页数据%@",self.dataArr); self.waitArray = [NSMutableArray arrayWithArray:self.waitModel.Items]; [self.tableView reloadData]; if (_currentChannel == 1) { [self changeMessageStatusWithNumber:self.waitArray.count]; }else { [self changeMessageStatusWithNumber:self.dataArr.count]; } } - (void)acceptMsg:(NSNotification *)notification{ NSString *notificationName = notification.name; if ([notificationName isEqualToString:TABLETOP]) { NSDictionary *userInfo = notification.userInfo; NSString *canScroll = userInfo[@"canScroll"]; if ([canScroll isEqualToString:@"1"]) { self.canScroll = YES; self.tableView.showsVerticalScrollIndicator = YES; } }else if ([notificationName isEqualToString:TABLELEAVETOP]){ self.tableView.showsVerticalScrollIndicator = NO; self.tableView.contentOffset = CGPointZero; } } - (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (_currentChannel == 1) { return self.waitArray.count; } else { return self.dataArr.count; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (_currentChannel == 1) { return 159.f; } Item *model = self.dataArr[indexPath.row]; if (model.ImageUrls.count == 0) { model.cellID = 1; }else if(model.ImageUrls.count == 1){ model.cellID = 2; }else{ model.cellID = 3; } switch (model.cellID) { case 1: { UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, SCREEN_WIDTH - 30, 18)]; label.text = model.Title; label.font = [UIFont systemFontOfSize:18]; label.numberOfLines = 2; CGSize maxSize = CGSizeMake(label.bounds.size.width, CGFLOAT_MAX); CGSize newSize = [label sizeThatFits:maxSize]; int height = 50 + newSize.height; return height; } break; case 2: return 115; break; case 3: { UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, SCREEN_WIDTH - 30, 18)]; label.text = model.Title; label.font = [UIFont systemFontOfSize:18]; label.numberOfLines = 2; CGSize maxSize = CGSizeMake(label.bounds.size.width, CGFLOAT_MAX); CGSize newSize = [label sizeThatFits:maxSize]; int height = 86 * (kGXScreenWidth / 375.0) + 67 + newSize.height; return height; } break; default: return 70; break; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 待办工作 if (_currentChannel == 1) { NSString *cellIdentifier = @"MyApprovalBCell"; MyApprovalBCell *cell = (MyApprovalBCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; cell.falgLab.hidden =YES; [cell loadDictionArray:self.waitArray[indexPath.row]]; return cell; } // 推荐 Item *model = self.dataArr[indexPath.row]; if (model.ImageUrls.count == 0) { model.cellID = 1; }else if(model.ImageUrls.count == 1){ model.cellID = 2; }else{ model.cellID = 3; } HomeTableViewCell *cell = [HomeTableViewCell cellWithTableView:tableView AndIndex:model.cellID]; [cell loadCurrentItemModel:model]; WEAKSELF cell.buttonBlock = ^{ STRONGSELF if ([strongSelf.delegate respondsToSelector:@selector(didCloseAddButtonChannel:andWith:)]) { NSLog(@"1点击不感兴趣的"); [strongSelf.delegate didCloseAddButtonChannel:strongSelf.channelModel andWith: model]; NSLog(@"2点击不感兴趣的"); } }; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if(_currentChannel == 1){ NSLog(@"待办事项1"); if ([self.delegate respondsToSelector:@selector(didClickWaitWorkChannel:andWith:)]) { NSLog(@"待办事项2"); [self.delegate didClickWaitWorkChannel:self.channelModel andWith:self.waitArray[indexPath.row]]; } } else { if ([self.delegate respondsToSelector:@selector(didClickAddButtonChannel:andWith:)]) { [self.delegate didClickAddButtonChannel:self.channelModel andWith:self.dataArr[indexPath.row]]; } } } - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ } - (NSMutableArray *)dataArr{ if (!_dataArr) { _dataArr = [NSMutableArray array]; } return _dataArr; } - (ChannelModel *)channelModel{ if (!_channelModel) { _channelModel = [[ChannelModel alloc]init]; } return _channelModel; } #pragma mark -文章数据刷新。 /// 推荐数据 - (void)updateNew{ WS(weakSelf); self.page = 1; [self homeDataSecondAdd:self.page success:^(id responseObject) { [weakSelf.tableView.mj_footer resetNoMoreData]; NSDictionary *dic = (NSDictionary *)responseObject; HomeArticleModel *model = [HomeArticleModel yy_modelWithDictionary:dic]; // HomeArticleModel *model = [[HomeArticleModel alloc] initWithDictionary:dic error:nil]; weakSelf.dataArr = [[NSMutableArray alloc] initWithArray:model.Items]; [weakSelf.tableView.mj_header endRefreshing]; [weakSelf changeMessageStatusWithNumber:weakSelf.dataArr.count]; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableView reloadData]; }); } failure:^(NSError *error) { [weakSelf.tableView.mj_header endRefreshing]; SHOWERROR([ZYCTool handerResultData:error]); }]; } - (void)addMore{ self.page += 1; WS(weakSelf); [self homeDataSecondAdd:self.page success:^(id responseObject) { [weakSelf.tableView.mj_footer endRefreshing]; NSDictionary *dic = (NSDictionary *)responseObject; HomeArticleModel *model = [HomeArticleModel yy_modelWithDictionary:dic]; // HomeArticleModel *model = [[HomeArticleModel alloc] initWithDictionary:dic error:nil]; [weakSelf.dataArr addObjectsFromArray:model.Items]; if (model.Items.count < 10) { [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData]; } dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableView reloadData]; }); } failure:^(NSError *error) { [weakSelf.tableView.mj_footer endRefreshing]; SHOWERROR([ZYCTool handerResultData:error]); }]; } // 网络请求 - (void)homeDataSecondAdd:(NSInteger)page success:(void (^)(id responseObject))successful failure:(void (^) (NSError *error))failure { NSDictionary *paraDic = @{ @"page":@(page), @"perPage":@10, @"GroupId":@(self.currentChannelID) }; [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Article_Notice_list_Post] parameters: paraDic responseStyle:JOSN success:^(id _Nonnull responseObject) { successful(responseObject); } failure:^(NSError * _Nonnull error) { failure(error); }]; } /// 待办事项数据 - (void)updateNewWaitWork{ self.page = 1; [self.waitArray removeAllObjects]; WS(weakSelf); [self homeWaitWorkAdd:self.page success:^(id responseObject) { NSDictionary *dic = (NSDictionary *)responseObject; TDWaitWorkModel *model = [[TDWaitWorkModel alloc] initWithDictionary:dic error:nil]; [weakSelf.waitArray addObjectsFromArray:model.Items.mutableCopy]; [weakSelf.tableView.mj_header endRefreshing]; if (model.Count == weakSelf.waitArray.count) { [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData]; } [weakSelf.tableView.mj_footer resetNoMoreData]; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableView reloadData]; }); [weakSelf changeMessageStatusWithNumber:self.waitArray.count]; } failure:^(NSError *error) { [weakSelf.tableView.mj_header endRefreshing]; SHOWERROR([ZYCTool handerResultData:error]); }]; } - (void)addMoreWaitWork{ WS(weakSelf); self.page += 1; [self homeWaitWorkAdd:self.page success:^(id responseObject) { [weakSelf.tableView.mj_footer endRefreshing]; NSDictionary *dic = (NSDictionary *)responseObject; TDWaitWorkModel *model = [[TDWaitWorkModel alloc] initWithDictionary:dic error:nil]; [weakSelf.waitArray addObjectsFromArray:model.Items]; if (model.Count == weakSelf.waitArray.count) { [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData]; } dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableView reloadData]; }); } failure:^(NSError *error) { [weakSelf.tableView.mj_footer endRefreshing]; SHOWERROR([ZYCTool handerResultData:error]); }]; } - (void)homeWaitWorkAdd:(NSInteger)page success:(void (^)(id responseObject))successful failure:(void (^) (NSError *error))failure { [[HttpManager sharedHttpManager] GETUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,WorkFlowWaitFlowHande_Get] parameters:@{@"page":@(page),@"PerPage":@(10)} responseStyle:JOSN success:^(id _Nonnull responseObject) { successful(responseObject); } failure:^(NSError * _Nonnull error) { failure(error); }]; } @end