// // GiMaskTimeHistoryVC.m // GIGA // // Created by lianxiang on 2018/9/3. // Copyright © 2018年 com.giga.ios. All rights reserved. // #import "GiMaskTimeHistoryVC.h" #import "MaskHistoryListViewCell.h" #import "MasHistoryFirstViewCell.h" #import "GiGaBaseAPiRequest.h" #import "MaskHistryRecord.h" #import #import "GiGaWebViewController.h" #import "HistoryNoDataViewCell.h" @interface GiMaskTimeHistoryVC () @property(nonatomic,strong) NSMutableArray *dataArr; @property(nonatomic) int currentPage; @property(nonatomic,copy) NSString *totalMinute; @property(nonatomic,assign) NSInteger ranking; @property(nonatomic,assign) NSUInteger totalPage; //@property(nonatomic,strong) MaskHistryRecordlist *record; @property(nonatomic,copy) NSString * guizeUrl; @property(nonatomic) BOOL isFreshing; @property(nonatomic,copy) NSString *exquisitePercent; @end @implementation GiMaskTimeHistoryVC - (void)viewDidLoad { [super viewDidLoad]; self.dataArr = [[NSMutableArray alloc] init]; _currentPage = 1; _totalMinute = @"0.0"; _ranking = 0; _isFreshing = NO; [self addNavTitile:@"历史记录"]; // Do any additional setup after loading the view. [self.view addSubview:self.tableView]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.backgroundColor = [UIColor whiteColor]; self.tableView.separatorStyle = UITableViewCellSelectionStyleNone; self.tableView.sectionFooterHeight = 0; self.tableView.estimatedSectionFooterHeight= 0; self.tableView.estimatedSectionHeaderHeight = 0; self.tableView.frame = CGRectMake(0,2, KMainW, self.view.bounds.size.height - SAFE_NAV_HEIGHT); self.tableView.estimatedRowHeight = 170; self.tableView.rowHeight = UITableViewAutomaticDimension; [self creatRefreshView]; [self loadDataWtih:[NSString stringWithFormat:@"%d",_currentPage] isFresh:YES]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ if (self.dataArr.count == 0) { return 1; } return 2; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if (self.dataArr.count ==0) { HistoryNoDataViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HistoryNoDataViewCell"]; if(!cell){ cell = [[[NSBundle mainBundle] loadNibNamed:@"HistoryNoDataViewCell" owner:self options:nil] lastObject]; } [cell.mianmoBtn addTarget:self action:@selector(mianmoBtnAction) forControlEvents:UIControlEventTouchUpInside]; return cell; } if(indexPath.section ==0){ MasHistoryFirstViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MasHistoryFirstViewCell"]; if(!cell){ cell = [[[NSBundle mainBundle] loadNibNamed:@"MasHistoryFirstViewCell" owner:self options:nil] lastObject]; } cell.timeLabe.text = [NSString stringWithFormat:@"%@",self.totalMinute]; if (self.exquisitePercent) { NSString *text = [NSString stringWithFormat:@"本月超过%@用户",self.exquisitePercent]; NSString *percent = self.exquisitePercent; NSRange range = [text rangeOfString:percent]; NSMutableAttributedString *atttri =[[NSMutableAttributedString alloc] initWithString:text]; [atttri addAttributes:@{NSFontAttributeName:GIGA_TEXTFONTMEDIUM(16)} range:range]; //cell.rankLabe.text = [NSString stringWithFormat:@"本月超过%@用户",self.exquisitePercent]; cell.rankLabe.attributedText = atttri; } [cell.guizeBtn addTarget:self action:@selector(guizeShow) forControlEvents:UIControlEventTouchUpInside]; return cell; } MaskHistoryListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MaskHistoryListViewCell"]; if(!cell){ cell = [[[NSBundle mainBundle] loadNibNamed:@"MaskHistoryListViewCell" owner:self options:nil] lastObject]; } MaskHistryRecord *recod = self.dataArr[indexPath.row]; [cell loadCellDataAt:indexPath recod:recod]; return cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (section == 0) { return 1; } return self.dataArr.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (section == 1) { return 6; } return 0.1; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *view = [[UIView alloc] init]; view.backgroundColor = GIGARGB(235, 235, 235, 1); return view; } -(void)loadDataWtih:(NSString *)page isFresh:(BOOL)isFresh{ self.isFreshing = YES; [self.view makeToastActivity:CSToastPositionCenter]; GiGaBaseAPiRequest *api = [GiGaBaseAPiRequest initWithRequestPath:kApiUserHistoryList method:RequestPostMethod parms:@{@"page":page,@"limit":@"10"}]; [api requstDataWithResult:^(GiGaAPIResult *result) { [self.view hideToastActivity]; self.isFreshing = NO; if (isFresh) { [self.tableView.mj_header endRefreshing]; }else{ [self.tableView.mj_footer endRefreshing]; } if (result.success) { NSDictionary *reDic =result.dic; self.ranking = [[reDic valueForKey:@"ranking"] integerValue]; self.totalMinute = [reDic valueForKey:@"totalMinute"]; self.exquisitePercent = [reDic valueForKey:@"exquisitePercent"]; self.guizeUrl = [reDic valueForKey:@"ruleUrl"]; NSDictionary *pageDic = reDic[@"page"]; //self.currentPage = [pageDic valueForKey:@"currPage"]; MaskHistryRecordlist *list = [[MaskHistryRecordlist alloc] initWithDictionary:pageDic error:nil]; self.totalPage = [pageDic[@"totalPage"] integerValue]; NSArray *listA = list.list; if (isFresh) { [self.dataArr removeAllObjects]; } for (MaskHistryRecord *recod in listA) { [self.dataArr addObject:recod]; } [self.tableView reloadData]; }else if (result.code == 401) { //token失效 [GiGaBaseAPiRequest userTokenTimeOutGologinFromVC:self]; }else{ GIGA_ShowToast(result.message); } }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)creatRefreshView{ self.tableView.mj_header = [MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)]; self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)]; } -(void)headerRefresh{ if (_isFreshing) { [self.tableView.mj_header endRefreshing]; return; } [self loadDataWtih:@"0" isFresh:YES]; } -(void)loadMore{ if (_isFreshing) { [self.tableView.mj_footer endRefreshing]; return; } if (_currentPage == _totalPage) { [self.tableView.mj_footer endRefreshingWithNoMoreData]; return; } if (_currentPage < _totalPage) { _currentPage ++; [self loadDataWtih:[NSString stringWithFormat:@"%d",self.currentPage] isFresh:NO]; } } //规则 -(void)guizeShow{ if (self.guizeUrl) { GiGaWebViewController *web = [[GiGaWebViewController alloc] init]; web.souceType = WebSourceTypeUrlLink; web.url = self.guizeUrl; [self.navigationController pushViewController:web animated:YES]; } } -(void)mianmoBtnAction{ [self.navigationController popToRootViewControllerAnimated:YES]; } @end