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> { class NetworkRequiredAlert: AlertOperation<UIViewController> {
init(context: UIViewController) { init(context: UIViewController) {
super.init(presentAlertFrom: context) super.init(presentAlertFrom: context)

View File

@ -50,7 +50,7 @@ class ControllerRetainer {
private var library: UIViewController? private var library: UIViewController?
class 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 ControllerRetainer.shared.library = controller
return controller return controller
} }

View File

@ -15,6 +15,8 @@ import DZNEmptyDataSet
class CloudBooksController: UITableViewController, NSFetchedResultsControllerDelegate, LanguageFilterUpdating, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate { class CloudBooksController: UITableViewController, NSFetchedResultsControllerDelegate, LanguageFilterUpdating, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
private(set) var isRefreshing = false private(set) var isRefreshing = false
private(set) var isOnScreen = false
private(set) var langFilterAlertPending = false
// MARK: - Override // MARK: - Override
@ -40,11 +42,18 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel
super.viewWillAppear(animated) super.viewWillAppear(animated)
configureNavBarButtons() configureNavBarButtons()
refreshAutomatically() refreshAutomatically()
isOnScreen = true
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if langFilterAlertPending {showLanguageFilterAlert()}
} }
override func viewWillDisappear(animated: Bool) { override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated) super.viewWillDisappear(animated)
tabBarController?.navigationItem.rightBarButtonItem = nil tabBarController?.navigationItem.rightBarButtonItem = nil
isOnScreen = false
} }
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
@ -73,7 +82,7 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel
// MARK: - Actions // MARK: - Actions
func showLanguageFilter() { func showLanguageFilterController() {
guard let splitViewController = splitViewController as? LibrarySplitViewController where !splitViewController.isShowingLangFilter else {return} guard let splitViewController = splitViewController as? LibrarySplitViewController where !splitViewController.isShowingLangFilter else {return}
guard let controller = UIStoryboard.library.initViewController(LanguageFilterController.self) else {return} guard let controller = UIStoryboard.library.initViewController(LanguageFilterController.self) else {return}
controller.delegate = self controller.delegate = self
@ -144,35 +153,28 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel
} }
func showLanguageFilterAlert() { func showLanguageFilterAlert() {
var names = NSLocale.preferredLangNames guard isOnScreen else {
guard let last = names.popLast() else {return} langFilterAlertPending = true
var parts = [String]() return
if names.count > 0 { parts.append(names.joinWithSeparator(", ")) } }
parts.append(last) let handler: AlertOperation<UIViewController> -> Void = { [weak self] _ in
let string = parts.joinWithSeparator(" and ") let context = NSManagedObjectContext.mainQueueContext
context.performBlock({
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({
let codes = NSLocale.preferredLangCodes let codes = NSLocale.preferredLangCodes
Language.fetchAll(self.managedObjectContext).forEach({ (language) in Language.fetchAll(context).forEach({ (language) in
language.isDisplayed = codes.contains(language.code) language.isDisplayed = codes.contains(language.code)
}) })
self.refreshFetchedResultController() self?.refreshFetchedResultController()
}) })
} }
alert.addAction(action) let operation = LanguageFilterAlert(context: self, handler: handler)
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)) GlobalQueue.shared.addOperation(operation)
alert.preferredAction = action langFilterAlertPending = false
presentViewController(alert, animated: true, completion: nil)
} }
func configureNavBarButtons() { func configureNavBarButtons() {
tabBarController?.navigationItem.rightBarButtonItem = Preference.libraryLastRefreshTime == nil ? nil 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 // MARK: - LanguageFilterUpdating

View File

@ -49,7 +49,7 @@
</dict> </dict>
</array> </array>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>1.8.924</string> <string>1.8.956</string>
<key>ITSAppUsesNonExemptEncryption</key> <key>ITSAppUsesNonExemptEncryption</key>
<false/> <false/>
<key>LSRequiresIPhoneOS</key> <key>LSRequiresIPhoneOS</key>

View File

@ -21,7 +21,7 @@
<key>CFBundleSignature</key> <key>CFBundleSignature</key>
<string>????</string> <string>????</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>1.8.927</string> <string>1.8.959</string>
<key>NSExtension</key> <key>NSExtension</key>
<dict> <dict>
<key>NSExtensionMainStoryboard</key> <key>NSExtensionMainStoryboard</key>

View File

@ -75,9 +75,9 @@ class Preference {
set{Defaults[.libraryRefreshInterval] = newValue} set{Defaults[.libraryRefreshInterval] = newValue}
} }
class var libraryHasShownPreferredLanguagePrompt: Bool { class var preferredLanguageAlertPending: Bool {
get{return Defaults[.libraryHasShownPreferredLanguagePrompt]} get{return Defaults[.preferredLanguageAlertPending]}
set{Defaults[.libraryHasShownPreferredLanguagePrompt] = newValue} set{Defaults[.preferredLanguageAlertPending] = newValue}
} }
class LangFilter { class LangFilter {
@ -111,7 +111,7 @@ extension DefaultsKeys {
static let libraryRefreshNotAllowCellularData = DefaultsKey<Bool>("libraryRefreshNotAllowCellularData") static let libraryRefreshNotAllowCellularData = DefaultsKey<Bool>("libraryRefreshNotAllowCellularData")
static let libraryLastRefreshTime = DefaultsKey<NSDate?>("libraryLastRefreshTime") static let libraryLastRefreshTime = DefaultsKey<NSDate?>("libraryLastRefreshTime")
static let libraryRefreshInterval = DefaultsKey<Double?>("libraryRefreshInterval") 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 langFilterSortByAlphabeticalAsc = DefaultsKey<Bool>("langFilterSortByAlphabeticalAsc")
static let langFilterNameDisplayInOriginalLocale = DefaultsKey<Bool>("langFilterNameDisplayInOriginalLocale") static let langFilterNameDisplayInOriginalLocale = DefaultsKey<Bool>("langFilterNameDisplayInOriginalLocale")