forked from TimOliver/TOSMBClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTORootViewController.m
153 lines (117 loc) · 4.65 KB
/
TORootViewController.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
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
//
// TORootViewController.m
// TOSMBClientExample
//
// Created by Tim Oliver on 8/10/15.
// Copyright © 2015 TimOliver. All rights reserved.
//
#import "TORootViewController.h"
#import "TORootTableViewController.h"
#import "TOSMBClient.h"
@interface TORootViewController () <TOSMBSessionDownloadTaskDelegate, UIDocumentInteractionControllerDelegate>
@property (nonatomic, strong) UIDocumentInteractionController *docController;
@property (nonatomic, strong) TOSMBSessionDownloadTask *downloadTask;
@property (nonatomic, strong) NSString *filePath;
@end
@implementation TORootViewController
#pragma mark - Properties
- (TOSMBSession *)session {
if (!_session) {
_session = [[TOSMBSession alloc] init];
}
return _session;
}
#pragma mark - View Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.noticeLabel.hidden = NO;
self.downloadView.hidden = YES;
}
#pragma mark - Actions
- (IBAction)addButtonTapped:(id)sender
{
TORootTableViewController *tableController = [[TORootTableViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:tableController];
controller.modalPresentationStyle = UIModalPresentationFormSheet;
tableController.rootController = self;
tableController.session = self.session;
[self presentViewController:controller animated:YES completion:nil];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(modalCancelButtonTapped:)];
tableController.navigationItem.rightBarButtonItem = item;
}
- (IBAction)suspendButtonTapped:(id)sender
{
if (self.downloadTask.state == TOSMBSessionTaskStateRunning) {
[self.downloadTask suspend];
[self.suspendButton setTitle:@"Resume" forState:UIControlStateNormal];
}
else {
[self.downloadTask resume];
[self.suspendButton setTitle:@"Suspend" forState:UIControlStateNormal];
}
}
- (IBAction)cancelButtonTapped:(id)sender
{
if (self.downloadTask.state != TOSMBSessionTaskStateCancelled) {
[self.downloadTask cancel];
self.cancelButton.enabled = NO;
self.progressView.progress = 0.0f;
[self.suspendButton setTitle:@"Resume" forState:UIControlStateNormal];
}
}
- (IBAction)actionButtonTapped:(id)sender
{
self.docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:self.filePath]];
self.docController.delegate = self;
[self.docController presentOpenInMenuFromBarButtonItem:self.navigationItem.rightBarButtonItem animated:YES];
}
- (void)modalCancelButtonTapped:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Logic
- (void)downloadFileFromSession:(TOSMBSession *)session atFilePath:(NSString *)filePath
{
self.noticeLabel.hidden = YES;
self.downloadView.hidden = NO;
self.fileNameLabel.text = [filePath lastPathComponent];
self.progressView.progress = 0.0f;
self.cancelButton.hidden = NO;
self.suspendButton.hidden = NO;
self.progressView.alpha = 1.0f;
self.session = session;
self.downloadTask = [session downloadTaskForFileAtPath:filePath destinationPath:nil delegate:self];
[self dismissViewControllerAnimated:YES completion:^{
[self.downloadTask resume];
}];
}
#pragma mark - Helpers
- (void)updateUiToDownloadFinishedState
{
self.cancelButton.hidden = YES;
self.suspendButton.hidden = YES;
self.progressView.alpha = 0.5f;
self.navigationItem.rightBarButtonItem.enabled = YES;
}
#pragma mark - TOSMBSessionDownloadTaskDelegate
- (void)downloadTask:(TOSMBSessionDownloadTask *)downloadTask didWriteBytes:(uint64_t)bytesWritten totalBytesReceived:(uint64_t)totalBytesReceived totalBytesExpectedToReceive:(int64_t)totalBytesToReceive
{
self.progressView.progress = (float)totalBytesReceived / (float)totalBytesToReceive;
}
- (void)downloadTask:(TOSMBSessionDownloadTask *)downloadTask didFinishDownloadingToPath:(NSString *)destinationPath
{
[self updateUiToDownloadFinishedState];
self.filePath = destinationPath;
}
- (void)downloadTask:(TOSMBSessionDownloadTask *)downloadTask didCompleteWithError:(NSError *)error
{
[self updateUiToDownloadFinishedState];
[[[UIAlertView alloc] initWithTitle:@"SMB Client Error" message:error.localizedDescription delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] show];
}
#pragma mark - UIDocumentInteractionControllerDelegate
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller
{
self.docController = nil;
}
@end