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

@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
94BA784B85E071D25EE89B59 /* libPods-pass.a in Frameworks */ = {isa = PBXBuildFile; fileRef = ADCE7A5C3CCC67D7D21BB3C4 /* libPods-pass.a */; }; 94BA784B85E071D25EE89B59 /* libPods-pass.a in Frameworks */ = {isa = PBXBuildFile; fileRef = ADCE7A5C3CCC67D7D21BB3C4 /* libPods-pass.a */; };
A262A58D1E68749C006B0890 /* Base32.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A262A58C1E68749C006B0890 /* Base32.framework */; }; A262A58D1E68749C006B0890 /* Base32.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A262A58C1E68749C006B0890 /* Base32.framework */; };
A27424D91E7C35960093F436 /* NotificationNames.swift in Sources */ = {isa = PBXBuildFile; fileRef = A27424D81E7C35960093F436 /* NotificationNames.swift */; };
A2802BF91E70813A00879216 /* SliderTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2802BF71E70813A00879216 /* SliderTableViewCell.swift */; }; A2802BF91E70813A00879216 /* SliderTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2802BF71E70813A00879216 /* SliderTableViewCell.swift */; };
A2802BFA1E70813A00879216 /* SliderTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = A2802BF81E70813A00879216 /* SliderTableViewCell.xib */; }; A2802BFA1E70813A00879216 /* SliderTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = A2802BF81E70813A00879216 /* SliderTableViewCell.xib */; };
DC037CA61E4B883900609409 /* OpenSourceComponentsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC037CA51E4B883900609409 /* OpenSourceComponentsTableViewController.swift */; }; DC037CA61E4B883900609409 /* OpenSourceComponentsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC037CA51E4B883900609409 /* OpenSourceComponentsTableViewController.swift */; };
@ -69,6 +70,7 @@
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
274CCFCF32444A2FF46BE7F4 /* Pods-pass.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-pass.debug.xcconfig"; path = "Pods/Target Support Files/Pods-pass/Pods-pass.debug.xcconfig"; sourceTree = "<group>"; }; 274CCFCF32444A2FF46BE7F4 /* Pods-pass.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-pass.debug.xcconfig"; path = "Pods/Target Support Files/Pods-pass/Pods-pass.debug.xcconfig"; sourceTree = "<group>"; };
A262A58C1E68749C006B0890 /* Base32.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Base32.framework; path = Carthage/Build/iOS/Base32.framework; sourceTree = "<group>"; }; A262A58C1E68749C006B0890 /* Base32.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Base32.framework; path = Carthage/Build/iOS/Base32.framework; sourceTree = "<group>"; };
A27424D81E7C35960093F436 /* NotificationNames.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationNames.swift; sourceTree = "<group>"; };
A2802BF71E70813A00879216 /* SliderTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SliderTableViewCell.swift; sourceTree = "<group>"; }; A2802BF71E70813A00879216 /* SliderTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SliderTableViewCell.swift; sourceTree = "<group>"; };
A2802BF81E70813A00879216 /* SliderTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SliderTableViewCell.xib; sourceTree = "<group>"; }; A2802BF81E70813A00879216 /* SliderTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SliderTableViewCell.xib; sourceTree = "<group>"; };
ADCE7A5C3CCC67D7D21BB3C4 /* libPods-pass.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-pass.a"; sourceTree = BUILT_PRODUCTS_DIR; }; ADCE7A5C3CCC67D7D21BB3C4 /* libPods-pass.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-pass.a"; sourceTree = BUILT_PRODUCTS_DIR; };
@ -204,6 +206,7 @@
DC4A746D1E30FBDE00E8EB18 /* Objective-CBridgingHeader.h */, DC4A746D1E30FBDE00E8EB18 /* Objective-CBridgingHeader.h */,
DCA049971E33586A00522E8F /* DefaultsKeys.swift */, DCA049971E33586A00522E8F /* DefaultsKeys.swift */,
DC19400A1E4B36B60077E0A3 /* Utils.swift */, DC19400A1E4B36B60077E0A3 /* Utils.swift */,
A27424D81E7C35960093F436 /* NotificationNames.swift */,
); );
path = Helpers; path = Helpers;
sourceTree = "<group>"; sourceTree = "<group>";
@ -447,6 +450,7 @@
DC7E6EEA1E432E48006C2443 /* Password.swift in Sources */, DC7E6EEA1E432E48006C2443 /* Password.swift in Sources */,
DC4914961E434301007FF592 /* LabelTableViewCell.swift in Sources */, DC4914961E434301007FF592 /* LabelTableViewCell.swift in Sources */,
DC5F385B1E56AADB00C69ACA /* PGPKeyArmorSettingTableViewController.swift in Sources */, DC5F385B1E56AADB00C69ACA /* PGPKeyArmorSettingTableViewController.swift in Sources */,
A27424D91E7C35960093F436 /* NotificationNames.swift in Sources */,
DCAAF7451E2FA66800AB94BC /* SettingsTableViewController.swift in Sources */, DCAAF7451E2FA66800AB94BC /* SettingsTableViewController.swift in Sources */,
DCE6C2671E71261C003038C6 /* PasswordWithFolderTableViewCell.swift in Sources */, DCE6C2671E71261C003038C6 /* PasswordWithFolderTableViewCell.swift in Sources */,
DCFB77A71E502DF9008DE471 /* EditPasswordTableViewController.swift in Sources */, DCFB77A71E502DF9008DE471 /* EditPasswordTableViewController.swift in Sources */,

View file

@ -38,7 +38,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
} }
func postSearchNotification() { func postSearchNotification() {
NotificationCenter.default.post(Notification(name: Notification.Name("search"))) NotificationCenter.default.post(name: .passwordSearch, object: nil)
} }
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) { func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {

View file

@ -32,7 +32,9 @@ class AboutRepositoryTableViewController: BasicStaticTableViewController {
tableView.addSubview(indicatorLabel) tableView.addSubview(indicatorLabel)
setTableData() 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) { override func viewWillAppear(_ animated: Bool) {
@ -93,11 +95,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() { func setNeedRefresh() {
needRefresh = true 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 alert.addAction(UIAlertAction(title: "Erase Password Data", style: UIAlertActionStyle.destructive, handler: {[unowned self] (action) -> Void in
SVProgressHUD.show(withStatus: "Erasing ...") SVProgressHUD.show(withStatus: "Erasing ...")
self.passwordStore.erase() self.passwordStore.erase()
NotificationCenter.default.post(Notification(name: Notification.Name("passwordStoreErased")))
self.navigationController!.popViewController(animated: true) self.navigationController!.popViewController(animated: true)
SVProgressHUD.showSuccess(withStatus: "Done") SVProgressHUD.showSuccess(withStatus: "Done")
SVProgressHUD.dismiss(withDelay: 1) SVProgressHUD.dismiss(withDelay: 1)
@ -42,9 +41,6 @@ class AdvancedSettingsTableViewController: UITableViewController {
DispatchQueue.main.async { DispatchQueue.main.async {
do { do {
let numberDiscarded = try self.passwordStore.reset() let numberDiscarded = try self.passwordStore.reset()
if numberDiscarded > 0 {
NotificationCenter.default.post(Notification(name: Notification.Name("passwordStoreChangeDiscarded")))
}
self.navigationController!.popViewController(animated: true) self.navigationController!.popViewController(animated: true)
switch numberDiscarded { switch numberDiscarded {
case 0: case 0:

View file

@ -184,7 +184,7 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController {
func showFolderSwitchAction(_ sender: Any?) { func showFolderSwitchAction(_ sender: Any?) {
Defaults[.isShowFolderOn] = showFolderSwitch.isOn 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 { DispatchQueue.main.async {
self.passwordEntity!.synced = false self.passwordEntity!.synced = false
self.passwordStore.saveUpdated(passwordEntity: self.passwordEntity!) self.passwordStore.saveUpdated(passwordEntity: self.passwordEntity!)
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
self.setTableData() self.setTableData()
self.tableView.reloadData() self.tableView.reloadData()
SVProgressHUD.showSuccess(withStatus: "Success") SVProgressHUD.showSuccess(withStatus: "Success")
@ -397,7 +396,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
} }
private func addNotificationObservers() { 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() { func setShouldPopCurrentView() {

View file

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

View file

@ -142,7 +142,6 @@ class SettingsTableViewController: UITableViewController {
DispatchQueue.main.async { DispatchQueue.main.async {
self.passwordStore.updatePasswordEntityCoreData() self.passwordStore.updatePasswordEntityCoreData()
Defaults[.lastUpdatedTime] = Date() Defaults[.lastUpdatedTime] = Date()
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
Defaults[.gitRepositoryURL] = URL(string: gitRepostiroyURL) Defaults[.gitRepositoryURL] = URL(string: gitRepostiroyURL)
Defaults[.gitRepositoryUsername] = username Defaults[.gitRepositoryUsername] = username
Defaults[.gitRepositoryAuthenticationMethod] = auth Defaults[.gitRepositoryAuthenticationMethod] = auth
@ -164,7 +163,7 @@ class SettingsTableViewController: UITableViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.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 self.passwordRepositoryTableViewCell.detailTextLabel?.text = Defaults[.gitRepositoryURL]?.host
touchIDTableViewCell.accessoryView = touchIDSwitch touchIDTableViewCell.accessoryView = touchIDSwitch
setPGPKeyTableViewCellDetailText() setPGPKeyTableViewCellDetailText()

View file

@ -0,0 +1,16 @@
//
// NotificationNames.swift
// pass
//
// Created by Yishi Lin on 17/3/17.
// Copyright © 2017 Yishi Lin, Bob Sun. All rights reserved.
//
import Foundation
extension Notification.Name {
static let passwordStoreUpdated = Notification.Name("passwordStoreUpdated")
static let passwordStoreErased = Notification.Name("passwordStoreErased")
static let passwordStoreChangeDiscarded = Notification.Name("passwordStoreChangeDiscarded")
static let passwordSearch = Notification.Name("passwordSearch")
}

View file

@ -285,6 +285,7 @@ class PasswordStore {
} }
storeRepository = try GTRepository(url: storeURL) storeRepository = try GTRepository(url: storeURL)
gitCredential = credential gitCredential = credential
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
} }
func pullRepository(transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void) throws { func pullRepository(transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
@ -297,6 +298,7 @@ class PasswordStore {
] ]
let remote = try GTRemote(name: "origin", in: storeRepository!) let remote = try GTRemote(name: "origin", in: storeRepository!)
try storeRepository?.pull((storeRepository?.currentBranch())!, from: remote, withOptions: options, progress: transferProgressBlock) try storeRepository?.pull((storeRepository?.currentBranch())!, from: remote, withOptions: options, progress: transferProgressBlock)
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
} }
@ -542,18 +544,22 @@ class PasswordStore {
print(saveURL.path) print(saveURL.path)
let _ = createAddCommitInRepository(message: "Add password for \(passwordEntity.nameWithCategory) to store using Pass for iOS.", fileData: encryptedData, filename: saveURL.lastPathComponent, progressBlock: progressBlock) let _ = createAddCommitInRepository(message: "Add password for \(passwordEntity.nameWithCategory) to store using Pass for iOS.", fileData: encryptedData, filename: saveURL.lastPathComponent, progressBlock: progressBlock)
progressBlock(1.0) progressBlock(1.0)
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
} catch { } catch {
print(error) print(error)
} }
} }
func update(passwordEntity: PasswordEntity, password: Password, progressBlock: (_ progress: Float) -> Void) { func update(passwordEntity: PasswordEntity, password: Password, progressBlock: (_ progress: Float) -> Void) {
progressBlock(0.0)
do { do {
let encryptedData = try passwordEntity.encrypt(password: password) let encryptedData = try passwordEntity.encrypt(password: password)
let saveURL = storeURL.appendingPathComponent(passwordEntity.path!) let saveURL = storeURL.appendingPathComponent(passwordEntity.path!)
try encryptedData.write(to: saveURL) try encryptedData.write(to: saveURL)
progressBlock(0.3) progressBlock(0.3)
let _ = createAddCommitInRepository(message: "Edit password for \(passwordEntity.nameWithCategory) using Pass for iOS.", fileData: encryptedData, filename: saveURL.lastPathComponent, progressBlock: progressBlock) let _ = createAddCommitInRepository(message: "Edit password for \(passwordEntity.nameWithCategory) using Pass for iOS.", fileData: encryptedData, filename: saveURL.lastPathComponent, progressBlock: progressBlock)
progressBlock(1.0)
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
} catch { } catch {
print(error) print(error)
} }
@ -621,6 +627,9 @@ class PasswordStore {
Defaults.removeAll() Defaults.removeAll()
storeRepository = nil storeRepository = nil
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
NotificationCenter.default.post(name: .passwordStoreErased, object: nil)
} }
// return the number of discarded commits // return the number of discarded commits
@ -645,7 +654,8 @@ class PasswordStore {
} }
try self.storeRepository?.reset(to: newHead, resetType: GTRepositoryResetType.hard) try self.storeRepository?.reset(to: newHead, resetType: GTRepositoryResetType.hard)
self.updatePasswordEntityCoreData() self.updatePasswordEntityCoreData()
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated"))) NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
NotificationCenter.default.post(name: .passwordStoreChangeDiscarded, object: nil)
self.setAllSynced() self.setAllSynced()
return localCommits.count return localCommits.count
} else { } else {

View file

@ -131,7 +131,6 @@ class LabelTableViewCell: UITableViewCell {
DispatchQueue.main.async { DispatchQueue.main.async {
passwordEntity.synced = false passwordEntity.synced = false
self.passwordStore.saveUpdated(passwordEntity: passwordEntity) self.passwordStore.saveUpdated(passwordEntity: passwordEntity)
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
// reload so that the "unsynced" symbol could be added // reload so that the "unsynced" symbol could be added
self.passwordTableView?.tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: UITableViewRowAnimation.automatic) self.passwordTableView?.tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: UITableViewRowAnimation.automatic)
SVProgressHUD.showSuccess(withStatus: "Password Copied\nCounter Updated") SVProgressHUD.showSuccess(withStatus: "Password Copied\nCounter Updated")