This commit is contained in:
Chris Li 2016-09-19 10:03:21 -04:00
parent 82de6d0707
commit f22415e3c9
8 changed files with 42 additions and 58 deletions

View File

@ -45,22 +45,22 @@ class BookDetailController: UITableViewController, DZNEmptyDataSetSource, DZNEmp
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
configureViews()
book?.addObserver(self, forKeyPath: "isLocal", options: .New, context: context)
// book?.addObserver(self, forKeyPath: "isLocal", options: .New, context: context)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
book?.removeObserver(self, forKeyPath: "isLocal", context: context)
// book?.removeObserver(self, forKeyPath: "isLocal", context: context)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
guard let book = object as? Book where context == self.context else {return}
NSOperationQueue.mainQueue().addOperationWithBlock {
print(book.isLocal)
self.configureActionSection(book)
self.tableView.reloadSections(NSIndexSet(index: 1), withRowAnimation: .Fade)
}
}
// override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
// guard let book = object as? Book where context == self.context else {return}
// NSOperationQueue.mainQueue().addOperationWithBlock {
// print(book.isLocal)
// self.configureActionSection(book)
// self.tableView.reloadSections(NSIndexSet(index: 1), withRowAnimation: .Fade)
// }
// }
// MARK: - Configure
@ -90,10 +90,10 @@ class BookDetailController: UITableViewController, DZNEmptyDataSetSource, DZNEmp
cellTitles.append([String]())
// Action Cells
sectionHeaders.append(nil)
sectionFooters.append(nil)
cellTitles.append([])
configureActionSection(book)
// sectionHeaders.append(nil)
// sectionFooters.append(nil)
// cellTitles.append([])
// configureActionSection(book)
// Book Info
sectionHeaders.append(Strings.bookInfo)

View File

@ -39,17 +39,12 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
tabBarController?.navigationItem.rightBarButtonItem = UIBarButtonItem(imageNamed: "LanguageFilter", target: self, action: #selector(CloudBooksController.showLanguageFilter))
if fetchedResultController.delegate !== self {
fetchedResultController.delegate = self
tableView.reloadData()
}
refreshAutomatically()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
tabBarController?.navigationItem.rightBarButtonItem = nil
fetchedResultController.delegate = nil
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
@ -262,6 +257,10 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel
header.textLabel?.font = UIFont.boldSystemFontOfSize(14)
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}

View File

@ -37,17 +37,12 @@ class DownloadTasksController: UITableViewController, NSFetchedResultsController
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(DownloadTasksController.refreshProgress), userInfo: nil, repeats: true)
if fetchedResultController.delegate !== self {
fetchedResultController.delegate = self
tableView.reloadData()
}
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
timer?.invalidate()
timer = nil
fetchedResultController.delegate = nil
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
@ -174,6 +169,10 @@ class DownloadTasksController: UITableViewController, NSFetchedResultsController
// header.textLabel?.font = UIFont.boldSystemFontOfSize(14)
// }
//
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
@ -200,19 +199,13 @@ class DownloadTasksController: UITableViewController, NSFetchedResultsController
tableView.setEditing(false, animated: true)
}
actions.insert(resume, atIndex: 0)
//
// if let book = downloadTask.book,
// let resumeData = Preference.resumeData[book.id] {
//
// } else {
// let restart = UITableViewRowAction(style: .Normal, title: "Restart") { (action, indexPath) in
// guard let bookID = downloadTask.book?.id,
// let operation = DownloadBookOperation(bookID: bookID) else {return}
// Network.shared.queue.addOperation(operation)
// tableView.setEditing(false, animated: true)
// }
// actions.insert(restart, atIndex: 0)
// }
case .Error:
let retry = UITableViewRowAction(style: .Normal, title: "Restart") { (action, indexPath) in
let operation = ResumeBookDwonloadOperation(bookID: bookID)
GlobalQueue.shared.addOperation(operation)
tableView.setEditing(false, animated: true)
}
actions.insert(retry, atIndex: 0)
default:
break
}
@ -294,7 +287,6 @@ class DownloadTasksController: UITableViewController, NSFetchedResultsController
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
refreshTabBarBadgeCount()
//refreshTabBarBadgeCount()
}
}

View File

@ -32,15 +32,6 @@ class LocalBooksController: UITableViewController, NSFetchedResultsControllerDel
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if fetchedResultController.delegate !== self {
fetchedResultController.delegate = self
tableView.reloadData()
}
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
fetchedResultController.delegate = nil
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
@ -131,6 +122,10 @@ class LocalBooksController: UITableViewController, NSFetchedResultsControllerDel
return true
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

View File

@ -49,7 +49,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>1.8.761</string>
<string>1.8.777</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSRequiresIPhoneOS</key>

View File

@ -21,7 +21,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.8.764</string>
<string>1.8.780</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionMainStoryboard</key>

View File

@ -54,14 +54,13 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe
self.context.performBlockAndWait {
guard let book = Book.fetch(bookID, context: self.context),
let downloadTask = book.downloadTask else {return}
if error.code == NSURLErrorCancelled {
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
guard let resumeData = error.userInfo[NSURLSessionDownloadTaskResumeData] as? NSData else {return}
Preference.resumeData[bookID] = resumeData
} else {
downloadTask.state = .Error

View File

@ -20,6 +20,11 @@ class DownloadBookOperation: URLSessionDownloadTaskOperation {
super.init(downloadTask: downloadTask)
name = downloadTask.taskDescription
if UIApplication.sharedApplication().applicationState == .Active,
let url = downloadTask.originalRequest?.URL {
addCondition(ReachabilityCondition(url: url, connectivity: .ViaWiFi))
}
// Update Coredata
let context = NSManagedObjectContext.mainQueueContext
context.performBlockAndWait {
@ -58,13 +63,7 @@ class DownloadBookOperation: URLSessionDownloadTaskOperation {
self.init(downloadTask: task)
}
override func operationWillCancel(errors: [ErrorType]) {
print("Download Task will cancel")
}
override func operationDidCancel() {
print("Download Task did cancel")
// Update CoreData
let context = NSManagedObjectContext.mainQueueContext
context.performBlockAndWait {