// // LDBrowserHistoryController.m // YouHuiProject // // Created by 小花 on 2018/2/1. // Copyright © 2018年 kuxuan. All rights reserved. // #import "LDBrowserHistoryController.h" #import "LDHistoryTool.h" #import "LDHistoryModel.h" #import "LDCollectionTicketCell.h" #import "LDDateHeaderView.h" #import "LDSimilarGoodsController.h" @interface LDBrowserHistoryController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataArr; @end @implementation LDBrowserHistoryController - (void)viewDidLoad { [super viewDidLoad]; [self configNavigationBar]; [self configTableView]; [self getLocationBrowserHistoryData]; } - (void)configNavigationBar { [self.navigationBar setNavTitle:@"浏览记录"]; self.navigationBar.showNavigationBarBottomLine = YES; UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; [self.navigationBar setCustomLeftButtons:@[leftBtn]]; } - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)getLocationBrowserHistoryData { NSMutableArray *hisArr = [[NSMutableDictionary dictionaryWithContentsOfFile:[LDHistoryTool getHistoryFilePath]] objectForKey:BrowserHistoryKey]; NSMutableSet *set = [NSMutableSet set]; NSMutableArray * _datas = [[NSMutableArray alloc] initWithCapacity:0]; [hisArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { [set addObject:obj[@"browserTime"]];//利用set不重复的特性,得到有多少组,根据数组中的MeasureType字段 }]; [set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {//遍历set数组 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"browserTime = %@", obj];//创建谓词筛选器 NSArray *group = [hisArr filteredArrayUsingPredicate:predicate]; [_datas addObject:group]; }]; for (NSArray *dicArr in _datas) { NSMutableArray *mArr = [NSMutableArray array]; for (NSDictionary *dic in dicArr) { LDHistoryModel *model = [[LDHistoryModel alloc] init]; [model setValuesForKeysWithDictionary:dic]; [mArr addObject:model]; } [self.dataArr addObject:mArr]; } [self.tableView reloadData]; } - (void)configTableView { [self.view addSubview:self.tableView]; self.tableView.showNoDataView = YES; self.tableView.defaultNoDataText = @"暂无数据"; self.tableView.defaultNoDataViewDidClickBlock = ^(UIView *view) { }; } /** 移除历史 */ - (void)deleteCollectionGoodAtIndexPath:(NSIndexPath *)indexPath { LDHistoryModel *model = self.dataArr[indexPath.section][indexPath.row]; NSMutableArray *hisArr = [[NSMutableDictionary dictionaryWithContentsOfFile:[LDHistoryTool getHistoryFilePath]] objectForKey:BrowserHistoryKey]; for (NSDictionary *dic in [hisArr mutableCopy]) { if ([dic[@"goods_id"] isEqualToString:model.goods_id]) { [hisArr removeObject:dic]; } } NSDictionary *historyDic = @{BrowserHistoryKey:hisArr}; [historyDic writeToFile:[LDHistoryTool getHistoryFilePath] atomically:YES]; // 删除模型 NSMutableArray *mArr = self.dataArr[indexPath.section]; [mArr removeObjectAtIndex:indexPath.row]; [self.dataArr replaceObjectAtIndex:indexPath.section withObject:mArr]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } #pragma mark ------------------------ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { [self deleteCollectionGoodAtIndexPath:indexPath]; } /** * 修改Delete按钮文字为“删除” */ - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"删除"; } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsMake(0, 50, 0, 0)]; } } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.dataArr.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSArray *arr = self.dataArr[section]; return arr.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 100; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 40; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { LDCollectionTicketCell *cell = [LDCollectionTicketCell cellWithTableView:tableView]; cell.selectionStyle = UITableViewCellSelectionStyleNone; LDHistoryModel *model = self.dataArr[indexPath.section][indexPath.row]; cell.historyModel = model; cell.similarClick = ^{ //找相似 LDSimilarGoodsController *similar = [[LDSimilarGoodsController alloc] init]; similar.goods_id = model.goods_id; [self.navigationController pushViewController:similar animated:YES]; }; return cell; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { LDDateHeaderView *header = [[LDDateHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)]; LDHistoryModel *model = [self.dataArr[section] firstObject]; [header setDateWith:model.browserTime]; return header; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { LDHistoryModel *model = self.dataArr[indexPath.section][indexPath.row]; LDGoodDetailViewController *detail = [[LDGoodDetailViewController alloc] init]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithId:model.goods_id is_coupon:model.is_coupon coupon_price:model.coupon_price price:model.price discount_price:model.discount_price commission_rate:model.commission_rate coupon_start_time:model.coupon_start_time coupon_end_time:model.coupon_end_time coupon_id:model.coupon_id]; detail.requestModel = requestModel; LDEventModel *evevtModel = [[LDEventModel alloc] initWithOrigin:@"0" category_id:@"0" source:browsingHistoryAction]; detail.eventModel = evevtModel; [self.navigationController pushViewController:detail animated:YES]; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) style:UITableViewStylePlain]; _tableView.estimatedSectionHeaderHeight = 0; _tableView.estimatedSectionFooterHeight = 0; _tableView.sectionFooterHeight = 0; _tableView.sectionHeaderHeight = 0; _tableView.delegate = self; _tableView.dataSource = self; _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; _tableView.backgroundColor = [UIColor yhGrayColor]; _tableView.bounces = YES; _tableView.showsVerticalScrollIndicator = NO; [_tableView setSeparatorColor:[UIColor YHColorWithHex:0xdddddd]]; } return _tableView; } - (NSMutableArray *)dataArr { if (!_dataArr) { _dataArr = [NSMutableArray array]; } return _dataArr; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end