From c436aab3a818e11536b988b6fd03b00ad623dd03 Mon Sep 17 00:00:00 2001 From: Chris Li Date: Tue, 20 Sep 2016 18:27:45 -0400 Subject: [PATCH] deferred lang filter alert --- Kiwix-iOS/Controller/Alerts.swift | 21 +++++++++ Kiwix-iOS/Controller/ControllerRetainer.swift | 2 +- .../Library/CloudBooksController.swift | 44 ++++++++++--------- Kiwix-iOS/Info.plist | 2 +- Kiwix-iOSWidgets/Bookmarks/Info.plist | 2 +- Kiwix/Tools/Preference.swift | 8 ++-- 6 files changed, 51 insertions(+), 28 deletions(-) diff --git a/Kiwix-iOS/Controller/Alerts.swift b/Kiwix-iOS/Controller/Alerts.swift index 881391f6..71f4d150 100644 --- a/Kiwix-iOS/Controller/Alerts.swift +++ b/Kiwix-iOS/Controller/Alerts.swift @@ -48,6 +48,27 @@ class RemoveBookConfirmationAlert: AlertOperation { } } +class LanguageFilterAlert: AlertOperation { + init(context: UIViewController, handler: AlertOperation -> Void) { + super.init(presentAlertFrom: context) + + title = NSLocalizedString("Only Show Preferred Language?", comment: "Library, Language Filter Alert") + message = { + var names = NSLocale.preferredLangNames + guard let last = names.popLast() else {return nil} + var parts = [String]() + if names.count > 0 { parts.append(names.joinWithSeparator(", ")) } + parts.append(last) + let glue = " " + LocalizedStrings.and + " " + let string = parts.joinWithSeparator(glue) + return NSLocalizedString(String(format: "Would you like to filter the library by %@?", string), comment: "Library, Language Filter Alert") + }() + addActionWithTitle(LocalizedStrings.yes, style: .Default, handler: handler) + addActionWithTitle(LocalizedStrings.cancel) + preferredAction = actions[0] + } +} + class NetworkRequiredAlert: AlertOperation { init(context: UIViewController) { super.init(presentAlertFrom: context) diff --git a/Kiwix-iOS/Controller/ControllerRetainer.swift b/Kiwix-iOS/Controller/ControllerRetainer.swift index 6927e0a3..bec45ad9 100644 --- a/Kiwix-iOS/Controller/ControllerRetainer.swift +++ b/Kiwix-iOS/Controller/ControllerRetainer.swift @@ -50,7 +50,7 @@ class ControllerRetainer { private var library: UIViewController? class var library: UIViewController { - let controller = ControllerRetainer.shared.bookmarkStar ?? UIStoryboard(name: "Library", bundle: nil).instantiateInitialViewController()! + let controller = ControllerRetainer.shared.library ?? UIStoryboard(name: "Library", bundle: nil).instantiateInitialViewController()! ControllerRetainer.shared.library = controller return controller } diff --git a/Kiwix-iOS/Controller/Library/CloudBooksController.swift b/Kiwix-iOS/Controller/Library/CloudBooksController.swift index d824e6b7..3d67cbf5 100644 --- a/Kiwix-iOS/Controller/Library/CloudBooksController.swift +++ b/Kiwix-iOS/Controller/Library/CloudBooksController.swift @@ -15,6 +15,8 @@ import DZNEmptyDataSet class CloudBooksController: UITableViewController, NSFetchedResultsControllerDelegate, LanguageFilterUpdating, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate { private(set) var isRefreshing = false + private(set) var isOnScreen = false + private(set) var langFilterAlertPending = false // MARK: - Override @@ -40,11 +42,18 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel super.viewWillAppear(animated) configureNavBarButtons() refreshAutomatically() + isOnScreen = true + } + + override func viewDidAppear(animated: Bool) { + super.viewDidAppear(animated) + if langFilterAlertPending {showLanguageFilterAlert()} } override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) tabBarController?.navigationItem.rightBarButtonItem = nil + isOnScreen = false } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { @@ -73,7 +82,7 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel // MARK: - Actions - func showLanguageFilter() { + func showLanguageFilterController() { guard let splitViewController = splitViewController as? LibrarySplitViewController where !splitViewController.isShowingLangFilter else {return} guard let controller = UIStoryboard.library.initViewController(LanguageFilterController.self) else {return} controller.delegate = self @@ -144,35 +153,28 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel } func showLanguageFilterAlert() { - var names = NSLocale.preferredLangNames - guard let last = names.popLast() else {return} - var parts = [String]() - if names.count > 0 { parts.append(names.joinWithSeparator(", ")) } - parts.append(last) - let string = parts.joinWithSeparator(" and ") - - let comment = "Library: Language Filter Alert" - let alert = UIAlertController(title: NSLocalizedString("Only Show Preferred Language?", comment: comment), - message: NSLocalizedString(String(format: "Would you like to filter the library by %@?", string), comment: comment), - preferredStyle: .Alert) - let action = UIAlertAction(title: "Yes", style: .Default) { (action) in - self.managedObjectContext.performBlock({ + guard isOnScreen else { + langFilterAlertPending = true + return + } + let handler: AlertOperation -> Void = { [weak self] _ in + let context = NSManagedObjectContext.mainQueueContext + context.performBlock({ let codes = NSLocale.preferredLangCodes - Language.fetchAll(self.managedObjectContext).forEach({ (language) in + Language.fetchAll(context).forEach({ (language) in language.isDisplayed = codes.contains(language.code) }) - self.refreshFetchedResultController() + self?.refreshFetchedResultController() }) } - alert.addAction(action) - alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)) - alert.preferredAction = action - presentViewController(alert, animated: true, completion: nil) + let operation = LanguageFilterAlert(context: self, handler: handler) + GlobalQueue.shared.addOperation(operation) + langFilterAlertPending = false } func configureNavBarButtons() { tabBarController?.navigationItem.rightBarButtonItem = Preference.libraryLastRefreshTime == nil ? nil - : UIBarButtonItem(imageNamed: "LanguageFilter", target: self, action: #selector(CloudBooksController.showLanguageFilter)) + : UIBarButtonItem(imageNamed: "LanguageFilter", target: self, action: #selector(CloudBooksController.showLanguageFilterController)) } // MARK: - LanguageFilterUpdating diff --git a/Kiwix-iOS/Info.plist b/Kiwix-iOS/Info.plist index 903ead86..1bf6d742 100644 --- a/Kiwix-iOS/Info.plist +++ b/Kiwix-iOS/Info.plist @@ -49,7 +49,7 @@ CFBundleVersion - 1.8.924 + 1.8.956 ITSAppUsesNonExemptEncryption LSRequiresIPhoneOS diff --git a/Kiwix-iOSWidgets/Bookmarks/Info.plist b/Kiwix-iOSWidgets/Bookmarks/Info.plist index 6916afa6..05bcad30 100644 --- a/Kiwix-iOSWidgets/Bookmarks/Info.plist +++ b/Kiwix-iOSWidgets/Bookmarks/Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 1.8.927 + 1.8.959 NSExtension NSExtensionMainStoryboard diff --git a/Kiwix/Tools/Preference.swift b/Kiwix/Tools/Preference.swift index f9f7a764..8dde6c35 100644 --- a/Kiwix/Tools/Preference.swift +++ b/Kiwix/Tools/Preference.swift @@ -75,9 +75,9 @@ class Preference { set{Defaults[.libraryRefreshInterval] = newValue} } - class var libraryHasShownPreferredLanguagePrompt: Bool { - get{return Defaults[.libraryHasShownPreferredLanguagePrompt]} - set{Defaults[.libraryHasShownPreferredLanguagePrompt] = newValue} + class var preferredLanguageAlertPending: Bool { + get{return Defaults[.preferredLanguageAlertPending]} + set{Defaults[.preferredLanguageAlertPending] = newValue} } class LangFilter { @@ -111,7 +111,7 @@ extension DefaultsKeys { static let libraryRefreshNotAllowCellularData = DefaultsKey("libraryRefreshNotAllowCellularData") static let libraryLastRefreshTime = DefaultsKey("libraryLastRefreshTime") static let libraryRefreshInterval = DefaultsKey("libraryRefreshInterval") - static let libraryHasShownPreferredLanguagePrompt = DefaultsKey("libraryHasShownPreferredLanguagePrompt") + static let preferredLanguageAlertPending = DefaultsKey("preferredLanguageAlertPending") static let langFilterSortByAlphabeticalAsc = DefaultsKey("langFilterSortByAlphabeticalAsc") static let langFilterNameDisplayInOriginalLocale = DefaultsKey("langFilterNameDisplayInOriginalLocale")