Polish code about edit/add passwords
This commit is contained in:
parent
56edfcfb9c
commit
5116cd4f3d
5 changed files with 54 additions and 95 deletions
|
|
@ -9,61 +9,19 @@
|
||||||
import UIKit
|
import UIKit
|
||||||
import SwiftyUserDefaults
|
import SwiftyUserDefaults
|
||||||
|
|
||||||
class AddPasswordTableViewController: UITableViewController, FillPasswordTableViewCellDelegate {
|
class AddPasswordTableViewController: PasswordEditorTableViewController {
|
||||||
let tableTitles = ["name", "password", "additions"]
|
|
||||||
let tableRowsInSection = [1, 2, 1]
|
|
||||||
var password: Password?
|
|
||||||
|
|
||||||
var passwordLengthCell: SliderTableViewCell?
|
var password: Password?
|
||||||
|
var tempContent: String = ""
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
|
tableData = [
|
||||||
|
[[.type: PasswordEditorCellType.textFieldCell, .title: "name"]],
|
||||||
|
[[.type: PasswordEditorCellType.fillPasswordCell, .title: "password"],
|
||||||
|
[.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"]],
|
||||||
|
[[.type: PasswordEditorCellType.textViewCell, .title: "additions"]],
|
||||||
|
]
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
tableView.register(UINib(nibName: "TextFieldTableViewCell", bundle: nil), forCellReuseIdentifier: "textFieldCell")
|
|
||||||
tableView.register(UINib(nibName: "TextViewTableViewCell", bundle: nil), forCellReuseIdentifier: "textViewCell")
|
|
||||||
tableView.register(UINib(nibName: "FillPasswordTableViewCell", bundle: nil), forCellReuseIdentifier: "fillPasswordCell")
|
|
||||||
tableView.register(UINib(nibName: "SliderTableViewCell", bundle: nil), forCellReuseIdentifier: "passwordLengthCell")
|
|
||||||
|
|
||||||
tableView.rowHeight = UITableViewAutomaticDimension
|
|
||||||
tableView.estimatedRowHeight = 48
|
|
||||||
tableView.allowsSelection = false
|
|
||||||
}
|
|
||||||
|
|
||||||
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
||||||
return tableRowsInSection[section]
|
|
||||||
}
|
|
||||||
|
|
||||||
override func numberOfSections(in tableView: UITableView) -> Int {
|
|
||||||
return tableTitles.count
|
|
||||||
}
|
|
||||||
|
|
||||||
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
||||||
switch tableTitles[indexPath.section] {
|
|
||||||
case "additions":
|
|
||||||
let cell = tableView.dequeueReusableCell(withIdentifier: "textViewCell", for: indexPath) as! TextViewTableViewCell
|
|
||||||
cell.contentTextView.text = ""
|
|
||||||
return cell
|
|
||||||
case "password":
|
|
||||||
switch indexPath.row {
|
|
||||||
case 0:
|
|
||||||
let cell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as! FillPasswordTableViewCell
|
|
||||||
cell.delegate = self
|
|
||||||
return cell
|
|
||||||
default:
|
|
||||||
passwordLengthCell = (tableView.dequeueReusableCell(withIdentifier: "passwordLengthCell", for: indexPath) as! SliderTableViewCell)
|
|
||||||
passwordLengthCell!.reset(title: "Length", minimumValue: 1, maximumValue: Globals.passwordMaximumLength, defaultValue: Globals.passwordDefaultLength)
|
|
||||||
return passwordLengthCell!
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as! TextFieldTableViewCell
|
|
||||||
cell.contentTextField.placeholder = tableTitles[indexPath.section]
|
|
||||||
return cell
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
|
|
||||||
let headerView = UITableViewHeaderFooterView()
|
|
||||||
headerView.textLabel?.text = tableTitles[section].uppercased()
|
|
||||||
return headerView
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
|
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
|
||||||
|
|
@ -76,7 +34,7 @@ class AddPasswordTableViewController: UITableViewController, FillPasswordTableVi
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
// check name
|
// check name
|
||||||
let nameCell = getCellForName(name: "name")! as! TextFieldTableViewCell
|
let nameCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! ContentTableViewCell
|
||||||
if nameCell.getContent()!.isEmpty {
|
if nameCell.getContent()!.isEmpty {
|
||||||
let alertTitle = "Cannot Add Password"
|
let alertTitle = "Cannot Add Password"
|
||||||
let alertMessage = "Please fill in the name."
|
let alertMessage = "Please fill in the name."
|
||||||
|
|
@ -88,33 +46,23 @@ class AddPasswordTableViewController: UITableViewController, FillPasswordTableVi
|
||||||
}
|
}
|
||||||
|
|
||||||
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
||||||
if segue.identifier == "saveAddPasswordSegue" {
|
if segue.identifier == "saveAddPasswordSegue" {let cells = tableView.visibleCells
|
||||||
let nameCell = getCellForName(name: "name")! as! TextFieldTableViewCell
|
var cellContents = [String: String]()
|
||||||
let passwordCell = getCellForName(name: "password")! as! FillPasswordTableViewCell
|
for cell in cells {
|
||||||
let additionsCell = getCellForName(name: "additions")! as! TextViewTableViewCell
|
let indexPath = tableView.indexPath(for: cell)!
|
||||||
password = Password(name: nameCell.contentTextField.text!, plainText: "\(passwordCell.contentTextField.text!)\n\(additionsCell.contentTextView.text!)")
|
let contentCell = cell as! ContentTableViewCell
|
||||||
|
let cellTitle = tableData[indexPath.section][indexPath.row][.title] as! String
|
||||||
|
if let cellContent = contentCell.getContent() {
|
||||||
|
cellContents[cellTitle] = cellContent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var plainText = ""
|
||||||
|
if cellContents["additions"]! != "" {
|
||||||
|
plainText = "\(cellContents["password"]!)\n\(cellContents["additions"]!)"
|
||||||
|
} else {
|
||||||
|
plainText = "\(cellContents["password"]!)"
|
||||||
|
}
|
||||||
|
password = Password(name: cellContents["name"]!, plainText: plainText)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
|
||||||
return 44
|
|
||||||
}
|
|
||||||
|
|
||||||
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
|
|
||||||
return 0.1
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCellAt(section: Int) -> UITableViewCell? {
|
|
||||||
return tableView.cellForRow(at: IndexPath(row: 0, section: section))
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCellForName(name: String) -> UITableViewCell? {
|
|
||||||
let index = tableTitles.index(of: name)!
|
|
||||||
return getCellAt(section: Int(index))
|
|
||||||
}
|
|
||||||
|
|
||||||
func generatePassword() -> String {
|
|
||||||
let length = passwordLengthCell?.roundedValue ?? Globals.passwordDefaultLength
|
|
||||||
return Utils.generatePassword(length: length)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,12 +15,10 @@ class EditPasswordTableViewController: PasswordEditorTableViewController {
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
tableData = [
|
tableData = [
|
||||||
[[.type: PasswordEditorCellType.textFieldCell, .title: "name", .content: password!.name]],
|
[[.type: PasswordEditorCellType.textFieldCell, .title: "name", .content: password!.name]],
|
||||||
[[.type: PasswordEditorCellType.fillPasswordCell, .title: "password", .content: password!.password]],
|
[[.type: PasswordEditorCellType.fillPasswordCell, .title: "password", .content: password!.password],
|
||||||
|
[.type: PasswordEditorCellType.passwordLengthCell, .title: "passwordlength"]],
|
||||||
[[.type: PasswordEditorCellType.textViewCell, .title: "additions", .content: password!.getAdditionsPlainText()]],
|
[[.type: PasswordEditorCellType.textViewCell, .title: "additions", .content: password!.getAdditionsPlainText()]],
|
||||||
]
|
]
|
||||||
sectionHeaderTitles = ["name", "password", "additions"].map {$0.uppercased()}
|
|
||||||
sectionFooterTitles = ["", "", "It is recommended to use \"key: value\" format to store additional fields as follows:\n url: https://www.apple.com\n username: passforios@gmail.com."]
|
|
||||||
|
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -47,7 +45,9 @@ class EditPasswordTableViewController: PasswordEditorTableViewController {
|
||||||
let indexPath = tableView.indexPath(for: cell)!
|
let indexPath = tableView.indexPath(for: cell)!
|
||||||
let contentCell = cell as! ContentTableViewCell
|
let contentCell = cell as! ContentTableViewCell
|
||||||
let cellTitle = tableData[indexPath.section][indexPath.row][.title] as! String
|
let cellTitle = tableData[indexPath.section][indexPath.row][.title] as! String
|
||||||
cellContents[cellTitle] = contentCell.getContent()!
|
if let cellContent = contentCell.getContent() {
|
||||||
|
cellContents[cellTitle] = cellContent
|
||||||
|
}
|
||||||
}
|
}
|
||||||
var plainText = ""
|
var plainText = ""
|
||||||
if cellContents["additions"]! != "" {
|
if cellContents["additions"]! != "" {
|
||||||
|
|
|
||||||
|
|
@ -8,20 +8,22 @@
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
enum PasswordEditorCellType {
|
enum PasswordEditorCellType {
|
||||||
case textFieldCell, textViewCell, fillPasswordCell
|
case textFieldCell, textViewCell, fillPasswordCell, passwordLengthCell
|
||||||
}
|
}
|
||||||
|
|
||||||
enum PasswordEditorCellKey {
|
enum PasswordEditorCellKey {
|
||||||
case type, title, content, placeholders
|
case type, title, content, placeholders
|
||||||
}
|
}
|
||||||
|
|
||||||
class PasswordEditorTableViewController: UITableViewController {
|
class PasswordEditorTableViewController: UITableViewController, FillPasswordTableViewCellDelegate {
|
||||||
var navigationItemTitle: String?
|
var navigationItemTitle: String?
|
||||||
var tableData = [
|
var tableData = [
|
||||||
[Dictionary<PasswordEditorCellKey, Any>]
|
[Dictionary<PasswordEditorCellKey, Any>]
|
||||||
]()
|
]()
|
||||||
var sectionHeaderTitles = [String]()
|
var sectionHeaderTitles = ["name", "password", "additions"].map {$0.uppercased()}
|
||||||
var sectionFooterTitles = [String]()
|
var sectionFooterTitles = ["", "", "It is recommended to use \"key: value\" format to store additional fields as follows:\n url: https://www.apple.com\n username: passforios@gmail.com."]
|
||||||
|
|
||||||
|
var passwordLengthCell: SliderTableViewCell?
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
|
|
@ -29,7 +31,7 @@ class PasswordEditorTableViewController: UITableViewController {
|
||||||
tableView.register(UINib(nibName: "TextFieldTableViewCell", bundle: nil), forCellReuseIdentifier: "textFieldCell")
|
tableView.register(UINib(nibName: "TextFieldTableViewCell", bundle: nil), forCellReuseIdentifier: "textFieldCell")
|
||||||
tableView.register(UINib(nibName: "TextViewTableViewCell", bundle: nil), forCellReuseIdentifier: "textViewCell")
|
tableView.register(UINib(nibName: "TextViewTableViewCell", bundle: nil), forCellReuseIdentifier: "textViewCell")
|
||||||
tableView.register(UINib(nibName: "FillPasswordTableViewCell", bundle: nil), forCellReuseIdentifier: "fillPasswordCell")
|
tableView.register(UINib(nibName: "FillPasswordTableViewCell", bundle: nil), forCellReuseIdentifier: "fillPasswordCell")
|
||||||
|
tableView.register(UINib(nibName: "SliderTableViewCell", bundle: nil), forCellReuseIdentifier: "passwordLengthCell")
|
||||||
|
|
||||||
tableView.rowHeight = UITableViewAutomaticDimension
|
tableView.rowHeight = UITableViewAutomaticDimension
|
||||||
tableView.estimatedRowHeight = 48
|
tableView.estimatedRowHeight = 48
|
||||||
|
|
@ -46,7 +48,13 @@ class PasswordEditorTableViewController: UITableViewController {
|
||||||
case .textViewCell:
|
case .textViewCell:
|
||||||
cell = tableView.dequeueReusableCell(withIdentifier: "textViewCell", for: indexPath) as! ContentTableViewCell
|
cell = tableView.dequeueReusableCell(withIdentifier: "textViewCell", for: indexPath) as! ContentTableViewCell
|
||||||
case .fillPasswordCell:
|
case .fillPasswordCell:
|
||||||
cell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as! ContentTableViewCell
|
let fillPasswordCell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as?FillPasswordTableViewCell
|
||||||
|
fillPasswordCell?.delegate = self
|
||||||
|
cell = fillPasswordCell!
|
||||||
|
case .passwordLengthCell:
|
||||||
|
passwordLengthCell = tableView.dequeueReusableCell(withIdentifier: "passwordLengthCell", for: indexPath) as? SliderTableViewCell
|
||||||
|
passwordLengthCell?.reset(title: "Length", minimumValue: 1, maximumValue: Globals.passwordMaximumLength, defaultValue: Globals.passwordDefaultLength)
|
||||||
|
cell = passwordLengthCell!
|
||||||
default:
|
default:
|
||||||
cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as! ContentTableViewCell
|
cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as! ContentTableViewCell
|
||||||
}
|
}
|
||||||
|
|
@ -76,5 +84,8 @@ class PasswordEditorTableViewController: UITableViewController {
|
||||||
return sectionFooterTitles[section]
|
return sectionFooterTitles[section]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func generatePassword() -> String {
|
||||||
|
let length = passwordLengthCell?.roundedValue ?? Globals.passwordDefaultLength
|
||||||
|
return Utils.generatePassword(length: length)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ class Globals {
|
||||||
static let red = UIColor(red:1.00, green:0.23, blue:0.19, alpha:1.0)
|
static let red = UIColor(red:1.00, green:0.23, blue:0.19, alpha:1.0)
|
||||||
static let blue = UIColor(red:0.00, green:0.48, blue:1.00, alpha:1.0)
|
static let blue = UIColor(red:0.00, green:0.48, blue:1.00, alpha:1.0)
|
||||||
|
|
||||||
static let passwordMaximumLength = 64
|
static let passwordMaximumLength = 32
|
||||||
static let passwordDefaultLength = 16
|
static let passwordDefaultLength = 16
|
||||||
|
|
||||||
private init() { }
|
private init() { }
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
class SliderTableViewCell: UITableViewCell {
|
class SliderTableViewCell: ContentTableViewCell {
|
||||||
|
|
||||||
@IBOutlet weak var titleLabel: UILabel!
|
@IBOutlet weak var titleLabel: UILabel!
|
||||||
@IBOutlet weak var valueLabel: UILabel!
|
@IBOutlet weak var valueLabel: UILabel!
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue