HWHistoryViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //
  2. // HWHistoryViewController.m
  3. // Private-X
  4. //
  5. // Created by 余衡武 on 2022/3/25.
  6. //
  7. #import "HWHistoryViewController.h"
  8. #import "HWBookmarkListCell.h"
  9. #import "HWWebViewController.h"
  10. #import "HWHistoryModel.h"
  11. @interface HWHistoryViewController ()<UITableViewDelegate,UITableViewDataSource>
  12. @property (weak, nonatomic) IBOutlet UIView *header;
  13. @property (weak, nonatomic) IBOutlet UITableView *tableView;
  14. @property (strong, nonatomic) NSMutableArray *dataSource1; // 今天
  15. @property (strong, nonatomic) NSMutableArray *dataSource2; // 昨天
  16. @property (strong, nonatomic) NSMutableArray *dataSource3; // 以前
  17. @property (strong, nonatomic) HWHistoryModel *deleteModel;
  18. @end
  19. @implementation HWHistoryViewController
  20. #pragma mark - 生命周期
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. [self drawView];
  24. [self getData];
  25. }
  26. #pragma mark - UI布局
  27. - (void)drawView {
  28. [self.header mas_updateConstraints:^(MASConstraintMaker *make) {
  29. make.height.mas_equalTo(NAVIHEIGHT);
  30. }];
  31. // tableView
  32. self.tableView.delegate = self;
  33. self.tableView.dataSource = self;
  34. self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
  35. [self.tableView registerNib:[UINib nibWithNibName:@"HWBookmarkListCell" bundle:nil] forCellReuseIdentifier:@"HWBookmarkListCell"];
  36. }
  37. #pragma mark - 获取数据
  38. - (void)getData {
  39. [self.dataSource1 removeAllObjects];
  40. [self.dataSource2 removeAllObjects];
  41. [self.dataSource3 removeAllObjects];
  42. NSArray *dataArray = [HWHistoryModel bg_findAll:DB_Bookmark_History_TableName];
  43. for (HWHistoryModel *model in dataArray) {
  44. double cTime = (double)model.cTime;
  45. double nowDataTime = [[NSDate date] timeIntervalSince1970];
  46. double cha = nowDataTime - cTime;
  47. double oneDay = 60*60*24.0;
  48. double twoDay = 60*60*24.0*2;
  49. if (cha < oneDay) { // 今天
  50. [self.dataSource1 addObject:model];
  51. }else if (cha < twoDay) { // 昨天
  52. [self.dataSource2 addObject:model];
  53. }else { // 以前
  54. [self.dataSource3 addObject:model];
  55. }
  56. }
  57. [self.tableView reloadData];
  58. }
  59. - (void)deleteWithModel:(HWHistoryModel *)model {
  60. NSString *webUrl = model.webUrl;
  61. NSString *where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"webUrl"),bg_sqlValue(webUrl)];
  62. [HWHistoryModel bg_deleteAsync:DB_Bookmark_History_TableName where:where complete:^(BOOL isSuccess) {
  63. HLog(@"HWHistoryModel 删除 %@", isSuccess ? @"成功":@"失败");
  64. mainBlock(^{
  65. // 刷新表格
  66. [self getData];
  67. });
  68. }];
  69. }
  70. - (void)clearData {
  71. [HWHistoryModel bg_clearAsync:DB_Bookmark_History_TableName complete:^(BOOL isSuccess) {
  72. HLog(@"DB_Bookmark_History_TableName 清除 %@", isSuccess ? @"成功":@"失败");
  73. mainBlock(^{
  74. // 刷新表格
  75. [self getData];
  76. });
  77. }];
  78. }
  79. #pragma mark - 列表委托
  80. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  81. return 3;
  82. }
  83. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  84. if (section == 0) {
  85. return self.dataSource1.count;
  86. }else if (section == 1) {
  87. return self.dataSource2.count;
  88. }else {
  89. return self.dataSource3.count;
  90. }
  91. }
  92. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  93. HWBookmarkListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HWBookmarkListCell" forIndexPath:indexPath];
  94. HWHistoryModel *historyModel;
  95. if (indexPath.section == 0) {
  96. historyModel = self.dataSource1[indexPath.row];
  97. }else if (indexPath.section == 1) {
  98. historyModel = self.dataSource2[indexPath.row];
  99. }else {
  100. historyModel = self.dataSource3[indexPath.row];
  101. }
  102. cell.historyModel = historyModel;
  103. // cell.delegate = self;
  104. return cell;
  105. }
  106. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  107. return 50;
  108. }
  109. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  110. CGFloat height = 50;
  111. if (section == 0 && self.dataSource1.count == 0) {
  112. height = 0.1;
  113. }else if (section == 1 && self.dataSource2.count == 0) {
  114. height = 0.1;
  115. }else if (section == 2 && self.dataSource3.count == 0) {
  116. height = 0.1;
  117. }
  118. return height;
  119. }
  120. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  121. // 1、无数据情况
  122. if (section == 0 && self.dataSource1.count == 0) {
  123. return [[UIView alloc] init];
  124. }else if (section == 1 && self.dataSource2.count == 0) {
  125. return [[UIView alloc] init];
  126. }else if (section == 2 && self.dataSource3.count == 0) {
  127. return [[UIView alloc] init];
  128. }
  129. // 2、有数据情况
  130. UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W - 30, 50)];
  131. UILabel *titleLabel = [[UILabel alloc] init];
  132. titleLabel.font = [UIFont boldSystemFontOfSize:15.f];
  133. titleLabel.numberOfLines = 1;
  134. titleLabel.textColor = HWFFFFFF80Color;
  135. titleLabel.textAlignment = NSTextAlignmentLeft;
  136. [header addSubview:titleLabel];
  137. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  138. make.left.mas_equalTo(0);
  139. make.right.mas_equalTo(20);
  140. make.centerY.mas_equalTo(header.mas_centerY);
  141. }];
  142. if (section == 0) {
  143. titleLabel.text = NSLocalizedString(@"history_today",nil);
  144. }else if (section == 1) {
  145. titleLabel.text = NSLocalizedString(@"history_yesterday",nil);
  146. }else if (section == 2) {
  147. titleLabel.text = NSLocalizedString(@"history_before",nil);
  148. }
  149. return header;
  150. }
  151. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  152. return 0.1;
  153. }
  154. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  155. UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W - 30, 5)];
  156. footer.backgroundColor = [UIColor hwColor:@"#101010"];
  157. return footer;
  158. }
  159. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  160. HWHistoryModel *historyModel;
  161. if (indexPath.section == 0) {
  162. historyModel = self.dataSource1[indexPath.row];
  163. }else if (indexPath.section == 1) {
  164. historyModel = self.dataSource2[indexPath.row];
  165. }else {
  166. historyModel = self.dataSource3[indexPath.row];
  167. }
  168. HWWebViewController *vc = [[HWWebViewController alloc] init];
  169. vc.webUrl = historyModel.webUrl;
  170. [self.navigationController pushViewController:vc animated:YES];
  171. }
  172. - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{
  173. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.03 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  174. [self setupSlideBtnWithEditingIndexPath:indexPath];
  175. });
  176. }
  177. - (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{
  178. }
  179. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  180. {
  181. return YES;
  182. }
  183. // 删除Cell回调
  184. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  185. // 发送请求
  186. HLog(@"左滑删除操作");
  187. // CloudGameDownLoadMode *model = self.dataSource[indexPath.row];
  188. // [self deleteModel:model];
  189. }
  190. #pragma mark - 左滑菜单按钮自定义
  191. // 判断系统是否是 iOS13 及以上版本
  192. - (void)setupSlideBtnWithEditingIndexPath:(NSIndexPath *)editingIndexPath {
  193. if (@available(iOS 13.0, *)) {
  194. for (UIView *subView in self.tableView.subviews) {
  195. if ([subView isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")] && [subView.subviews count] >= 1) {
  196. // 修改图片
  197. UIView *remarkContentView = subView.subviews.firstObject;
  198. [self setupRowActionView:remarkContentView row:editingIndexPath];
  199. }
  200. }
  201. return;
  202. }
  203. // 判断系统是否是 iOS11 及以上版本
  204. if (@available(iOS 11.0, *)) {
  205. for (UIView *subView in self.tableView.subviews) {
  206. if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subView.subviews count] >= 1) {
  207. // 修改图片
  208. UIView *remarkContentView = subView;
  209. [self setupRowActionView:remarkContentView row:editingIndexPath];
  210. }
  211. }
  212. return;
  213. }
  214. // iOS11 以下的版本
  215. HWBookmarkListCell *cell = [self.tableView cellForRowAtIndexPath:editingIndexPath];
  216. for (UIView *subView in cell.subviews) {
  217. if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] && [subView.subviews count] >= 1) {
  218. // 修改图片
  219. UIView *remarkContentView = subView;
  220. [self setupRowActionView:remarkContentView row:editingIndexPath];
  221. }
  222. }
  223. }
  224. - (void)setupRowActionView:(UIView *)rowActionView row:(NSIndexPath *)indexPath {
  225. if (indexPath.section == 0) {
  226. self.deleteModel = self.dataSource1[indexPath.row];
  227. }else if (indexPath.section == 1) {
  228. self.deleteModel = self.dataSource2[indexPath.row];
  229. }else {
  230. self.deleteModel = self.dataSource3[indexPath.row];
  231. }
  232. [rowActionView setBackgroundColor:[UIColor clearColor]];
  233. UIButton *deleteBtn = [[UIButton alloc] initWithFrame:(CGRectMake(15, 0, 60.f, 50.f))];
  234. [deleteBtn.layer setCornerRadius:10.f];
  235. deleteBtn.clipsToBounds = YES;
  236. [deleteBtn setBackgroundColor:[UIColor redColor]];
  237. [deleteBtn setTitle:@"删除" forState:(UIControlStateNormal)];
  238. [deleteBtn.titleLabel setFont:[UIFont systemFontOfSize:12.f]];
  239. [deleteBtn setImage:[UIImage imageNamed:@"faceback_list_delete"] forState:(UIControlStateNormal)];
  240. [deleteBtn setTitleEdgeInsets:UIEdgeInsetsMake(deleteBtn.imageView.frame.size.height+3.f ,-deleteBtn.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度,距离左边框减少imageView的宽度,距离下边框和右边框距离不变
  241. [deleteBtn setImageEdgeInsets:UIEdgeInsetsMake(-deleteBtn.imageView.frame.size.height+7 , 0.0,0.0, -deleteBtn.titleLabel.bounds.size.width)];
  242. [deleteBtn addTarget:self
  243. action:@selector(deleteMessage:)
  244. forControlEvents:(UIControlEventTouchUpInside)];
  245. [deleteBtn setTag:12345+indexPath.row];
  246. [rowActionView addSubview:deleteBtn];
  247. UIButton *button = rowActionView.subviews.firstObject;
  248. [button removeFromSuperview];
  249. }
  250. - (void)deleteMessage:(id)sender {
  251. HLog(@"删除");
  252. [self deleteWithModel:self.deleteModel];
  253. }
  254. // UITableView分组切圆角方法
  255. - (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
  256. ///每个分组的行数
  257. NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];
  258. ///圆角大小
  259. CGSize cornerRadii = CGSizeMake(10, 10);
  260. ///切圆角位置
  261. UIRectCorner corners = UIRectCornerAllCorners;
  262. if (indexPath.row == 0 && numberOfRows == 1) {
  263. ///一个为一组时,四个角都为圆角
  264. corners = UIRectCornerAllCorners;
  265. }else if (indexPath.row == 0) {
  266. ///分组第一行,左上、右上角为圆角
  267. corners = UIRectCornerTopLeft | UIRectCornerTopRight;
  268. }else if (indexPath.row + 1 == numberOfRows) {
  269. ///分组最后一行,左下、右下角为圆角
  270. corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;
  271. }else {
  272. ///中间的都为矩形
  273. cornerRadii = CGSizeZero;
  274. }
  275. CGRect cellRect = cell.contentView.bounds;
  276. UIBezierPath*maskPath = [UIBezierPath bezierPathWithRoundedRect:cellRect byRoundingCorners:corners cornerRadii:cornerRadii];
  277. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  278. maskLayer.frame = cellRect;
  279. maskLayer.path = maskPath.CGPath;
  280. cell.contentView.layer.mask = maskLayer;
  281. }
  282. #pragma mark - 点击事件
  283. - (IBAction)backBtnClick:(id)sender {
  284. [self.navigationController popViewControllerAnimated:YES];
  285. }
  286. - (IBAction)clearBtnClick:(UIButton *)sender {
  287. HLog(@"清空");
  288. [self toShowTips];
  289. }
  290. - (void)toShowTips {
  291. NSString *tipsStr = NSLocalizedString(@"history_delete_history_tips",nil);
  292. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"other_confirm",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  293. [self clearData];
  294. }];
  295. [sureAction setValue:[UIColor hwColor:@"#3B7FFF"] forKey:@"titleTextColor"];//iOS8.3
  296. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"other_cancel",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  297. }];
  298. [cancelAction setValue:[UIColor colorWithHexString:@"#FFFFFF" alpha:0.5] forKey:@"titleTextColor"];//iOS8.3
  299. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:tipsStr message:@"" preferredStyle:(UIAlertControllerStyleAlert)];
  300. alertVC.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;
  301. [alertVC addAction:sureAction];
  302. [alertVC addAction:cancelAction];
  303. [self presentViewController:alertVC animated:YES completion:^{
  304. }];
  305. }
  306. #pragma mark - 懒加载
  307. - (NSMutableArray *)dataSource1 {
  308. if (!_dataSource1) {
  309. _dataSource1 = [NSMutableArray array];
  310. }
  311. return _dataSource1;
  312. }
  313. - (NSMutableArray *)dataSource2 {
  314. if (!_dataSource2) {
  315. _dataSource2 = [NSMutableArray array];
  316. }
  317. return _dataSource2;
  318. }
  319. - (NSMutableArray *)dataSource3 {
  320. if (!_dataSource3) {
  321. _dataSource3 = [NSMutableArray array];
  322. }
  323. return _dataSource3;
  324. }
  325. @end