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.
// 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)
}
}

View file

@ -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() { }
}

View file

@ -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) ?? ""

View file

@ -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)

View file

@ -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()

View file

@ -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)
}

View file

@ -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