Repository navigation
Expand file tree
/
Copy pathFileManagerViewController.m
More file actions
1179 lines (966 loc) · 53.5 KB
/
Copy pathFileManagerViewController.m
File metadata and controls
1179 lines (966 loc) · 53.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "FileManagerViewController.h"
#import "PlistViewerViewController.h"
#import "PXRootHidePath.h"
#import <objc/runtime.h>
// Constants for operation types
typedef NS_ENUM(NSInteger, FileOperationType) {
FileOperationTypeNone = 0,
FileOperationTypeCopy = 1,
FileOperationTypeMove = 2
};
@interface FileManagerViewController () <UITableViewDelegate, UITableViewDataSource, UIDocumentInteractionControllerDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSString *currentPath;
@property (nonatomic, strong) NSArray *directoryContents;
@property (nonatomic, strong) NSFileManager *fileManager;
@property (nonatomic, strong) UIBarButtonItem *navigationBackButton;
@property (nonatomic, strong) UIDocumentInteractionController *documentController;
@property (nonatomic, strong) UILabel *emptyDirectoryLabel;
@property (nonatomic, strong) UIRefreshControl *refreshControl;
// Properties for file operations
@property (nonatomic, strong) NSString *sourceFilePath;
@property (nonatomic, strong) NSArray<NSString *> *sourceFilePaths;
@property (nonatomic, assign) FileOperationType operationType;
@property (nonatomic, strong) UIBarButtonItem *pasteButton;
// Properties for multiple selection
@property (nonatomic, assign) BOOL isMultipleSelectionMode;
@property (nonatomic, strong) NSMutableArray<NSString *> *selectedFilePaths;
@property (nonatomic, strong) UIBarButtonItem *selectButton;
@property (nonatomic, strong) UIBarButtonItem *cancelSelectButton;
@property (nonatomic, strong) UIBarButtonItem *actionButton;
@property (nonatomic, strong) UIBarButtonItem *selectAllButton;
@end
@implementation FileManagerViewController
- (instancetype)initWithPath:(NSString *)path {
return [self initWithPath:path sourceFilePath:nil operationType:FileOperationTypeNone];
}
- (instancetype)initWithPath:(NSString *)path sourceFilePath:(NSString *)sourceFilePath operationType:(NSInteger)operationType {
self = [super init];
if (self) {
_currentPath = path ? path : PXJBRootPath(@"/");
_fileManager = [NSFileManager defaultManager];
_sourceFilePath = sourceFilePath;
_operationType = operationType;
_selectedFilePaths = [NSMutableArray array];
}
return self;
}
- (instancetype)initWithPath:(NSString *)path sourceFilePaths:(NSArray<NSString *> *)sourceFilePaths operationType:(NSInteger)operationType {
self = [super init];
if (self) {
_currentPath = path ? path : PXJBRootPath(@"/");
_fileManager = [NSFileManager defaultManager];
_sourceFilePaths = sourceFilePaths;
_operationType = operationType;
_selectedFilePaths = [NSMutableArray array];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor systemBackgroundColor];
// Configure navigation bar
self.title = [self displayNameForPath:self.currentPath];
// Add close button
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(closeButtonTapped)];
self.navigationItem.leftBarButtonItem = closeButton;
// Add navigation back button if not at root
if (![self.currentPath isEqualToString:PXJBRootPath(@"/")]) {
self.navigationBackButton = [[UIBarButtonItem alloc] initWithImage:[UIImage systemImageNamed:@"arrow.up.doc"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(navigateToParentDirectory)];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:@selector(refreshDirectory)];
self.navigationItem.rightBarButtonItems = @[refreshButton, self.navigationBackButton];
} else {
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:@selector(refreshDirectory)];
self.navigationItem.rightBarButtonItem = refreshButton;
}
// Add selection button if not in copy/move mode
if (self.operationType == FileOperationTypeNone) {
// Create select button
self.selectButton = [[UIBarButtonItem alloc] initWithTitle:@"Select"
style:UIBarButtonItemStylePlain
target:self
action:@selector(toggleSelectionMode)];
NSMutableArray *rightBarItems = [NSMutableArray arrayWithArray:self.navigationItem.rightBarButtonItems ?: @[]];
[rightBarItems addObject:self.selectButton];
self.navigationItem.rightBarButtonItems = rightBarItems;
}
// Add paste button if we're in copy/move mode
if ((self.sourceFilePath || self.sourceFilePaths.count > 0) &&
(self.operationType == FileOperationTypeCopy || self.operationType == FileOperationTypeMove)) {
// Update the title based on operation type
NSString *operationTitle = (self.operationType == FileOperationTypeCopy) ? @"Copy Here" : @"Move Here";
UIBarButtonItem *operationButton = [[UIBarButtonItem alloc] initWithTitle:operationTitle
style:UIBarButtonItemStylePlain
target:self
action:@selector(pasteFile)];
NSMutableArray *rightBarItems = [NSMutableArray arrayWithArray:self.navigationItem.rightBarButtonItems ?: @[]];
[rightBarItems addObject:operationButton];
self.navigationItem.rightBarButtonItems = rightBarItems;
// Set title to indicate operation
NSString *promptText;
if (self.sourceFilePath) {
NSString *fileName = [self.sourceFilePath lastPathComponent];
promptText = [NSString stringWithFormat:@"%@ %@",
(self.operationType == FileOperationTypeCopy) ? @"Copy" : @"Move",
fileName];
} else {
promptText = [NSString stringWithFormat:@"%@ %lu files",
(self.operationType == FileOperationTypeCopy) ? @"Copy" : @"Move",
(unsigned long)self.sourceFilePaths.count];
}
self.navigationItem.prompt = promptText;
}
// Configure table view
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleInsetGrouped];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.allowsMultipleSelection = NO; // Will be toggled in selection mode
[self.view addSubview:self.tableView];
// Add refresh control
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:@selector(refreshDirectory) forControlEvents:UIControlEventValueChanged];
self.tableView.refreshControl = self.refreshControl;
// Empty directory label
self.emptyDirectoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 80)];
self.emptyDirectoryLabel.text = @"This directory is empty";
self.emptyDirectoryLabel.textColor = [UIColor secondaryLabelColor];
self.emptyDirectoryLabel.textAlignment = NSTextAlignmentCenter;
self.emptyDirectoryLabel.font = [UIFont systemFontOfSize:16];
self.emptyDirectoryLabel.hidden = YES;
[self.view addSubview:self.emptyDirectoryLabel];
// Add swipe gesture recognizer for navigation
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; // Right swipe to go back
[self.view addGestureRecognizer:swipeGesture];
// Load directory contents
[self loadDirectoryContents];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// Center empty directory label
self.emptyDirectoryLabel.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2 - 40);
}
#pragma mark - Directory Management
- (void)loadDirectoryContents {
NSError *error = nil;
NSArray *contents = [self.fileManager contentsOfDirectoryAtPath:self.currentPath error:&error];
if (error) {
NSLog(@"Error loading directory contents: %@", error.localizedDescription);
[self showAlertWithTitle:@"Error" message:[NSString stringWithFormat:@"Could not load contents of directory: %@", error.localizedDescription]];
self.directoryContents = @[];
} else {
// Sort contents: directories first, then files alphabetically
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES];
contents = [contents sortedArrayUsingDescriptors:@[sortDescriptor]];
NSMutableArray *sortedContents = [NSMutableArray array];
NSMutableArray *directories = [NSMutableArray array];
NSMutableArray *files = [NSMutableArray array];
for (NSString *item in contents) {
if ([item hasPrefix:@"."]) continue; // Skip hidden files
NSString *fullPath = [self.currentPath stringByAppendingPathComponent:item];
BOOL isDirectory = NO;
if ([self.fileManager fileExistsAtPath:fullPath isDirectory:&isDirectory]) {
if (isDirectory) {
[directories addObject:item];
} else {
[files addObject:item];
}
}
}
[sortedContents addObjectsFromArray:directories];
[sortedContents addObjectsFromArray:files];
self.directoryContents = [sortedContents copy];
}
[self.tableView reloadData];
self.emptyDirectoryLabel.hidden = self.directoryContents.count > 0;
}
- (void)navigateToDirectory:(NSString *)directoryName {
NSString *newPath = [self.currentPath stringByAppendingPathComponent:directoryName];
// Create new view controller, preserving operation state
FileManagerViewController *fileManagerVC;
if (self.sourceFilePaths.count > 0) {
fileManagerVC = [[FileManagerViewController alloc] initWithPath:newPath
sourceFilePaths:self.sourceFilePaths
operationType:self.operationType];
} else {
fileManagerVC = [[FileManagerViewController alloc] initWithPath:newPath
sourceFilePath:self.sourceFilePath
operationType:self.operationType];
}
[self.navigationController pushViewController:fileManagerVC animated:YES];
}
- (void)navigateToParentDirectory {
NSString *parentPath = [self.currentPath stringByDeletingLastPathComponent];
if ([parentPath hasPrefix:PXJBRootPath(@"/")]) {
// Create new view controller, preserving operation state
FileManagerViewController *fileManagerVC;
if (self.sourceFilePaths.count > 0) {
fileManagerVC = [[FileManagerViewController alloc] initWithPath:parentPath
sourceFilePaths:self.sourceFilePaths
operationType:self.operationType];
} else {
fileManagerVC = [[FileManagerViewController alloc] initWithPath:parentPath
sourceFilePath:self.sourceFilePath
operationType:self.operationType];
}
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
if (viewControllers.count > 1) {
[viewControllers removeLastObject];
}
[viewControllers addObject:fileManagerVC];
[self.navigationController setViewControllers:viewControllers animated:YES];
}
}
- (void)refreshDirectory {
[self loadDirectoryContents];
[self.refreshControl endRefreshing];
}
#pragma mark - Table View Delegate/DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.directoryContents.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"FileCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
NSString *itemName = self.directoryContents[indexPath.row];
NSString *fullPath = [self.currentPath stringByAppendingPathComponent:itemName];
BOOL isDirectory = NO;
[self.fileManager fileExistsAtPath:fullPath isDirectory:&isDirectory];
// Configure cell
cell.textLabel.text = itemName;
// Get file attributes
NSError *error = nil;
NSDictionary *attributes = [self.fileManager attributesOfItemAtPath:fullPath error:&error];
NSString *fileSize = @"";
if (!error) {
if (!isDirectory) {
fileSize = [self formattedFileSize:[attributes fileSize]];
}
NSDate *modDate = [attributes fileModificationDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@%@",
fileSize,
[dateFormatter stringFromDate:modDate]];
} else {
cell.detailTextLabel.text = @"";
}
// Set icon
NSString *fileExtension = @"";
if (isDirectory) {
cell.imageView.image = [UIImage systemImageNamed:@"folder.fill"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else {
fileExtension = [itemName pathExtension];
NSString *iconName = [self iconNameForFileExtension:fileExtension];
cell.imageView.image = [UIImage systemImageNamed:iconName];
cell.accessoryType = UITableViewCellAccessoryDetailButton;
}
// Set tint color based on file type
if (isDirectory) {
cell.imageView.tintColor = [UIColor systemBlueColor];
} else {
UIColor *tintColor = [self colorForFileExtension:fileExtension];
cell.imageView.tintColor = tintColor;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *selectedItem = self.directoryContents[indexPath.row];
NSString *fullPath = [self.currentPath stringByAppendingPathComponent:selectedItem];
BOOL isDirectory = NO;
if ([self.fileManager fileExistsAtPath:fullPath isDirectory:&isDirectory]) {
if (isDirectory) {
[self navigateToDirectory:selectedItem];
} else {
// Preview file
[self previewFileAtPath:fullPath];
}
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
NSString *selectedItem = self.directoryContents[indexPath.row];
NSString *fullPath = [self.currentPath stringByAppendingPathComponent:selectedItem];
[self showFileActionsForPath:fullPath];
}
- (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point API_AVAILABLE(ios(13.0)) {
NSString *selectedItem = self.directoryContents[indexPath.row];
NSString *fullPath = [self.currentPath stringByAppendingPathComponent:selectedItem];
return [UIContextMenuConfiguration configurationWithIdentifier:nil
previewProvider:nil
actionProvider:^UIMenu * _Nullable(NSArray<UIMenuElement *> * _Nonnull suggestedActions) {
// Get file attributes
BOOL isDirectory = NO;
[self.fileManager fileExistsAtPath:fullPath isDirectory:&isDirectory];
NSMutableArray *actions = [NSMutableArray array];
if (isDirectory) {
[actions addObject:[UIAction actionWithTitle:@"Open"
image:[UIImage systemImageNamed:@"folder"]
identifier:nil
handler:^(__kindof UIAction * _Nonnull action) {
[self navigateToDirectory:selectedItem];
}]];
} else {
[actions addObject:[UIAction actionWithTitle:@"Preview"
image:[UIImage systemImageNamed:@"eye"]
identifier:nil
handler:^(__kindof UIAction * _Nonnull action) {
[self previewFileAtPath:fullPath];
}]];
}
[actions addObject:[UIAction actionWithTitle:@"Info"
image:[UIImage systemImageNamed:@"info.circle"]
identifier:nil
handler:^(__kindof UIAction * _Nonnull action) {
[self showFileInfoForPath:fullPath];
}]];
// Copy action
[actions addObject:[UIAction actionWithTitle:@"Copy"
image:[UIImage systemImageNamed:@"doc.on.doc"]
identifier:nil
handler:^(__kindof UIAction * _Nonnull action) {
FileManagerViewController *fileManagerVC = [[FileManagerViewController alloc] initWithPath:self.currentPath
sourceFilePath:fullPath
operationType:FileOperationTypeCopy];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:fileManagerVC];
navController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:navController animated:YES completion:nil];
}]];
// Move action
[actions addObject:[UIAction actionWithTitle:@"Move"
image:[UIImage systemImageNamed:@"folder.badge.plus"]
identifier:nil
handler:^(__kindof UIAction * _Nonnull action) {
FileManagerViewController *fileManagerVC = [[FileManagerViewController alloc] initWithPath:self.currentPath
sourceFilePath:fullPath
operationType:FileOperationTypeMove];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:fileManagerVC];
navController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:navController animated:YES completion:nil];
}]];
// Rename action
[actions addObject:[UIAction actionWithTitle:@"Rename"
image:[UIImage systemImageNamed:@"pencil"]
identifier:nil
handler:^(__kindof UIAction * _Nonnull action) {
[self renameItemAtPath:fullPath];
}]];
[actions addObject:[UIAction actionWithTitle:@"Share"
image:[UIImage systemImageNamed:@"square.and.arrow.up"]
identifier:nil
handler:^(__kindof UIAction * _Nonnull action) {
[self shareItemAtPath:fullPath];
}]];
// Add delete action
UIAction *deleteAction = [UIAction actionWithTitle:@"Delete"
image:[UIImage systemImageNamed:@"trash"]
identifier:nil
handler:^(__kindof UIAction * _Nonnull action) {
[self confirmDeleteItemAtPath:fullPath];
}];
deleteAction.attributes = UIMenuElementAttributesDestructive;
[actions addObject:deleteAction];
return [UIMenu menuWithTitle:@"" children:actions];
}];
}
#pragma mark - Helper Methods
- (NSString *)iconNameForFileExtension:(NSString *)extension {
// Default icon
NSString *iconName = @"doc.fill";
// Define icons for common file types
NSDictionary *extensionToIcon = @{
@"jpg": @"photo.fill",
@"jpeg": @"photo.fill",
@"png": @"photo.fill",
@"gif": @"photo.fill",
@"heic": @"photo.fill",
@"mp4": @"video.fill",
@"mov": @"video.fill",
@"mp3": @"music.note",
@"m4a": @"music.note",
@"wav": @"music.note",
@"pdf": @"doc.text.fill",
@"txt": @"doc.text.fill",
@"rtf": @"doc.text.fill",
@"html": @"doc.text.fill",
@"plist": @"list.bullet",
@"zip": @"archivebox.fill",
@"tar": @"archivebox.fill",
@"gz": @"archivebox.fill",
@"deb": @"shippingbox.fill",
@"ipa": @"app.badge.fill",
@"app": @"app.fill",
@"dylib": @"puzzlepiece.fill",
@"bundle": @"cube.box.fill",
};
NSString *matchedIcon = extensionToIcon[extension.lowercaseString];
if (matchedIcon) {
iconName = matchedIcon;
}
return iconName;
}
- (UIColor *)colorForFileExtension:(NSString *)extension {
// Default color
UIColor *color = [UIColor systemGrayColor];
// Define colors for common file types
NSDictionary *extensionToColor = @{
@"jpg": [UIColor systemPinkColor],
@"jpeg": [UIColor systemPinkColor],
@"png": [UIColor systemPinkColor],
@"gif": [UIColor systemPinkColor],
@"heic": [UIColor systemPinkColor],
@"mp4": [UIColor systemPurpleColor],
@"mov": [UIColor systemPurpleColor],
@"mp3": [UIColor systemRedColor],
@"m4a": [UIColor systemRedColor],
@"wav": [UIColor systemRedColor],
@"pdf": [UIColor systemOrangeColor],
@"txt": [UIColor systemGreenColor],
@"rtf": [UIColor systemGreenColor],
@"html": [UIColor systemGreenColor],
@"plist": [UIColor systemTealColor],
@"zip": [UIColor systemIndigoColor],
@"tar": [UIColor systemIndigoColor],
@"gz": [UIColor systemIndigoColor],
@"deb": [UIColor systemOrangeColor],
@"ipa": [UIColor systemBlueColor],
@"app": [UIColor systemBlueColor],
@"dylib": [UIColor systemYellowColor],
@"bundle": [UIColor systemBlueColor],
};
UIColor *matchedColor = extensionToColor[extension.lowercaseString];
if (matchedColor) {
color = matchedColor;
}
return color;
}
- (NSString *)displayNameForPath:(NSString *)path {
if ([path isEqualToString:PXJBRootPath(@"/")]) {
return @"Root";
}
return [path lastPathComponent];
}
- (NSString *)formattedFileSize:(unsigned long long)size {
NSByteCountFormatter *formatter = [[NSByteCountFormatter alloc] init];
formatter.countStyle = NSByteCountFormatterCountStyleFile;
NSString *formattedSize = [formatter stringFromByteCount:size];
return [NSString stringWithFormat:@"%@ • ", formattedSize];
}
#pragma mark - File Actions
- (void)previewFileAtPath:(NSString *)path {
// If it's a plist file, use our custom plist viewer
if ([[path pathExtension].lowercaseString isEqualToString:@"plist"]) {
PlistViewerViewController *plistVC = [[PlistViewerViewController alloc] initWithPlistPath:path];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:plistVC];
navController.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentViewController:navController animated:YES completion:nil];
return;
}
// Check if file is too large
NSError *error = nil;
NSDictionary *attributes = [self.fileManager attributesOfItemAtPath:path error:&error];
if (!error && attributes) {
unsigned long long fileSize = [attributes fileSize];
// Don't try to preview files larger than 10MB
if (fileSize > 10 * 1024 * 1024) {
[self showAlertWithTitle:@"File Too Large"
message:@"This file is too large to preview directly. You can share it or view its information."];
return;
}
}
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];
self.documentController.delegate = self;
BOOL success = [self.documentController presentPreviewAnimated:YES];
if (!success) {
[self showAlertWithTitle:@"Cannot Preview"
message:@"This file type cannot be previewed."];
}
}
- (void)showFileActionsForPath:(NSString *)path {
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
BOOL isDirectory = NO;
[self.fileManager fileExistsAtPath:path isDirectory:&isDirectory];
// Preview action (for files only)
if (!isDirectory) {
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Preview"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
[self previewFileAtPath:path];
}]];
}
// Info action
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Info"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
[self showFileInfoForPath:path];
}]];
// Copy action
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Copy"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
// Create new file manager to browse for destination
FileManagerViewController *fileManagerVC = [[FileManagerViewController alloc] initWithPath:self.currentPath
sourceFilePath:path
operationType:FileOperationTypeCopy];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:fileManagerVC];
navController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:navController animated:YES completion:nil];
}]];
// Move action
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Move"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
// Create new file manager to browse for destination
FileManagerViewController *fileManagerVC = [[FileManagerViewController alloc] initWithPath:self.currentPath
sourceFilePath:path
operationType:FileOperationTypeMove];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:fileManagerVC];
navController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:navController animated:YES completion:nil];
}]];
// Rename action
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Rename"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
[self renameItemAtPath:path];
}]];
// Share action
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Share"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
[self shareItemAtPath:path];
}]];
// Delete action
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Delete"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * _Nonnull action) {
[self confirmDeleteItemAtPath:path];
}]];
// Cancel action
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:nil]];
// For iPad compatibility
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]];
actionSheet.popoverPresentationController.sourceView = cell;
actionSheet.popoverPresentationController.sourceRect = cell.bounds;
}
// Present action sheet
[self presentViewController:actionSheet animated:YES completion:nil];
}
- (void)showFileInfoForPath:(NSString *)path {
NSError *error = nil;
NSDictionary *attributes = [self.fileManager attributesOfItemAtPath:path error:&error];
if (error) {
[self showAlertWithTitle:@"Error" message:[NSString stringWithFormat:@"Could not get file information: %@", error.localizedDescription]];
return;
}
BOOL isDirectory = NO;
[self.fileManager fileExistsAtPath:path isDirectory:&isDirectory];
NSString *name = [path lastPathComponent];
NSString *type = isDirectory ? @"Directory" : @"File";
NSString *extension = [path pathExtension];
NSDate *creationDate = [attributes fileCreationDate];
NSDate *modificationDate = [attributes fileModificationDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *creationDateStr = [dateFormatter stringFromDate:creationDate];
NSString *modificationDateStr = [dateFormatter stringFromDate:modificationDate];
NSString *size = @"--";
if (!isDirectory) {
NSByteCountFormatter *formatter = [[NSByteCountFormatter alloc] init];
formatter.countStyle = NSByteCountFormatterCountStyleFile;
size = [formatter stringFromByteCount:[attributes fileSize]];
}
NSString *permissions = [NSString stringWithFormat:@"%@ (%lo)",
[attributes fileType],
(unsigned long)[attributes filePosixPermissions]];
NSString *message = [NSString stringWithFormat:
@"Name: %@\n"
@"Type: %@\n"
@"%@"
@"Size: %@\n"
@"Created: %@\n"
@"Modified: %@\n"
@"Permissions: %@\n"
@"Path: %@",
name,
type,
extension.length > 0 ? [NSString stringWithFormat:@"Extension: %@\n", extension] : @"",
size,
creationDateStr,
modificationDateStr,
permissions,
path];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"File Information"
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)shareItemAtPath:(NSString *)path {
NSURL *fileURL = [NSURL fileURLWithPath:path];
NSArray *activityItems = @[fileURL];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
// For iPad
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
activityVC.popoverPresentationController.sourceView = self.view;
activityVC.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2, 0, 0);
}
[self presentViewController:activityVC animated:YES completion:nil];
}
- (void)confirmDeleteItemAtPath:(NSString *)path {
NSString *message = [NSString stringWithFormat:@"Are you sure you want to delete '%@'? This action cannot be undone.", [path lastPathComponent]];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Confirm Delete"
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
[self deleteItemAtPath:path];
}]];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)deleteItemAtPath:(NSString *)path {
NSError *error = nil;
BOOL success = [self.fileManager removeItemAtPath:path error:&error];
if (!success) {
[self showAlertWithTitle:@"Delete Failed" message:[NSString stringWithFormat:@"Could not delete item: %@", error.localizedDescription]];
} else {
[self loadDirectoryContents];
}
}
#pragma mark - Document Interaction Controller Delegate
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
#pragma mark - Navigation
- (void)closeButtonTapped {
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Alerts
- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
// Handle swipe gesture for navigation
- (void)handleSwipeGesture:(UISwipeGestureRecognizer *)gesture {
if (gesture.direction == UISwipeGestureRecognizerDirectionRight) {
// Only navigate back if not at root
if (![self.currentPath isEqualToString:PXJBRootPath(@"/")]) {
[self navigateToParentDirectory];
}
}
}
- (void)pasteFile {
if (!self.sourceFilePath && self.sourceFilePaths.count == 0) {
return;
}
// Handle single file operation
if (self.sourceFilePath) {
NSString *sourceFileName = [self.sourceFilePath lastPathComponent];
NSString *destinationPath = [self.currentPath stringByAppendingPathComponent:sourceFileName];
// Check if destination file already exists
if ([self.fileManager fileExistsAtPath:destinationPath]) {
// Ask for confirmation to overwrite
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"File Already Exists"
message:[NSString stringWithFormat:@"'%@' already exists at this location. Do you want to replace it?", sourceFileName]
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"Replace" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
[self performFileOperation];
}]];
[self presentViewController:alert animated:YES completion:nil];
} else {
[self performFileOperation];
}
}
// Handle multiple files operation
else if (self.sourceFilePaths.count > 0) {
// Check if any destination files already exist
BOOL filesExist = NO;
NSMutableArray *existingFiles = [NSMutableArray array];
for (NSString *sourcePath in self.sourceFilePaths) {
NSString *sourceFileName = [sourcePath lastPathComponent];
NSString *destinationPath = [self.currentPath stringByAppendingPathComponent:sourceFileName];
if ([self.fileManager fileExistsAtPath:destinationPath]) {
filesExist = YES;
[existingFiles addObject:sourceFileName];
}
}
if (filesExist) {
// Ask for confirmation to overwrite
NSString *existingFilesList = [existingFiles componentsJoinedByString:@", "];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Files Already Exist"
message:[NSString stringWithFormat:@"The following files already exist: %@. Do you want to replace them?", existingFilesList]
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"Replace All" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
[self performFileOperation];
}]];
[self presentViewController:alert animated:YES completion:nil];
} else {
[self performFileOperation];
}
}
}
- (void)performFileOperation {
NSError *error = nil;
BOOL success = YES;
NSString *destinationPath;
// Handle single file operation
if (self.sourceFilePath) {
NSString *sourceFileName = [self.sourceFilePath lastPathComponent];
destinationPath = [self.currentPath stringByAppendingPathComponent:sourceFileName];
if (self.operationType == FileOperationTypeCopy) {
// Perform copy operation
success = [self.fileManager copyItemAtPath:self.sourceFilePath toPath:destinationPath error:&error];
} else if (self.operationType == FileOperationTypeMove) {
// Perform move operation
success = [self.fileManager moveItemAtPath:self.sourceFilePath toPath:destinationPath error:&error];
}
}
// Handle multiple files operation
else if (self.sourceFilePaths.count > 0) {
for (NSString *sourcePath in self.sourceFilePaths) {
NSString *sourceFileName = [sourcePath lastPathComponent];
destinationPath = [self.currentPath stringByAppendingPathComponent:sourceFileName];
NSError *singleError = nil;
BOOL singleSuccess = YES;
if (self.operationType == FileOperationTypeCopy) {
// Perform copy operation
singleSuccess = [self.fileManager copyItemAtPath:sourcePath toPath:destinationPath error:&singleError];
} else if (self.operationType == FileOperationTypeMove) {
// Perform move operation
singleSuccess = [self.fileManager moveItemAtPath:sourcePath toPath:destinationPath error:&singleError];
}
if (!singleSuccess) {
success = NO;
error = singleError;
break;
}
}
}
if (success) {
// Operation was successful
NSString *message = (self.operationType == FileOperationTypeCopy) ? @"Files copied successfully." : @"Files moved successfully.";
[self showAlertWithTitle:@"Success" message:message];
// Reset operation state and reload directory
self.sourceFilePath = nil;
self.sourceFilePaths = nil;
self.operationType = FileOperationTypeNone;
self.navigationItem.prompt = nil;
// Update paste button
NSMutableArray *rightBarItems = [NSMutableArray arrayWithArray:self.navigationItem.rightBarButtonItems];
if (rightBarItems.count > 0) {
[rightBarItems removeLastObject];
self.navigationItem.rightBarButtonItems = rightBarItems;
}
[self loadDirectoryContents];
} else {
// Operation failed
[self showAlertWithTitle:@"Operation Failed" message:[NSString stringWithFormat:@"Error: %@", error.localizedDescription]];
}
}
- (void)renameItemAtPath:(NSString *)path {
NSString *fileName = [path lastPathComponent];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Rename"
message:[NSString stringWithFormat:@"Enter new name for '%@'", fileName]
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.text = fileName;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
}];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"Rename" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *textField = alert.textFields.firstObject;
NSString *newName = textField.text;
if (newName.length == 0 || [newName isEqualToString:fileName]) {
return;
}
NSString *newPath = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];
NSError *error = nil;
BOOL success = [self.fileManager moveItemAtPath:path toPath:newPath error:&error];
if (success) {
[self loadDirectoryContents];
} else {
[self showAlertWithTitle:@"Rename Failed" message:[NSString stringWithFormat:@"Error: %@", error.localizedDescription]];
}
}]];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)toggleSelectionMode {
self.isMultipleSelectionMode = !self.isMultipleSelectionMode;
if (self.isMultipleSelectionMode) {
// Clear selection
[self.selectedFilePaths removeAllObjects];
// Create cancel button
self.cancelSelectButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(toggleSelectionMode)];