// // TFCompleteCommController.m // TFReader // // Created by 谢腾飞 on 2020/12/17. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFCompleteCommController.h" #import "TFProductionListViewCell.h" #import "TFPublicAdvertisementViewCell.h" @interface TFCompleteCommController () @property (nonatomic ,assign) BOOL needRefresh; @end @implementation TFCompleteCommController - (void)viewDidLoad { [super viewDidLoad]; [self initialize]; [self createSubviews]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self setStatusBarDefaultStyle]; } - (void)initialize { self.needRefresh = YES; [self hiddenNavigationBar:YES]; } - (void)createSubviews { self.mainTableView.delegate = self; self.mainTableView.dataSource = self; self.mainTableView.contentInset = UIEdgeInsetsMake(0, 0, PUB_TABBAR_OFFSET, 0); [self.view addSubview:self.mainTableView]; [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.top.mas_equalTo(self.view.mas_top); make.bottom.mas_equalTo(self.view.mas_bottom).with.offset(- PUB_NAVBAR_HEIGHT - self.pageViewHeight); make.width.mas_equalTo(self.view.mas_width); }]; [self setEmptyOnView:self.mainTableView title:TFLocalizedString(@"暂无数据") buttonTitle:@"" tapBlock:^{ }]; WS(weakSelf) self.mainTableView.mj_header = [TFRefreshHeader headerWithRefreshingBlock:^{ weakSelf.currentPageNumber = 1; [weakSelf netRequest]; }]; [self.mainTableView.mj_header beginRefreshing]; self.mainTableView.mj_footer = [TFRefreshFooter footerWithRefreshingBlock:^{ weakSelf.currentPageNumber ++; [weakSelf netRequest]; }]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSourceArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TFProductionModel *t_model = [self.dataSourceArray objectOrNilAtIndex:indexPath.row]; if (t_model.ad_type == 0) { TFProductionListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TFProductionListViewCell"]; if (!cell) { cell = [[TFProductionListViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TFProductionListViewCell"]; } cell.productionType = self.productionType; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.productionModel = t_model; cell.hiddenEndLine = (indexPath.row == self.dataSourceArray.count - 1); return cell; } else { TFPublicAdvertisementViewCell *cell = [self.advertDict objectForKey:[TFUtilsHelper formatStringWithInteger:indexPath.row]]; if (!cell) { static NSString *cellName = @"TFPublicAdvertisementViewCell"; cell = [[TFPublicAdvertisementViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName]; cell.mainTableView = tableView; cell.selectionStyle = UITableViewCellSelectionStyleNone; [self.advertDict setObject:cell forKey:[TFUtilsHelper formatStringWithInteger:indexPath.row]]; } [cell setAdModel:t_model refresh:self.needRefresh]; return cell; } } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return kHalfMargin; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHalfMargin)]; view.backgroundColor = [UIColor clearColor]; return view; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return CGFLOAT_MIN; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, PUB_TABBAR_OFFSET == 0 ? 10 : PUB_TABBAR_OFFSET)]; view.backgroundColor = [UIColor clearColor]; return view; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { TFProductionModel *t_model = [self.dataSourceArray objectOrNilAtIndex:indexPath.row]; switch (self.productionType) { #if TF_Enable_Book case TFProductionTypeNovel: { TFNovelDetailViewController *vc = [[TFNovelDetailViewController alloc] init]; vc.book_id = t_model.production_id; [self.navigationController pushViewController:vc animated:YES]; } break; #endif #if TF_Enable_Comic case TFProductionTypeComic: { TFComicDetailViewController *comicDetail = [[TFComicDetailViewController alloc] init]; comicDetail.comic_id = t_model.production_id; [self.navigationController pushViewController:comicDetail animated:YES]; } break; #endif #if TF_Enable_Audio case TFProductionTypeAudio: { TFAudioDetailViewController *vc = [[TFAudioDetailViewController alloc] init]; vc.audio_id = t_model.production_id; [self.navigationController pushViewController:vc animated:YES]; } break; #endif default: break; } } - (void)netRequest { WS(weakSelf) NSString *url = @""; switch (self.productionType) { case TFProductionTypeNovel: url = Book_Finish; break; case TFProductionTypeComic: url = Comic_Finish; break; case TFProductionTypeAudio: url = Audio_Finish; break; default: break; } NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"channel_id"] = self.channel; params[@"page_num"] = [TFUtilsHelper formatStringWithInteger:self.currentPageNumber]; [TFNetworkTools POST:url parameters:params model:TFProductionListModel.class success:^(BOOL isSuccess, TFProductionListModel *_Nullable t_model, TFNetworkRequestModel *requestModel) { [weakSelf.mainTableView endRefreshing]; if (isSuccess) { if (weakSelf.currentPageNumber == 1) { [weakSelf.mainTableView showRefreshFooter]; [weakSelf.dataSourceArray removeAllObjects]; weakSelf.dataSourceArray = [NSMutableArray arrayWithArray:t_model.list]; } else { [weakSelf.dataSourceArray addObjectsFromArray:t_model.list]; } if (t_model.total_page <= t_model.current_page) { [weakSelf.mainTableView hideRefreshFooter]; } } weakSelf.needRefresh = YES; [weakSelf.mainTableView reloadData]; dispatch_async(dispatch_get_main_queue(), ^{ weakSelf.needRefresh = NO; }); [weakSelf.mainTableView xtfei_endLoading]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { [weakSelf.mainTableView endRefreshing]; [weakSelf.mainTableView xtfei_endLoading]; [TFPromptManager showPromptWithError:error defaultText:nil]; }]; } @end