From 553a464157c9aa16efb80c29d08995cb0b1d3a05 Mon Sep 17 00:00:00 2001 From: Chris Li Date: Tue, 21 Jun 2016 13:58:09 -0400 Subject: [PATCH] Multiple changes - iPhone js, adjust thumbs layout, now they take up the whole width - NetworkObserver, correct task count - refactor and very minor improvements --- .../Controller/LibraryDownloadTBVC.swift | 2 +- Kiwix-iOS/Controller/SearchResultTBVC.swift | 2 - Kiwix-iOS/Info.plist | 2 +- Kiwix-iOS/JavaScripts/adjustlayoutiPhone.js | 48 +++++++++++++++---- .../Observer/NetworkObserver.swift | 9 +++- Kiwix/RefreshLibraryOperation.swift | 1 + Kiwix/SearchOperation.swift | 7 +-- Kiwix/ZimMultiReader.swift | 11 ++++- 8 files changed, 63 insertions(+), 19 deletions(-) diff --git a/Kiwix-iOS/Controller/LibraryDownloadTBVC.swift b/Kiwix-iOS/Controller/LibraryDownloadTBVC.swift index 7b43311c..dbf5b9ae 100644 --- a/Kiwix-iOS/Controller/LibraryDownloadTBVC.swift +++ b/Kiwix-iOS/Controller/LibraryDownloadTBVC.swift @@ -45,7 +45,7 @@ class LibraryDownloadTBVC: UITableViewController, NSFetchedResultsControllerDele let downloadTask = fetchedResultController.objectAtIndexPath(indexPath) as? DownloadTask, let book = downloadTask.book else {return} switch downloadTask.state { - case .Downloading: + case .Downloading, .Queued: Network.sharedInstance.pause(book) case .Paused, .Error: Network.sharedInstance.resume(book) diff --git a/Kiwix-iOS/Controller/SearchResultTBVC.swift b/Kiwix-iOS/Controller/SearchResultTBVC.swift index 2ee07ada..22c2d1fc 100644 --- a/Kiwix-iOS/Controller/SearchResultTBVC.swift +++ b/Kiwix-iOS/Controller/SearchResultTBVC.swift @@ -123,8 +123,6 @@ class SearchResultTBVC: UIViewController, UITableViewDataSource, UITableViewDele if results.count > 0 { self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: .Top, animated: true) } - // This line below works prefectly if tableview doesn't hide keyboard on drag - //self.tableView.setContentOffset(CGPointMake(0, 0 - self.tableView.contentInset.top), animated: true) } ZIMMultiReader.sharedInstance.searchQueue.addOperation(operation) } diff --git a/Kiwix-iOS/Info.plist b/Kiwix-iOS/Info.plist index 96903faf..6a0082f1 100644 --- a/Kiwix-iOS/Info.plist +++ b/Kiwix-iOS/Info.plist @@ -36,7 +36,7 @@ CFBundleSignature ???? CFBundleVersion - 291 + 366 ITSAppUsesNonExemptEncryption LSRequiresIPhoneOS diff --git a/Kiwix-iOS/JavaScripts/adjustlayoutiPhone.js b/Kiwix-iOS/JavaScripts/adjustlayoutiPhone.js index f7330b6f..ea25db78 100644 --- a/Kiwix-iOS/JavaScripts/adjustlayoutiPhone.js +++ b/Kiwix-iOS/JavaScripts/adjustlayoutiPhone.js @@ -1,13 +1,41 @@ /** * Created by chrisli on 10/5/15. */ -var tables = document.getElementsByTagName("table"); -var i; -for (i = 0; i < tables.length; i++) { - org_html = tables[i].outerHTML; - new_html = "
" + org_html + "
"; - tables[i].outerHTML = new_html; - tables[i].parentNode.style.overflow = "auto"; - tables[i].style.margin = "auto"; - tables[i].style.width = "100%"; -} \ No newline at end of file +function adjustTables() { + var tables = document.getElementsByTagName("table"); + var i; + for (i = 0; i < tables.length; i++) { + org_html = tables[i].outerHTML; + new_html = "
" + org_html + "
"; + tables[i].outerHTML = new_html; + tables[i].parentNode.style.overflow = "auto"; + tables[i].style.margin = "auto"; + tables[i].style.width = "100%"; + } +} + +function adjustThumbLayout(thumbs) { + var i; + for (i = 0; i < thumbs.length; i++) { + thumbs[i].style.width = "100%"; + thumbs[i].style.paddingLeft = "4px"; + thumbs[i].style.paddingRight = "4px"; + var inner = thumbs[i].getElementsByClassName("thumbinner")[0]; + if (inner) { + inner.style.width = "100%"; + var captions = inner.getElementsByClassName("thumbcaption"); + var j; + for (j= 0; j < captions.length; j++) { + captions[j].style.textAlign = "center"; + } + } + } +} + +adjustTables(); +adjustThumbLayout(document.getElementsByClassName("thumb tright")); +adjustThumbLayout(document.getElementsByClassName("thumb tleft")); +adjustThumbLayout(document.getElementsByClassName("thumb tmulti tright")); +adjustThumbLayout(document.getElementsByClassName("thumb tmulti tleft")); + + diff --git a/Kiwix/OperationLib/Observer/NetworkObserver.swift b/Kiwix/OperationLib/Observer/NetworkObserver.swift index b90d2c02..b84e4033 100644 --- a/Kiwix/OperationLib/Observer/NetworkObserver.swift +++ b/Kiwix/OperationLib/Observer/NetworkObserver.swift @@ -18,6 +18,7 @@ struct NetworkObserver: OperationObserver { init() { } func operationDidStart(operation: Operation) { + print("NetworkObserver: \(operation.name ?? "Unknown") operation did start") dispatch_async(dispatch_get_main_queue()) { // Increment the network indicator's "reference count" NetworkIndicatorController.sharedIndicatorController.networkActivityDidStart() @@ -27,13 +28,19 @@ struct NetworkObserver: OperationObserver { func operation(operation: Operation, didProduceOperation newOperation: NSOperation) { } func operationDidFinish(operation: Operation, errors: [NSError]) { + // If an op is cancelled, operationDidStart will not be called + print("NetworkObserver: \(operation.name ?? "Unknown") operation did finish") + guard !operation.cancelled else {return} + dispatch_async(dispatch_get_main_queue()) { // Decrement the network indicator's "reference count". NetworkIndicatorController.sharedIndicatorController.networkActivityDidEnd() } } - func operationDidCancel(operation: Operation) { } + func operationDidCancel(operation: Operation) { + print("NetworkObserver: \(operation.name ?? "Unknown") operation did cancel") + } } diff --git a/Kiwix/RefreshLibraryOperation.swift b/Kiwix/RefreshLibraryOperation.swift index ea8d7bde..f18e276f 100644 --- a/Kiwix/RefreshLibraryOperation.swift +++ b/Kiwix/RefreshLibraryOperation.swift @@ -28,6 +28,7 @@ class RefreshLibraryOperation: GroupOperation { parseOperation.xmlData = data } let fetchOperation = URLSessionTaskOperation(task: task) + fetchOperation.name = "Library XML download operation" #if os(iOS) || os(watchOS) || os(tvOS) fetchOperation.addObserver(NetworkObserver()) diff --git a/Kiwix/SearchOperation.swift b/Kiwix/SearchOperation.swift index f628d002..65995c7b 100644 --- a/Kiwix/SearchOperation.swift +++ b/Kiwix/SearchOperation.swift @@ -24,7 +24,7 @@ class SearchOperation: GroupOperation { let managedObjectContext = UIApplication.appDelegate.managedObjectContext guard let book = Book.fetch(id, context: managedObjectContext) else {continue} guard book.includeInSearch else {continue} - let operation = SingleBookSearchOperation(zimReader: zimReader, searchTerm: searchTerm, completionHandler: { [unowned sortOperation] (results) in + let operation = SingleBookSearchOperation(zimReader: zimReader, searchTerm: searchTerm.lowercaseString, completionHandler: { [unowned sortOperation] (results) in sortOperation.results += results }) @@ -61,6 +61,7 @@ private class SingleBookSearchOperation: Operation { var results = [SearchResult]() for dic in resultDics { guard let result = SearchResult(rawResult: dic) else {continue} + print(result) results.append(result) } completionHandler(results) @@ -77,7 +78,7 @@ private class SortSearchResultsOperation: Operation { } override private func execute() { - sortBySearchMethod() + sort() completionHandler(results) finish() } @@ -87,7 +88,7 @@ private class SortSearchResultsOperation: Operation { 2. Among Xapian results: sort by percent, then title case insensitive compare 3. Among searchSuggestionSmart results: sort by title case insensitive compare */ - private func sortBySearchMethod() { + private func sort() { results.sortInPlace { (result0, result1) -> Bool in let result0Percent = result0.percent ?? -1 let result1Percent = result1.percent ?? -1 diff --git a/Kiwix/ZimMultiReader.swift b/Kiwix/ZimMultiReader.swift index 98b50d1d..b181f028 100644 --- a/Kiwix/ZimMultiReader.swift +++ b/Kiwix/ZimMultiReader.swift @@ -160,10 +160,11 @@ extension ZimReader { } } -class SearchResult { +class SearchResult: CustomStringConvertible { let bookID: String let title: String let percent: Double? // range: 0-100 + let distance: Int64 // Levenshtein distance, non negative integer let path: String? let snippet: String? @@ -172,10 +173,18 @@ class SearchResult { self.title = (rawResult["title"] as? String) ?? "" self.percent = (rawResult["percent"] as? NSNumber)?.doubleValue + self.distance = (rawResult["distance"]as? NSNumber)?.longLongValue ?? 0 self.path = rawResult["path"] as? String self.snippet = rawResult["snippet"] as? String if bookID == "" {return nil} if title == "" {return nil} } + + var description: String { + var parts = [bookID, title] + if let percent = percent {parts.append("\(percent)%")} + parts.append("dist: \(distance)") + return parts.joinWithSeparator(", ") + } }