// // HomeProblemVC.m // smartRhino // // Created by niuzhen on 2020/6/23. // Copyright © 2020 tederen. All rights reserved. // #import "HomeProblemVC.h" #import "HomeSubModel.h" #import "HomeSubItemModel.h" #import "HomeProblemTopCell.h" #import "BookProblemModel.h" @interface HomeProblemVC () @property (nonatomic, strong) UITableView * tableView; @property (nonatomic, strong) NSMutableArray * dataSource; @property (nonatomic, assign) NSUInteger currentPage; @property (nonatomic, assign) NSUInteger Total; @property (nonatomic, strong) BookProblemModel * model; @end @implementation HomeProblemVC - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.tableView]; self.tableView.delegate = self; self.tableView.dataSource = self; [self getData]; [self setTableViewRefresh]; } - (void)setHeight:(CGFloat)height { [self.tableView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, height - 45)]; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.showsVerticalScrollIndicator = NO; } return _tableView; } - (NSMutableArray *)dataSource { if (!_dataSource) { _dataSource = [NSMutableArray array]; } return _dataSource; } - (void)setTableViewRefresh { WeakSelf(self) self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ [weakself headRefresh]; }]; self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{ [weakself footerRefresh]; }]; } - (void)headRefresh{ self.currentPage = 1; self.Total = 0; [self.dataSource removeAllObjects]; [self getListData]; } - (void)footerRefresh{ self.currentPage += 1; if (self.Total == self.dataSource.count) { self.currentPage --; [self.tableView.mj_footer resetNoMoreData]; return; } [self getListData]; } - (void)getData { WS(weakSelf); dispatch_semaphore_t sem = dispatch_semaphore_create(0); __block NSInteger httpFinishCount = 0; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [[HttpManager sharedHttpManager] POSTUrl:Host(API_APP_RANDOM_DAY_Skill) parameters:@{@"StyleCss":weakSelf.style} responseStyle:JOSN success:^(id _Nonnull responseObject) { NSLog(@"%@",responseObject); if ([responseObject isKindOfClass:[NSDictionary class]]) { weakSelf.model = [BookProblemModel modelWithDictionary:responseObject]; } if (++httpFinishCount == 2) { dispatch_semaphore_signal(sem); } } failure:^(NSError * _Nonnull error) { if (++httpFinishCount == 2) { dispatch_semaphore_signal(sem); } }]; [[HttpManager sharedHttpManager] POSTUrl:Host(API_APP_DAY_Skill) parameters:@{@"StyleCss":weakSelf.style,@"Page":@(weakSelf.currentPage),@"PerPage":@20} responseStyle:JOSN success:^(id _Nonnull responseObject) { NSLog(@"++++%@",responseObject); if ([responseObject isKindOfClass:[NSDictionary class]]) { for (NSDictionary * dict in responseObject[@"Items"]) { HomeSubItemModel *model = [HomeSubItemModel modelWithDictionary:dict]; [weakSelf.dataSource addObject:model]; } weakSelf.Total = [responseObject[@"Total"] integerValue]; if (weakSelf.dataSource.count == weakSelf.Total) { [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData]; weakSelf.tableView.mj_footer.hidden = YES; } } [weakSelf.tableView.mj_header endRefreshing]; [weakSelf.tableView.mj_footer endRefreshing]; if (++httpFinishCount == 2) { dispatch_semaphore_signal(sem); } } failure:^(NSError * _Nonnull error) { if (++httpFinishCount == 2) { dispatch_semaphore_signal(sem); } }]; dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableView reloadData]; }); }); } - (void)getListData { WS(weakSelf); [[HttpManager sharedHttpManager] POSTUrl:Host(API_APP_DAY_Skill) parameters:@{@"StyleCss":self.style,@"Page":@(self.currentPage),@"PerPage":@20} responseStyle:JOSN success:^(id _Nonnull responseObject) { NSLog(@"++++%@",responseObject); if ([responseObject isKindOfClass:[NSDictionary class]]) { for (NSDictionary * dict in responseObject[@"Items"]) { HomeSubItemModel *model = [HomeSubItemModel modelWithDictionary:dict]; [weakSelf.dataSource addObject:model]; } weakSelf.Total = [responseObject[@"Total"] integerValue]; if (weakSelf.dataSource.count == weakSelf.Total) { [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData]; weakSelf.tableView.mj_footer.hidden = YES; } } [weakSelf.tableView.mj_header endRefreshing]; [weakSelf.tableView.mj_footer endRefreshing]; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableView reloadData]; }); } failure:^(NSError * _Nonnull error) { }]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSource.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { WS(weakSelf); if (indexPath.row == 0) { HomeProblemTopCell * cell = [HomeProblemTopCell configCell0:tableView indexPath:indexPath]; cell.height.constant = -2.f; [cell setDataModel:self.model]; [cell.TopBtn setAction:^{ [weakSelf pushVC:weakSelf.model.Id]; }]; return cell; }else{ HomeSubItemModel * model = [self.dataSource objectAtIndex:indexPath.row]; HomeProblemTopCell * cell = [HomeProblemTopCell configCell1:tableView indexPath:indexPath]; [cell setDataListModel:model]; return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; HomeSubItemModel * model = [self.dataSource objectAtIndex:indexPath.row]; [self pushVC:model.Id]; } - (void)pushVC:(NSInteger)Id { MyTDTopicDetailVC * vc = [MyTDTopicDetailVC initMyTDTopicDetailVC]; vc.Id = Id; vc.type = CollectModel_MediaAritle; [self.navigationController pushViewController:vc animated:YES]; } @end