deferred lang filter alert

This commit is contained in:
Chris Li 2016-09-20 18:27:45 -04:00
parent c0ca954d80
commit c436aab3a8
6 changed files with 51 additions and 28 deletions

View File

@ -48,6 +48,27 @@ class RemoveBookConfirmationAlert: AlertOperation<UIViewController> {
}
}
class LanguageFilterAlert: AlertOperation<UIViewController> {
init(context: UIViewController, handler: AlertOperation<UIViewController> -> 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<UIViewController> {
init(context: UIViewController) {
super.init(presentAlertFrom: context)

View File

@ -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
}

View File

@ -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<UIViewController> -> 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

View File

@ -49,7 +49,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>1.8.924</string>
<string>1.8.956</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.927</string>
<string>1.8.959</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionMainStoryboard</key>

View File

@ -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<Bool>("libraryRefreshNotAllowCellularData")
static let libraryLastRefreshTime = DefaultsKey<NSDate?>("libraryLastRefreshTime")
static let libraryRefreshInterval = DefaultsKey<Double?>("libraryRefreshInterval")
static let libraryHasShownPreferredLanguagePrompt = DefaultsKey<Bool>("libraryHasShownPreferredLanguagePrompt")
static let preferredLanguageAlertPending = DefaultsKey<Bool>("preferredLanguageAlertPending")
static let langFilterSortByAlphabeticalAsc = DefaultsKey<Bool>("langFilterSortByAlphabeticalAsc")
static let langFilterNameDisplayInOriginalLocale = DefaultsKey<Bool>("langFilterNameDisplayInOriginalLocale")