猎豆优选

LDBrowserHistoryController.m 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // LDBrowserHistoryController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/2/1.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LDBrowserHistoryController.h"
  9. #import "LDHistoryTool.h"
  10. #import "LDHistoryModel.h"
  11. #import "LDCollectionTicketCell.h"
  12. #import "LDDateHeaderView.h"
  13. #import "LDSimilarGoodsController.h"
  14. @interface LDBrowserHistoryController ()<UITableViewDelegate,UITableViewDataSource>
  15. @property (nonatomic, strong) UITableView *tableView;
  16. @property (nonatomic, strong) NSMutableArray *dataArr;
  17. @end
  18. @implementation LDBrowserHistoryController
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. [self configNavigationBar];
  22. [self configTableView];
  23. [self getLocationBrowserHistoryData];
  24. }
  25. - (void)configNavigationBar {
  26. [self.navigationBar setNavTitle:@"浏览记录"];
  27. self.navigationBar.showNavigationBarBottomLine = YES;
  28. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  29. [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  30. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  31. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  32. }
  33. - (void)backAction {
  34. [self.navigationController popViewControllerAnimated:YES];
  35. }
  36. - (void)getLocationBrowserHistoryData {
  37. NSMutableArray *hisArr = [[NSMutableDictionary dictionaryWithContentsOfFile:[LDHistoryTool getHistoryFilePath]] objectForKey:BrowserHistoryKey];
  38. NSMutableSet *set = [NSMutableSet set];
  39. NSMutableArray * _datas = [[NSMutableArray alloc] initWithCapacity:0];
  40. [hisArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  41. [set addObject:obj[@"browserTime"]];//利用set不重复的特性,得到有多少组,根据数组中的MeasureType字段
  42. }];
  43. [set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {//遍历set数组
  44. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"browserTime = %@", obj];//创建谓词筛选器
  45. NSArray *group = [hisArr filteredArrayUsingPredicate:predicate];
  46. [_datas addObject:group];
  47. }];
  48. for (NSArray *dicArr in _datas) {
  49. NSMutableArray *mArr = [NSMutableArray array];
  50. for (NSDictionary *dic in dicArr) {
  51. LDHistoryModel *model = [[LDHistoryModel alloc] init];
  52. [model setValuesForKeysWithDictionary:dic];
  53. [mArr addObject:model];
  54. }
  55. [self.dataArr addObject:mArr];
  56. }
  57. [self.tableView reloadData];
  58. }
  59. - (void)configTableView {
  60. [self.view addSubview:self.tableView];
  61. self.tableView.showNoDataView = YES;
  62. self.tableView.defaultNoDataText = @"暂无数据";
  63. self.tableView.defaultNoDataViewDidClickBlock = ^(UIView *view) {
  64. };
  65. }
  66. /**
  67. 移除历史
  68. */
  69. - (void)deleteCollectionGoodAtIndexPath:(NSIndexPath *)indexPath {
  70. LDHistoryModel *model = self.dataArr[indexPath.section][indexPath.row];
  71. NSMutableArray *hisArr = [[NSMutableDictionary dictionaryWithContentsOfFile:[LDHistoryTool getHistoryFilePath]] objectForKey:BrowserHistoryKey];
  72. for (NSDictionary *dic in [hisArr mutableCopy]) {
  73. if ([dic[@"goods_id"] isEqualToString:model.goods_id]) {
  74. [hisArr removeObject:dic];
  75. }
  76. }
  77. NSDictionary *historyDic = @{BrowserHistoryKey:hisArr};
  78. [historyDic writeToFile:[LDHistoryTool getHistoryFilePath] atomically:YES];
  79. // 删除模型
  80. NSMutableArray *mArr = self.dataArr[indexPath.section];
  81. [mArr removeObjectAtIndex:indexPath.row];
  82. [self.dataArr replaceObjectAtIndex:indexPath.section withObject:mArr];
  83. [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  84. }
  85. #pragma mark ------------------------
  86. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  87. {
  88. [self deleteCollectionGoodAtIndexPath:indexPath];
  89. }
  90. /**
  91. * 修改Delete按钮文字为“删除”
  92. */
  93. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  94. {
  95. return @"删除";
  96. }
  97. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  98. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  99. [cell setSeparatorInset:UIEdgeInsetsMake(0, 50, 0, 0)];
  100. }
  101. }
  102. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  103. return self.dataArr.count;
  104. }
  105. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  106. NSArray *arr = self.dataArr[section];
  107. return arr.count;
  108. }
  109. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  110. return 100;
  111. }
  112. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  113. return 40;
  114. }
  115. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  116. LDCollectionTicketCell *cell = [LDCollectionTicketCell cellWithTableView:tableView];
  117. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  118. LDHistoryModel *model = self.dataArr[indexPath.section][indexPath.row];
  119. cell.historyModel = model;
  120. cell.similarClick = ^{
  121. //找相似
  122. LDSimilarGoodsController *similar = [[LDSimilarGoodsController alloc] init];
  123. similar.goods_id = model.goods_id;
  124. [self.navigationController pushViewController:similar animated:YES];
  125. };
  126. return cell;
  127. }
  128. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  129. LDDateHeaderView *header = [[LDDateHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
  130. LDHistoryModel *model = [self.dataArr[section] firstObject];
  131. [header setDateWith:model.browserTime];
  132. return header;
  133. }
  134. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  135. LDHistoryModel *model = self.dataArr[indexPath.section][indexPath.row];
  136. LDGoodDetailViewController *detail = [[LDGoodDetailViewController alloc] init];
  137. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithId:model.goods_id
  138. is_coupon:model.is_coupon
  139. coupon_price:model.coupon_price
  140. price:model.price
  141. discount_price:model.discount_price
  142. commission_rate:model.commission_rate
  143. coupon_start_time:model.coupon_start_time
  144. coupon_end_time:model.coupon_end_time
  145. coupon_id:model.coupon_id];
  146. detail.requestModel = requestModel;
  147. LDEventModel *evevtModel = [[LDEventModel alloc] initWithOrigin:@"0" category_id:@"0" source:browsingHistoryAction];
  148. detail.eventModel = evevtModel;
  149. [self.navigationController pushViewController:detail animated:YES];
  150. }
  151. - (UITableView *)tableView {
  152. if (!_tableView) {
  153. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) style:UITableViewStylePlain];
  154. _tableView.estimatedSectionHeaderHeight = 0;
  155. _tableView.estimatedSectionFooterHeight = 0;
  156. _tableView.sectionFooterHeight = 0;
  157. _tableView.sectionHeaderHeight = 0;
  158. _tableView.delegate = self;
  159. _tableView.dataSource = self;
  160. _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  161. _tableView.backgroundColor = [UIColor yhGrayColor];
  162. _tableView.bounces = YES;
  163. _tableView.showsVerticalScrollIndicator = NO;
  164. [_tableView setSeparatorColor:[UIColor YHColorWithHex:0xdddddd]];
  165. }
  166. return _tableView;
  167. }
  168. - (NSMutableArray *)dataArr {
  169. if (!_dataArr) {
  170. _dataArr = [NSMutableArray array];
  171. }
  172. return _dataArr;
  173. }
  174. - (void)didReceiveMemoryWarning {
  175. [super didReceiveMemoryWarning];
  176. // Dispose of any resources that can be recreated.
  177. }
  178. /*
  179. #pragma mark - Navigation
  180. // In a storyboard-based application, you will often want to do a little preparation before navigation
  181. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  182. // Get the new view controller using [segue destinationViewController].
  183. // Pass the selected object to the new view controller.
  184. }
  185. */
  186. @end