2017-02-02 21:04:31 +08:00
|
|
|
//
|
|
|
|
|
// LabelTableViewCell.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 2/2/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
2017-03-07 00:52:22 +08:00
|
|
|
import SVProgressHUD
|
2017-02-02 21:04:31 +08:00
|
|
|
|
2017-02-05 13:56:37 +08:00
|
|
|
|
|
|
|
|
struct LabelTableViewCellData {
|
|
|
|
|
var title: String
|
|
|
|
|
var content: String
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 21:04:31 +08:00
|
|
|
class LabelTableViewCell: UITableViewCell {
|
|
|
|
|
|
|
|
|
|
@IBOutlet weak var contentLabel: UILabel!
|
|
|
|
|
@IBOutlet weak var titleLabel: UILabel!
|
2017-02-05 13:56:37 +08:00
|
|
|
|
2017-02-06 11:55:27 +08:00
|
|
|
var isPasswordCell = false
|
2017-02-08 17:55:18 +08:00
|
|
|
var isURLCell = false
|
2017-02-06 11:55:27 +08:00
|
|
|
var isReveal = false
|
2017-03-07 00:52:22 +08:00
|
|
|
var isHOTPCell = false
|
2017-02-05 13:56:37 +08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
weak var delegatePasswordTableView : PasswordDetailTableViewController?
|
2017-03-07 00:52:22 +08:00
|
|
|
|
2017-02-05 13:56:37 +08:00
|
|
|
var cellData: LabelTableViewCellData? {
|
|
|
|
|
didSet {
|
2017-03-07 00:52:22 +08:00
|
|
|
titleLabel.text = cellData?.title ?? ""
|
2017-02-06 11:55:27 +08:00
|
|
|
if isPasswordCell {
|
2017-03-07 00:52:22 +08:00
|
|
|
if isReveal {
|
|
|
|
|
contentLabel.attributedText = Utils.attributedPassword(plainPassword: cellData?.content ?? "")
|
|
|
|
|
} else {
|
2017-03-22 01:39:26 +08:00
|
|
|
contentLabel.text = Globals.passwordDots
|
2017-03-07 00:52:22 +08:00
|
|
|
}
|
2017-03-22 01:39:26 +08:00
|
|
|
contentLabel.font = UIFont(name: Globals.passwordFonts, size: contentLabel.font.pointSize)
|
2017-03-07 00:52:22 +08:00
|
|
|
} else if isHOTPCell {
|
|
|
|
|
if isReveal {
|
|
|
|
|
contentLabel.text = cellData?.content ?? ""
|
|
|
|
|
} else {
|
2017-03-22 01:39:26 +08:00
|
|
|
contentLabel.text = Globals.passwordDots
|
2017-03-07 00:52:22 +08:00
|
|
|
}
|
2017-02-06 11:55:27 +08:00
|
|
|
} else {
|
|
|
|
|
contentLabel.text = cellData?.content
|
|
|
|
|
}
|
2017-02-05 13:56:37 +08:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-02 21:04:31 +08:00
|
|
|
|
2017-02-05 00:35:23 +08:00
|
|
|
override var canBecomeFirstResponder: Bool {
|
|
|
|
|
get {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 21:04:31 +08:00
|
|
|
override func awakeFromNib() {
|
|
|
|
|
super.awakeFromNib()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func setSelected(_ selected: Bool, animated: Bool) {
|
|
|
|
|
super.setSelected(selected, animated: animated)
|
|
|
|
|
}
|
2017-02-05 00:35:23 +08:00
|
|
|
|
|
|
|
|
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
|
2017-02-06 11:55:27 +08:00
|
|
|
if isPasswordCell {
|
|
|
|
|
if isReveal {
|
|
|
|
|
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.concealPassword(_:))
|
|
|
|
|
} else {
|
|
|
|
|
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.revealPassword(_:))
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-08 17:55:18 +08:00
|
|
|
if isURLCell {
|
|
|
|
|
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.openLink(_:))
|
|
|
|
|
}
|
2017-03-07 00:52:22 +08:00
|
|
|
if isHOTPCell {
|
|
|
|
|
if isReveal {
|
2017-03-22 01:39:26 +08:00
|
|
|
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.concealPassword(_:)) || action == #selector(LabelTableViewCell.getNextHOTP(_:))
|
2017-03-07 00:52:22 +08:00
|
|
|
} else {
|
2017-03-22 01:39:26 +08:00
|
|
|
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.revealPassword(_:)) || action == #selector(LabelTableViewCell.getNextHOTP(_:))
|
2017-03-07 00:52:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-05 00:35:23 +08:00
|
|
|
return action == #selector(copy(_:))
|
|
|
|
|
}
|
2017-02-06 11:55:27 +08:00
|
|
|
|
2017-02-05 00:35:23 +08:00
|
|
|
override func copy(_ sender: Any?) {
|
2017-02-23 17:56:12 +08:00
|
|
|
Utils.copyToPasteboard(textToCopy: cellData?.content)
|
2017-02-05 00:35:23 +08:00
|
|
|
}
|
2017-02-08 17:55:18 +08:00
|
|
|
|
2017-02-06 11:55:27 +08:00
|
|
|
func revealPassword(_ sender: Any?) {
|
2017-02-24 00:24:39 +08:00
|
|
|
if let plainPassword = cellData?.content {
|
2017-03-07 00:52:22 +08:00
|
|
|
if isHOTPCell {
|
|
|
|
|
contentLabel.text = plainPassword
|
|
|
|
|
} else {
|
|
|
|
|
contentLabel.attributedText = Utils.attributedPassword(plainPassword: plainPassword)
|
|
|
|
|
}
|
2017-02-24 00:24:39 +08:00
|
|
|
} else {
|
|
|
|
|
contentLabel.text = ""
|
|
|
|
|
}
|
2017-02-06 11:55:27 +08:00
|
|
|
isReveal = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func concealPassword(_ sender: Any?) {
|
2017-03-22 01:39:26 +08:00
|
|
|
contentLabel.text = Globals.passwordDots
|
2017-02-06 11:55:27 +08:00
|
|
|
isReveal = false
|
|
|
|
|
}
|
2017-02-08 17:55:18 +08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
func openLink(_ sender: Any?) {
|
|
|
|
|
// if isURLCell, passwordTableView should not be nil
|
|
|
|
|
delegatePasswordTableView!.openLink()
|
2017-03-07 00:52:22 +08:00
|
|
|
}
|
|
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
func getNextHOTP(_ sender: Any?) {
|
|
|
|
|
// if isHOTPCell, passwordTableView should not be nil
|
|
|
|
|
delegatePasswordTableView!.getNextHOTP()
|
2017-02-08 17:55:18 +08:00
|
|
|
}
|
2017-02-02 21:04:31 +08:00
|
|
|
}
|