PDFListController.m 4.0 KB

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