HWBookmarkViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. //
  2. // HWBookmarkViewController.m
  3. // Private-X
  4. //
  5. // Created by 余衡武 on 2022/3/24.
  6. //
  7. #import "HWBookmarkViewController.h"
  8. #import "HWFolderModel.h"
  9. #import "HWBookmarkListCell.h"
  10. #import "HWBookmarkHeader.h"
  11. #import "HWAddFolderViewController.h"
  12. #import "HWWebViewController.h"
  13. @interface HWBookmarkViewController ()<UITableViewDelegate,UITableViewDataSource,HWAddFolderViewControllerDelegate,HWBookmarkHeaderDelegate,HWBookmarkListCellDelegate>
  14. @property (weak, nonatomic) IBOutlet UIView *header;
  15. @property (weak, nonatomic) IBOutlet UIView *footer;
  16. @property (weak, nonatomic) IBOutlet UIButton *downOneBtn;
  17. @property (weak, nonatomic) IBOutlet UIButton *downTwoBtn;
  18. @property (weak, nonatomic) IBOutlet UITableView *tableView;
  19. @property (strong, nonatomic) NSMutableArray *dataSource;
  20. @end
  21. @implementation HWBookmarkViewController
  22. #pragma mark - 生命周期
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. [self drawView];
  26. [self getDataWithID:-1];
  27. }
  28. #pragma mark - UI布局
  29. - (void)drawView {
  30. [self.header mas_updateConstraints:^(MASConstraintMaker *make) {
  31. make.height.mas_equalTo(NAVIHEIGHT);
  32. }];
  33. [self.footer mas_updateConstraints:^(MASConstraintMaker *make) {
  34. make.height.mas_equalTo(TABBARHEIGHT);
  35. }];
  36. // tableView
  37. self.tableView.delegate = self;
  38. self.tableView.dataSource = self;
  39. self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
  40. [self.tableView registerNib:[UINib nibWithNibName:@"HWBookmarkListCell" bundle:nil] forCellReuseIdentifier:@"HWBookmarkListCell"];
  41. }
  42. #pragma mark - 获取数据
  43. - (void)getDataWithID:(NSInteger)folderID {
  44. [self.dataSource removeAllObjects];
  45. NSArray *dataArray = [HWFolderModel bg_findAll:DB_BookMark_TableName];
  46. if (dataArray.count == 0) {
  47. HWFolderModel *model = [[HWFolderModel alloc] init];
  48. model.ID = 0;
  49. model.name = @"Personal collection";
  50. model.bookmarkArray = [NSMutableArray array];
  51. model.isExpand = YES;
  52. model.isEditing = NO;
  53. [self.dataSource addObject:model];
  54. // 更新数据库
  55. model.bg_tableName = DB_BookMark_TableName;
  56. [model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  57. HLog(@"HWFolderModel 更新: %@", isSuccess ? @"成功":@"失败");
  58. }];
  59. }else {
  60. for (HWFolderModel *model in dataArray) {
  61. model.isExpand = NO;
  62. if (model.ID == folderID) {
  63. model.isExpand = YES;
  64. }
  65. model.isEditing = NO;
  66. [self.dataSource addObject:model];
  67. }
  68. }
  69. if (self.dataSource.count != 0) {
  70. HWFolderModel *model = self.dataSource.firstObject;
  71. model.isExpand = YES;
  72. }
  73. [self.tableView reloadData];
  74. }
  75. #pragma mark - 列表委托
  76. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  77. return self.dataSource.count;
  78. }
  79. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  80. HWFolderModel *model = self.dataSource[section];
  81. if (model.isExpand) {
  82. return model.bookmarkArray.count;
  83. }else {
  84. return 0;
  85. }
  86. }
  87. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  88. HWBookmarkListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HWBookmarkListCell" forIndexPath:indexPath];
  89. HWFolderModel *folderModel = self.dataSource[indexPath.section];
  90. HWBookmarkModel *bookmarkModel = folderModel.bookmarkArray[indexPath.row];
  91. cell.model = bookmarkModel;
  92. cell.delegate = self;
  93. return cell;
  94. }
  95. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  96. return 50;
  97. }
  98. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  99. return 50;
  100. }
  101. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  102. HWBookmarkHeader *header = [[HWBookmarkHeader alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W - 30, 50)];
  103. header.delegate = self;
  104. HWFolderModel *folderModel = self.dataSource[section];
  105. header.folderModel = folderModel;
  106. return header;
  107. }
  108. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  109. return 10;
  110. }
  111. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  112. UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W - 30, 5)];
  113. footer.backgroundColor = [UIColor hwColor:@"#101010"];
  114. return footer;
  115. }
  116. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  117. HWFolderModel *folderModel = self.dataSource[indexPath.section];
  118. HWBookmarkModel *bookmarkModel = folderModel.bookmarkArray[indexPath.row];
  119. HWWebViewController *vc = [[HWWebViewController alloc] init];
  120. vc.webUrl = bookmarkModel.webUrl;
  121. [self.navigationController pushViewController:vc animated:YES];
  122. }
  123. // UITableView分组切圆角方法
  124. - (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
  125. ///每个分组的行数
  126. NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];
  127. ///圆角大小
  128. CGSize cornerRadii = CGSizeMake(10, 10);
  129. ///切圆角位置
  130. UIRectCorner corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;
  131. if (indexPath.row == 0 && numberOfRows == 1) {
  132. ///一个为一组时,四个角都为圆角
  133. corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;
  134. }else if (indexPath.row == 0) {
  135. ///分组第一行,左上、右上角为圆角
  136. cornerRadii = CGSizeZero;
  137. }else if (indexPath.row + 1 == numberOfRows) {
  138. ///分组最后一行,左下、右下角为圆角
  139. corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;
  140. }else {
  141. ///中间的都为矩形
  142. cornerRadii = CGSizeZero;
  143. }
  144. CGRect cellRect = cell.contentView.bounds;
  145. UIBezierPath*maskPath = [UIBezierPath bezierPathWithRoundedRect:cellRect byRoundingCorners:corners cornerRadii:cornerRadii];
  146. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  147. maskLayer.frame = cellRect;
  148. maskLayer.path = maskPath.CGPath;
  149. cell.contentView.layer.mask = maskLayer;
  150. }
  151. #pragma mark - HWBookmarkHeaderDelegate
  152. - (void)bookmarkHeaderDidClickExpandBtn:(HWFolderModel *)model {
  153. HLog(@"展开-收缩");
  154. for (HWFolderModel *folderModel in self.dataSource) {
  155. if (folderModel.ID == model.ID) {
  156. folderModel.isExpand = model.isExpand;
  157. break;
  158. }
  159. }
  160. [self.tableView reloadData];
  161. }
  162. - (void)bookmarkHeaderDidClickDeleteBtn:(HWFolderModel *)model {
  163. HLog(@"删除");
  164. NSString *ID = [NSString stringWithFormat:@"%ld", model.ID];
  165. NSString *where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"ID"),bg_sqlValue(ID)];
  166. [HWFolderModel bg_deleteAsync:DB_BookMark_TableName where:where complete:^(BOOL isSuccess) {
  167. HLog(@"HWFolderModel 删除:%d", isSuccess);
  168. mainBlock(^{
  169. if (isSuccess) {
  170. [[iToast makeText:NSLocalizedString(@"folder_deleted_successfully",nil)] show];
  171. // 刷新列表
  172. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  173. [self getDataWithID:-1];
  174. });
  175. }else {
  176. [[iToast makeText:NSLocalizedString(@"folder_deleted_failed",nil)] show];
  177. }
  178. });
  179. }];
  180. }
  181. #pragma mark - HWBookmarkListCellDelegate
  182. - (void)bookmarkListCellDeleteBtnClick:(HWBookmarkModel *)model {
  183. NSString *ID = [NSString stringWithFormat:@"%ld", model.folderID];
  184. NSString *where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"ID"),bg_sqlValue(ID)];
  185. NSArray *dataArray = [HWFolderModel bg_find:DB_BookMark_TableName where:where];
  186. if (dataArray.count == 0) {
  187. HLog(@"删除失败");
  188. return;
  189. }
  190. HWFolderModel *folderModel = dataArray.firstObject;
  191. for (HWBookmarkModel *itemModel in folderModel.bookmarkArray) {
  192. if (itemModel.ID == model.ID) {
  193. [folderModel.bookmarkArray removeObject:itemModel];
  194. [folderModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  195. HLog(@"HWFolderModel 更新: %@", isSuccess ? @"成功":@"失败");
  196. }];
  197. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  198. [self getDataWithID:folderModel.ID];
  199. });
  200. break;
  201. }
  202. }
  203. }
  204. #pragma mark - 点击事件
  205. - (IBAction)backBtnClick:(UIButton *)sender {
  206. [self.navigationController popViewControllerAnimated:YES];
  207. }
  208. - (IBAction)downOneBtnClick:(UIButton *)sender {
  209. NSString *title = sender.currentTitle;
  210. HLog(@"%@", title);
  211. if ([title isEqualToString:NSLocalizedString(@"edit_folder",nil)]) {
  212. HLog(@"编辑文件夹");
  213. [self setModelTwo];
  214. [self setFolderEditing];
  215. }else {
  216. HLog(@"新建文件夹");
  217. HWAddFolderViewController *vc = [[HWAddFolderViewController alloc] init];
  218. vc.delegate = self;
  219. [self presentViewController:vc animated:YES completion:^{
  220. }];
  221. }
  222. }
  223. - (IBAction)downTwoBtnClick:(UIButton *)sender {
  224. NSString *title = sender.currentTitle;
  225. HLog(@"%@", title);
  226. if ([title isEqualToString:@"编辑书签"]) {
  227. HLog(@"编辑书签");
  228. [self setModelTwo];
  229. [self setBookmarkEditing];
  230. }else {
  231. HLog(@"完成");
  232. [self setModelOne];
  233. [self setBookmarkNormal];
  234. }
  235. }
  236. - (void)setModelOne {
  237. [self.downOneBtn setTitle:@"编辑文件夹" forState:(UIControlStateNormal)];
  238. [self.downTwoBtn setTitle:@"编辑书签" forState:(UIControlStateNormal)];
  239. }
  240. - (void)setModelTwo {
  241. [self.downOneBtn setTitle:@"新建文件夹" forState:(UIControlStateNormal)];
  242. [self.downTwoBtn setTitle:@"完成" forState:(UIControlStateNormal)];
  243. }
  244. - (void)setBookmarkNormal {
  245. for (HWFolderModel *model in self.dataSource) { // 全部展开
  246. model.isEditing = NO;
  247. for (HWBookmarkModel *model1 in model.bookmarkArray) {
  248. model1.isEditing = NO;
  249. }
  250. }
  251. if (self.dataSource.count != 0) {
  252. HWFolderModel *model = self.dataSource.firstObject;
  253. model.isExpand = YES;
  254. }
  255. [self.tableView reloadData];
  256. }
  257. - (void)setBookmarkEditing {
  258. for (HWFolderModel *model in self.dataSource) { // 全部展开
  259. model.isEditing = NO;
  260. for (HWBookmarkModel *model1 in model.bookmarkArray) {
  261. model1.isEditing = YES;
  262. }
  263. }
  264. if (self.dataSource.count != 0) {
  265. HWFolderModel *model = self.dataSource.firstObject;
  266. model.isExpand = YES;
  267. }
  268. [self.tableView reloadData];
  269. }
  270. - (void)setFolderEditing {
  271. for (HWFolderModel *model in self.dataSource) { // 全部展开
  272. model.isEditing = YES;
  273. model.isExpand = NO;
  274. for (HWBookmarkModel *model1 in model.bookmarkArray) {
  275. model1.isEditing = NO;
  276. }
  277. }
  278. [self.tableView reloadData];
  279. }
  280. #pragma mark - HWAddFolderViewControllerDelegate
  281. - (void)addFolderWithName:(NSString *)name {
  282. HLog(@"添加文件夹:%@", name);
  283. NSString *where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"name"),bg_sqlValue(name)];
  284. NSArray *dataArray = [HWFolderModel bg_find:DB_BookMark_TableName where:where];
  285. if (dataArray.count != 0) {
  286. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  287. [[iToast makeText:@"已存在同名文件夹"] show];
  288. });
  289. return;
  290. }
  291. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  292. [self addFolderUseName:name];
  293. });
  294. }
  295. - (void)addFolderUseName:(NSString *)name {
  296. [[iToast makeText:@"成功添加文件夹"] show];
  297. NSArray *dataArray = [HWFolderModel bg_findAll:DB_BookMark_TableName];
  298. // 保存文件夹
  299. NSInteger ID = 0;
  300. for (HWFolderModel *model in dataArray) { // 找出ID最大的model
  301. if (model.ID > ID) {
  302. ID = model.ID;
  303. }
  304. }
  305. HWFolderModel *model = [[HWFolderModel alloc] init];
  306. model.ID = ID + 1;
  307. model.name = name;
  308. model.bookmarkArray = [NSMutableArray array];
  309. model.isExpand = NO;
  310. model.isEditing = NO;
  311. [self.dataSource addObject:model];
  312. // 更新数据库
  313. model.bg_tableName = DB_BookMark_TableName;
  314. [model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  315. HLog(@"HWFolderModel 更新: %@", isSuccess ? @"成功":@"失败");
  316. }];
  317. // 刷新列表
  318. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  319. [self getDataWithID:-1];
  320. });
  321. [self setBookmarkNormal];
  322. }
  323. #pragma mark - 懒加载
  324. - (NSMutableArray *)dataSource {
  325. if (!_dataSource) {
  326. _dataSource = [NSMutableArray array];
  327. }
  328. return _dataSource;
  329. }
  330. @end