Repository navigation
Expand file tree
/
Copy pathDownload.m
More file actions
97 lines (79 loc) · 3.21 KB
/
Copy pathDownload.m
File metadata and controls
97 lines (79 loc) · 3.21 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
#import "Download.h"
@implementation SCIDownloadDelegate
- (instancetype)initWithAction:(DownloadAction)action showProgress:(BOOL)showProgress {
self = [super init];
if (self) {
// Read-only properties
_action = action;
_showProgress = showProgress;
// Properties
self.downloadManager = [[SCIDownloadManager alloc] initWithDelegate:self];
self.hud = [[JGProgressHUD alloc] init];
}
return self;
}
- (void)downloadFileWithURL:(NSURL *)url fileExtension:(NSString *)fileExtension hudLabel:(NSString *)hudLabel {
// Show progress gui
self.hud = [[JGProgressHUD alloc] init];
self.hud.textLabel.text = hudLabel != nil ? hudLabel : @"Downloading";
if (self.showProgress) {
JGProgressHUDRingIndicatorView *indicatorView = [[JGProgressHUDRingIndicatorView alloc] init ];
indicatorView.roundProgressLine = YES;
indicatorView.ringWidth = 3.5;
self.hud.indicatorView = indicatorView;
self.hud.detailTextLabel.text = [NSString stringWithFormat:@"00%% Complete"];
// Allow dismissing longer downloads (requiring progress updates)
__weak typeof(self) weakSelf = self;
self.hud.tapOutsideBlock = ^(JGProgressHUD * _Nonnull HUD) {
[weakSelf.downloadManager cancelDownload];
};
}
[self.hud showInView:topMostController().view];
NSLog(@"[SCInsta] Download: Will start download for url \"%@\" with file extension: \".%@\"", url, fileExtension);
// Start download using manager
[self.downloadManager downloadFileWithURL:url fileExtension:fileExtension];
}
// Delegate methods
- (void)downloadDidStart {
NSLog(@"[SCInsta] Download: Download started");
}
- (void)downloadDidCancel {
dispatch_async(dispatch_get_main_queue(), ^{
[self.hud dismiss];
});
NSLog(@"[SCInsta] Download: Download cancelled");
}
- (void)downloadDidProgress:(float)progress {
NSLog(@"[SCInsta] Download: Download progress: %f", progress);
if (self.showProgress) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.hud setProgress:progress animated:false];
self.hud.detailTextLabel.text = [NSString stringWithFormat:@"%02d%% Complete", (int)(progress * 100)];
});
}
}
- (void)downloadDidFinishWithError:(NSError *)error {
dispatch_async(dispatch_get_main_queue(), ^{
// Check if it actually errored (not cancelled)
if (error && error.code != NSURLErrorCancelled) {
NSLog(@"[SCInsta] Download: Download failed with error: \"%@\"", error);
[SCIUtils showErrorHUDWithDescription:@"Error, try again later"];
}
});
}
- (void)downloadDidFinishWithFileURL:(NSURL *)fileURL {
dispatch_async(dispatch_get_main_queue(), ^{
[self.hud dismiss];
NSLog(@"[SCInsta] Download: Download finished with url: \"%@\"", [fileURL absoluteString]);
NSLog(@"[SCInsta] Download: Completed with action %d", (int)self.action);
switch (self.action) {
case share:
[SCIUtils showShareVC:fileURL];
break;
case quickLook:
[SCIUtils showQuickLookVC:@[fileURL]];
break;
}
});
}
@end