support edit password

This commit is contained in:
Bob Sun 2017-02-13 01:15:42 +08:00
parent 41d45bfbf9
commit b2ee8c429f
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
14 changed files with 459 additions and 72 deletions

View file

@ -0,0 +1,53 @@
//
// EditPasswordTableViewController.swift
// pass
//
// Created by Mingshen Sun on 12/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import UIKit
class EditPasswordTableViewController: PasswordEditorTableViewController {
var password: Password?
override func viewDidLoad() {
tableData = [
[[.type: PasswordEditorCellType.textFieldCell, .title: "name", .content: password!.name]],
[[.type: PasswordEditorCellType.fillPasswordCell, .title: "password", .content: password!.password]],
[[.type: PasswordEditorCellType.textViewCell, .title: "additions", .content: password!.getAdditionsPlainText()]],
]
sectionHeaderTitles = ["name", "password", "additions"].map {$0.uppercased()}
super.viewDidLoad()
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if identifier == "saveEditPasswordSegue" {
let nameCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! ContentTableViewCell
if nameCell.getContent() != password?.name {
let alertMessage = "Editing name is not supported."
let alert = UIAlertController(title: "Cannot Save Edit", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true) {
nameCell.setContent(content: self.password!.name)
}
return false
}
}
return true
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let cells = tableView.visibleCells
var cellContents = [String: String]()
for cell in cells {
let indexPath = tableView.indexPath(for: cell)!
let contentCell = cell as! ContentTableViewCell
let cellTitle = tableData[indexPath.section][indexPath.row][.title] as! String
cellContents[cellTitle] = contentCell.getContent()!
}
password!.updatePassword(name: cellContents["name"]!, plainText: "\(cellContents["password"]!)\n\(cellContents["additions"]!)")
}
}

View file

@ -64,14 +64,13 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
indicator.startAnimating()
tableView.addSubview(indicator)
tableView.addSubview(indicatorLable)
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(pressEdit(_:)))
if let imageData = passwordEntity?.image {
let image = UIImage(data: imageData as Data)
passwordImage = image
}
tableData.append(TableSection(title: "", item: []))
tableData[0].item.append(TableCell())
DispatchQueue.global(qos: .userInitiated).async {
do {
@ -84,21 +83,23 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
self.present(alert, animated: true, completion: nil)
}
var tableDataIndex = 1
self.tableData.append(TableSection(title: "", item: []))
// var tableDataIndex = 1
// self.tableData.append(TableSection(title: "", item: []))
// let password = self.password!
// if let username = password.getUsername() {
// self.tableData[tableDataIndex].item.append(TableCell(title: "username", content: username))
// }
// self.tableData[tableDataIndex].item.append(TableCell(title: "password", content: password.password))
// if password.additions.count > 0 {
// self.tableData.append(TableSection(title: "additions", item: []))
// tableDataIndex += 1
// for additionKey in password.additionKeys {
// self.tableData[tableDataIndex].item.append(TableCell(title: additionKey, content: password.additions[additionKey]!))
//
// }
// }
let password = self.password!
if let username = password.getUsername() {
self.tableData[tableDataIndex].item.append(TableCell(title: "username", content: username))
}
self.tableData[tableDataIndex].item.append(TableCell(title: "password", content: password.password))
if password.additions.count > 0 {
self.tableData.append(TableSection(title: "additions", item: []))
tableDataIndex += 1
for additionKey in password.additionKeys {
self.tableData[tableDataIndex].item.append(TableCell(title: additionKey, content: password.additions[additionKey]!))
}
}
self.setTableData()
DispatchQueue.main.async { [weak self] in
self?.tableView.reloadData()
indicator.stopAnimating()
@ -112,6 +113,56 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
}
}
func pressEdit(_ sender: Any?) {
performSegue(withIdentifier: "editPasswordSegue", sender: self)
}
@IBAction func cancelEditPassword(segue: UIStoryboardSegue) {
}
@IBAction func saveEditPassword(segue: UIStoryboardSegue) {
if password!.changed {
PasswordStore.shared.update(passwordEntity: passwordEntity!, password: password!, progressBlock: { progress in
})
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
setTableData()
tableView.reloadData()
}
}
func setTableData() {
self.tableData = Array<TableSection>()
tableData.append(TableSection(title: "", item: []))
tableData[0].item.append(TableCell())
var tableDataIndex = 1
self.tableData.append(TableSection(title: "", item: []))
let password = self.password!
if let username = password.getUsername() {
self.tableData[tableDataIndex].item.append(TableCell(title: "username", content: username))
}
self.tableData[tableDataIndex].item.append(TableCell(title: "password", content: password.password))
if password.additions.count > 0 {
self.tableData.append(TableSection(title: "additions", item: []))
tableDataIndex += 1
for additionKey in password.additionKeys {
self.tableData[tableDataIndex].item.append(TableCell(title: additionKey, content: password.additions[additionKey]!))
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "editPasswordSegue" {
if let controller = segue.destination as? UINavigationController {
if let editController = controller.viewControllers.first as? EditPasswordTableViewController {
editController.password = password
}
}
}
}
func updatePasswordImage(url: String) {
do {
try FavIcon.downloadPreferred(url) { [weak self] result in

View file

@ -0,0 +1,77 @@
//
// PasswordEditorTableViewController.swift
// pass
//
// Created by Mingshen Sun on 12/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import UIKit
enum PasswordEditorCellType {
case textFieldCell, textViewCell, fillPasswordCell
}
enum PasswordEditorCellKey {
case type, title, content, placeholders
}
class PasswordEditorTableViewController: UITableViewController {
var navigationItemTitle: String?
var tableData = [
[Dictionary<PasswordEditorCellKey, Any>]
]()
var sectionHeaderTitles = [String]()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = navigationItemTitle
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.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 48
tableView.allowsSelection = false
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellData = tableData[indexPath.section][indexPath.row]
var cell = ContentTableViewCell()
switch cellData[PasswordEditorCellKey.type] as! PasswordEditorCellType {
case .textViewCell:
cell = tableView.dequeueReusableCell(withIdentifier: "textViewCell", for: indexPath) as! ContentTableViewCell
case .fillPasswordCell:
cell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as! ContentTableViewCell
default:
cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as! ContentTableViewCell
}
if let content = cellData[PasswordEditorCellKey.content] as? String {
cell.setContent(content: content)
}
return cell
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.1
}
override func numberOfSections(in tableView: UITableView) -> Int {
return tableData.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData[section].count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionHeaderTitles[section]
}
}

View file

@ -67,6 +67,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
}
})
}
PasswordStore.shared.updatePasswordEntityCoreData()
DispatchQueue.main.async {
self.passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
self.reloadTableView(data: self.passwordEntities!)