天天省钱快报

KBBrowserHistoryController.m 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // KBBrowserHistoryController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/2/1.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBBrowserHistoryController.h"
  9. #import "KBHistoryTool.h"
  10. #import "KBHistoryModel.h"
  11. #import "KBCollectionTicketCell.h"
  12. #import "KBDateHeaderView.h"
  13. #import "KBGoodDetailViewController.h"
  14. #import "KBSimilarGoodsController.h"
  15. @interface KBBrowserHistoryController ()<UITableViewDelegate,UITableViewDataSource>
  16. @property (nonatomic, strong) UITableView *tableView;
  17. @property (nonatomic, strong) NSMutableArray *dataArr;
  18. @end
  19. @implementation KBBrowserHistoryController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. [self configNavigationBar];
  23. [self configTableView];
  24. [self getLocationBrowserHistoryData];
  25. }
  26. - (void)configNavigationBar {
  27. [self.navigationBar setNavTitle:@"浏览记录"];
  28. self.navigationBar.showNavigationBarBottomLine = YES;
  29. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  30. [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  31. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  32. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  33. }
  34. - (void)backAction {
  35. [self.navigationController popViewControllerAnimated:YES];
  36. }
  37. - (void)getLocationBrowserHistoryData {
  38. NSMutableArray *hisArr = [[NSMutableDictionary dictionaryWithContentsOfFile:[KBHistoryTool getHistoryFilePath]] objectForKey:BrowserHistoryKey];
  39. NSMutableSet *set = [NSMutableSet set];
  40. NSMutableArray * _datas = [[NSMutableArray alloc] initWithCapacity:0];
  41. [hisArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  42. [set addObject:obj[@"browserTime"]];//利用set不重复的特性,得到有多少组,根据数组中的MeasureType字段
  43. }];
  44. [set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {//遍历set数组
  45. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"browserTime = %@", obj];//创建谓词筛选器
  46. NSArray *group = [hisArr filteredArrayUsingPredicate:predicate];
  47. [_datas addObject:group];
  48. }];
  49. for (NSArray *dicArr in _datas) {
  50. NSMutableArray *mArr = [NSMutableArray array];
  51. for (NSDictionary *dic in dicArr) {
  52. KBHistoryModel *model = [[KBHistoryModel alloc] init];
  53. [model setValuesForKeysWithDictionary:dic];
  54. [mArr addObject:model];
  55. }
  56. [self.dataArr addObject:mArr];
  57. }
  58. [self.tableView reloadData];
  59. }
  60. - (void)configTableView {
  61. [self.view addSubview:self.tableView];
  62. self.tableView.showNoDataView = YES;
  63. self.tableView.defaultNoDataText = @"暂无数据";
  64. self.tableView.defaultNoDataViewDidClickBlock = ^(UIView *view) {
  65. };
  66. }
  67. /**
  68. 移除历史
  69. */
  70. - (void)deleteCollectionGoodAtIndexPath:(NSIndexPath *)indexPath {
  71. KBHistoryModel *model = self.dataArr[indexPath.section][indexPath.row];
  72. NSMutableArray *hisArr = [[NSMutableDictionary dictionaryWithContentsOfFile:[KBHistoryTool getHistoryFilePath]] objectForKey:BrowserHistoryKey];
  73. for (NSDictionary *dic in [hisArr mutableCopy]) {
  74. if ([dic[@"goods_id"] isEqualToString:model.goods_id]) {
  75. [hisArr removeObject:dic];
  76. }
  77. }
  78. NSDictionary *historyDic = @{BrowserHistoryKey:hisArr};
  79. [historyDic writeToFile:[KBHistoryTool getHistoryFilePath] atomically:YES];
  80. // 删除模型
  81. NSMutableArray *mArr = self.dataArr[indexPath.section];
  82. [mArr removeObjectAtIndex:indexPath.row];
  83. [self.dataArr replaceObjectAtIndex:indexPath.section withObject:mArr];
  84. [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  85. }
  86. #pragma mark ------------------------
  87. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  88. {
  89. [self deleteCollectionGoodAtIndexPath:indexPath];
  90. }
  91. /**
  92. * 修改Delete按钮文字为“删除”
  93. */
  94. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  95. {
  96. return @"删除";
  97. }
  98. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  99. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  100. [cell setSeparatorInset:UIEdgeInsetsMake(0, 50, 0, 0)];
  101. }
  102. }
  103. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  104. return self.dataArr.count;
  105. }
  106. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  107. NSArray *arr = self.dataArr[section];
  108. return arr.count;
  109. }
  110. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  111. return 100;
  112. }
  113. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  114. return 40;
  115. }
  116. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  117. KBCollectionTicketCell *cell = [KBCollectionTicketCell cellWithTableView:tableView];
  118. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  119. KBHistoryModel *model = self.dataArr[indexPath.section][indexPath.row];
  120. cell.historyModel = model;
  121. cell.similarClick = ^{
  122. //找相似
  123. KBSimilarGoodsController *similar = [[KBSimilarGoodsController alloc] init];
  124. similar.goods_id = model.goods_id;
  125. [self.navigationController pushViewController:similar animated:YES];
  126. };
  127. return cell;
  128. }
  129. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  130. KBDateHeaderView *header = [[KBDateHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
  131. KBHistoryModel *model = [self.dataArr[section] firstObject];
  132. [header setDateWith:model.browserTime];
  133. return header;
  134. }
  135. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  136. KBHistoryModel *model = self.dataArr[indexPath.section][indexPath.row];
  137. KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init];
  138. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithId:model.goods_id
  139. is_coupon:model.is_coupon
  140. coupon_price:model.coupon_price
  141. price:model.price
  142. discount_price:model.discount_price
  143. commission_rate:model.commission_rate
  144. coupon_start_time:model.coupon_start_time
  145. coupon_end_time:model.coupon_end_time];
  146. detail.requestModel = requestModel;
  147. KBEventModel *evevtModel = [[KBEventModel 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