2017-02-02 21:04:31 +08:00
|
|
|
//
|
|
|
|
|
// PasswordDetailTableViewController.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 2/2/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
2017-02-09 13:17:11 +08:00
|
|
|
import FavIcon
|
2017-02-15 20:01:17 +08:00
|
|
|
import SVProgressHUD
|
2017-06-13 11:42:49 +08:00
|
|
|
import passKit
|
2017-02-02 21:04:31 +08:00
|
|
|
|
2017-02-05 00:35:23 +08:00
|
|
|
class PasswordDetailTableViewController: UITableViewController, UIGestureRecognizerDelegate {
|
2017-02-02 21:04:31 +08:00
|
|
|
var passwordEntity: PasswordEntity?
|
2017-03-22 01:39:26 +08:00
|
|
|
private var password: Password?
|
|
|
|
|
private var passwordImage: UIImage?
|
|
|
|
|
private var oneTimePasswordIndexPath : IndexPath?
|
|
|
|
|
private var shouldPopCurrentView = false
|
|
|
|
|
private let passwordStore = PasswordStore.shared
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
private lazy var editUIBarButtonItem: UIBarButtonItem = {
|
2017-02-28 17:33:45 +08:00
|
|
|
let uiBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(pressEdit(_:)))
|
|
|
|
|
return uiBarButtonItem
|
|
|
|
|
}()
|
2017-02-28 12:25:52 +08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
private struct TableSection {
|
2017-03-31 00:28:37 -07:00
|
|
|
var type: PasswordDetailTableViewControllerSectionType
|
|
|
|
|
var header: String?
|
2018-12-15 21:48:35 +01:00
|
|
|
var item: [AdditionField] = []
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-12-15 21:48:35 +01:00
|
|
|
init(type: PasswordDetailTableViewControllerSectionType, header: String? = nil) {
|
|
|
|
|
self.type = type
|
2017-03-31 00:28:37 -07:00
|
|
|
self.header = header
|
|
|
|
|
}
|
2017-02-04 20:38:40 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-12-15 21:48:35 +01:00
|
|
|
private var tableData = [TableSection]()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-31 00:28:37 -07:00
|
|
|
private enum PasswordDetailTableViewControllerSectionType {
|
|
|
|
|
case name, main, addition, misc
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-02 21:04:31 +08:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
tableView.register(UINib(nibName: "LabelTableViewCell", bundle: nil), forCellReuseIdentifier: "labelCell")
|
2017-02-06 20:48:20 +08:00
|
|
|
tableView.register(UINib(nibName: "PasswordDetailTitleTableViewCell", bundle: nil), forCellReuseIdentifier: "passwordDetailTitleTableViewCell")
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-05 00:35:23 +08:00
|
|
|
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(PasswordDetailTableViewController.tapMenu(recognizer:)))
|
2017-03-31 22:44:30 -07:00
|
|
|
tapGesture.cancelsTouchesInView = false
|
2017-02-05 00:35:23 +08:00
|
|
|
tableView.addGestureRecognizer(tapGesture)
|
|
|
|
|
tapGesture.delegate = self
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-29 22:16:57 -07:00
|
|
|
tableView.contentInset = UIEdgeInsetsMake(-36, 0, 44, 0);
|
2017-02-05 14:08:19 +08:00
|
|
|
tableView.rowHeight = UITableViewAutomaticDimension
|
|
|
|
|
tableView.estimatedRowHeight = 52
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-15 21:25:03 +08:00
|
|
|
editUIBarButtonItem.isEnabled = false
|
|
|
|
|
navigationItem.rightBarButtonItem = editUIBarButtonItem
|
2017-10-07 10:42:09 -07:00
|
|
|
if #available(iOS 11.0, *) {
|
|
|
|
|
navigationItem.largeTitleDisplayMode = .never
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-11-10 22:38:12 -08:00
|
|
|
if let imageData = passwordEntity?.getImage() {
|
2017-02-09 14:41:59 +08:00
|
|
|
let image = UIImage(data: imageData as Data)
|
|
|
|
|
passwordImage = image
|
|
|
|
|
}
|
2017-04-23 10:03:09 -07:00
|
|
|
self.decryptThenShowPassword()
|
2017-03-22 01:39:26 +08:00
|
|
|
self.setupOneTimePasswordAutoRefresh()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-24 20:40:24 +08:00
|
|
|
// pop the current view because this password might be "discarded"
|
2017-03-22 01:39:26 +08:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(setShouldPopCurrentView), name: .passwordStoreChangeDiscarded, object: nil)
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-24 20:40:24 +08:00
|
|
|
// reset the data table if some password (maybe another one) has been updated
|
2017-06-03 17:50:33 -07:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(decryptThenShowPassword), name: .passwordStoreUpdated, object: nil)
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-24 20:40:24 +08:00
|
|
|
// reset the data table if the disaply settings have been changed
|
2017-06-03 17:50:33 -07:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(decryptThenShowPassword), name: .passwordDetailDisplaySettingChanged, object: nil)
|
2017-03-22 01:39:26 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
if self.shouldPopCurrentView {
|
2019-01-14 20:57:45 +01:00
|
|
|
let alert = UIAlertController(title: "Notice".localize(), message: "PreviousChangesDiscarded.".localize(), preferredStyle: UIAlertControllerStyle.alert)
|
|
|
|
|
alert.addAction(UIAlertAction(title: "Ok".localize(), style: UIAlertActionStyle.default, handler: {_ in
|
2017-03-22 01:39:26 +08:00
|
|
|
_ = self.navigationController?.popViewController(animated: true)
|
|
|
|
|
}))
|
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
|
|
|
|
}
|
2017-02-28 12:25:52 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-04-23 10:03:09 -07:00
|
|
|
private func requestPGPKeyPassphrase() -> String {
|
|
|
|
|
let sem = DispatchSemaphore(value: 0)
|
|
|
|
|
var passphrase = ""
|
|
|
|
|
DispatchQueue.main.async {
|
2019-01-14 20:57:45 +01:00
|
|
|
let alert = UIAlertController(title: "Passphrase".localize(), message: "FillInPgpPassphrase.".localize(), preferredStyle: UIAlertControllerStyle.alert)
|
|
|
|
|
alert.addAction(UIAlertAction(title: "Ok".localize(), style: UIAlertActionStyle.default, handler: {_ in
|
2017-04-23 10:03:09 -07:00
|
|
|
passphrase = alert.textFields!.first!.text!
|
|
|
|
|
sem.signal()
|
|
|
|
|
}))
|
|
|
|
|
alert.addTextField(configurationHandler: {(textField: UITextField!) in
|
|
|
|
|
textField.text = ""
|
|
|
|
|
textField.isSecureTextEntry = true
|
|
|
|
|
})
|
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
|
|
|
|
}
|
|
|
|
|
let _ = sem.wait(timeout: DispatchTime.distantFuture)
|
2017-10-08 21:37:58 +08:00
|
|
|
if SharedDefaults[.isRememberPGPPassphraseOn] {
|
2017-03-16 22:39:03 -07:00
|
|
|
self.passwordStore.pgpKeyPassphrase = passphrase
|
2017-02-28 12:25:52 +08:00
|
|
|
}
|
2017-04-23 10:03:09 -07:00
|
|
|
return passphrase
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-06-03 17:50:33 -07:00
|
|
|
@objc private func decryptThenShowPassword() {
|
2018-11-10 22:38:12 -08:00
|
|
|
guard let passwordEntity = passwordEntity else {
|
2019-01-14 20:57:45 +01:00
|
|
|
Utils.alert(title: "CannotShowPassword".localize(), message: "PasswordDoesNotExist".localize(), controller: self, handler: {(UIAlertAction) -> Void in
|
2017-06-03 17:50:33 -07:00
|
|
|
self.navigationController!.popViewController(animated: true)
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-02-08 18:57:15 +08:00
|
|
|
DispatchQueue.global(qos: .userInitiated).async {
|
2017-03-22 00:22:47 +08:00
|
|
|
// decrypt password
|
2017-02-06 15:03:34 +08:00
|
|
|
do {
|
2017-06-03 17:50:33 -07:00
|
|
|
self.password = try self.passwordStore.decrypt(passwordEntity: passwordEntity, requestPGPKeyPassphrase: self.requestPGPKeyPassphrase)
|
2017-02-06 15:03:34 +08:00
|
|
|
} catch {
|
2017-02-22 12:56:06 +08:00
|
|
|
DispatchQueue.main.async {
|
2017-05-08 21:12:48 +08:00
|
|
|
// remove the wrong passphrase so that users could enter it next time
|
|
|
|
|
self.passwordStore.pgpKeyPassphrase = nil
|
2017-06-07 21:11:01 +08:00
|
|
|
// alert: cancel or try again
|
2019-01-14 20:57:45 +01:00
|
|
|
let alert = UIAlertController(title: "CannotShowPassword".localize(), message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
|
|
|
|
|
alert.addAction(UIAlertAction(title: "Cancel".localize(), style: UIAlertActionStyle.default) { _ in
|
2017-02-22 12:56:06 +08:00
|
|
|
self.navigationController!.popViewController(animated: true)
|
2017-06-03 17:50:33 -07:00
|
|
|
})
|
2019-01-14 20:57:45 +01:00
|
|
|
alert.addAction(UIAlertAction(title: "TryAgain".localize(), style: UIAlertActionStyle.destructive) {_ in
|
2017-06-07 21:11:01 +08:00
|
|
|
self.decryptThenShowPassword()
|
|
|
|
|
})
|
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
2017-02-22 12:56:06 +08:00
|
|
|
}
|
|
|
|
|
return
|
2017-02-06 15:03:34 +08:00
|
|
|
}
|
2017-03-22 00:22:47 +08:00
|
|
|
// display password
|
|
|
|
|
self.showPassword()
|
2017-02-28 12:25:52 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-06-03 17:50:33 -07:00
|
|
|
private func showPassword() {
|
2017-03-22 00:22:47 +08:00
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
|
self?.setTableData()
|
2017-04-10 22:09:39 -07:00
|
|
|
self?.tableView.reloadData()
|
2017-03-22 00:22:47 +08:00
|
|
|
self?.editUIBarButtonItem.isEnabled = true
|
2019-02-27 21:49:21 +10:00
|
|
|
if !SharedDefaults[.isHidePasswordImagesOn] {
|
|
|
|
|
if let urlString = self?.password?.urlString {
|
|
|
|
|
if self?.passwordEntity?.getImage() == nil {
|
|
|
|
|
self?.updatePasswordImage(urlString: urlString)
|
|
|
|
|
}
|
2017-03-22 00:22:47 +08:00
|
|
|
}
|
2017-02-09 13:17:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
private func setupOneTimePasswordAutoRefresh() {
|
2017-03-05 02:54:36 +08:00
|
|
|
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {
|
|
|
|
|
[weak self] timer in
|
|
|
|
|
// bail out of the timer code if the object has been freed
|
|
|
|
|
guard let strongSelf = self,
|
2017-03-24 23:14:44 +08:00
|
|
|
let otpType = strongSelf.password?.otpType,
|
|
|
|
|
otpType != .none,
|
2017-03-05 02:54:36 +08:00
|
|
|
let indexPath = strongSelf.oneTimePasswordIndexPath,
|
|
|
|
|
let cell = strongSelf.tableView.cellForRow(at: indexPath) as? LabelTableViewCell else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-03-24 23:14:44 +08:00
|
|
|
switch otpType {
|
|
|
|
|
case .totp:
|
2017-03-07 09:50:18 +08:00
|
|
|
if let (title, otp) = strongSelf.password?.getOtpStrings() {
|
2018-12-15 21:48:35 +01:00
|
|
|
strongSelf.tableData[indexPath.section].item[indexPath.row] = title => otp
|
2017-03-07 09:50:18 +08:00
|
|
|
cell.cellData?.title = title
|
2017-03-05 02:54:36 +08:00
|
|
|
cell.cellData?.content = otp
|
|
|
|
|
}
|
2017-03-07 09:50:18 +08:00
|
|
|
default:
|
|
|
|
|
break
|
2017-03-05 02:54:36 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
@objc private func pressEdit(_ sender: Any?) {
|
2017-02-13 01:15:42 +08:00
|
|
|
performSegue(withIdentifier: "editPasswordSegue", sender: self)
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
@objc private func setShouldPopCurrentView() {
|
|
|
|
|
self.shouldPopCurrentView = true
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
@IBAction private func cancelEditPassword(segue: UIStoryboardSegue) {
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
@IBAction private func saveEditPassword(segue: UIStoryboardSegue) {
|
2017-04-25 13:01:17 -07:00
|
|
|
if self.password!.changed != 0 {
|
2019-01-14 20:57:45 +01:00
|
|
|
SVProgressHUD.show(withStatus: "Saving".localize())
|
2017-04-25 13:01:17 -07:00
|
|
|
do {
|
|
|
|
|
self.passwordEntity = try self.passwordStore.edit(passwordEntity: self.passwordEntity!, password: self.password!)
|
|
|
|
|
} catch {
|
2019-01-14 20:57:45 +01:00
|
|
|
Utils.alert(title: "Error".localize(), message: error.localizedDescription, controller: self, completion: nil)
|
2017-02-15 20:01:17 +08:00
|
|
|
}
|
2017-04-25 13:01:17 -07:00
|
|
|
self.setTableData()
|
|
|
|
|
self.tableView.reloadData()
|
2019-01-14 20:57:45 +01:00
|
|
|
SVProgressHUD.showSuccess(withStatus: "Success".localize())
|
2017-04-25 13:01:17 -07:00
|
|
|
SVProgressHUD.dismiss(withDelay: 1)
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-21 13:34:26 -07:00
|
|
|
@IBAction private func deletePassword(segue: UIStoryboardSegue) {
|
2017-04-25 13:01:17 -07:00
|
|
|
do {
|
|
|
|
|
try passwordStore.delete(passwordEntity: passwordEntity!)
|
|
|
|
|
} catch {
|
2019-01-14 20:57:45 +01:00
|
|
|
Utils.alert(title: "Error".localize(), message: error.localizedDescription, controller: self, completion: nil)
|
2017-04-25 13:01:17 -07:00
|
|
|
}
|
2017-03-22 17:40:04 -07:00
|
|
|
let _ = navigationController?.popViewController(animated: true)
|
2017-03-21 13:16:25 -07:00
|
|
|
}
|
2017-03-21 13:34:26 -07:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
private func setTableData() {
|
2017-02-13 01:15:42 +08:00
|
|
|
self.tableData = Array<TableSection>()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-31 00:28:37 -07:00
|
|
|
// name section
|
|
|
|
|
var section = TableSection(type: .name)
|
2018-12-15 21:48:35 +01:00
|
|
|
section.item.append(AdditionField())
|
2017-03-31 00:28:37 -07:00
|
|
|
tableData.append(section)
|
|
|
|
|
|
|
|
|
|
// main section
|
|
|
|
|
section = TableSection(type: .main)
|
2017-02-13 01:15:42 +08:00
|
|
|
let password = self.password!
|
2018-06-23 20:42:56 +02:00
|
|
|
if let username = password.username {
|
2018-12-15 21:48:35 +01:00
|
|
|
section.item.append(Constants.USERNAME_KEYWORD => username)
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
2018-06-23 20:42:56 +02:00
|
|
|
if let login = password.login {
|
2018-12-15 21:48:35 +01:00
|
|
|
section.item.append(Constants.LOGIN_KEYWORD => login)
|
2017-06-28 00:32:50 +08:00
|
|
|
}
|
2018-12-15 21:48:35 +01:00
|
|
|
section.item.append(Constants.PASSWORD_KEYWORD => password.password)
|
2017-03-31 00:28:37 -07:00
|
|
|
tableData.append(section)
|
|
|
|
|
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-31 00:28:37 -07:00
|
|
|
// addition section
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-05 02:54:36 +08:00
|
|
|
// show one time password
|
2017-03-24 23:14:44 +08:00
|
|
|
if password.otpType != .none {
|
|
|
|
|
if let (title, otp) = self.password?.getOtpStrings() {
|
2019-01-14 20:57:45 +01:00
|
|
|
section = TableSection(type: .addition, header: "OneTimePassword".localize())
|
2018-12-15 21:48:35 +01:00
|
|
|
section.item.append(title => otp)
|
2017-03-31 00:28:37 -07:00
|
|
|
tableData.append(section)
|
2017-04-01 08:59:22 -07:00
|
|
|
oneTimePasswordIndexPath = IndexPath(row: 0, section: tableData.count - 1)
|
2017-03-03 14:45:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-05 02:54:36 +08:00
|
|
|
// show additional information
|
2017-06-28 00:18:06 +08:00
|
|
|
let filteredAdditionKeys = password.getFilteredAdditions()
|
2017-03-03 17:12:25 +08:00
|
|
|
if filteredAdditionKeys.count > 0 {
|
2019-02-13 21:31:40 +01:00
|
|
|
section = TableSection(type: .addition, header: "Additions".localize())
|
2018-12-15 21:48:35 +01:00
|
|
|
section.item.append(contentsOf: filteredAdditionKeys)
|
2017-03-31 00:28:37 -07:00
|
|
|
tableData.append(section)
|
2017-03-03 17:12:25 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-31 00:28:37 -07:00
|
|
|
// misc section
|
|
|
|
|
section = TableSection(type: .misc)
|
2019-01-14 20:57:45 +01:00
|
|
|
section.item.append(AdditionField(title: "ShowRaw".localize()))
|
2017-03-31 00:28:37 -07:00
|
|
|
tableData.append(section)
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-13 01:15:42 +08:00
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
|
|
|
|
if segue.identifier == "editPasswordSegue" {
|
|
|
|
|
if let controller = segue.destination as? UINavigationController {
|
|
|
|
|
if let editController = controller.viewControllers.first as? EditPasswordTableViewController {
|
|
|
|
|
editController.password = password
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-31 22:44:30 -07:00
|
|
|
} else if segue.identifier == "showRawPasswordSegue" {
|
|
|
|
|
if let controller = segue.destination as? UINavigationController {
|
|
|
|
|
if let controller = controller.viewControllers.first as? RawPasswordViewController {
|
|
|
|
|
controller.password = password
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
private func updatePasswordImage(urlString: String) {
|
2017-03-15 14:14:42 -07:00
|
|
|
var newUrlString = urlString
|
|
|
|
|
if urlString.lowercased().hasPrefix("http://") {
|
|
|
|
|
// try to replace http url to https url
|
|
|
|
|
newUrlString = urlString.replacingOccurrences(of: "http://",
|
|
|
|
|
with: "https://",
|
|
|
|
|
options: .caseInsensitive,
|
|
|
|
|
range: urlString.range(of: "http://"))
|
|
|
|
|
} else if urlString.lowercased().hasPrefix("https://") {
|
|
|
|
|
// do nothing here
|
|
|
|
|
} else {
|
|
|
|
|
// if a url does not start with http or https, try to add https
|
|
|
|
|
newUrlString = "https://\(urlString)"
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-11-17 21:27:06 -08:00
|
|
|
try? FavIcon.downloadPreferred(newUrlString) { [weak self] result in
|
|
|
|
|
if case let .success(image) = result {
|
|
|
|
|
let indexPath = IndexPath(row: 0, section: 0)
|
|
|
|
|
self?.passwordImage = image
|
|
|
|
|
self?.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
|
|
|
|
|
let imageData = UIImageJPEGRepresentation(image, 1)
|
|
|
|
|
if let entity = self?.passwordEntity {
|
|
|
|
|
self?.passwordStore.updateImage(passwordEntity: entity, image: imageData)
|
2017-02-09 13:17:11 +08:00
|
|
|
}
|
2017-02-06 15:03:34 +08:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-05 00:35:23 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
@objc private func tapMenu(recognizer: UITapGestureRecognizer) {
|
2017-02-05 00:35:23 +08:00
|
|
|
if recognizer.state == UIGestureRecognizerState.ended {
|
|
|
|
|
let tapLocation = recognizer.location(in: self.tableView)
|
|
|
|
|
if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
|
|
|
|
|
if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) as? LabelTableViewCell {
|
|
|
|
|
tappedCell.becomeFirstResponder()
|
|
|
|
|
let menuController = UIMenuController.shared
|
2019-01-14 20:57:45 +01:00
|
|
|
let revealItem = UIMenuItem(title: "Reveal".localize(), action: #selector(LabelTableViewCell.revealPassword(_:)))
|
|
|
|
|
let concealItem = UIMenuItem(title: "Conceal".localize(), action: #selector(LabelTableViewCell.concealPassword(_:)))
|
|
|
|
|
let nextHOTPItem = UIMenuItem(title: "NextPassword".localize(), action: #selector(LabelTableViewCell.getNextHOTP(_:)))
|
|
|
|
|
let openURLItem = UIMenuItem(title: "CopyAndOpen".localize(), action: #selector(LabelTableViewCell.openLink(_:)))
|
2017-03-22 01:39:26 +08:00
|
|
|
menuController.menuItems = [revealItem, concealItem, nextHOTPItem, openURLItem]
|
2017-02-05 00:35:23 +08:00
|
|
|
menuController.setTargetRect(tappedCell.contentLabel.frame, in: tappedCell.contentLabel.superview!)
|
|
|
|
|
menuController.setMenuVisible(true, animated: true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-04 20:38:40 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-04-01 08:36:47 -07:00
|
|
|
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
|
|
|
|
|
if touch.view!.isKind(of: UIButton.classForCoder()) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-31 22:44:30 -07:00
|
|
|
@IBAction func back(segue:UIStoryboardSegue) {
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
func getNextHOTP() {
|
|
|
|
|
guard password != nil, passwordEntity != nil, password?.otpType == .hotp else {
|
|
|
|
|
DispatchQueue.main.async {
|
2019-01-14 20:57:45 +01:00
|
|
|
Utils.alert(title: "Error".localize(), message: "GetNextPasswordOfNonHotp.".localize(), controller: self, completion: nil)
|
2017-03-22 01:39:26 +08:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-04-09 02:54:05 +08:00
|
|
|
// copy HOTP to pasteboard (will update counter)
|
|
|
|
|
if let plainPassword = password!.getNextHotp() {
|
2017-07-27 23:56:24 +08:00
|
|
|
SecurePasteboard.shared.copy(textToCopy: plainPassword)
|
2017-03-22 01:39:26 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
// commit the change of HOTP counter
|
2017-04-25 13:01:17 -07:00
|
|
|
if password!.changed != 0 {
|
|
|
|
|
do {
|
|
|
|
|
self.passwordEntity = try self.passwordStore.edit(passwordEntity: self.passwordEntity!, password: self.password!)
|
|
|
|
|
} catch {
|
2019-01-14 20:57:45 +01:00
|
|
|
Utils.alert(title: "Error".localize(), message: error.localizedDescription, controller: self, completion: nil)
|
2017-03-22 01:39:26 +08:00
|
|
|
}
|
2019-01-20 12:15:54 +01:00
|
|
|
SVProgressHUD.showSuccess(withStatus: "PasswordCopied".localize() | "CounterUpdated".localize())
|
2017-04-25 13:01:17 -07:00
|
|
|
SVProgressHUD.dismiss(withDelay: 1)
|
2017-03-22 01:39:26 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-07 21:55:30 +01:00
|
|
|
|
|
|
|
|
func openLink(to address: String?) {
|
|
|
|
|
guard address != nil, let url = URL(string: formActualWebAddress(from: address!)) else {
|
|
|
|
|
return DispatchQueue.main.async {
|
2019-01-14 20:57:45 +01:00
|
|
|
Utils.alert(title: "Error".localize(), message: "CannotFindValidUrl".localize(), controller: self, completion: nil)
|
2017-03-22 01:39:26 +08:00
|
|
|
}
|
|
|
|
|
}
|
2017-07-27 23:56:24 +08:00
|
|
|
SecurePasteboard.shared.copy(textToCopy: password?.password)
|
2017-03-22 01:39:26 +08:00
|
|
|
UIApplication.shared.open(url, options: [:], completionHandler: nil)
|
|
|
|
|
}
|
2017-02-02 21:04:31 +08:00
|
|
|
|
2018-12-07 21:55:30 +01:00
|
|
|
private func formActualWebAddress(from: String) -> String {
|
|
|
|
|
let lowercased = from.lowercased()
|
|
|
|
|
if !(lowercased.starts(with: "https://") || lowercased.starts(with: "http://")) {
|
2018-12-07 21:56:48 +01:00
|
|
|
return "https://\(from)"
|
2018-12-07 21:55:30 +01:00
|
|
|
}
|
|
|
|
|
return from
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 21:04:31 +08:00
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2017-02-09 15:47:42 +08:00
|
|
|
return tableData.count
|
2017-02-02 21:04:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2017-02-09 15:47:42 +08:00
|
|
|
return tableData[section].item.count
|
2017-02-02 21:04:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
2017-02-06 20:48:20 +08:00
|
|
|
let sectionIndex = indexPath.section
|
|
|
|
|
let rowIndex = indexPath.row
|
2017-03-31 00:28:37 -07:00
|
|
|
let tableDataItem = tableData[sectionIndex].item[rowIndex]
|
|
|
|
|
switch(tableData[sectionIndex].type) {
|
|
|
|
|
case .name:
|
2017-02-06 20:48:20 +08:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "passwordDetailTitleTableViewCell", for: indexPath) as! PasswordDetailTitleTableViewCell
|
2019-02-27 21:49:21 +10:00
|
|
|
if !SharedDefaults[.isHidePasswordImagesOn] {
|
|
|
|
|
cell.passwordImageImageView.image = passwordImage ?? #imageLiteral(resourceName: "PasswordImagePlaceHolder")
|
|
|
|
|
} else {
|
|
|
|
|
cell.passwordImageImageView.image = #imageLiteral(resourceName: "PasswordImagePlaceHolder")
|
|
|
|
|
}
|
2018-11-10 22:38:12 -08:00
|
|
|
let passwordName = passwordEntity!.getName()
|
|
|
|
|
if passwordEntity!.synced == false {
|
|
|
|
|
cell.nameLabel.text = "\(passwordName) ↻"
|
|
|
|
|
} else {
|
|
|
|
|
cell.nameLabel.text = passwordName
|
2017-02-19 00:55:13 +08:00
|
|
|
}
|
2017-04-23 10:03:09 -07:00
|
|
|
cell.categoryLabel.text = passwordEntity!.getCategoryText()
|
2017-03-31 22:44:30 -07:00
|
|
|
cell.selectionStyle = .none
|
2017-02-06 20:48:20 +08:00
|
|
|
return cell
|
2017-03-31 00:28:37 -07:00
|
|
|
case .main, .addition:
|
2017-02-06 20:48:20 +08:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell", for: indexPath) as! LabelTableViewCell
|
2017-03-31 00:28:37 -07:00
|
|
|
let titleData = tableDataItem.title
|
|
|
|
|
let contentData = tableDataItem.content
|
2017-03-22 01:39:26 +08:00
|
|
|
cell.delegatePasswordTableView = self
|
2017-02-06 20:48:20 +08:00
|
|
|
cell.cellData = LabelTableViewCellData(title: titleData, content: contentData)
|
2017-03-31 22:44:30 -07:00
|
|
|
cell.selectionStyle = .none
|
2017-02-06 20:48:20 +08:00
|
|
|
return cell
|
2017-03-31 00:28:37 -07:00
|
|
|
case .misc:
|
2018-12-08 00:57:54 +01:00
|
|
|
let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
|
2017-03-31 00:28:37 -07:00
|
|
|
cell.textLabel?.text = tableDataItem.title
|
2017-03-31 22:44:30 -07:00
|
|
|
cell.selectionStyle = .default
|
2018-12-08 00:57:54 +01:00
|
|
|
addHiddenFieldInformation(to: cell)
|
2017-03-31 00:28:37 -07:00
|
|
|
return cell
|
2017-02-06 20:48:20 +08:00
|
|
|
}
|
2017-02-02 21:04:31 +08:00
|
|
|
}
|
2018-12-08 00:57:54 +01:00
|
|
|
|
|
|
|
|
private func addHiddenFieldInformation(to cell: UITableViewCell) {
|
|
|
|
|
guard password != nil, let detailTextLabel = cell.detailTextLabel else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var numberOfHiddenFields = 0
|
|
|
|
|
numberOfHiddenFields += SharedDefaults[.isHideUnknownOn] ? password!.numberOfUnknowns : 0
|
|
|
|
|
numberOfHiddenFields += SharedDefaults[.isHideOTPOn] ? password!.numberOfOtpRelated : 0
|
|
|
|
|
guard numberOfHiddenFields > 0 else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
detailTextLabel.textAlignment = .center
|
|
|
|
|
detailTextLabel.textColor = .gray
|
2019-01-14 20:57:45 +01:00
|
|
|
detailTextLabel.text = "HiddenFields(%d)".localize(numberOfHiddenFields)
|
2018-12-08 00:57:54 +01:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-04 20:38:40 +08:00
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
2017-03-31 00:28:37 -07:00
|
|
|
return tableData[section].header
|
2017-02-04 20:38:40 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-27 08:54:51 +08:00
|
|
|
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
|
|
|
|
|
if section == tableData.count - 1 {
|
|
|
|
|
let view = UIView()
|
2017-02-27 15:21:25 +08:00
|
|
|
let footerLabel = UILabel(frame: CGRect(x: 15, y: 15, width: tableView.frame.width, height: 60))
|
2017-02-27 08:54:51 +08:00
|
|
|
footerLabel.numberOfLines = 0
|
|
|
|
|
footerLabel.font = UIFont.preferredFont(forTextStyle: .footnote)
|
2017-02-27 17:37:53 +08:00
|
|
|
footerLabel.textColor = UIColor.gray
|
2018-11-10 22:38:12 -08:00
|
|
|
let dateString = self.passwordStore.getLatestUpdateInfo(filename: password!.url.path)
|
2019-01-14 20:57:45 +01:00
|
|
|
footerLabel.text = "LastUpdated".localize(dateString)
|
2017-02-27 08:54:51 +08:00
|
|
|
view.addSubview(footerLabel)
|
|
|
|
|
return view
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-03 18:02:40 +08:00
|
|
|
override func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
|
|
|
|
|
if action == #selector(copy(_:)) {
|
2017-07-27 23:56:24 +08:00
|
|
|
SecurePasteboard.shared.copy(textToCopy: tableData[indexPath.section].item[indexPath.row].content)
|
2017-02-03 18:02:40 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-03 18:02:40 +08:00
|
|
|
override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
|
2017-03-31 22:44:30 -07:00
|
|
|
let section = tableData[indexPath.section]
|
|
|
|
|
switch(section.type) {
|
|
|
|
|
case .main, .addition:
|
|
|
|
|
return action == #selector(UIResponderStandardEditActions.copy(_:))
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
2017-02-03 18:02:40 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-03 18:02:40 +08:00
|
|
|
override func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-31 22:44:30 -07:00
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
|
let section = tableData[indexPath.section]
|
|
|
|
|
if section.type == .misc {
|
2019-01-14 20:57:45 +01:00
|
|
|
if section.item[indexPath.row].title == "ShowRaw".localize() {
|
2017-03-31 22:44:30 -07:00
|
|
|
performSegue(withIdentifier: "showRawPasswordSegue", sender: self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
|
}
|
2017-02-02 21:04:31 +08:00
|
|
|
}
|