From 5115071aaf024d3ed30abed3ba5e0d1144b7d4e4 Mon Sep 17 00:00:00 2001 From: Chris Li Date: Thu, 10 Nov 2016 09:57:37 -0500 Subject: [PATCH] DirectoryMonitor --- .../Library/CloudBooksController.swift | 87 +++++++------------ .../Library/LibrarySplitViewController.swift | 10 +++ Kiwix-iOS/Info.plist | 2 +- Kiwix-iOSWidgets/Bookmarks/Info.plist | 2 +- Kiwix/ZimMultiReader/DirectoryMonitor.swift | 22 ++--- 5 files changed, 55 insertions(+), 68 deletions(-) diff --git a/Kiwix-iOS/Controller/Library/CloudBooksController.swift b/Kiwix-iOS/Controller/Library/CloudBooksController.swift index 5eb56feb..94e42a24 100644 --- a/Kiwix-iOS/Controller/Library/CloudBooksController.swift +++ b/Kiwix-iOS/Controller/Library/CloudBooksController.swift @@ -108,6 +108,7 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel }) }) operation.add(observer: DidFinishObserver { (operation, errors) in + guard let operation = operation as? RefreshLibraryOperation else {return} OperationQueue.main.addOperation({ defer { self.refreshControl?.endRefreshing() @@ -115,42 +116,19 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel self.tableView.reloadEmptyDataSet() } - // handle error - // show reachibility alert - // show lang filter alert - + if let error = errors.first { + // handle error [network, xmlparse] + } else { + if operation.firstTime { + //self.showLanguageFilterAlert() + //self.configureNavBarButtons() + } else { + self.showRefreshSuccessMessage() + } + } }) }) GlobalQueue.shared.add(operation: operation) - -// let operation = RefreshLibraryOperation() -// operation.add(WillExecuteObserver { (operation) in -// }) -// -// operation.add(DidFinishObserver { (operation, errors) in -// guard let operation = operation as? RefreshLibraryOperation else {return} -// NSOperationQueue.mainQueue().addOperationWithBlock({ -// defer { -// self.refreshControl?.endRefreshing() -// self.isRefreshing = false -// self.tableView.reloadEmptyDataSet() -// } -// -// if errors.count > 0 { -// if let error = errors.first as? ReachabilityCondition.Error, error == ReachabilityCondition.Error.NotReachable && invokedByUser == true { -// self.showReachibilityAlert() -// } -// } else{ -// if operation.firstTime { -// self.showLanguageFilterAlert() -// self.configureNavBarButtons() -// } else { -// self.showRefreshSuccessMessage() -// } -// } -// }) -// }) -// GlobalQueue.shared.add(operation) } func showRefreshSuccessMessage() { @@ -292,32 +270,31 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {} override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?{ -// guard let book = fetchedResultController.object(at: indexPath) as? Book else {return nil} -// switch book.spaceState { -// case .enough: -// let action = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: LocalizedStrings.download, handler: { _ in + let book = fetchedResultController.object(at: indexPath) + switch book.spaceState { + case .enough: + let action = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: LocalizedStrings.download, handler: { _ in // guard let download = DownloadBookOperation(bookID: book.id) else {return} // Network.shared.queue.addOperation(download) -// }) -// action.backgroundColor = UIColor.defaultTint -// return [action] -// case .caution: -// let action = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: LocalizedStrings.download, handler: { _ in + }) + action.backgroundColor = UIColor.defaultTint + return [action] + case .caution: + let action = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: LocalizedStrings.download, handler: { _ in // let alert = SpaceCautionAlert(context: self, bookID: book.id) // GlobalQueue.shared.addOperation(alert) -// self.tableView.setEditing(false, animated: true) -// }) -// action.backgroundColor = UIColor.orange -// return [action] -// case .notEnough: -// let action = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: LocalizedStrings.spaceNotEnough, handler: { _ in + self.tableView.setEditing(false, animated: true) + }) + action.backgroundColor = UIColor.orange + return [action] + case .notEnough: + let action = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: LocalizedStrings.spaceNotEnough, handler: { _ in // let alert = SpaceNotEnoughAlert(context: self) // GlobalQueue.shared.addOperation(alert) -// self.tableView.setEditing(false, animated: true) -// }) -// return [action] -// } - return [] + self.tableView.setEditing(false, animated: true) + }) + return [action] + } } // MARK: - Fetched Results Controller @@ -342,7 +319,7 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel tableView.reloadData() } - fileprivate var langPredicate: NSPredicate { + private var langPredicate: NSPredicate { let displayedLanguages = Language.fetch(displayed: true, context: managedObjectContext) if displayedLanguages.count > 0 { return NSPredicate(format: "language IN %@", displayedLanguages) @@ -351,7 +328,7 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel } } - fileprivate var onlineCompoundPredicate: NSCompoundPredicate { + private var onlineCompoundPredicate: NSCompoundPredicate { let isCloudPredicate = NSPredicate(format: "stateRaw == 0") return NSCompoundPredicate(andPredicateWithSubpredicates: [langPredicate, isCloudPredicate]) } diff --git a/Kiwix-iOS/Controller/Library/LibrarySplitViewController.swift b/Kiwix-iOS/Controller/Library/LibrarySplitViewController.swift index 916544b9..9d3fcbb4 100644 --- a/Kiwix-iOS/Controller/Library/LibrarySplitViewController.swift +++ b/Kiwix-iOS/Controller/Library/LibrarySplitViewController.swift @@ -19,6 +19,16 @@ class LibrarySplitViewController: UISplitViewController, UISplitViewControllerDe configureDismissButton() } + override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { + guard traitCollection != previousTraitCollection else {return} + let controller: UITableViewController? = { + let nav = viewControllers.first as? UINavigationController + let tab = nav?.topViewController as? UITabBarController + return tab?.selectedViewController as? UITableViewController + }() + controller?.tableView.reloadData() + } + func configureDismissButton() { guard let master = viewControllers.first as? UINavigationController else {return} let barButtonItem = UIBarButtonItem(image: UIImage(named: "Cross"), style: .plain, target: self, action: #selector(dismiss(sender:))) diff --git a/Kiwix-iOS/Info.plist b/Kiwix-iOS/Info.plist index 6c909496..7215e9bd 100644 --- a/Kiwix-iOS/Info.plist +++ b/Kiwix-iOS/Info.plist @@ -49,7 +49,7 @@ CFBundleVersion - 1.8.2256 + 1.8.2274 ITSAppUsesNonExemptEncryption LSRequiresIPhoneOS diff --git a/Kiwix-iOSWidgets/Bookmarks/Info.plist b/Kiwix-iOSWidgets/Bookmarks/Info.plist index e0ccdffa..786b632a 100644 --- a/Kiwix-iOSWidgets/Bookmarks/Info.plist +++ b/Kiwix-iOSWidgets/Bookmarks/Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 1.8.2256 + 1.8.2274 NSExtension NSExtensionMainStoryboard diff --git a/Kiwix/ZimMultiReader/DirectoryMonitor.swift b/Kiwix/ZimMultiReader/DirectoryMonitor.swift index 22051374..ceddb558 100644 --- a/Kiwix/ZimMultiReader/DirectoryMonitor.swift +++ b/Kiwix/ZimMultiReader/DirectoryMonitor.swift @@ -78,19 +78,19 @@ class DirectoryMonitor { // MARK: - Custom Methods - var isCheckingChanges = false - var previousDirectoryHash: [String]? = nil - var currentDirectoryHash: [String]? = nil - var hashEqualCheck = 0 + private var isCheckingChanges = false + private var previousDirectoryHash: [String]? = nil + private var currentDirectoryHash: [String]? = nil + private var hashEqualCheck = 0 - fileprivate func directoryContentDidChange() { + private func directoryContentDidChange() { hashEqualCheck = 0 if isCheckingChanges == false { checkDirectoryChanges() } } - fileprivate func checkDirectoryChanges() { + private func checkDirectoryChanges() { isCheckingChanges = true previousDirectoryHash = currentDirectoryHash @@ -116,13 +116,13 @@ class DirectoryMonitor { } } - fileprivate func directoryDidReachStasis() { + private func directoryDidReachStasis() { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(NSEC_PER_SEC/10)) / Double(NSEC_PER_SEC) , execute: { () -> Void in self.delegate?.directoryMonitorDidObserveChange() }) } - fileprivate func waitAndCheckAgain() { + private func waitAndCheckAgain() { directoryMonitorQueue.asyncAfter(deadline: DispatchTime.now() + Double(Int64(NSEC_PER_SEC/2)) / Double(NSEC_PER_SEC) , execute: { () -> Void in self.checkDirectoryChanges() }) @@ -130,7 +130,7 @@ class DirectoryMonitor { // MARK: - Generate directory file info array - fileprivate func directoryHashes() -> [String] { + private func directoryHashes() -> [String] { var hashes = [String]() do { let contents = try FileManager.default.contentsOfDirectory(atPath: url.path) @@ -146,7 +146,7 @@ class DirectoryMonitor { return hashes } - fileprivate func fileHash(_ fileName: String) -> String? { + private func fileHash(_ fileName: String) -> String? { if let fileSize = fileSize(fileName) { return fileName + "_\(fileSize)" } else { @@ -154,7 +154,7 @@ class DirectoryMonitor { } } - fileprivate func fileSize(_ fileName: String) -> Int64? { + private func fileSize(_ fileName: String) -> Int64? { let path = self.url.appendingPathComponent(fileName).path if FileManager.default.fileExists(atPath: path) { do {