From 710cbc50d72977df68020ffb32edb0ea23827f4f Mon Sep 17 00:00:00 2001 From: Chris Li Date: Fri, 26 Aug 2016 15:38:42 -0400 Subject: [PATCH] DownloadBookOperation --- Kiwix-iOS/AppDelegate.swift | 4 +- .../Library/BookDetailController.swift | 14 +++-- .../Library/DownloadTasksController.swift | 9 ++- Kiwix-iOS/Info.plist | 2 +- .../{Network.swift => Network_old.swift} | 16 ++--- Kiwix-iOS/Storyboard/Library.storyboard | 37 ++++++------ Kiwix-iOSWidgets/Bookmarks/Info.plist | 2 +- Kiwix.xcodeproj/project.pbxproj | 22 +++++-- Kiwix/Network/Network.swift | 40 +++++++++++++ Kiwix/Operations/DownloadBookOperation.swift | 40 +++++-------- Kiwix/Operations/UIOperations.swift | 14 ----- .../URLSessionDownloadTaskOperation.swift | 60 +++++++++++-------- 12 files changed, 150 insertions(+), 110 deletions(-) rename Kiwix-iOS/Model/{Network.swift => Network_old.swift} (93%) create mode 100644 Kiwix/Network/Network.swift diff --git a/Kiwix-iOS/AppDelegate.swift b/Kiwix-iOS/AppDelegate.swift index b24288f8..da915ac1 100644 --- a/Kiwix-iOS/AppDelegate.swift +++ b/Kiwix-iOS/AppDelegate.swift @@ -27,7 +27,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { NSURLProtocol.registerClass(KiwixURLProtocol) - Network.shared.restoreProgresses() +// Network.shared.restoreProgresses() // Register notification let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil) // Here are the notification permission the app wants @@ -132,7 +132,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } func application(application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: () -> Void) { - Network.shared.rejoinSessionWithIdentifier(identifier, completionHandler: completionHandler) +// Network.shared.rejoinSessionWithIdentifier(identifier, completionHandler: completionHandler) } // MARK: Background Refresh diff --git a/Kiwix-iOS/Controller/Library/BookDetailController.swift b/Kiwix-iOS/Controller/Library/BookDetailController.swift index aa0a852f..e4f98a4c 100644 --- a/Kiwix-iOS/Controller/Library/BookDetailController.swift +++ b/Kiwix-iOS/Controller/Library/BookDetailController.swift @@ -80,19 +80,23 @@ class BookDetailController: UITableViewController, CenterButtonCellDelegate, DZN switch title { case Strings.downloadNow: + func startDownload() { + guard let bookID = book.id, + let download = DownloadBookOperation(bookID: bookID) else {return} + Network.shared.queue.addOperation(download) + } + if book.spaceState == .Caution { let cancel = UIAlertAction(title: Strings.cancel, style: .Cancel, handler: nil) let download = UIAlertAction(title: Strings.SpaceAlert.downloadAnyway, style: .Destructive, handler: { (alert) in - + startDownload() }) let alertController = UIAlertController(title: Strings.SpaceAlert.spaceAlert, message: Strings.SpaceAlert.message, actions: [download, cancel]) presentViewController(alertController, animated: true, completion: nil) } else { - + startDownload() } -// guard let download = DownloadBookOperation(book: book) else {return} -// GlobalOperationQueue.sharedInstance.addOperation(download) - default: + default: return } } diff --git a/Kiwix-iOS/Controller/Library/DownloadTasksController.swift b/Kiwix-iOS/Controller/Library/DownloadTasksController.swift index e9c90c85..7aea84b9 100644 --- a/Kiwix-iOS/Controller/Library/DownloadTasksController.swift +++ b/Kiwix-iOS/Controller/Library/DownloadTasksController.swift @@ -47,8 +47,7 @@ class DownloadTasksController: UITableViewController, NSFetchedResultsController } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { - let cellIdentifier = "Cell" - let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) + let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) self.configureCell(cell, atIndexPath: indexPath) return cell } @@ -60,9 +59,9 @@ class DownloadTasksController: UITableViewController, NSFetchedResultsController cell.titleLabel.text = book.title cell.favIcon.image = UIImage(data: book.favIcon ?? NSData()) - - guard let progress = Network.shared.progresses[id] else {return} - cell.progressView.progress = Float(progress.fractionCompleted) +// +// guard let progress = Network.shared.progresses[id] else {return} +// cell.progressView.progress = Float(progress.fractionCompleted) // switch downloadTask.state { // case .Queued, .Downloading: diff --git a/Kiwix-iOS/Info.plist b/Kiwix-iOS/Info.plist index ab6f9a01..f059fd22 100644 --- a/Kiwix-iOS/Info.plist +++ b/Kiwix-iOS/Info.plist @@ -49,7 +49,7 @@ CFBundleVersion - 1.7.1326 + 1.7.1353 ITSAppUsesNonExemptEncryption LSRequiresIPhoneOS diff --git a/Kiwix-iOS/Model/Network.swift b/Kiwix-iOS/Model/Network_old.swift similarity index 93% rename from Kiwix-iOS/Model/Network.swift rename to Kiwix-iOS/Model/Network_old.swift index 81d9c298..fe811b01 100644 --- a/Kiwix-iOS/Model/Network.swift +++ b/Kiwix-iOS/Model/Network_old.swift @@ -14,7 +14,7 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate, NSU weak var delegate: DownloadProgressReporting? private let context = NSManagedObjectContext.mainQueueContext - let operationQueue = OperationQueue() + let queue = OperationQueue() var progresses = [String: DownloadProgress]() private var timer: NSTimer? @@ -30,7 +30,7 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate, NSU override init() { super.init() - operationQueue.delegate = self + queue.delegate = self } func restoreProgresses() { @@ -44,7 +44,7 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate, NSU let operation = URLSessionDownloadTaskOperation(downloadTask: task) operation.name = task.taskDescription operation.addObserver(NetworkObserver()) - self.operationQueue.addOperation(operation) + self.queue.addOperation(operation) } } } @@ -77,13 +77,13 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate, NSU func pause(book: Book) { guard let id = book.id, - let operation = operationQueue.getOperation(id) as? URLSessionDownloadTaskOperation else {return} + let operation = queue.getOperation(id) as? URLSessionDownloadTaskOperation else {return} operation.cancel(produceResumeData: true) } func cancel(book: Book) { guard let id = book.id, - let operation = operationQueue.getOperation(id) as? URLSessionDownloadTaskOperation else {return} + let operation = queue.getOperation(id) as? URLSessionDownloadTaskOperation else {return} operation.cancel(produceResumeData: false) } @@ -97,7 +97,7 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate, NSU let operation = URLSessionDownloadTaskOperation(downloadTask: task) operation.name = id operation.addObserver(NetworkObserver()) - operationQueue.addOperation(operation) + queue.addOperation(operation) let progress = DownloadProgress(book: book) progress.downloadStarted(task) @@ -107,7 +107,7 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate, NSU // MARK: - OperationQueueDelegate func operationQueue(queue: OperationQueue, willAddOperation operation: NSOperation) { - guard operationQueue.operationCount == 0 else {return} + guard queue.operationCount == 0 else {return} shouldReportProgress = true NSOperationQueue.mainQueue().addOperationWithBlock { () -> Void in self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(Network.resetProgressReportingFlag), userInfo: nil, repeats: true) @@ -117,7 +117,7 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate, NSU func operationQueue(queue: OperationQueue, willFinishOperation operation: NSOperation, withErrors errors: [ErrorType]) {} func operationQueue(queue: OperationQueue, didFinishOperation operation: NSOperation, withErrors errors: [ErrorType]) { - guard operationQueue.operationCount == 1 else {return} + guard queue.operationCount == 1 else {return} NSOperationQueue.mainQueue().addOperationWithBlock { () -> Void in self.timer?.invalidate() self.shouldReportProgress = false diff --git a/Kiwix-iOS/Storyboard/Library.storyboard b/Kiwix-iOS/Storyboard/Library.storyboard index 17179f93..16245c52 100644 --- a/Kiwix-iOS/Storyboard/Library.storyboard +++ b/Kiwix-iOS/Storyboard/Library.storyboard @@ -21,7 +21,7 @@ - +