Merge and resolve conflit
This commit is contained in:
commit
c21502a10f
5 changed files with 139 additions and 111 deletions
|
|
@ -13,26 +13,25 @@ import SVProgressHUD
|
|||
|
||||
class PasswordDetailTableViewController: UITableViewController, UIGestureRecognizerDelegate {
|
||||
var passwordEntity: PasswordEntity?
|
||||
var passwordCategoryText = ""
|
||||
var password: Password?
|
||||
var passwordImage: UIImage?
|
||||
var oneTimePasswordIndexPath : IndexPath?
|
||||
var shouldPopCurrentView = false
|
||||
let passwordStore = PasswordStore.shared
|
||||
private var password: Password?
|
||||
private var passwordCategoryText = ""
|
||||
private var passwordImage: UIImage?
|
||||
private var oneTimePasswordIndexPath : IndexPath?
|
||||
private var shouldPopCurrentView = false
|
||||
private let passwordStore = PasswordStore.shared
|
||||
|
||||
let indicator: UIActivityIndicatorView = {
|
||||
private let indicator: UIActivityIndicatorView = {
|
||||
let indicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
|
||||
indicator.center = CGPoint(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height * 0.382)
|
||||
return indicator
|
||||
}()
|
||||
|
||||
lazy var editUIBarButtonItem: UIBarButtonItem = {
|
||||
private lazy var editUIBarButtonItem: UIBarButtonItem = {
|
||||
let uiBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(pressEdit(_:)))
|
||||
return uiBarButtonItem
|
||||
}()
|
||||
|
||||
|
||||
struct TableCell {
|
||||
private struct TableCell {
|
||||
var title: String
|
||||
var content: String
|
||||
init() {
|
||||
|
|
@ -46,12 +45,12 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
|||
}
|
||||
}
|
||||
|
||||
struct TableSection {
|
||||
private struct TableSection {
|
||||
var title: String
|
||||
var item: Array<TableCell>
|
||||
}
|
||||
|
||||
var tableData = Array<TableSection>()
|
||||
private var tableData = Array<TableSection>()
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
|
@ -67,7 +66,6 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
|||
tableView.contentInset = UIEdgeInsetsMake(-36, 0, 0, 0);
|
||||
tableView.rowHeight = UITableViewAutomaticDimension
|
||||
tableView.estimatedRowHeight = 52
|
||||
|
||||
|
||||
indicator.startAnimating()
|
||||
tableView.addSubview(indicator)
|
||||
|
|
@ -96,16 +94,29 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
|||
self.present(alert, animated: true, completion: nil)
|
||||
}
|
||||
|
||||
self.setupUpdateOneTimePassword()
|
||||
self.addNotificationObservers()
|
||||
|
||||
self.setupOneTimePasswordAutoRefresh()
|
||||
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(setShouldPopCurrentView), name: .passwordStoreChangeDiscarded, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(showPassword), name: .passwordStoreUpdated, object: nil)
|
||||
}
|
||||
|
||||
func decryptThenShowPassword(passphrase: String) {
|
||||
override func viewDidAppear(_ animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
if self.shouldPopCurrentView {
|
||||
let alert = UIAlertController(title: "Notice", message: "All previous local changes have been discarded. Your current Password Store will be shown.", preferredStyle: UIAlertControllerStyle.alert)
|
||||
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {_ in
|
||||
_ = self.navigationController?.popViewController(animated: true)
|
||||
}))
|
||||
self.present(alert, animated: true, completion: nil)
|
||||
}
|
||||
}
|
||||
|
||||
private func decryptThenShowPassword(passphrase: String) {
|
||||
if Defaults[.isRememberPassphraseOn] {
|
||||
self.passwordStore.pgpKeyPassphrase = passphrase
|
||||
}
|
||||
DispatchQueue.global(qos: .userInitiated).async {
|
||||
// decrypt password
|
||||
do {
|
||||
self.password = try self.passwordEntity!.decrypt(passphrase: passphrase)!
|
||||
} catch {
|
||||
|
|
@ -118,27 +129,30 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
|||
}
|
||||
return
|
||||
}
|
||||
|
||||
let password = self.password!
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
self?.showPassword(password: password)
|
||||
// display password
|
||||
self.showPassword()
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func showPassword() {
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
self?.indicator.stopAnimating()
|
||||
self?.setTableData()
|
||||
UIView.performWithoutAnimation {
|
||||
self?.tableView.reloadData()
|
||||
// add layoutIfNeeded solves the "flickering problem" during refresh
|
||||
self?.tableView.layoutIfNeeded()
|
||||
}
|
||||
self?.editUIBarButtonItem.isEnabled = true
|
||||
if let urlString = self?.password?.getURLString() {
|
||||
if self?.passwordEntity?.image == nil {
|
||||
self?.updatePasswordImage(urlString: urlString)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func showPassword(password: Password) {
|
||||
setTableData()
|
||||
self.tableView.reloadData()
|
||||
indicator.stopAnimating()
|
||||
editUIBarButtonItem.isEnabled = true
|
||||
if let urlString = password.getURLString() {
|
||||
if self.passwordEntity?.image == nil{
|
||||
self.updatePasswordImage(urlString: urlString)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setupUpdateOneTimePassword() {
|
||||
private func setupOneTimePasswordAutoRefresh() {
|
||||
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {
|
||||
[weak self] timer in
|
||||
// bail out of the timer code if the object has been freed
|
||||
|
|
@ -163,15 +177,19 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
|||
}
|
||||
}
|
||||
|
||||
func pressEdit(_ sender: Any?) {
|
||||
@objc private func pressEdit(_ sender: Any?) {
|
||||
performSegue(withIdentifier: "editPasswordSegue", sender: self)
|
||||
}
|
||||
|
||||
@IBAction func cancelEditPassword(segue: UIStoryboardSegue) {
|
||||
@objc private func setShouldPopCurrentView() {
|
||||
self.shouldPopCurrentView = true
|
||||
}
|
||||
|
||||
@IBAction private func cancelEditPassword(segue: UIStoryboardSegue) {
|
||||
|
||||
}
|
||||
|
||||
@IBAction func saveEditPassword(segue: UIStoryboardSegue) {
|
||||
@IBAction private func saveEditPassword(segue: UIStoryboardSegue) {
|
||||
if self.password!.changed {
|
||||
SVProgressHUD.show(withStatus: "Saving")
|
||||
DispatchQueue.global(qos: .userInitiated).async {
|
||||
|
|
@ -192,13 +210,13 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
|||
}
|
||||
}
|
||||
|
||||
@IBAction func deletePassword(segue: UIStoryboardSegue) {
|
||||
@IBAction private func deletePassword(segue: UIStoryboardSegue) {
|
||||
print("delete")
|
||||
passwordStore.delete(passwordEntity: passwordEntity!)
|
||||
navigationController?.popViewController(animated: true)
|
||||
}
|
||||
|
||||
func setTableData() {
|
||||
|
||||
private func setTableData() {
|
||||
self.tableData = Array<TableSection>()
|
||||
tableData.append(TableSection(title: "", item: []))
|
||||
tableData[0].item.append(TableCell())
|
||||
|
|
@ -260,7 +278,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
|||
}
|
||||
}
|
||||
|
||||
func updatePasswordImage(urlString: String) {
|
||||
private func updatePasswordImage(urlString: String) {
|
||||
var newUrlString = urlString
|
||||
if urlString.lowercased().hasPrefix("http://") {
|
||||
// try to replace http url to https url
|
||||
|
|
@ -299,7 +317,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
|||
}
|
||||
}
|
||||
|
||||
func tapMenu(recognizer: UITapGestureRecognizer) {
|
||||
@objc private func tapMenu(recognizer: UITapGestureRecognizer) {
|
||||
if recognizer.state == UIGestureRecognizerState.ended {
|
||||
let tapLocation = recognizer.location(in: self.tableView)
|
||||
if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
|
||||
|
|
@ -308,9 +326,9 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
|||
let menuController = UIMenuController.shared
|
||||
let revealItem = UIMenuItem(title: "Reveal", action: #selector(LabelTableViewCell.revealPassword(_:)))
|
||||
let concealItem = UIMenuItem(title: "Conceal", action: #selector(LabelTableViewCell.concealPassword(_:)))
|
||||
let nextPasswordItem = UIMenuItem(title: "Next Password", action: #selector(LabelTableViewCell.nextPassword(_:)))
|
||||
let nextHOTPItem = UIMenuItem(title: "Next Password", action: #selector(LabelTableViewCell.getNextHOTP(_:)))
|
||||
let openURLItem = UIMenuItem(title: "Copy Password & Open Link", action: #selector(LabelTableViewCell.openLink(_:)))
|
||||
menuController.menuItems = [revealItem, concealItem, nextPasswordItem, openURLItem]
|
||||
menuController.menuItems = [revealItem, concealItem, nextHOTPItem, openURLItem]
|
||||
menuController.setTargetRect(tappedCell.contentLabel.frame, in: tappedCell.contentLabel.superview!)
|
||||
menuController.setMenuVisible(true, animated: true)
|
||||
}
|
||||
|
|
@ -318,6 +336,46 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
|||
}
|
||||
}
|
||||
|
||||
func getNextHOTP() {
|
||||
guard password != nil, passwordEntity != nil, password?.otpType == .hotp else {
|
||||
DispatchQueue.main.async {
|
||||
Utils.alert(title: "Error", message: "Get next password of a non-HOTP entry.", controller: self, completion: nil)
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// increase HOTP counter
|
||||
password!.increaseHotpCounter()
|
||||
|
||||
// copy HOTP to pasteboard
|
||||
if let plainPassword = password!.otpToken?.currentPassword {
|
||||
Utils.copyToPasteboard(textToCopy: plainPassword)
|
||||
}
|
||||
|
||||
// commit the change of HOTP counter
|
||||
if password!.changed {
|
||||
DispatchQueue.global(qos: .userInitiated).async {
|
||||
self.passwordStore.update(passwordEntity: self.passwordEntity!, password: self.password!, progressBlock: {_ in })
|
||||
DispatchQueue.main.async {
|
||||
self.passwordEntity!.synced = false
|
||||
self.passwordStore.saveUpdated(passwordEntity: self.passwordEntity!)
|
||||
SVProgressHUD.showSuccess(withStatus: "Password Copied\nCounter Updated")
|
||||
SVProgressHUD.dismiss(withDelay: 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func openLink() {
|
||||
guard let urlString = self.password?.getURLString(), let url = URL(string: urlString) else {
|
||||
DispatchQueue.main.async {
|
||||
Utils.alert(title: "Error", message: "Cannot find a valid URL", controller: self, completion: nil)
|
||||
}
|
||||
return;
|
||||
}
|
||||
Utils.copyToPasteboard(textToCopy: password?.password)
|
||||
UIApplication.shared.open(url, options: [:], completionHandler: nil)
|
||||
}
|
||||
|
||||
override func numberOfSections(in tableView: UITableView) -> Int {
|
||||
return tableData.count
|
||||
|
|
@ -345,7 +403,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
|||
let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell", for: indexPath) as! LabelTableViewCell
|
||||
let titleData = tableData[sectionIndex].item[rowIndex].title
|
||||
let contentData = tableData[sectionIndex].item[rowIndex].content
|
||||
cell.passwordTableView = self
|
||||
cell.delegatePasswordTableView = self
|
||||
cell.isPasswordCell = (titleData.lowercased() == "password" ? true : false)
|
||||
cell.isURLCell = (titleData.lowercased() == "url" ? true : false)
|
||||
cell.isHOTPCell = (titleData == "HMAC-based" ? true : false)
|
||||
|
|
@ -386,23 +444,4 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
|||
override func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
|
||||
return true
|
||||
}
|
||||
|
||||
private func addNotificationObservers() {
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(setShouldPopCurrentView), name: .passwordStoreChangeDiscarded, object: nil)
|
||||
}
|
||||
|
||||
func setShouldPopCurrentView() {
|
||||
self.shouldPopCurrentView = true
|
||||
}
|
||||
|
||||
override func viewDidAppear(_ animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
if self.shouldPopCurrentView {
|
||||
let alert = UIAlertController(title: "Notice", message: "All previous local changes have been discarded. Your current Password Store will be shown.", preferredStyle: UIAlertControllerStyle.alert)
|
||||
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {_ in
|
||||
_ = self.navigationController?.popViewController(animated: true)
|
||||
}))
|
||||
self.present(alert, animated: true, completion: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue