passforios/pass/Views/LabelTableViewCell.swift

152 lines
5.2 KiB
Swift
Raw Normal View History

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
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
var isPasswordCell = false
2017-02-08 17:55:18 +08:00
var isURLCell = false
var isReveal = false
var isHOTPCell = false
let passwordDots = "••••••••••••"
2017-02-05 13:56:37 +08:00
weak var passwordTableView : PasswordDetailTableViewController?
2017-02-05 13:56:37 +08:00
var cellData: LabelTableViewCellData? {
didSet {
titleLabel.text = cellData?.title ?? ""
if isPasswordCell {
if isReveal {
contentLabel.attributedText = Utils.attributedPassword(plainPassword: cellData?.content ?? "")
} else {
contentLabel.text = passwordDots
}
2017-02-24 21:33:36 +08:00
contentLabel.font = UIFont(name: "Menlo", size: contentLabel.font.pointSize)
} else if isHOTPCell {
if isReveal {
contentLabel.text = cellData?.content ?? ""
} else {
contentLabel.text = passwordDots
}
} 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 {
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(_:))
}
if isHOTPCell {
if isReveal {
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.concealPassword(_:)) || action == #selector(LabelTableViewCell.nextPassword(_:))
} else {
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.revealPassword(_:)) || action == #selector(LabelTableViewCell.nextPassword(_:))
}
}
2017-02-05 00:35:23 +08:00
return action == #selector(copy(_:))
}
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
func revealPassword(_ sender: Any?) {
if let plainPassword = cellData?.content {
if isHOTPCell {
contentLabel.text = plainPassword
} else {
contentLabel.attributedText = Utils.attributedPassword(plainPassword: plainPassword)
}
} else {
contentLabel.text = ""
}
isReveal = true
}
func concealPassword(_ sender: Any?) {
contentLabel.text = passwordDots
isReveal = false
}
2017-02-08 17:55:18 +08:00
func nextPassword(_ sender: Any?) {
guard let password = passwordTableView?.password,
let passwordEntity = passwordTableView?.passwordEntity else {
print("Cannot find password/passwordEntity of a cell")
return;
}
// increase HOTP counter
password.increaseHotpCounter()
// only the HOTP password needs update
if let plainPassword = password.otpToken?.currentPassword {
cellData?.content = plainPassword
// contentLabel will be updated automatically
}
// commit
if password.changed {
DispatchQueue.global(qos: .userInitiated).async {
PasswordStore.shared.update(passwordEntity: passwordEntity, password: password, progressBlock: {_ in })
DispatchQueue.main.async {
passwordEntity.synced = false
PasswordStore.shared.saveUpdated(passwordEntity: passwordEntity)
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
// reload so that the "unsynced" symbol could be added
self.passwordTableView?.tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: UITableViewRowAnimation.automatic)
SVProgressHUD.showSuccess(withStatus: "Password Copied\nCounter Updated")
SVProgressHUD.dismiss(withDelay: 1)
}
}
}
}
2017-02-08 17:55:18 +08:00
func openLink(_ sender: Any?) {
guard let password = passwordTableView?.password else {
print("Cannot find password of a cell")
return;
}
Utils.copyToPasteboard(textToCopy: password.password)
2017-02-15 15:29:06 +08:00
UIApplication.shared.open(URL(string: cellData!.content)!, options: [:], completionHandler: nil)
2017-02-08 17:55:18 +08:00
}
2017-02-02 21:04:31 +08:00
}