diff --git a/Kiwix-iOS/Controller/Library/DownloadTasksController.swift b/Kiwix-iOS/Controller/Library/DownloadTasksController.swift index d421c695..102ca681 100644 --- a/Kiwix-iOS/Controller/Library/DownloadTasksController.swift +++ b/Kiwix-iOS/Controller/Library/DownloadTasksController.swift @@ -18,7 +18,7 @@ class DownloadTasksController: UITableViewController, NSFetchedResultsController required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) - tabBarItem.title = LocalizedStrings.LibraryTabTitle.download + tabBarItem.title = LocalizedStrings.download tabBarItem.image = UIImage(named: "Download") tabBarItem.selectedImage = UIImage(named: "DownloadFilled") } @@ -188,29 +188,29 @@ class DownloadTasksController: UITableViewController, NSFetchedResultsController case .Downloading: let pause = UITableViewRowAction(style: .Normal, title: "Pause") { (action, indexPath) in let operation = PauseBookDwonloadOperation(bookID: bookID) - GlobalQueue.shared.addOperation(operation) + Network.shared.queue.addOperation(operation) tableView.setEditing(false, animated: true) } actions.insert(pause, atIndex: 0) case .Paused: let resume = UITableViewRowAction(style: .Normal, title: "Resume") { (action, indexPath) in let operation = ResumeBookDwonloadOperation(bookID: bookID) - GlobalQueue.shared.addOperation(operation) + Network.shared.queue.addOperation(operation) tableView.setEditing(false, animated: true) } actions.insert(resume, atIndex: 0) case .Error: - let retry = UITableViewRowAction(style: .Normal, title: "Restart") { (action, indexPath) in + let restart = UITableViewRowAction(style: .Normal, title: "Restart") { (action, indexPath) in let operation = ResumeBookDwonloadOperation(bookID: bookID) - GlobalQueue.shared.addOperation(operation) + Network.shared.queue.addOperation(operation) tableView.setEditing(false, animated: true) } - actions.insert(retry, atIndex: 0) + actions.insert(restart, atIndex: 0) default: break } - let cancel = UITableViewRowAction(style: .Destructive, title: LocalizedStrings.Common.cancel) { (action, indexPath) -> Void in + let cancel = UITableViewRowAction(style: .Destructive, title: LocalizedStrings.cancel) { (action, indexPath) -> Void in if let bookID = downloadTask.book?.id { if let operation = Network.shared.operations[bookID] { // When download is ongoing @@ -289,4 +289,12 @@ class DownloadTasksController: UITableViewController, NSFetchedResultsController tableView.endUpdates() //refreshTabBarBadgeCount() } + + // MARK: - LocalizedStrings + + class LocalizedStrings { + static let download = NSLocalizedString("Download", comment: "Library, download tab") + } + + } diff --git a/Kiwix-iOS/Info.plist b/Kiwix-iOS/Info.plist index 9f83679e..2f84c5f5 100644 --- a/Kiwix-iOS/Info.plist +++ b/Kiwix-iOS/Info.plist @@ -49,7 +49,7 @@ CFBundleVersion - 1.8.777 + 1.8.827 ITSAppUsesNonExemptEncryption LSRequiresIPhoneOS diff --git a/Kiwix-iOSWidgets/Bookmarks/Info.plist b/Kiwix-iOSWidgets/Bookmarks/Info.plist index 40a7eed8..b4d17f96 100644 --- a/Kiwix-iOSWidgets/Bookmarks/Info.plist +++ b/Kiwix-iOSWidgets/Bookmarks/Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 1.8.780 + 1.8.830 NSExtension NSExtensionMainStoryboard diff --git a/Kiwix/Network/Network.swift b/Kiwix/Network/Network.swift index 5f1fb260..9ae05d9c 100644 --- a/Kiwix/Network/Network.swift +++ b/Kiwix/Network/Network.swift @@ -50,21 +50,18 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) { if let error = error {print(error.localizedDescription)} - guard let error = error, let bookID = task.taskDescription else {return} - self.context.performBlockAndWait { - guard let book = Book.fetch(bookID, context: self.context), - let downloadTask = book.downloadTask else {return} - if let resumeData = error.userInfo[NSURLSessionDownloadTaskResumeData] as? NSData { - // If download task doesnt exist, it must mean download is cancelled by user - // DownloadTask object will have been deleted when user tap Cancel button / table row action - downloadTask.totalBytesWritten = task.countOfBytesReceived - downloadTask.state = .Paused - - // Save resume data to disk - Preference.resumeData[bookID] = resumeData - } else { - downloadTask.state = .Error - } + + let context = NSManagedObjectContext.mainQueueContext + guard let error = error, + let bookID = task.taskDescription, + let downloadTask = Book.fetch(bookID, context: context)?.downloadTask else {return} + + if let resumeData = error.userInfo[NSURLSessionDownloadTaskResumeData] as? NSData { + Preference.resumeData[bookID] = resumeData + downloadTask.state = .Paused + downloadTask.totalBytesWritten = task.countOfBytesReceived + } else { + downloadTask.state = .Error } } diff --git a/Kiwix/Operations/BookOperation.swift b/Kiwix/Operations/BookOperation.swift index 5056650c..452dd619 100644 --- a/Kiwix/Operations/BookOperation.swift +++ b/Kiwix/Operations/BookOperation.swift @@ -64,20 +64,32 @@ class DownloadBookOperation: URLSessionDownloadTaskOperation { } override func operationDidCancel() { - // Update CoreData - let context = NSManagedObjectContext.mainQueueContext - context.performBlockAndWait { - guard let bookID = self.bookID, - let book = Book.fetch(bookID, context: context) else {return} - if !self.produceResumeData {book.isLocal = false} - - guard let downloadTask = book.downloadTask else {return} - if self.produceResumeData { - downloadTask.state = .Paused - } else { - context.deleteObject(downloadTask) - } + // Not Reachable + if let error = errors.first as? Operations.ReachabilityCondition.Error where error == .NotReachable { + return } + + // Update Core Data + if produceResumeData { + let context = NSManagedObjectContext.mainQueueContext + context.performBlockAndWait({ + guard let bookID = self.bookID, + let book = Book.fetch(bookID, context: context) else {return} + book.isLocal = nil + }) + } else { + let context = NSManagedObjectContext.mainQueueContext + context.performBlockAndWait({ + guard let bookID = self.bookID, + let book = Book.fetch(bookID, context: context) else {return} + book.isLocal = false + + guard let downloadTask = book.downloadTask else {return} + context.deleteObject(downloadTask) + }) + } + + // URLSessionDelegate save resume data and update downloadTask } // MARK: - Helper @@ -192,7 +204,14 @@ class ResumeBookDwonloadOperation: Operation { override func execute() { guard let data: NSData = Preference.resumeData[bookID], - let operation = DownloadBookOperation(bookID: bookID, resumeData: data) else {return} + let operation = DownloadBookOperation(bookID: bookID, resumeData: data) else { + if let operation = DownloadBookOperation(bookID: bookID) { + produceOperation(operation) + } + + finish() + return + } Network.shared.queue.addOperation(operation) finish() } diff --git a/Kiwix/Operations/URLSessionDownloadTaskOperation.swift b/Kiwix/Operations/URLSessionDownloadTaskOperation.swift index b75cbcd9..3e0c105f 100644 --- a/Kiwix/Operations/URLSessionDownloadTaskOperation.swift +++ b/Kiwix/Operations/URLSessionDownloadTaskOperation.swift @@ -27,12 +27,11 @@ class URLSessionDownloadTaskOperation: Operation { addObserver(NetworkObserver()) addObserver(DidCancelObserver { _ in if self.produceResumeData { - downloadTask.cancelByProducingResumeData({ (data) in }) + downloadTask.cancelByProducingResumeData({ _ in }) } else { downloadTask.cancel() } - } - ) + }) } func cancel(produceResumeData produceResumeData: Bool) {