copy password and open link

This commit is contained in:
Bob Sun 2017-02-08 17:55:18 +08:00
parent dcdf7852fe
commit 4c53d20b77
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
2 changed files with 17 additions and 3 deletions

View file

@ -20,7 +20,9 @@ class LabelTableViewCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var titleLabel: UILabel!
var isPasswordCell = false var isPasswordCell = false
var isURLCell = false
var isReveal = false var isReveal = false
var password: Password?
let passwordDots = "••••••••••••" let passwordDots = "••••••••••••"
var cellData: LabelTableViewCellData? { var cellData: LabelTableViewCellData? {
@ -56,13 +58,16 @@ class LabelTableViewCell: UITableViewCell {
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.revealPassword(_:)) return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.revealPassword(_:))
} }
} }
if isURLCell {
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.openLink(_:))
}
return action == #selector(copy(_:)) return action == #selector(copy(_:))
} }
override func copy(_ sender: Any?) { override func copy(_ sender: Any?) {
UIPasteboard.general.string = cellData?.content UIPasteboard.general.string = cellData?.content
} }
func revealPassword(_ sender: Any?) { func revealPassword(_ sender: Any?) {
contentLabel.text = cellData?.content contentLabel.text = cellData?.content
isReveal = true isReveal = true
@ -72,4 +77,9 @@ class LabelTableViewCell: UITableViewCell {
contentLabel.text = passwordDots contentLabel.text = passwordDots
isReveal = false isReveal = false
} }
func openLink(_ sender: Any?) {
UIPasteboard.general.string = password?.password
UIApplication.shared.open(NSURL(string: cellData!.content) as! URL, options: [:], completionHandler: nil)
}
} }

View file

@ -98,13 +98,15 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
let menuController = UIMenuController.shared let menuController = UIMenuController.shared
let revealItem = UIMenuItem(title: "Reveal", action: #selector(LabelTableViewCell.revealPassword(_:))) let revealItem = UIMenuItem(title: "Reveal", action: #selector(LabelTableViewCell.revealPassword(_:)))
let concealItem = UIMenuItem(title: "Conceal", action: #selector(LabelTableViewCell.concealPassword(_:))) let concealItem = UIMenuItem(title: "Conceal", action: #selector(LabelTableViewCell.concealPassword(_:)))
menuController.menuItems = [revealItem, concealItem] let openURLItem = UIMenuItem(title: "Copy Password & Open Link", action: #selector(LabelTableViewCell.openLink(_:)))
menuController.menuItems = [revealItem, concealItem, openURLItem]
menuController.setTargetRect(tappedCell.contentLabel.frame, in: tappedCell.contentLabel.superview!) menuController.setTargetRect(tappedCell.contentLabel.frame, in: tappedCell.contentLabel.superview!)
menuController.setMenuVisible(true, animated: true) menuController.setMenuVisible(true, animated: true)
} }
} }
} }
} }
override func numberOfSections(in tableView: UITableView) -> Int { override func numberOfSections(in tableView: UITableView) -> Int {
return tableData.count + 1 return tableData.count + 1
@ -132,7 +134,9 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell", for: indexPath) as! LabelTableViewCell let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell", for: indexPath) as! LabelTableViewCell
let titleData = tableData[sectionIndex - 1].item[rowIndex].title let titleData = tableData[sectionIndex - 1].item[rowIndex].title
let contentData = tableData[sectionIndex - 1].item[rowIndex].content let contentData = tableData[sectionIndex - 1].item[rowIndex].content
cell.isPasswordCell = (titleData == "password" ? true : false) cell.password = password
cell.isPasswordCell = (titleData.lowercased() == "password" ? true : false)
cell.isURLCell = (titleData.lowercased() == "url" ? true : false)
cell.cellData = LabelTableViewCellData(title: titleData, content: contentData) cell.cellData = LabelTableViewCellData(title: titleData, content: contentData)
return cell return cell
} }