HWPageViewController.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // HWMoreViewController.m
  3. // Private-X
  4. //
  5. // Created by 余衡武 on 2022/3/22.
  6. //
  7. #import "HWPageViewController.h"
  8. #import "HWPageListCell.h"
  9. #import "HWWebViewController.h"
  10. #import <WebKit/WebKit.h>
  11. @interface HWPageViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,HWPageListCellDelegate>
  12. @property (weak, nonatomic) IBOutlet UIView *headerView;
  13. @property (weak, nonatomic) IBOutlet UIView *footerView;
  14. @property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
  15. @property (strong, nonatomic) NSMutableArray *dataSource;
  16. @end
  17. @implementation HWPageViewController
  18. #pragma mark 生命周期
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. [self drawView];
  22. [self getData];
  23. }
  24. #pragma mark UI布局
  25. - (void)drawView {
  26. [self.headerView mas_updateConstraints:^(MASConstraintMaker *make) {
  27. make.height.mas_equalTo(20+AdaptNaviHeight);
  28. }];
  29. [self.footerView mas_updateConstraints:^(MASConstraintMaker *make) {
  30. make.height.mas_equalTo(49+AdaptTabHeight);
  31. }];
  32. self.collectionView.delegate = self;
  33. self.collectionView.dataSource = self;
  34. self.collectionView.contentInset = UIEdgeInsetsMake(15, 15, 0, 15);
  35. self.collectionView.backgroundColor = [UIColor clearColor];
  36. [self.collectionView registerNib:[UINib nibWithNibName:@"HWPageListCell" bundle:nil] forCellWithReuseIdentifier:@"HWPageListCell"];
  37. UICollectionViewFlowLayout *dataLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
  38. CGFloat W = (SCREEN_W - 15*3)*0.5;
  39. CGFloat H = 280*WAUTOSCALE;
  40. dataLayout.itemSize = CGSizeMake(W, H);
  41. dataLayout.minimumLineSpacing = 0;
  42. dataLayout.minimumInteritemSpacing = 5;
  43. dataLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
  44. }
  45. #pragma mark 获取数据
  46. - (void)getData {
  47. [self.dataSource removeAllObjects];
  48. NSArray *dataArray = [BaseModel bg_findAll:DB_BrowserWindows_TableName];
  49. if (dataArray.count == 0) {
  50. // 主页
  51. [self.navigationController popViewControllerAnimated:YES];
  52. }else {
  53. [self.dataSource addObjectsFromArray:dataArray];
  54. [self.collectionView reloadData];
  55. }
  56. }
  57. #pragma mark UICollectionViewDelegate, UICollectionViewDataSource
  58. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  59. return self.dataSource.count;
  60. }
  61. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  62. HWPageListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HWPageListCell" forIndexPath:indexPath];
  63. BaseModel *model = self.dataSource[indexPath.row];
  64. cell.model = model;
  65. cell.delegate = self;
  66. return cell;
  67. }
  68. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  69. BaseModel *model = self.dataSource[indexPath.row];
  70. // 浏览器当前窗口索引ID
  71. [HWDataManager setIntegerWithKey:BrowserWindowsCurrentID value:model.ID];
  72. if ([model.webUrl isEqualToString:Const_HomeUrl]) {
  73. // 主页
  74. HWWebViewController *vc = [[HWWebViewController alloc] init];
  75. vc.webUrl = Const_HomeUrl;
  76. UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  77. UIWindow *keyWindow = [iTools getKeyWindow];
  78. keyWindow.rootViewController = nvc;
  79. }else {
  80. // 网页
  81. HWWebViewController *vc = [[HWWebViewController alloc] init];
  82. vc.webUrl = model.webUrl;
  83. UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  84. UIWindow *keyWindow = [iTools getKeyWindow];
  85. keyWindow.rootViewController = nvc;
  86. }
  87. }
  88. #pragma mark HWPageListCellDelegate
  89. - (void)deleteBtnClickWithModel:(BaseModel *)model {
  90. NSString *ID = [NSString stringWithFormat:@"%ld", model.ID];
  91. NSString *where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"ID"),bg_sqlValue(ID)];
  92. [BaseModel bg_deleteAsync:DB_BrowserWindows_TableName where:where complete:^(BOOL isSuccess) {
  93. HLog(@"BaseModel 删除 %@", isSuccess ? @"成功":@"失败");
  94. mainBlock(^{
  95. // 刷新表格
  96. [self getData];
  97. });
  98. }];
  99. }
  100. #pragma mark 点击事件
  101. - (IBAction)backBtnClick:(UIButton *)sender {
  102. HLog(@"返回");
  103. [[NSNotificationCenter defaultCenter] postNotificationName:WebviewReloadNotification object:nil];
  104. [self.navigationController popViewControllerAnimated:YES];
  105. }
  106. - (IBAction)addBtnClick:(UIButton *)sender {
  107. NSArray *dataArray = [BaseModel bg_findAll:DB_BrowserWindows_TableName];
  108. NSInteger index = 0;
  109. for (BaseModel *model in dataArray) { // 找出ID最大的model
  110. if (model.ID > index) {
  111. index = model.ID;
  112. }
  113. }
  114. HLog(@"添加");
  115. // 浏览器当前窗口索引ID
  116. NSInteger ID = index + 1;
  117. BaseModel *model = [[BaseModel alloc] init];
  118. model.ID = ID;
  119. model.name = @"主页";
  120. // model.iconFile = imageUrl;
  121. model.webUrl = Const_HomeUrl;
  122. // 更新数据库
  123. [HWDataManager setIntegerWithKey:BrowserWindowsCurrentID value:ID];
  124. model.bg_tableName = DB_BrowserWindows_TableName;
  125. [model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  126. HLog(@"BaseModel更新: %@", isSuccess ? @"成功":@"失败");
  127. }];
  128. // 跳转到主页
  129. HWWebViewController *vc = [[HWWebViewController alloc] init];
  130. vc.webUrl = Const_HomeUrl;
  131. UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  132. UIWindow *keyWindow = [iTools getKeyWindow];
  133. keyWindow.rootViewController = nvc;
  134. }
  135. #pragma mark 懒加载
  136. - (NSMutableArray *)dataSource {
  137. if (!_dataSource) {
  138. _dataSource = [NSMutableArray array];
  139. }
  140. return _dataSource;
  141. }
  142. @end