Polish the label table view cell
This commit is contained in:
parent
7a3000fcdd
commit
2519472ba2
3 changed files with 75 additions and 54 deletions
|
|
@ -448,9 +448,6 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
||||||
let titleData = tableDataItem.title
|
let titleData = tableDataItem.title
|
||||||
let contentData = tableDataItem.content
|
let contentData = tableDataItem.content
|
||||||
cell.delegatePasswordTableView = 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)
|
|
||||||
cell.cellData = LabelTableViewCellData(title: titleData, content: contentData)
|
cell.cellData = LabelTableViewCellData(title: titleData, content: contentData)
|
||||||
cell.selectionStyle = .none
|
cell.selectionStyle = .none
|
||||||
return cell
|
return cell
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ class Globals {
|
||||||
"Apple": (min: 15, max: 15, def: 15)]
|
"Apple": (min: 15, max: 15, def: 15)]
|
||||||
|
|
||||||
static let passwordDots = "••••••••••••"
|
static let passwordDots = "••••••••••••"
|
||||||
|
static let oneTimePasswordDots = "••••••"
|
||||||
static let passwordFonts = "Menlo"
|
static let passwordFonts = "Menlo"
|
||||||
|
|
||||||
// UI related
|
// UI related
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@
|
||||||
import UIKit
|
import UIKit
|
||||||
import SVProgressHUD
|
import SVProgressHUD
|
||||||
|
|
||||||
|
|
||||||
struct LabelTableViewCellData {
|
struct LabelTableViewCellData {
|
||||||
var title: String
|
var title: String
|
||||||
var content: String
|
var content: String
|
||||||
|
|
@ -19,36 +18,53 @@ class LabelTableViewCell: UITableViewCell {
|
||||||
|
|
||||||
@IBOutlet weak var contentLabel: UILabel!
|
@IBOutlet weak var contentLabel: UILabel!
|
||||||
@IBOutlet weak var titleLabel: UILabel!
|
@IBOutlet weak var titleLabel: UILabel!
|
||||||
|
|
||||||
|
private enum CellType {
|
||||||
|
case password, URL, HOTP, other
|
||||||
|
}
|
||||||
|
|
||||||
var isPasswordCell = false
|
private var type = CellType.other
|
||||||
var isURLCell = false
|
private var isReveal = false
|
||||||
var isReveal = false
|
|
||||||
var isHOTPCell = false
|
|
||||||
|
|
||||||
weak var delegatePasswordTableView : PasswordDetailTableViewController?
|
weak var delegatePasswordTableView : PasswordDetailTableViewController?
|
||||||
|
|
||||||
var passwordDisplayButton: UIButton?
|
private var passwordDisplayButton: UIButton?
|
||||||
var buttons: UIView?
|
private var buttons: UIView?
|
||||||
|
|
||||||
var cellData: LabelTableViewCellData? {
|
var cellData: LabelTableViewCellData? {
|
||||||
didSet {
|
didSet {
|
||||||
titleLabel.text = cellData?.title ?? ""
|
guard let title = cellData?.title, let content = cellData?.content else {
|
||||||
if isPasswordCell {
|
type = .other
|
||||||
|
return
|
||||||
|
}
|
||||||
|
titleLabel.text = title
|
||||||
|
switch title.lowercased() {
|
||||||
|
case "password":
|
||||||
|
type = .password
|
||||||
if isReveal {
|
if isReveal {
|
||||||
contentLabel.attributedText = Utils.attributedPassword(plainPassword: cellData?.content ?? "")
|
contentLabel.attributedText = Utils.attributedPassword(plainPassword: content)
|
||||||
} else {
|
} else {
|
||||||
contentLabel.text = Globals.passwordDots
|
if content == "" {
|
||||||
|
contentLabel.text = ""
|
||||||
|
} else {
|
||||||
|
contentLabel.text = Globals.passwordDots
|
||||||
|
}
|
||||||
}
|
}
|
||||||
contentLabel.font = UIFont(name: Globals.passwordFonts, size: contentLabel.font.pointSize)
|
contentLabel.font = UIFont(name: Globals.passwordFonts, size: contentLabel.font.pointSize)
|
||||||
} else if isHOTPCell {
|
case "hmac-based":
|
||||||
|
type = .HOTP
|
||||||
if isReveal {
|
if isReveal {
|
||||||
contentLabel.text = cellData?.content ?? ""
|
contentLabel.text = content
|
||||||
} else {
|
} else {
|
||||||
contentLabel.text = Globals.passwordDots
|
contentLabel.text = Globals.oneTimePasswordDots
|
||||||
}
|
}
|
||||||
contentLabel.font = UIFont(name: Globals.passwordFonts, size: contentLabel.font.pointSize)
|
contentLabel.font = UIFont(name: Globals.passwordFonts, size: contentLabel.font.pointSize)
|
||||||
} else {
|
case "url":
|
||||||
contentLabel.text = cellData?.content
|
type = .URL
|
||||||
|
contentLabel.text = content
|
||||||
|
default:
|
||||||
|
type = .other
|
||||||
|
contentLabel.text = content
|
||||||
}
|
}
|
||||||
updateButtons()
|
updateButtons()
|
||||||
}
|
}
|
||||||
|
|
@ -76,46 +92,51 @@ class LabelTableViewCell: UITableViewCell {
|
||||||
}
|
}
|
||||||
|
|
||||||
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
|
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
|
||||||
if isPasswordCell {
|
switch type {
|
||||||
|
case .password:
|
||||||
if isReveal {
|
if isReveal {
|
||||||
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.concealPassword(_:))
|
return action == #selector(copy(_:)) || action == #selector(concealPassword(_:))
|
||||||
} else {
|
} else {
|
||||||
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.revealPassword(_:))
|
return action == #selector(copy(_:)) || action == #selector(revealPassword(_:))
|
||||||
}
|
}
|
||||||
}
|
case .URL:
|
||||||
if isURLCell {
|
return action == #selector(copy(_:)) || action == #selector(openLink(_:))
|
||||||
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.openLink(_:))
|
case .HOTP:
|
||||||
}
|
|
||||||
if isHOTPCell {
|
|
||||||
if isReveal {
|
if isReveal {
|
||||||
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.concealPassword(_:)) || action == #selector(LabelTableViewCell.getNextHOTP(_:))
|
return action == #selector(copy(_:)) || action == #selector(concealPassword(_:)) || action == #selector(getNextHOTP(_:))
|
||||||
} else {
|
} else {
|
||||||
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.revealPassword(_:)) || action == #selector(LabelTableViewCell.getNextHOTP(_:))
|
return action == #selector(copy(_:)) || action == #selector(revealPassword(_:)) || action == #selector(getNextHOTP(_:))
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
return action == #selector(copy(_:))
|
||||||
}
|
}
|
||||||
return action == #selector(copy(_:))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override func copy(_ sender: Any?) {
|
override func copy(_ sender: Any?) {
|
||||||
Utils.copyToPasteboard(textToCopy: cellData?.content)
|
Utils.copyToPasteboard(textToCopy: cellData?.content)
|
||||||
}
|
}
|
||||||
|
|
||||||
func revealPassword(_ sender: Any?) {
|
func revealPassword(_ sender: Any?) {
|
||||||
if let plainPassword = cellData?.content {
|
let plainPassword = cellData?.content ?? ""
|
||||||
if isHOTPCell {
|
if type == .password {
|
||||||
contentLabel.text = plainPassword
|
contentLabel.attributedText = Utils.attributedPassword(plainPassword: plainPassword)
|
||||||
} else {
|
|
||||||
contentLabel.attributedText = Utils.attributedPassword(plainPassword: plainPassword)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
contentLabel.text = ""
|
contentLabel.text = plainPassword
|
||||||
}
|
}
|
||||||
isReveal = true
|
isReveal = true
|
||||||
passwordDisplayButton?.setImage(#imageLiteral(resourceName: "Invisible"), for: .normal)
|
passwordDisplayButton?.setImage(#imageLiteral(resourceName: "Invisible"), for: .normal)
|
||||||
}
|
}
|
||||||
|
|
||||||
func concealPassword(_ sender: Any?) {
|
func concealPassword(_ sender: Any?) {
|
||||||
contentLabel.text = Globals.passwordDots
|
if type == .password {
|
||||||
|
if cellData?.content.isEmpty == false {
|
||||||
|
contentLabel.text = Globals.passwordDots
|
||||||
|
} else {
|
||||||
|
contentLabel.text = ""
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
contentLabel.text = Globals.oneTimePasswordDots
|
||||||
|
}
|
||||||
isReveal = false
|
isReveal = false
|
||||||
passwordDisplayButton?.setImage(#imageLiteral(resourceName: "Visible"), for: .normal)
|
passwordDisplayButton?.setImage(#imageLiteral(resourceName: "Visible"), for: .normal)
|
||||||
}
|
}
|
||||||
|
|
@ -130,7 +151,6 @@ class LabelTableViewCell: UITableViewCell {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func openLink(_ sender: Any?) {
|
func openLink(_ sender: Any?) {
|
||||||
// if isURLCell, passwordTableView should not be nil
|
// if isURLCell, passwordTableView should not be nil
|
||||||
delegatePasswordTableView!.openLink()
|
delegatePasswordTableView!.openLink()
|
||||||
|
|
@ -141,10 +161,7 @@ class LabelTableViewCell: UITableViewCell {
|
||||||
delegatePasswordTableView!.getNextHOTP()
|
delegatePasswordTableView!.getNextHOTP()
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateButtons() {
|
private func updateButtons() {
|
||||||
passwordDisplayButton = nil
|
|
||||||
buttons = nil
|
|
||||||
|
|
||||||
// total width and height of a button
|
// total width and height of a button
|
||||||
let height = min(self.bounds.height, 36.0)
|
let height = min(self.bounds.height, 36.0)
|
||||||
let width = max(height * 0.8, Globals.tableCellButtonSize)
|
let width = max(height * 0.8, Globals.tableCellButtonSize)
|
||||||
|
|
@ -153,16 +170,19 @@ class LabelTableViewCell: UITableViewCell {
|
||||||
let marginY = max((height - Globals.tableCellButtonSize) / 2, 0.0)
|
let marginY = max((height - Globals.tableCellButtonSize) / 2, 0.0)
|
||||||
let marginX = max((width - Globals.tableCellButtonSize) / 2, 0.0)
|
let marginX = max((width - Globals.tableCellButtonSize) / 2, 0.0)
|
||||||
|
|
||||||
if isPasswordCell {
|
switch type {
|
||||||
// password button
|
case .password:
|
||||||
passwordDisplayButton = UIButton(type: .system)
|
if let content = cellData?.content, content != "" {
|
||||||
passwordDisplayButton!.frame = CGRect(x: 0, y: 0, width: width, height: height)
|
// password button
|
||||||
passwordDisplayButton!.setImage(#imageLiteral(resourceName: "Visible"), for: .normal)
|
passwordDisplayButton = UIButton(type: .system)
|
||||||
passwordDisplayButton!.imageView?.contentMode = .scaleAspectFit
|
passwordDisplayButton!.frame = CGRect(x: 0, y: 0, width: width, height: height)
|
||||||
passwordDisplayButton!.contentEdgeInsets = UIEdgeInsetsMake(marginY, marginX, marginY, marginX)
|
passwordDisplayButton!.setImage(#imageLiteral(resourceName: "Visible"), for: .normal)
|
||||||
passwordDisplayButton!.addTarget(self, action: #selector(reversePasswordDisplay), for: UIControlEvents.touchUpInside)
|
passwordDisplayButton!.imageView?.contentMode = .scaleAspectFit
|
||||||
buttons = passwordDisplayButton
|
passwordDisplayButton!.contentEdgeInsets = UIEdgeInsetsMake(marginY, marginX, marginY, marginX)
|
||||||
} else if isHOTPCell {
|
passwordDisplayButton!.addTarget(self, action: #selector(reversePasswordDisplay), for: UIControlEvents.touchUpInside)
|
||||||
|
buttons = passwordDisplayButton
|
||||||
|
}
|
||||||
|
case .HOTP:
|
||||||
// hotp button
|
// hotp button
|
||||||
let nextButton = UIButton(type: .system)
|
let nextButton = UIButton(type: .system)
|
||||||
nextButton.frame = CGRect(x: 0, y: 0, width: width, height: height)
|
nextButton.frame = CGRect(x: 0, y: 0, width: width, height: height)
|
||||||
|
|
@ -184,6 +204,9 @@ class LabelTableViewCell: UITableViewCell {
|
||||||
buttons!.frame = CGRect(x: 0, y: 0, width: width * 2, height: height)
|
buttons!.frame = CGRect(x: 0, y: 0, width: width * 2, height: height)
|
||||||
buttons!.addSubview(nextButton)
|
buttons!.addSubview(nextButton)
|
||||||
buttons!.addSubview(passwordDisplayButton!)
|
buttons!.addSubview(passwordDisplayButton!)
|
||||||
|
default:
|
||||||
|
passwordDisplayButton = nil
|
||||||
|
buttons = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue