Polish notification

- define notification names
- move "post notification" to PasswordStore (todo: move "search" and the one for "show folder switch")
- "erase" and "reset" also post the "passwordStoreUpdated" notification
This commit is contained in:
Yishi Lin 2017-03-18 00:18:55 +08:00
parent 0ad9713fc0
commit 1f829fffcc
11 changed files with 41 additions and 29 deletions

View file

@ -32,7 +32,9 @@ class AboutRepositoryTableViewController: BasicStaticTableViewController {
tableView.addSubview(indicatorLabel)
setTableData()
addNotificationObservers()
// all password store updates (including erase, discard) will trigger the refresh
NotificationCenter.default.addObserver(self, selector: #selector(setNeedRefresh), name: .passwordStoreUpdated, object: nil)
}
override func viewWillAppear(_ animated: Bool) {
@ -92,11 +94,6 @@ class AboutRepositoryTableViewController: BasicStaticTableViewController {
}
}
}
private func addNotificationObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(setNeedRefresh), name: NSNotification.Name(rawValue: "passwordUpdated"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(setNeedRefresh), name: NSNotification.Name(rawValue: "passwordStoreErased"), object: nil)
}
func setNeedRefresh() {
needRefresh = true

View file

@ -26,7 +26,6 @@ class AdvancedSettingsTableViewController: UITableViewController {
alert.addAction(UIAlertAction(title: "Erase Password Data", style: UIAlertActionStyle.destructive, handler: {[unowned self] (action) -> Void in
SVProgressHUD.show(withStatus: "Erasing ...")
self.passwordStore.erase()
NotificationCenter.default.post(Notification(name: Notification.Name("passwordStoreErased")))
self.navigationController!.popViewController(animated: true)
SVProgressHUD.showSuccess(withStatus: "Done")
SVProgressHUD.dismiss(withDelay: 1)
@ -42,9 +41,6 @@ class AdvancedSettingsTableViewController: UITableViewController {
DispatchQueue.main.async {
do {
let numberDiscarded = try self.passwordStore.reset()
if numberDiscarded > 0 {
NotificationCenter.default.post(Notification(name: Notification.Name("passwordStoreChangeDiscarded")))
}
self.navigationController!.popViewController(animated: true)
switch numberDiscarded {
case 0:

View file

@ -184,7 +184,7 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController {
func showFolderSwitchAction(_ sender: Any?) {
Defaults[.isShowFolderOn] = showFolderSwitch.isOn
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
}
}

View file

@ -197,7 +197,6 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
DispatchQueue.main.async {
self.passwordEntity!.synced = false
self.passwordStore.saveUpdated(passwordEntity: self.passwordEntity!)
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
self.setTableData()
self.tableView.reloadData()
SVProgressHUD.showSuccess(withStatus: "Success")
@ -397,7 +396,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
}
private func addNotificationObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(setShouldPopCurrentView), name: NSNotification.Name(rawValue: "passwordStoreChangeDiscarded"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(setShouldPopCurrentView), name: .passwordStoreChangeDiscarded, object: nil)
}
func setShouldPopCurrentView() {

View file

@ -92,7 +92,6 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
DispatchQueue.main.async {
SVProgressHUD.showSuccess(withStatus: "Done")
SVProgressHUD.dismiss(withDelay: 1)
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
}
} catch {
DispatchQueue.main.async {
@ -142,9 +141,8 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
}
private func addNotificationObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(PasswordsViewController.actOnPasswordUpdatedNotification), name: NSNotification.Name(rawValue: "passwordUpdated"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(PasswordsViewController.actOnPasswordStoreErasedNotification), name: NSNotification.Name(rawValue: "passwordStoreErased"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(PasswordsViewController.actOnSearchNotification), name: NSNotification.Name(rawValue: "search"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(PasswordsViewController.actOnPasswordStoreUpdatedNotification), name: .passwordStoreUpdated, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(PasswordsViewController.actOnSearchNotification), name: .passwordSearch, object: nil)
}
override func viewDidLoad() {
@ -351,7 +349,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
sections.append(newSection)
}
func actOnPasswordUpdatedNotification() {
func actOnPasswordStoreUpdatedNotification() {
initPasswordsTableEntries(parent: nil)
reloadTableView(data: passwordsTableEntries)
setNavigationItemTitle()
@ -372,12 +370,6 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
}
}
func actOnPasswordStoreErasedNotification() {
initPasswordsTableEntries(parent: nil)
reloadTableView(data: passwordsTableEntries)
setNavigationItemTitle()
}
func actOnSearchNotification() {
searchController.searchBar.becomeFirstResponder()
}

View file

@ -142,7 +142,6 @@ class SettingsTableViewController: UITableViewController {
DispatchQueue.main.async {
self.passwordStore.updatePasswordEntityCoreData()
Defaults[.lastUpdatedTime] = Date()
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
Defaults[.gitRepositoryURL] = URL(string: gitRepostiroyURL)
Defaults[.gitRepositoryUsername] = username
Defaults[.gitRepositoryAuthenticationMethod] = auth
@ -164,7 +163,7 @@ class SettingsTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(SettingsTableViewController.actOnPasswordStoreErasedNotification), name: NSNotification.Name(rawValue: "passwordStoreErased"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(SettingsTableViewController.actOnPasswordStoreErasedNotification), name: .passwordStoreErased, object: nil)
self.passwordRepositoryTableViewCell.detailTextLabel?.text = Defaults[.gitRepositoryURL]?.host
touchIDTableViewCell.accessoryView = touchIDSwitch
setPGPKeyTableViewCellDetailText()