FileListViewController.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // ViewController.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "FileListViewController.h"
  11. #import "PDFViewController.h"
  12. #import "PDFSettingsViewController.h"
  13. static const CGFloat TUpdateTimeInternal = 2.0;
  14. @interface FileListViewController () <UITableViewDelegate, UITableViewDataSource>
  15. @property (nonatomic,retain) NSMutableArray<NSString *> *fileArray;
  16. @property (nonatomic,retain) NSTimer *updateFolderTimer;
  17. @property (nonatomic,retain) UITableView *tableView;
  18. @end
  19. @implementation FileListViewController
  20. #pragma mark - Init Methods
  21. - (id)init {
  22. if (self = [super init]) {
  23. }
  24. return self;
  25. }
  26. - (void)dealloc {
  27. _tableView.delegate = nil;
  28. _tableView.dataSource = nil;
  29. [_tableView release];
  30. if ([_updateFolderTimer isValid]){
  31. [_updateFolderTimer invalidate];
  32. }
  33. [_updateFolderTimer release];
  34. [_fileArray release];
  35. [_path release];
  36. [super dealloc];
  37. }
  38. #pragma mark - UIViewController Methods
  39. - (void)viewDidLoad {
  40. [super viewDidLoad];
  41. // Do any additional setup after loading the view.
  42. if (!self.path) {
  43. self.path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  44. }
  45. self.title = [self.path lastPathComponent];
  46. [self loadSamplesFiles];
  47. self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain] autorelease];
  48. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  49. self.tableView.delegate = self;
  50. self.tableView.dataSource = self;
  51. [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
  52. [self.view addSubview:self.tableView];
  53. UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Settings", nil)
  54. style:UIBarButtonItemStylePlain
  55. target:self
  56. action:@selector(buttonItemClicked_Settings)];
  57. self.navigationItem.rightBarButtonItem = item;
  58. [item release];
  59. }
  60. - (void)viewWillAppear:(BOOL)animated {
  61. [super viewWillAppear:animated];
  62. self.navigationController.navigationBar.translucent = NO;
  63. // 开启定时器
  64. [self registTimer];
  65. // 刷新数据
  66. [self reloadFileManagerData];
  67. }
  68. - (void)viewWillDisappear:(BOOL)animated {
  69. [super viewWillDisappear:animated];
  70. // 关闭定时器
  71. [self unregistTimer];
  72. }
  73. - (void)buttonItemClicked_Settings {
  74. PDFSettingsViewController *vc = [[PDFSettingsViewController alloc] init];
  75. [self.navigationController pushViewController:vc animated:YES];
  76. [vc release];
  77. }
  78. - (void)loadSamplesFiles {
  79. NSURL *samplesURL = [NSBundle.mainBundle.resourceURL URLByAppendingPathComponent:@"Samples"];
  80. NSString *docsFolder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
  81. NSArray *tFileNames = [NSArray arrayWithObjects:@"Form_Widgets_Test.pdf", @"PDF32000_2008.pdf", @"Quick Start Guide.pdf", @"Quick Start Guide.docx", nil];
  82. NSFileManager *fileManager = [NSFileManager defaultManager];
  83. for ( int i = 0; i < [tFileNames count]; i++) {
  84. NSURL *tBFilePath = [samplesURL URLByAppendingPathComponent:[tFileNames objectAtIndex:i]];
  85. NSURL *tDFilePathName = [NSURL fileURLWithPath:[docsFolder stringByAppendingPathComponent:[tFileNames objectAtIndex:i]]];
  86. if (![fileManager fileExistsAtPath:tDFilePathName.path]) {
  87. if ([fileManager fileExistsAtPath:tBFilePath.path]) {
  88. [fileManager copyItemAtURL:tBFilePath toURL:tDFilePathName error:nil];
  89. }
  90. }
  91. }
  92. }
  93. #pragma mark - Private Methods
  94. - (void)registTimer {
  95. self.updateFolderTimer = [NSTimer timerWithTimeInterval:TUpdateTimeInternal
  96. target:self
  97. selector:@selector(updateFileManagerData)
  98. userInfo:nil
  99. repeats:YES];
  100. [[NSRunLoop currentRunLoop] addTimer:self.updateFolderTimer forMode:NSDefaultRunLoopMode];
  101. }
  102. - (void)unregistTimer {
  103. if ([self.updateFolderTimer isValid]){
  104. [self.updateFolderTimer invalidate];
  105. }
  106. self.updateFolderTimer = nil;
  107. }
  108. - (void)updateFileManagerData {
  109. [self reloadFileManagerData];
  110. }
  111. - (void)reloadFileManagerData {
  112. [self searchFiles];
  113. [self.tableView reloadData];
  114. }
  115. - (void)searchFiles {
  116. NSMutableArray *folders = [NSMutableArray array];
  117. NSMutableArray *files = [NSMutableArray array];
  118. NSURL *url = [NSURL fileURLWithPath:self.path];
  119. NSFileManager *fileManager = [NSFileManager defaultManager];
  120. NSArray *contents = [fileManager contentsOfDirectoryAtURL:url
  121. includingPropertiesForKeys:nil
  122. options:(NSDirectoryEnumerationSkipsSubdirectoryDescendants |
  123. NSDirectoryEnumerationSkipsPackageDescendants |
  124. NSDirectoryEnumerationSkipsHiddenFiles)
  125. error:nil];
  126. for (int i=0; i<[contents count]; i++) {
  127. NSString *filePath = [[contents objectAtIndex:i] path];
  128. BOOL isDirectory = NO;
  129. [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
  130. if (isDirectory) {
  131. [folders addObject:filePath];
  132. } else {
  133. NSString *pathExtension = [filePath.pathExtension lowercaseString];
  134. if ([pathExtension isEqualToString:@"pdf"] ||
  135. [pathExtension isEqualToString:@"docx"]) {
  136. [files addObject:filePath];
  137. }
  138. }
  139. }
  140. [self.fileArray removeAllObjects];
  141. self.fileArray = [NSMutableArray array];
  142. [self.fileArray addObjectsFromArray:folders];
  143. [self.fileArray addObjectsFromArray:files];
  144. }
  145. #pragma mark - UITableViewDataSource
  146. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  147. return self.fileArray.count;
  148. }
  149. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  150. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  151. NSString *filePath = self.fileArray[indexPath.row];
  152. BOOL isDirectory = NO;
  153. [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
  154. cell.textLabel.text = [filePath lastPathComponent];
  155. return cell;
  156. }
  157. #pragma mark - UITableViewDelegate
  158. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  159. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  160. NSString *filePath = self.fileArray[indexPath.row];
  161. BOOL isDirectory = NO;
  162. [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
  163. if (isDirectory) {
  164. FileListViewController *vc = [[FileListViewController alloc] init];
  165. vc.path = filePath;
  166. [self.navigationController pushViewController:vc animated:YES];
  167. [vc release];
  168. } else {
  169. if ([filePath.pathExtension isEqualToString:@"pdf"]) {
  170. // Initializers, Make sure you enter the local document path in PDF format
  171. PDFViewController *vc = [[PDFViewController alloc] initWithFilePath:filePath];
  172. [self.navigationController pushViewController:vc animated:YES];
  173. [vc release];
  174. } else {
  175. UIActivityIndicatorView *aiView = [[[UIActivityIndicatorView alloc] init] autorelease];
  176. aiView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
  177. aiView.color = [UIColor grayColor];
  178. [aiView sizeToFit];
  179. aiView.center = self.view.center;
  180. aiView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  181. [self.view addSubview:aiView];
  182. [aiView startAnimating];
  183. self.view.userInteractionEnabled = NO;
  184. NSString *fileName = [[filePath.lastPathComponent stringByDeletingPathExtension] stringByAppendingPathExtension:@"pdf"];
  185. NSString *pdfFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
  186. NSFileManager *fileManager = [NSFileManager defaultManager];
  187. if ([fileManager fileExistsAtPath:pdfFilePath]) {
  188. [fileManager removeItemAtPath:pdfFilePath error:nil];
  189. }
  190. // Convert files (doc, docx, xls, xlsx, ppt, pptx, txt, jpeg, and png) to PDF (offline)
  191. [[CPDFKit sharedInstance] convertFilePath:filePath toPath:pdfFilePath completion:^(BOOL result) {
  192. self.view.userInteractionEnabled = YES;
  193. [aiView removeFromSuperview];
  194. if (result) {
  195. PDFViewController *vc = [[PDFViewController alloc] initWithFilePath:pdfFilePath];
  196. [self.navigationController pushViewController:vc animated:YES];
  197. [vc release];
  198. }
  199. }];
  200. }
  201. }
  202. }
  203. @end