ArticleLoadOperation

This commit is contained in:
Chris Li 2016-09-13 15:12:05 -04:00
parent 71e43c60e4
commit 99e5193a96
7 changed files with 86 additions and 17 deletions

View File

@ -125,8 +125,6 @@ class MainController: UIViewController {
return
}
guard let url = url else {return}
webView.hidden = false
hideWelcome()
let request = NSURLRequest(URL: url)
webView.loadRequest(request)
}

View File

@ -25,6 +25,13 @@ extension MainController: UIWebViewDelegate, SFSafariViewControllerDelegate,
func webViewDidStartLoad(webView: UIWebView) {
URLResponseCache.shared.start()
// UI Updates
if webView.hidden {
webView.hidden = false
hideWelcome()
}
hideSearch(animated: true)
}
func webViewDidFinishLoad(webView: UIWebView) {

View File

@ -106,11 +106,10 @@ class SearchResultTBVC: UIViewController, UITableViewDataSource, UITableViewDele
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
guard let mainVC = parentViewController?.parentViewController as? MainController else {return}
let result = searchResults[indexPath.row]
let url = NSURL.kiwixURLWithZimFileid(result.bookID, articleTitle: result.title)
mainVC.load(url)
mainVC.hideSearch(animated: true)
let operation = ArticleLoadOperation(bookID: result.bookID, articleTitle: result.title)
GlobalQueue.shared.add(load: operation)
}
// MARK: - Search

View File

@ -49,7 +49,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>1.8.369</string>
<string>1.8.401</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.372</string>
<string>1.8.404</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionMainStoryboard</key>

View File

@ -10,15 +10,71 @@ import UIKit
import Operations
class ArticleLoadOperation: Operation {
let url: NSURL
let bookID: String?
let path: String?
let title: String?
let url: NSURL?
init?(url: NSURL) {
init(url: NSURL) {
self.bookID = nil
self.path = nil
self.title = nil
self.url = url
super.init()
}
override func execute() {
init(bookID: String) {
self.bookID = bookID
self.path = nil
self.title = nil
self.url = nil
super.init()
}
init(bookID: String, articlePath: String) {
self.bookID = bookID
self.path = articlePath
self.title = nil
self.url = nil
super.init()
}
init(bookID: String, articleTitle: String) {
self.bookID = bookID
self.path = nil
self.title = articleTitle
self.url = nil
super.init()
}
override func execute() {
let controller = ((UIApplication.sharedApplication().delegate as! AppDelegate)
.window?.rootViewController as! UINavigationController)
.topViewController as! MainController
guard let url: NSURL = {
if let url = self.url { return url}
if let bookID = bookID, let path = path { return NSURL(bookID: bookID, contentPath: path) }
if let bookID = bookID, let title = title {
guard let path = ZimMultiReader.shared.readers[bookID]?.pageURLFromTitle(title) else {return nil}
return NSURL(bookID: bookID, contentPath: path)
}
if let bookID = bookID {
guard let reader = ZimMultiReader.shared.readers[bookID] else {return nil}
let path = reader.mainPageURL()
return NSURL(bookID: bookID, contentPath: path)
}
return nil
}() else {
// TODO - should produce error
finish()
return
}
let request = NSURLRequest(URL: url)
NSOperationQueue.mainQueue().addOperationWithBlock {
controller.webView.loadRequest(request)
self.finish()
}
}
}

View File

@ -13,6 +13,7 @@ class GlobalQueue: OperationQueue {
private weak var scanOperation: ScanLocalBookOperation?
private weak var searchOperation: SearchOperation?
private weak var articleLoadOperation: ArticleLoadOperation?
func add(scan operation: ScanLocalBookOperation) {
addOperation(operation)
@ -20,13 +21,8 @@ class GlobalQueue: OperationQueue {
}
func add(search operation: SearchOperation) {
if let _ = searchOperation {
print("search is not released")
}
if let scanOperation = scanOperation {
operation.addDependency(scanOperation)
print("scan not finished")
}
if let searchOperation = self.searchOperation {
@ -35,6 +31,19 @@ class GlobalQueue: OperationQueue {
addOperation(operation)
searchOperation = operation
}
func add(load operation: ArticleLoadOperation) {
if let scanOperation = scanOperation {
operation.addDependency(scanOperation)
}
if let articleLoadOperation = self.articleLoadOperation {
operation.addDependency(articleLoadOperation)
}
addOperation(operation)
articleLoadOperation = operation
}
}
public enum OperationErrorCode: Int {