From b7f4c3e03d7957537df88f7f09ff1e7a2132def3 Mon Sep 17 00:00:00 2001 From: Chris Li Date: Wed, 7 Sep 2016 09:41:28 -0400 Subject: [PATCH] Cloud Controller Update --- .../Library/CloudBooksController.swift | 40 +++++++++++++++---- .../Library/EmptyTableConfigExtension.swift | 5 ++- Kiwix-iOS/Info.plist | 2 +- Kiwix-iOSWidgets/Bookmarks/Info.plist | 2 +- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Kiwix-iOS/Controller/Library/CloudBooksController.swift b/Kiwix-iOS/Controller/Library/CloudBooksController.swift index 260318da..b65e4be6 100644 --- a/Kiwix-iOS/Controller/Library/CloudBooksController.swift +++ b/Kiwix-iOS/Controller/Library/CloudBooksController.swift @@ -9,10 +9,13 @@ import UIKit import CoreData import Operations +import MBProgressHUD import DZNEmptyDataSet class CloudBooksController: UITableViewController, NSFetchedResultsControllerDelegate, LanguageFilterUpdating, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate { + private(set) var isRefreshing = false + // MARK: - Override required init?(coder aDecoder: NSCoder) { @@ -87,20 +90,43 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel func refresh(invokedByUser invokedByUser: Bool) { let operation = RefreshLibraryOperation() - operation.addObserver(DidFinishObserver { (operation, errors) in + operation.addObserver(WillExecuteObserver { (operation) in NSOperationQueue.mainQueue().addOperationWithBlock({ - self.refreshControl?.endRefreshing() + self.isRefreshing = true + self.tableView.reloadEmptyDataSet() }) + }) + + operation.addObserver(DidFinishObserver { (operation, errors) in - if let error = errors.first as? ReachabilityCondition.Error{ - guard error == ReachabilityCondition.Error.NotReachable && invokedByUser == true else {return} + NSOperationQueue.mainQueue().addOperationWithBlock({ + defer { + self.refreshControl?.endRefreshing() + self.isRefreshing = false + self.tableView.reloadEmptyDataSet() + } + + // make sure do have error + guard errors.count > 0 else { + guard let view = self.splitViewController?.view else {return} + let hud = MBProgressHUD.showHUDAddedTo(view, animated: true) + hud.mode = .Text + hud.label.numberOfLines = 0 + hud.label.text = NSLocalizedString("Library is refreshed successfully!", comment: "Cloud Book Controller") + hud.hideAnimated(true, afterDelay: 2) + return + } + + // test if is Reachability error + guard let error = errors.first as? ReachabilityCondition.Error + where error == ReachabilityCondition.Error.NotReachable && invokedByUser == true else {return} let cancel = UIAlertAction(title: LocalizedStrings.Common.ok, style: .Cancel, handler: nil) let alertController = UIAlertController(title: NSLocalizedString("Network Required", comment: "Network Required Alert"), - message: NSLocalizedString("Unable to connect to server. Please check your Internet connection.", comment: "Network Required Alert"), - preferredStyle: .Alert) + message: NSLocalizedString("Unable to connect to server. Please check your Internet connection.", comment: "Network Required Alert"), + preferredStyle: .Alert) alertController.addAction(cancel) self.presentViewController(alertController, animated: true, completion: nil) - } + }) }) GlobalQueue.shared.addOperation(operation) } diff --git a/Kiwix-iOS/Controller/Library/EmptyTableConfigExtension.swift b/Kiwix-iOS/Controller/Library/EmptyTableConfigExtension.swift index 79abd4f0..6a062b66 100644 --- a/Kiwix-iOS/Controller/Library/EmptyTableConfigExtension.swift +++ b/Kiwix-iOS/Controller/Library/EmptyTableConfigExtension.swift @@ -22,12 +22,13 @@ extension CloudBooksController { } func buttonTitleForEmptyDataSet(scrollView: UIScrollView!, forState state: UIControlState) -> NSAttributedString! { - let string = NSLocalizedString("Refresh", comment: "Cloud Book Controller") - let attributes = [NSFontAttributeName: UIFont.boldSystemFontOfSize(17), NSForegroundColorAttributeName: AppColors.theme] + let string = isRefreshing ? NSLocalizedString("Refreshing...", comment: "Cloud Book Controller") : NSLocalizedString("Refresh", comment: "Cloud Book Controller") + let attributes = [NSFontAttributeName: UIFont.boldSystemFontOfSize(17), NSForegroundColorAttributeName: isRefreshing ? UIColor.grayColor() : AppColors.theme] return NSAttributedString(string: string, attributes: attributes) } func emptyDataSetDidTapButton(scrollView: UIScrollView!) { + guard !isRefreshing else {return} refresh(invokedByUser: true) } diff --git a/Kiwix-iOS/Info.plist b/Kiwix-iOS/Info.plist index 18342c5a..7a43e57d 100644 --- a/Kiwix-iOS/Info.plist +++ b/Kiwix-iOS/Info.plist @@ -49,7 +49,7 @@ CFBundleVersion - 1.7.1759 + 1.7.1781 ITSAppUsesNonExemptEncryption LSRequiresIPhoneOS diff --git a/Kiwix-iOSWidgets/Bookmarks/Info.plist b/Kiwix-iOSWidgets/Bookmarks/Info.plist index fac83a2a..11c718f2 100644 --- a/Kiwix-iOSWidgets/Bookmarks/Info.plist +++ b/Kiwix-iOSWidgets/Bookmarks/Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 1.7.2294 + 1.7.2326 NSExtension NSExtensionMainStoryboard