change Globals class to static

This commit is contained in:
Bob Sun 2017-02-08 10:15:38 +08:00
parent 825cd5446d
commit f9e42b9490
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
7 changed files with 27 additions and 28 deletions

View file

@ -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. // 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 your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
if PasscodeLockRepository().hasPasscode { 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) UIApplication.shared.keyWindow?.rootViewController?.present(passcodeEnterViewController, animated: true, completion: nil)
} }
} }

View file

@ -9,11 +9,10 @@
import Foundation import Foundation
class Globals { class Globals {
static let shared = Globals() static let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0];
let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]; static let secringPath = "\(documentPath)/secring.gpg"
let secringPath = "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/secring.gpg" static let sshPublicKeyPath = URL(fileURLWithPath: "\(documentPath)/ssh_key.pub")
let sshPublicKeyPath = URL(fileURLWithPath: "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/ssh_key.pub") static let sshPrivateKeyPath = URL(fileURLWithPath: "\(documentPath)/ssh_key")
let sshPrivateKeyPath = URL(fileURLWithPath: "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/ssh_key") static var passcodeConfiguration = PasscodeLockConfiguration()
var passcodeConfiguration = PasscodeLockConfiguration()
private init() { } private init() { }
} }

View file

@ -38,7 +38,7 @@ class Password {
extension PasswordEntity { extension PasswordEntity {
func decrypt() throws -> Password? { func decrypt() throws -> Password? {
var password: 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 encryptedData = try Data(contentsOf: encryptedDataPath)
let decryptedData = try PasswordStore.shared.pgp.decryptData(encryptedData, passphrase: Defaults[.pgpKeyPassphrase]) let decryptedData = try PasswordStore.shared.pgp.decryptData(encryptedData, passphrase: Defaults[.pgpKeyPassphrase])
let plain = String(data: decryptedData, encoding: .ascii) ?? "" let plain = String(data: decryptedData, encoding: .ascii) ?? ""

View file

@ -41,8 +41,8 @@ struct GitCredential {
class PasswordStore { class PasswordStore {
static let shared = PasswordStore() static let shared = PasswordStore()
let storeURL = URL(fileURLWithPath: "\(Globals.shared.documentPath)/password-store") let storeURL = URL(fileURLWithPath: "\(Globals.documentPath)/password-store")
let tempStoreURL = URL(fileURLWithPath: "\(Globals.shared.documentPath)/password-store-temp") let tempStoreURL = URL(fileURLWithPath: "\(Globals.documentPath)/password-store-temp")
var storeRepository: GTRepository? var storeRepository: GTRepository?
var gitCredential: GitCredential? var gitCredential: GitCredential?
@ -60,12 +60,12 @@ class PasswordStore {
print(error) print(error)
} }
if Defaults[.pgpKeyID] != "" { if Defaults[.pgpKeyID] != "" {
pgp.importKeys(fromFile: Globals.shared.secringPath, allowDuplicates: false) pgp.importKeys(fromFile: Globals.secringPath, allowDuplicates: false)
} }
if Defaults[.gitRepositoryAuthenticationMethod] == "Password" { if Defaults[.gitRepositoryAuthenticationMethod] == "Password" {
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: Defaults[.gitRepositoryUsername], password: Defaults[.gitRepositoryPassword])) gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: Defaults[.gitRepositoryUsername], password: Defaults[.gitRepositoryPassword]))
} else if Defaults[.gitRepositoryAuthenticationMethod] == "SSH Key"{ } 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 { } else {
gitCredential = nil gitCredential = nil
} }
@ -196,16 +196,16 @@ class PasswordStore {
try fm.removeItem(at: storeURL) try fm.removeItem(at: storeURL)
} }
if fm.fileExists(atPath: Globals.shared.secringPath) { if fm.fileExists(atPath: Globals.secringPath) {
try fm.removeItem(atPath: Globals.shared.secringPath) try fm.removeItem(atPath: Globals.secringPath)
} }
if fm.fileExists(atPath: Globals.shared.sshPrivateKeyPath.path) { if fm.fileExists(atPath: Globals.sshPrivateKeyPath.path) {
try fm.removeItem(at: Globals.shared.sshPrivateKeyPath) try fm.removeItem(at: Globals.sshPrivateKeyPath)
} }
if fm.fileExists(atPath: Globals.shared.sshPublicKeyPath.path) { if fm.fileExists(atPath: Globals.sshPublicKeyPath.path) {
try fm.removeItem(at: Globals.shared.sshPublicKeyPath) try fm.removeItem(at: Globals.sshPublicKeyPath)
} }
} catch { } catch {
print(error) print(error)

View file

@ -58,7 +58,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
super.viewDidLoad() super.viewDidLoad()
if PasscodeLockRepository().hasPasscode { 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) UIApplication.shared.keyWindow?.rootViewController?.present(passcodeEnterViewController, animated: true, completion: nil)
} }
passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData() passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()

View file

@ -36,8 +36,8 @@ class SSHKeySettingTableViewController: UITableViewController {
Defaults[.gitRepositorySSHPrivateKeyPassphrase] = passphraseTextField.text! Defaults[.gitRepositorySSHPrivateKeyPassphrase] = passphraseTextField.text!
do { do {
try Data(contentsOf: Defaults[.gitRepositorySSHPublicKeyURL]!).write(to: Globals.shared.sshPublicKeyPath, options: .atomic) try Data(contentsOf: Defaults[.gitRepositorySSHPublicKeyURL]!).write(to: Globals.sshPublicKeyPath, options: .atomic)
try Data(contentsOf: Defaults[.gitRepositorySSHPrivateKeyURL]!).write(to: Globals.shared.sshPrivateKeyPath, options: .atomic) try Data(contentsOf: Defaults[.gitRepositorySSHPrivateKeyURL]!).write(to: Globals.sshPrivateKeyPath, options: .atomic)
} catch { } catch {
print(error) print(error)
} }

View file

@ -37,7 +37,7 @@ class SettingsTableViewController: UITableViewController {
if auth == "Password" { if auth == "Password" {
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: username, password: password)) gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: username, password: password))
} else { } 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 { DispatchQueue.global(qos: .userInitiated).async {
@ -91,7 +91,7 @@ class SettingsTableViewController: UITableViewController {
SVProgressHUD.show(withStatus: "Fetching PGP Key") SVProgressHUD.show(withStatus: "Fetching PGP Key")
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
do { 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 { DispatchQueue.main.async {
self.pgpKeyTableViewCell.detailTextLabel?.text = Defaults[.pgpKeyID] self.pgpKeyTableViewCell.detailTextLabel?.text = Defaults[.pgpKeyID]
SVProgressHUD.showSuccess(withStatus: "Success. Remember to remove the key from the server.") SVProgressHUD.showSuccess(withStatus: "Success. Remember to remove the key from the server.")
@ -150,16 +150,16 @@ class SettingsTableViewController: UITableViewController {
func touchIDSwitchAction(uiSwitch: UISwitch) { func touchIDSwitchAction(uiSwitch: UISwitch) {
if uiSwitch.isOn { if uiSwitch.isOn {
Defaults[.isTouchIDOn] = true Defaults[.isTouchIDOn] = true
Globals.shared.passcodeConfiguration.isTouchIDAllowed = true Globals.passcodeConfiguration.isTouchIDAllowed = true
} else { } else {
Defaults[.isTouchIDOn] = false Defaults[.isTouchIDOn] = false
Globals.shared.passcodeConfiguration.isTouchIDAllowed = false Globals.passcodeConfiguration.isTouchIDAllowed = false
} }
} }
func showPasscodeActionSheet() { func showPasscodeActionSheet() {
let passcodeChangeViewController = PasscodeLockViewController(state: .change, configuration: Globals.shared.passcodeConfiguration) let passcodeChangeViewController = PasscodeLockViewController(state: .change, configuration: Globals.passcodeConfiguration)
let passcodeRemoveViewController = PasscodeLockViewController(state: .remove, configuration: Globals.shared.passcodeConfiguration) let passcodeRemoveViewController = PasscodeLockViewController(state: .remove, configuration: Globals.passcodeConfiguration)
let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let removePasscodeAction = UIAlertAction(title: "Remove Passcode", style: .destructive) { [unowned self] _ in let removePasscodeAction = UIAlertAction(title: "Remove Passcode", style: .destructive) { [unowned self] _ in
@ -182,7 +182,7 @@ class SettingsTableViewController: UITableViewController {
} }
func setPasscodeLock() { func setPasscodeLock() {
let passcodeSetViewController = PasscodeLockViewController(state: .set, configuration: Globals.shared.passcodeConfiguration) let passcodeSetViewController = PasscodeLockViewController(state: .set, configuration: Globals.passcodeConfiguration)
passcodeSetViewController.successCallback = { _ in passcodeSetViewController.successCallback = { _ in
self.passcodeTableViewCell.detailTextLabel?.text = "On" self.passcodeTableViewCell.detailTextLabel?.text = "On"
self.touchIDSwitch.isEnabled = true self.touchIDSwitch.isEnabled = true