123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- //
- // CPDFController.m
- // PDFViewer
- //
- // Created by kdan on 2022/11/14.
- //
- #import <ComPDFKit/ComPDFKit.h>
- #import "PDFListController.h"
- #import "PDFModel.h"
- #import "PDFViewController.h"
- extern BOOL gImagePreviewIsFirstLayout;
- extern BOOL gTextPreviewIsFirstLayout;
- @interface PDFListController () <UITableViewDataSource,UITableViewDelegate>
- @property (nonatomic,strong) UITableView *tableView;
- @property (nonatomic,strong) NSMutableArray *dataArray;
- @end
- @implementation PDFListController
- #pragma mark - Initializers
- - (instancetype)initWithModel:(PDFModel *)model {
- if (self = [super init]) {
- [self setPath:model.filePath];
- }
- return self;
- }
- #pragma mark - Accessors
- - (NSArray *)dataArray {
- if (!_dataArray) {
-
- _dataArray = [NSMutableArray array];
-
- NSFileManager *defaultManager = [NSFileManager defaultManager];
- NSArray *fileArray = [defaultManager contentsOfDirectoryAtPath:self.path error:nil];
-
- for (NSInteger i = 0; i < fileArray.count; ++i) {
-
- if ([fileArray[i] isEqualToString:@".DS_Store"]) {
- continue;
- }
-
- NSString *subFilePath = [[self.path stringByAppendingString:@"/"] stringByAppendingString:fileArray[i]];
-
- PDFModel *model = [[PDFModel alloc] init];
- model.fileName = fileArray[i];
- model.filePath = subFilePath;
-
- [_dataArray addObject:model];
-
- }
- }
- return _dataArray;
- }
- #pragma mark - UIViewController Methods
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- // Uncomment the following line to preserve selection between presentations.
- // self.clearsSelectionOnViewWillAppear = NO;
-
- // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
- // self.navigationItem.rightBarButtonItem = self.editButtonItem;
- _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
-
- _tableView.delegate = self;
- _tableView.dataSource = self;
-
- self.tableView.rowHeight = 50;
-
- self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
-
- self.tableView.tableFooterView = [[UIView alloc] init];
-
- [self.view addSubview:self.tableView];
- }
- #pragma mark - UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.dataArray.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *MyIdentifier = @"Path";
-
- UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
-
- if (!cell) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier];
- }
-
- cell.textLabel.text = [self.dataArray[indexPath.row] fileName];
- return cell;
- }
- #pragma mark - UITableViewDelegate
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
-
- BOOL isDir = NO;
-
- NSFileManager *defaultManager = [NSFileManager defaultManager];
- [defaultManager fileExistsAtPath:[self.dataArray[indexPath.row] filePath] isDirectory:&isDir];
-
- if (isDir) {
- PDFModel *model = [[PDFModel alloc] init];
- model.filePath = [[self.dataArray[indexPath.row] filePath] stringByAppendingString:@"/"];
- PDFListController *pdfListController = [[PDFListController alloc] initWithModel:model];
- [self.navigationController pushViewController:pdfListController animated:YES];
-
- } else {
-
- PDFViewController *pdfViewcontroller = [[PDFViewController alloc] initWithPath:[self.dataArray[indexPath.row] filePath]];
- [self.navigationController pushViewController:pdfViewcontroller animated:YES];
- gImagePreviewIsFirstLayout = YES;
- gTextPreviewIsFirstLayout = YES;
- }
- }
- @end
|