forked from TimOliver/TOSMBClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTOFilesTableViewController.m
125 lines (98 loc) · 4.53 KB
/
TOFilesTableViewController.m
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
//
// TOFilesViewControllerTableViewController.m
// TOSMBClientExample
//
// Created by Tim Oliver on 8/5/15.
// Copyright (c) 2015 TimOliver. All rights reserved.
//
#import "TOFilesTableViewController.h"
#import "TOSMBClient.h"
#import "TORootViewController.h"
@interface TOFilesTableViewController ()
@property (nonatomic, copy) NSString *directoryTitle;
@property (nonatomic, strong) TOSMBSession *session;
@property (nonatomic, strong) TOSMBSessionUploadTask *uploadTask;
@end
@implementation TOFilesTableViewController
- (instancetype)initWithSession:(TOSMBSession *)session title:(NSString *)title
{
if (self = [super initWithStyle:UITableViewStylePlain]) {
_directoryTitle = title;
_session = session;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"Loading...";
if (self.path.length) {
UIBarButtonItem *uploadButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(upload:)];
self.navigationItem.rightBarButtonItems = @[self.navigationItem.rightBarButtonItems.firstObject, uploadButton];
}
}
- (void)upload:(id)sender {
self.navigationItem.rightBarButtonItems.lastObject.enabled = NO;
NSString *path = [[self.path stringByAppendingPathComponent:[NSUUID UUID].UUIDString] stringByAppendingPathExtension:@"txt"];
NSData *data = [path dataUsingEncoding:NSUTF8StringEncoding];
__weak typeof(self) weakSelf = self;
self.uploadTask = [self.session uploadTaskForFileAtPath:path data:data progressHandler:nil completionHandler:^{
[weakSelf reloadData];
weakSelf.navigationItem.rightBarButtonItems.lastObject.enabled = YES;
} failHandler:^(NSError *error) {
weakSelf.navigationItem.rightBarButtonItems.lastObject.enabled = YES;
}];
[self.uploadTask resume];
}
- (void)reloadData {
__weak typeof(self) weakSelf = self;
[self.session requestContentsOfDirectoryAtFilePath:self.path success:^(NSArray *files) {
weakSelf.files = files;
[weakSelf.tableView reloadData];
} error:^(NSError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SMB Client Error" message:error.localizedDescription delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
}];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.files.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
TOSMBSessionFile *file = self.files[indexPath.row];
cell.textLabel.text = file.name;
cell.detailTextLabel.text = file.directory ? @"Directory" : [NSString stringWithFormat:@"File | Size: %ld", (long)file.fileSize];
cell.accessoryType = file.directory ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
TOSMBSessionFile *file = self.files[indexPath.row];
if (file.directory == NO) {
[self.rootController downloadFileFromSession:self.session atFilePath:file.filePath];
return;
}
TOFilesTableViewController *controller = [[TOFilesTableViewController alloc] initWithSession:self.session title:file.name];
controller.rootController = self.rootController;
controller.path = file.filePath;
controller.navigationItem.rightBarButtonItems = self.navigationItem.rightBarButtonItems;
[self.navigationController pushViewController:controller animated:YES];
[self.session requestContentsOfDirectoryAtFilePath:file.filePath success:^(NSArray *files) {
controller.files = files;
} error:^(NSError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SMB Client Error" message:error.localizedDescription delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
}];
}
- (void)setFiles:(NSArray <TOSMBSessionFile *> *)files
{
_files = files;
self.navigationItem.title = self.directoryTitle;
[self.tableView reloadData];
}
@end