Commit a702fdf0 authored by Felix Paul Kühne's avatar Felix Paul Kühne

HTTPFileDownloader: clean-up code and implement a proper delegation system

parent fdd1e3e8
......@@ -8,13 +8,25 @@
// Refer to the COPYING file of the official project for license.
//
@class VLCMenuViewController;
@protocol VLCHTTPFileDownloader <NSObject>
@required
- (void)downloadStarted;
- (void)downloadEnded;
@optional
- (void)downloadFailedWithError:(NSError *)error;
- (void)progressUpdatedTo:(CGFloat)percentage;
@end
@interface VLCHTTPFileDownloader : NSObject
@property (nonatomic, retain) VLCMenuViewController *mediaViewController;
@property (readonly, nonatomic) NSString *userReadableDownloadName;
@property (nonatomic, readonly) BOOL downloadInProgress;
@property (nonatomic, retain) id delegate;
- (void)cancelDownload;
- (void)downloadFileFromURL:(NSURL *)url;
@end
......@@ -15,31 +15,35 @@
@interface VLCHTTPFileDownloader ()
{
VLCCircularProgressIndicator *_progressIndicator;
NSString *_filePath;
NSUInteger _expectedDownloadSize;
NSUInteger _receivedDataSize;
NSString *_fileName;
NSURLConnection *_urlConnection;
}
@end
@implementation VLCHTTPFileDownloader
- (NSString *)userReadableDownloadName
{
return _fileName;
}
- (void)downloadFileFromURL:(NSURL *)url
{
_progressIndicator = self.mediaViewController.httpDownloadProgressIndicator;
_progressIndicator.progress = 0.;
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
_filePath = [searchPaths[0] stringByAppendingPathComponent:url.lastPathComponent];
_fileName = url.lastPathComponent;
_filePath = [searchPaths[0] stringByAppendingPathComponent:_fileName];
_expectedDownloadSize = _receivedDataSize = 0;
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (!theConnection) {
_urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (!_urlConnection) {
APLog(@"failed to establish connection");
_downloadInProgress = NO;
} else {
_downloadInProgress = YES;
_progressIndicator.hidden = NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
}
......@@ -49,6 +53,7 @@
NSUInteger statusCode = [response statusCode];
if (statusCode == 200) {
_expectedDownloadSize = [response expectedContentLength];
[self.delegate downloadStarted];
APLog(@"expected download size: %i", _expectedDownloadSize);
}
}
......@@ -63,6 +68,8 @@
if (!fileHandle) {
APLog(@"file creation failed, no data was saved");
if ([self.delegate respondsToSelector:@selector(downloadFailedWithError:)])
[self.delegate downloadFailedWithError:nil];
return;
}
}
......@@ -72,7 +79,8 @@
[fileHandle writeData:data];
_receivedDataSize = _receivedDataSize + data.length;
_progressIndicator.progress = (float)_receivedDataSize / (float)_expectedDownloadSize;
if ([self.delegate respondsToSelector:@selector(progressUpdatedTo:)])
[self.delegate progressUpdatedTo: (float)_receivedDataSize / (float)_expectedDownloadSize];
}
@catch (NSException * e) {
APLog(@"exception when writing to file %@", _filePath);
......@@ -84,22 +92,28 @@
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
APLog(@"http file download complete");
_downloadInProgress = NO;
_progressIndicator.hidden = YES;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
VLCAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate updateMediaList];
[_mediaViewController dismiss:nil];
[self.delegate downloadEnded];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
APLog(@"http file download failed (%i)", error.code);
_downloadInProgress = NO;
_progressIndicator.hidden = YES;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[_mediaViewController dismiss:nil];
if ([self.delegate respondsToSelector:@selector(downloadFailedWithError:)])
[self.delegate downloadFailedWithError:error];
[self.delegate downloadEnded];
}
- (void)cancelDownload
{
[_urlConnection cancel];
}
@end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment