This commit is contained in:
Chris Li 2017-01-30 14:08:59 -05:00
parent df1c858b48
commit fa3b0afcbc
3 changed files with 57 additions and 37 deletions

View File

@ -58,16 +58,6 @@ class LibraryBooksController: CoreDataCollectionBaseController, UICollectionView
collectionView?.collectionViewLayout.invalidateLayout()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showLangFilter" {
let nav = segue.destination as? UINavigationController
let controller = nav?.topViewController as? LibraryLanguageController
controller?.dismissBlock = {[unowned self] in
self.reloadFetchedResultController()
}
}
}
// MARK: - UIControls
let languageFilterButton = UIBarButtonItem(image: UIImage(named: "LanguageFilter"), style: .plain, target: nil, action: nil)
@ -89,10 +79,14 @@ class LibraryBooksController: CoreDataCollectionBaseController, UICollectionView
}
func languageFilterButtonTapped(sender: UIBarButtonItem) {
let controller = UIStoryboard(name: "Library", bundle: nil).instantiateViewController(withIdentifier: "LibraryLanguageNavController")
controller.modalPresentationStyle = .popover
controller.popoverPresentationController?.barButtonItem = sender
present(controller, animated: true, completion: nil)
let nav = UIStoryboard(name: "Library", bundle: nil).instantiateViewController(withIdentifier: "LibraryLanguageNavController") as! UINavigationController
(nav.topViewController as? LibraryLanguageController)?.dismissBlock = {[unowned self] in
self.reloadFetchedResultController()
}
nav.modalPresentationStyle = .popover
nav.popoverPresentationController?.barButtonItem = sender
present(nav, animated: true, completion: nil)
}
func downloadButtonTapped(sender: UIBarButtonItem) {

View File

@ -2,4 +2,22 @@
<Bucket
type = "0"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Kiwix/Network/Network.swift"
timestampString = "507495797.445676"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "142"
endingLineNumber = "142"
landmarkName = "urlSession(_:downloadTask:didFinishDownloadingTo:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>

View File

@ -31,22 +31,18 @@ class Network: NSObject, URLSessionDelegate, URLSessionTaskDelegate, URLSessionD
// MARK: - actions
func startDownload(bookID: String) {
self.managedObjectContext.perform {
guard let book = Book.fetch(bookID, context: self.managedObjectContext) else {return}
book.state = .local
}
// guard let url = book.url else {return}
// let task = (book.fileSize > 100000000 ? wifiSession: cellularSession).downloadTask(with: url)
// task.taskDescription = book.id
// task.resume()
//
// let downloadTask = DownloadTask.fetch(bookID: book.id, context: managedObjectContext)
// downloadTask?.state = .queued
//
// if self.managedObjectContext.hasChanges { try? self.managedObjectContext.save() }
//
// progresses[book.id] = 0
// if progresses.count == 1 { startTimer() }
guard let book = Book.fetch(bookID, context: managedObjectContext), let url = book.url else {return}
let task = (book.fileSize > 100000000 ? wifiSession: cellularSession).downloadTask(with: url)
task.taskDescription = book.id
task.resume()
let downloadTask = DownloadTask.fetch(bookID: book.id, context: managedObjectContext)
downloadTask?.state = .queued
if self.managedObjectContext.hasChanges { try? self.managedObjectContext.save() }
progresses[book.id] = 0
if progresses.count == 1 { startTimer() }
}
func pause(bookID: String) {
@ -131,16 +127,28 @@ class Network: NSObject, URLSessionDelegate, URLSessionTaskDelegate, URLSessionD
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
managedObjectContext.perform {
guard let bookID = downloadTask.taskDescription,
let book = Book.fetch(bookID, context: self.managedObjectContext),
let downloadTask = DownloadTask.fetch(bookID: bookID, context: self.managedObjectContext) else {return}
//book.state = .local
self.managedObjectContext.delete(downloadTask)
print("finish downloading")
managedObjectContext.perform {
guard let bookID = downloadTask.taskDescription else {return}
if let book = Book.fetch(bookID, context: self.managedObjectContext),
let downloadTask = DownloadTask.fetch(bookID: bookID, context: self.managedObjectContext) {
book.state = .local
self.managedObjectContext.delete(downloadTask)
}
self.progresses[bookID] = nil
if self.progresses.count == 0 { self.timer?.invalidate() }
if let docDirURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileName = {
return downloadTask.response?.suggestedFilename
?? downloadTask.originalRequest?.url?.lastPathComponent
?? bookID
}()
let destination = docDirURL.appendingPathComponent(fileName)
try? FileManager.default.moveItem(at: location, to: destination)
}
print("finish downloading")
}