CPDFListController.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // CPDFController.m
  3. // PDFViewer
  4. //
  5. // Created by kdan on 2022/11/14.
  6. //
  7. #import <ComPDFKit/ComPDFKit.h>
  8. #import "CPDFListController.h"
  9. #import "CPDFModel.h"
  10. #import "CPDFViewController.h"
  11. extern BOOL gImagePreviewIsFirstLayout;
  12. extern BOOL gTextPreviewIsFirstLayout;
  13. @interface CPDFListController () <UITableViewDataSource,UITableViewDelegate>
  14. @property (nonatomic,strong) UITableView *tableView;
  15. @property (nonatomic,strong) NSMutableArray *dataArray;
  16. @end
  17. @implementation CPDFListController
  18. - (instancetype)initWithModel:(CPDFModel *)model {
  19. if (self = [super init]) {
  20. [self setPath:model.filePath];
  21. }
  22. return self;
  23. }
  24. - (NSArray *)dataArray {
  25. if (!_dataArray) {
  26. _dataArray = [NSMutableArray array];
  27. NSFileManager *defaultManager = [NSFileManager defaultManager];
  28. NSArray *fileArray = [defaultManager contentsOfDirectoryAtPath:self.path error:nil];
  29. for (NSInteger i = 0; i < fileArray.count; ++i) {
  30. if ([fileArray[i] isEqualToString:@".DS_Store"]) {
  31. continue;
  32. }
  33. NSString *subFilePath = [[self.path stringByAppendingString:@"/"] stringByAppendingString:fileArray[i]];
  34. CPDFModel *model = [[CPDFModel alloc] init];
  35. model.fileName = fileArray[i];
  36. model.filePath = subFilePath;
  37. [_dataArray addObject:model];
  38. }
  39. }
  40. return _dataArray;
  41. }
  42. - (void)viewDidLoad {
  43. [super viewDidLoad];
  44. // Uncomment the following line to preserve selection between presentations.
  45. // self.clearsSelectionOnViewWillAppear = NO;
  46. // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  47. // self.navigationItem.rightBarButtonItem = self.editButtonItem;
  48. [self setTableView:[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]];
  49. [self.tableView setDataSource:self];
  50. [self.tableView setDelegate:self];
  51. self.tableView.rowHeight = 50;
  52. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  53. self.tableView.tableFooterView = [[UIView alloc] init];
  54. [self.view addSubview:self.tableView];
  55. }
  56. #pragma mark - UITableViewDataSource
  57. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  58. return self.dataArray.count;
  59. }
  60. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  61. static NSString *MyIdentifier = @"Path";
  62. UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
  63. if (!cell) {
  64. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier];
  65. }
  66. cell.textLabel.text = [self.dataArray[indexPath.row] fileName];
  67. return cell;
  68. }
  69. #pragma mark - UITableViewDelegate
  70. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  71. BOOL isDir = NO;
  72. NSFileManager *defaultManager = [NSFileManager defaultManager];
  73. [defaultManager fileExistsAtPath:[self.dataArray[indexPath.row] filePath] isDirectory:&isDir];
  74. if (isDir) {
  75. CPDFModel *model = [[CPDFModel alloc] init];
  76. model.filePath = [[self.dataArray[indexPath.row] filePath] stringByAppendingString:@"/"];
  77. CPDFListController *pdfListController = [[CPDFListController alloc] initWithModel:model];
  78. [self.navigationController pushViewController:pdfListController animated:YES];
  79. } else {
  80. CPDFViewController *pdfViewcontroller = [[CPDFViewController alloc] initWithPath:[self.dataArray[indexPath.row] filePath]];
  81. [self.navigationController pushViewController:pdfViewcontroller animated:YES];
  82. gImagePreviewIsFirstLayout = YES;
  83. gTextPreviewIsFirstLayout = YES;
  84. }
  85. }
  86. @end