From f9e42b9490d9cae5579caa2d27dd5f51fa25220f Mon Sep 17 00:00:00 2001 From: Bob Sun Date: Wed, 8 Feb 2017 10:15:38 +0800 Subject: [PATCH] change Globals class to static --- pass/AppDelegate.swift | 2 +- pass/Globals.swift | 11 +++++------ pass/Password.swift | 2 +- pass/PasswordStore.swift | 20 ++++++++++---------- pass/PasswordsViewController.swift | 2 +- pass/SSHKeySettingTableViewController.swift | 4 ++-- pass/SettingsTableViewController.swift | 14 +++++++------- 7 files changed, 27 insertions(+), 28 deletions(-) diff --git a/pass/AppDelegate.swift b/pass/AppDelegate.swift index ff28f94..a460f3b 100644 --- a/pass/AppDelegate.swift +++ b/pass/AppDelegate.swift @@ -30,7 +30,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. if PasscodeLockRepository().hasPasscode { - let passcodeEnterViewController = PasscodeLockViewController(state: .enter, configuration: Globals.shared.passcodeConfiguration) + let passcodeEnterViewController = PasscodeLockViewController(state: .enter, configuration: Globals.passcodeConfiguration) UIApplication.shared.keyWindow?.rootViewController?.present(passcodeEnterViewController, animated: true, completion: nil) } } diff --git a/pass/Globals.swift b/pass/Globals.swift index 302fafc..23111cf 100644 --- a/pass/Globals.swift +++ b/pass/Globals.swift @@ -9,11 +9,10 @@ import Foundation class Globals { - static let shared = Globals() - let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]; - let secringPath = "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/secring.gpg" - let sshPublicKeyPath = URL(fileURLWithPath: "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/ssh_key.pub") - let sshPrivateKeyPath = URL(fileURLWithPath: "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/ssh_key") - var passcodeConfiguration = PasscodeLockConfiguration() + static let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]; + static let secringPath = "\(documentPath)/secring.gpg" + static let sshPublicKeyPath = URL(fileURLWithPath: "\(documentPath)/ssh_key.pub") + static let sshPrivateKeyPath = URL(fileURLWithPath: "\(documentPath)/ssh_key") + static var passcodeConfiguration = PasscodeLockConfiguration() private init() { } } diff --git a/pass/Password.swift b/pass/Password.swift index 9bcfea3..a31c2cb 100644 --- a/pass/Password.swift +++ b/pass/Password.swift @@ -38,7 +38,7 @@ class Password { extension PasswordEntity { func decrypt() throws -> Password? { var password: Password? - let encryptedDataPath = URL(fileURLWithPath: "\(Globals.shared.documentPath)/\(rawPath!)") + let encryptedDataPath = URL(fileURLWithPath: "\(Globals.documentPath)/\(rawPath!)") let encryptedData = try Data(contentsOf: encryptedDataPath) let decryptedData = try PasswordStore.shared.pgp.decryptData(encryptedData, passphrase: Defaults[.pgpKeyPassphrase]) let plain = String(data: decryptedData, encoding: .ascii) ?? "" diff --git a/pass/PasswordStore.swift b/pass/PasswordStore.swift index 30edc2a..d256b46 100644 --- a/pass/PasswordStore.swift +++ b/pass/PasswordStore.swift @@ -41,8 +41,8 @@ struct GitCredential { class PasswordStore { static let shared = PasswordStore() - let storeURL = URL(fileURLWithPath: "\(Globals.shared.documentPath)/password-store") - let tempStoreURL = URL(fileURLWithPath: "\(Globals.shared.documentPath)/password-store-temp") + let storeURL = URL(fileURLWithPath: "\(Globals.documentPath)/password-store") + let tempStoreURL = URL(fileURLWithPath: "\(Globals.documentPath)/password-store-temp") var storeRepository: GTRepository? var gitCredential: GitCredential? @@ -60,12 +60,12 @@ class PasswordStore { print(error) } if Defaults[.pgpKeyID] != "" { - pgp.importKeys(fromFile: Globals.shared.secringPath, allowDuplicates: false) + pgp.importKeys(fromFile: Globals.secringPath, allowDuplicates: false) } if Defaults[.gitRepositoryAuthenticationMethod] == "Password" { gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: Defaults[.gitRepositoryUsername], password: Defaults[.gitRepositoryPassword])) } else if Defaults[.gitRepositoryAuthenticationMethod] == "SSH Key"{ - gitCredential = GitCredential(credential: GitCredential.Credential.ssh(userName: Defaults[.gitRepositoryUsername], password: Defaults[.gitRepositorySSHPrivateKeyPassphrase]!, publicKeyFile: Globals.shared.sshPublicKeyPath, privateKeyFile: Globals.shared.sshPrivateKeyPath)) + gitCredential = GitCredential(credential: GitCredential.Credential.ssh(userName: Defaults[.gitRepositoryUsername], password: Defaults[.gitRepositorySSHPrivateKeyPassphrase]!, publicKeyFile: Globals.sshPublicKeyPath, privateKeyFile: Globals.sshPrivateKeyPath)) } else { gitCredential = nil } @@ -196,16 +196,16 @@ class PasswordStore { try fm.removeItem(at: storeURL) } - if fm.fileExists(atPath: Globals.shared.secringPath) { - try fm.removeItem(atPath: Globals.shared.secringPath) + if fm.fileExists(atPath: Globals.secringPath) { + try fm.removeItem(atPath: Globals.secringPath) } - if fm.fileExists(atPath: Globals.shared.sshPrivateKeyPath.path) { - try fm.removeItem(at: Globals.shared.sshPrivateKeyPath) + if fm.fileExists(atPath: Globals.sshPrivateKeyPath.path) { + try fm.removeItem(at: Globals.sshPrivateKeyPath) } - if fm.fileExists(atPath: Globals.shared.sshPublicKeyPath.path) { - try fm.removeItem(at: Globals.shared.sshPublicKeyPath) + if fm.fileExists(atPath: Globals.sshPublicKeyPath.path) { + try fm.removeItem(at: Globals.sshPublicKeyPath) } } catch { print(error) diff --git a/pass/PasswordsViewController.swift b/pass/PasswordsViewController.swift index 702cfcf..4e6e59c 100644 --- a/pass/PasswordsViewController.swift +++ b/pass/PasswordsViewController.swift @@ -58,7 +58,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV super.viewDidLoad() if PasscodeLockRepository().hasPasscode { - let passcodeEnterViewController = PasscodeLockViewController(state: .enter, configuration: Globals.shared.passcodeConfiguration) + let passcodeEnterViewController = PasscodeLockViewController(state: .enter, configuration: Globals.passcodeConfiguration) UIApplication.shared.keyWindow?.rootViewController?.present(passcodeEnterViewController, animated: true, completion: nil) } passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData() diff --git a/pass/SSHKeySettingTableViewController.swift b/pass/SSHKeySettingTableViewController.swift index 99639b1..1cfb779 100644 --- a/pass/SSHKeySettingTableViewController.swift +++ b/pass/SSHKeySettingTableViewController.swift @@ -36,8 +36,8 @@ class SSHKeySettingTableViewController: UITableViewController { Defaults[.gitRepositorySSHPrivateKeyPassphrase] = passphraseTextField.text! do { - try Data(contentsOf: Defaults[.gitRepositorySSHPublicKeyURL]!).write(to: Globals.shared.sshPublicKeyPath, options: .atomic) - try Data(contentsOf: Defaults[.gitRepositorySSHPrivateKeyURL]!).write(to: Globals.shared.sshPrivateKeyPath, options: .atomic) + try Data(contentsOf: Defaults[.gitRepositorySSHPublicKeyURL]!).write(to: Globals.sshPublicKeyPath, options: .atomic) + try Data(contentsOf: Defaults[.gitRepositorySSHPrivateKeyURL]!).write(to: Globals.sshPrivateKeyPath, options: .atomic) } catch { print(error) } diff --git a/pass/SettingsTableViewController.swift b/pass/SettingsTableViewController.swift index ffa917a..c8ee106 100644 --- a/pass/SettingsTableViewController.swift +++ b/pass/SettingsTableViewController.swift @@ -37,7 +37,7 @@ class SettingsTableViewController: UITableViewController { if auth == "Password" { gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: username, password: password)) } else { - gitCredential = GitCredential(credential: GitCredential.Credential.ssh(userName: username, password: Defaults[.gitRepositorySSHPrivateKeyPassphrase]!, publicKeyFile: Globals.shared.sshPublicKeyPath, privateKeyFile: Globals.shared.sshPrivateKeyPath)) + gitCredential = GitCredential(credential: GitCredential.Credential.ssh(userName: username, password: Defaults[.gitRepositorySSHPrivateKeyPassphrase]!, publicKeyFile: Globals.sshPublicKeyPath, privateKeyFile: Globals.sshPrivateKeyPath)) } DispatchQueue.global(qos: .userInitiated).async { @@ -91,7 +91,7 @@ class SettingsTableViewController: UITableViewController { SVProgressHUD.show(withStatus: "Fetching PGP Key") DispatchQueue.global(qos: .userInitiated).async { [unowned self] in do { - try PasswordStore.shared.initPGP(pgpKeyURL: Defaults[.pgpKeyURL]!, pgpKeyLocalPath: Globals.shared.secringPath) + try PasswordStore.shared.initPGP(pgpKeyURL: Defaults[.pgpKeyURL]!, pgpKeyLocalPath: Globals.secringPath) DispatchQueue.main.async { self.pgpKeyTableViewCell.detailTextLabel?.text = Defaults[.pgpKeyID] SVProgressHUD.showSuccess(withStatus: "Success. Remember to remove the key from the server.") @@ -150,16 +150,16 @@ class SettingsTableViewController: UITableViewController { func touchIDSwitchAction(uiSwitch: UISwitch) { if uiSwitch.isOn { Defaults[.isTouchIDOn] = true - Globals.shared.passcodeConfiguration.isTouchIDAllowed = true + Globals.passcodeConfiguration.isTouchIDAllowed = true } else { Defaults[.isTouchIDOn] = false - Globals.shared.passcodeConfiguration.isTouchIDAllowed = false + Globals.passcodeConfiguration.isTouchIDAllowed = false } } func showPasscodeActionSheet() { - let passcodeChangeViewController = PasscodeLockViewController(state: .change, configuration: Globals.shared.passcodeConfiguration) - let passcodeRemoveViewController = PasscodeLockViewController(state: .remove, configuration: Globals.shared.passcodeConfiguration) + let passcodeChangeViewController = PasscodeLockViewController(state: .change, configuration: Globals.passcodeConfiguration) + let passcodeRemoveViewController = PasscodeLockViewController(state: .remove, configuration: Globals.passcodeConfiguration) let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) let removePasscodeAction = UIAlertAction(title: "Remove Passcode", style: .destructive) { [unowned self] _ in @@ -182,7 +182,7 @@ class SettingsTableViewController: UITableViewController { } func setPasscodeLock() { - let passcodeSetViewController = PasscodeLockViewController(state: .set, configuration: Globals.shared.passcodeConfiguration) + let passcodeSetViewController = PasscodeLockViewController(state: .set, configuration: Globals.passcodeConfiguration) passcodeSetViewController.successCallback = { _ in self.passcodeTableViewCell.detailTextLabel?.text = "On" self.touchIDSwitch.isEnabled = true