Refactor GitCredential to simplify it and to add tests

This commit is contained in:
Danny Moesch 2020-08-23 01:15:23 +02:00 committed by Mingshen Sun
parent 56b7b24fce
commit 6044098278
11 changed files with 295 additions and 225 deletions

View file

@ -1,53 +0,0 @@
//
// GitCredentialPassword.swift
// pass
//
// Created by Sun, Mingshen on 11/30/19.
// Copyright © 2019 Bob Sun. All rights reserved.
//
import Foundation
import passKit
import SVProgressHUD
public func requestGitCredentialPassword(
credential: GitCredential.Credential,
lastPassword: String?,
controller: UIViewController
) -> String? {
let sem = DispatchSemaphore(value: 0)
var password: String?
let message: String = {
switch credential {
case .http:
return "FillInGitAccountPassword.".localize()
case .ssh:
return "FillInSshKeyPassphrase.".localize()
}
}()
DispatchQueue.main.async {
SVProgressHUD.dismiss()
let alert = UIAlertController(title: "Password".localize(), message: message, preferredStyle: .alert)
alert.addTextField {
$0.text = lastPassword ?? ""
$0.isSecureTextEntry = true
}
alert.addAction(
UIAlertAction.ok { _ in
password = alert.textFields?.first?.text
sem.signal()
}
)
alert.addAction(
UIAlertAction.cancel { _ in
password = nil
sem.signal()
}
)
controller.present(alert, animated: true)
}
_ = sem.wait(timeout: .distantFuture)
return password
}

View file

@ -0,0 +1,45 @@
//
// PasswordAlertPresenter.swift
// pass
//
// Created by Danny Moesch on 23.08.20.
// Copyright © 2020 Bob Sun. All rights reserved.
//
import SVProgressHUD
protocol PasswordAlertPresenter {
func present(message: String, lastPassword: String?) -> String?
}
extension PasswordAlertPresenter where Self: UIViewController {
func present(message: String, lastPassword: String?) -> String? {
let sem = DispatchSemaphore(value: 0)
var password: String?
DispatchQueue.main.async {
SVProgressHUD.dismiss()
let alert = UIAlertController(title: "Password".localize(), message: message, preferredStyle: .alert)
alert.addTextField {
$0.text = lastPassword ?? ""
$0.isSecureTextEntry = true
}
alert.addAction(
.ok { _ in
password = alert.textFields?.first?.text
sem.signal()
}
)
alert.addAction(
.cancel { _ in
password = nil
sem.signal()
}
)
self.present(alert, animated: true)
}
_ = sem.wait(timeout: .distantFuture)
return password
}
}