Skip to content

Commit

Permalink
Merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Michelangelo Chasseur committed Oct 24, 2015
1 parent 5191e17 commit 712e2f5
Showing 1 changed file with 30 additions and 69 deletions.
99 changes: 30 additions & 69 deletions TWRDownloadManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,24 @@ - (instancetype)init {
// Default session
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
self.session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

// Background session
NSURLSessionConfiguration *backgroundConfiguration = nil;

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) {
backgroundConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[[NSBundle mainBundle] bundleIdentifier]];
}
else {
backgroundConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"re.touchwa.downloadmanager"];
}
else {
} else {
backgroundConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"re.touchwa.downloadmanager"];
}

self.backgroundSession = [NSURLSession sessionWithConfiguration:backgroundConfiguration delegate:self delegateQueue:nil];

self.downloads = [NSMutableDictionary new];
}
return self;
}

#pragma mark - Downloading...
#pragma mark - Downloading...

- (void)downloadFileForURL:(NSString *)urlString
withName:(NSString *)fileName
Expand All @@ -70,11 +66,11 @@ - (void)downloadFileForURL:(NSString *)urlString
if (!fileName) {
fileName = [urlString lastPathComponent];
}

if (!friendlyName) {
friendlyName = fileName;
}

if (![self fileDownloadCompletedForUrl:urlString]) {
NSLog(@"File is downloading!");
} else if (![self fileExistsWithName:fileName inDirectory:directory]) {
Expand Down Expand Up @@ -104,7 +100,7 @@ - (void)downloadFileForURL:(NSString *)urlString
remainingTime:(void(^)(NSUInteger seconds))remainingTimeBlock
completionBlock:(void(^)(BOOL completed))completionBlock
enableBackgroundMode:(BOOL)backgroundMode {

}

- (void)downloadFileForURL:(NSString *)url
Expand Down Expand Up @@ -187,7 +183,7 @@ - (void)cancelDownloadForUrl:(NSString *)fileIdentifier {
}
if (self.downloads.count == 0) {
[self cleanTmpDirectory];

}
}

Expand Down Expand Up @@ -227,7 +223,7 @@ - (void)URLSession:(NSURLSession *)session
}
});
}

CGFloat remainingTime = [self remainingTimeForDownload:download bytesTransferred:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite];
if (download.remainingTimeBlock) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
Expand All @@ -240,30 +236,30 @@ - (void)URLSession:(NSURLSession *)session

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
// NSLog(@"Download finisehd!");

NSError *error;
NSURL *destinationLocation;

NSString *fileIdentifier = downloadTask.originalRequest.URL.absoluteString;
TWRDownloadObject *download = [self.downloads objectForKey:fileIdentifier];

BOOL success = YES;

if ([downloadTask.response isKindOfClass:[NSHTTPURLResponse class]]) {
NSInteger statusCode = [(NSHTTPURLResponse*)downloadTask.response statusCode];
if (statusCode >= 400) {
NSLog(@"ERROR: HTTP status code %@", @(statusCode));
success = NO;
}
}

if (success) {
if (download.directoryName) {
destinationLocation = [[[self cachesDirectoryUrlPath] URLByAppendingPathComponent:download.directoryName] URLByAppendingPathComponent:download.fileName];
} else {
destinationLocation = [[self cachesDirectoryUrlPath] URLByAppendingPathComponent:download.fileName];
}

// Move downloaded item from tmp directory to te caches directory
// (not synced with user's iCloud documents)
[[NSFileManager defaultManager] moveItemAtURL:location
Expand All @@ -279,10 +275,10 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
download.completionBlock(success);
});
}

// remove object from the download
[self.downloads removeObjectForKey:fileIdentifier];

dispatch_async(dispatch_get_main_queue(), ^{
// Show a local notification when download is over.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
Expand All @@ -291,22 +287,6 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
});
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
NSString *fileIdentifier = task.originalRequest.URL.absoluteString;
TWRDownloadObject *download = [self.downloads objectForKey:fileIdentifier];
if (error) {
if (download.completionBlock) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
download.completionBlock(!error);
});
}

// remove object from the download
[self.downloads removeObjectForKey:fileIdentifier];
}
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
if (error) {
Expand All @@ -320,26 +300,7 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp
download.completionBlock(NO);
});
}

// remove object from the download
[self.downloads removeObjectForKey:fileIdentifier];
}
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
if (error) {
NSLog(@"ERROR: %@", error);

NSString *fileIdentifier = task.originalRequest.URL.absoluteString;
TWRDownloadObject *download = [self.downloads objectForKey:fileIdentifier];

if (download.completionBlock) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
download.completionBlock(NO);
});
}

// remove object from the download
[self.downloads removeObjectForKey:fileIdentifier];
}
Expand All @@ -361,7 +322,7 @@ - (BOOL)createDirectoryNamed:(NSString *)directory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
NSString *targetDirectory = [cachesDirectory stringByAppendingPathComponent:directory];

NSError *error;
return [[NSFileManager defaultManager] createDirectoryAtPath:targetDirectory
withIntermediateDirectories:YES
Expand Down Expand Up @@ -439,15 +400,15 @@ - (BOOL)fileExistsForUrl:(NSString *)urlString inDirectory:(NSString *)directory
- (BOOL)fileExistsWithName:(NSString *)fileName
inDirectory:(NSString *)directoryName {
BOOL exists = NO;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];

// if no directory was provided, we look by default in the base cached dir
if ([[NSFileManager defaultManager] fileExistsAtPath:[[cachesDirectory stringByAppendingPathComponent:directoryName] stringByAppendingPathComponent:fileName]]) {
exists = YES;
}

return exists;
}

Expand All @@ -472,20 +433,20 @@ - (BOOL)deleteFileWithName:(NSString *)fileName {
- (BOOL)deleteFileWithName:(NSString *)fileName
inDirectory:(NSString *)directoryName {
BOOL deleted = NO;

NSError *error;
NSURL *fileLocation;
if (directoryName) {
fileLocation = [[[self cachesDirectoryUrlPath] URLByAppendingPathComponent:directoryName] URLByAppendingPathComponent:fileName];
} else {
fileLocation = [[self cachesDirectoryUrlPath] URLByAppendingPathComponent:fileName];
}


// Move downloaded item from tmp directory to te caches directory
// (not synced with user's iCloud documents)
[[NSFileManager defaultManager] removeItemAtURL:fileLocation error:&error];

if (error) {
deleted = NO;
NSLog(@"Error deleting file: %@", error);
Expand Down Expand Up @@ -521,17 +482,17 @@ - (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
if (self.backgroundTransferCompletionHandler != nil) {
// Copy locally the completion handler.
void(^completionHandler)() = self.backgroundTransferCompletionHandler;

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// Call the completion handler to tell the system that there are no other background transfers.
completionHandler();

// Show a local notification when all downloads are over.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = @"All files have been downloaded!";
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}];

// Make nil the backgroundTransferCompletionHandler.
self.backgroundTransferCompletionHandler = nil;
}
Expand Down

0 comments on commit 712e2f5

Please sign in to comment.