diff --git a/pass/Base.lproj/Main.storyboard b/pass/Base.lproj/Main.storyboard index 059fe5f..bc3b458 100644 --- a/pass/Base.lproj/Main.storyboard +++ b/pass/Base.lproj/Main.storyboard @@ -89,11 +89,11 @@ - + - + @@ -109,18 +109,18 @@ - + - + - + @@ -136,18 +136,18 @@ - + - + - + @@ -164,18 +164,18 @@ - + - + - + @@ -192,7 +192,7 @@ - + @@ -209,11 +209,11 @@ - + - + @@ -233,11 +233,11 @@ - + - + @@ -727,6 +727,9 @@ + + + @@ -951,7 +954,7 @@ - + @@ -972,7 +975,7 @@ - + diff --git a/pass/Controllers/AddPasswordTableViewController.swift b/pass/Controllers/AddPasswordTableViewController.swift index d50afb1..7c59287 100644 --- a/pass/Controllers/AddPasswordTableViewController.swift +++ b/pass/Controllers/AddPasswordTableViewController.swift @@ -10,8 +10,6 @@ import UIKit import SwiftyUserDefaults class AddPasswordTableViewController: PasswordEditorTableViewController { - - var password: Password? var tempContent: String = "" let passwordStore = PasswordStore.shared diff --git a/pass/Controllers/EditPasswordTableViewController.swift b/pass/Controllers/EditPasswordTableViewController.swift index 405e321..a6ef6cf 100644 --- a/pass/Controllers/EditPasswordTableViewController.swift +++ b/pass/Controllers/EditPasswordTableViewController.swift @@ -9,15 +9,13 @@ 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.passwordLengthCell, .title: "passwordlength"]], [[.type: PasswordEditorCellType.textViewCell, .title: "additions", .content: password!.getAdditionsPlainText()]], + [[.type: PasswordEditorCellType.deletePasswordCell]], ] super.viewDidLoad() } diff --git a/pass/Controllers/PasswordDetailTableViewController.swift b/pass/Controllers/PasswordDetailTableViewController.swift index 2262e76..01cff75 100644 --- a/pass/Controllers/PasswordDetailTableViewController.swift +++ b/pass/Controllers/PasswordDetailTableViewController.swift @@ -192,6 +192,12 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni } } + @IBAction func deletePassword(segue: UIStoryboardSegue) { + print("delete") + passwordStore.delete(passwordEntity: passwordEntity!) + navigationController?.popViewController(animated: true) + } + func setTableData() { self.tableData = Array() tableData.append(TableSection(title: "", item: [])) diff --git a/pass/Controllers/PasswordEditorTableViewController.swift b/pass/Controllers/PasswordEditorTableViewController.swift index 874e3e9..d3a055e 100644 --- a/pass/Controllers/PasswordEditorTableViewController.swift +++ b/pass/Controllers/PasswordEditorTableViewController.swift @@ -8,7 +8,7 @@ import UIKit enum PasswordEditorCellType { - case textFieldCell, textViewCell, fillPasswordCell, passwordLengthCell + case textFieldCell, textViewCell, fillPasswordCell, passwordLengthCell, deletePasswordCell } enum PasswordEditorCellKey { @@ -17,14 +17,24 @@ enum PasswordEditorCellKey { class PasswordEditorTableViewController: UITableViewController, FillPasswordTableViewCellDelegate { var navigationItemTitle: String? + var password: Password? var tableData = [ [Dictionary] ]() - var sectionHeaderTitles = ["name", "password", "additions"].map {$0.uppercased()} - 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 sectionHeaderTitles = ["name", "password", "additions",""].map {$0.uppercased()} + var sectionFooterTitles = ["", "", "Use \"key: value\" format for additional fields.", ""] var passwordLengthCell: SliderTableViewCell? - + var deletePasswordCell: UITableViewCell? + + override func loadView() { + super.loadView() + + deletePasswordCell = UITableViewCell(style: .default, reuseIdentifier: "default") + deletePasswordCell!.textLabel?.text = "Delete Password" + deletePasswordCell!.textLabel?.textColor = Globals.red + deletePasswordCell?.selectionStyle = .default + } override func viewDidLoad() { super.viewDidLoad() navigationItem.title = navigationItemTitle @@ -35,33 +45,34 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 48 - tableView.allowsSelection = false self.tableView.sectionFooterHeight = UITableViewAutomaticDimension; self.tableView.estimatedSectionFooterHeight = 0; } 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 + let cell = tableView.dequeueReusableCell(withIdentifier: "textViewCell", for: indexPath) as! ContentTableViewCell + cell.setContent(content: cellData[PasswordEditorCellKey.content] as? String) + return cell case .fillPasswordCell: - let fillPasswordCell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as?FillPasswordTableViewCell - fillPasswordCell?.delegate = self - cell = fillPasswordCell! + let cell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as! FillPasswordTableViewCell + cell.delegate = self + cell.setContent(content: cellData[PasswordEditorCellKey.content] as? String) + return cell case .passwordLengthCell: passwordLengthCell = tableView.dequeueReusableCell(withIdentifier: "passwordLengthCell", for: indexPath) as? SliderTableViewCell passwordLengthCell?.reset(title: "Length", minimumValue: Globals.passwordMinimumLength, maximumValue: Globals.passwordMaximumLength, defaultValue: Globals.passwordDefaultLength) - cell = passwordLengthCell! + return passwordLengthCell! + case .deletePasswordCell: + return deletePasswordCell! default: - cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as! ContentTableViewCell + let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as! ContentTableViewCell + cell.setContent(content: cellData[PasswordEditorCellKey.content] as? String) + return cell } - if let content = cellData[PasswordEditorCellKey.content] as? String { - cell.setContent(content: content) - } - return cell } override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { @@ -84,6 +95,18 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl return sectionFooterTitles[section] } + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + if tableView.cellForRow(at: indexPath) == deletePasswordCell { + let alert = UIAlertController(title: "Delete Password?", message: nil, preferredStyle: UIAlertControllerStyle.alert) + alert.addAction(UIAlertAction(title: "Delete", style: UIAlertActionStyle.destructive, handler: {[unowned self] (action) -> Void in + self.performSegue(withIdentifier: "deletePasswordSegue", sender: self) + })) + alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler:nil)) + self.present(alert, animated: true, completion: nil) + } + tableView.deselectRow(at: indexPath, animated: true) + } + func generatePassword() -> String { let length = passwordLengthCell?.roundedValue ?? Globals.passwordDefaultLength return Utils.generatePassword(length: length) diff --git a/pass/Models/PasswordStore.swift b/pass/Models/PasswordStore.swift index 8cc99d6..7163358 100644 --- a/pass/Models/PasswordStore.swift +++ b/pass/Models/PasswordStore.swift @@ -485,19 +485,17 @@ class PasswordStore { return nil } - func createRemoveCommitInRepository(message: String, filename: String, progressBlock: (_ progress: Float) -> Void) -> GTCommit? { + func createRemoveCommitInRepository(message: String, path: String) -> GTCommit? { do { - try storeRepository?.index().removeFile(filename) + try storeRepository?.index().removeFile(path) try storeRepository?.index().write() let newTree = try storeRepository!.index().writeTree() let headReference = try storeRepository!.headReference() let commitEnum = try GTEnumerator(repository: storeRepository!) try commitEnum.pushSHA(headReference.targetOID.sha!) let parent = commitEnum.nextObject() as! GTCommit - progressBlock(0.5) let signature = gitSignatureForNow let commit = try storeRepository!.createCommit(with: newTree, message: message, author: signature, committer: signature, parents: [parent], updatingReferenceNamed: headReference.name) - progressBlock(0.7) return commit } catch { print(error) @@ -568,6 +566,18 @@ class PasswordStore { } } + public func delete(passwordEntity: PasswordEntity) { + Utils.removeFileIfExists(at: storeURL.appendingPathComponent(passwordEntity.path!)) + let _ = createRemoveCommitInRepository(message: "Remove \(passwordEntity.nameWithCategory) from store using Pass for iOS", path: passwordEntity.path!) + context.delete(passwordEntity) + do { + try context.save() + } catch { + fatalError("Failed to delete a PasswordEntity: \(error)") + } + NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil) + } + func saveUpdated(passwordEntity: PasswordEntity) { do { try context.save() diff --git a/pass/Views/ContentTableViewCell.swift b/pass/Views/ContentTableViewCell.swift index a434dd2..d68dea4 100644 --- a/pass/Views/ContentTableViewCell.swift +++ b/pass/Views/ContentTableViewCell.swift @@ -23,6 +23,6 @@ class ContentTableViewCell: UITableViewCell { return nil } - func setContent(content: String) { } + func setContent(content: String?) { } } diff --git a/pass/Views/FillPasswordTableViewCell.swift b/pass/Views/FillPasswordTableViewCell.swift index 75dfb9f..23c2646 100644 --- a/pass/Views/FillPasswordTableViewCell.swift +++ b/pass/Views/FillPasswordTableViewCell.swift @@ -38,7 +38,7 @@ class FillPasswordTableViewCell: ContentTableViewCell { return contentTextField.attributedText?.string } - override func setContent(content: String) { - contentTextField.attributedText = Utils.attributedPassword(plainPassword: content) + override func setContent(content: String?) { + contentTextField.attributedText = Utils.attributedPassword(plainPassword: content ?? "") } } diff --git a/pass/Views/TextFieldTableViewCell.swift b/pass/Views/TextFieldTableViewCell.swift index d558abf..1522d36 100644 --- a/pass/Views/TextFieldTableViewCell.swift +++ b/pass/Views/TextFieldTableViewCell.swift @@ -23,7 +23,7 @@ class TextFieldTableViewCell: ContentTableViewCell { override func getContent() -> String? { return contentTextField.text } - override func setContent(content: String) { + override func setContent(content: String?) { contentTextField.text = content } } diff --git a/pass/Views/TextViewTableViewCell.swift b/pass/Views/TextViewTableViewCell.swift index bd54287..616d254 100644 --- a/pass/Views/TextViewTableViewCell.swift +++ b/pass/Views/TextViewTableViewCell.swift @@ -22,7 +22,7 @@ class TextViewTableViewCell: ContentTableViewCell { return contentTextView.text } - override func setContent(content: String) { + override func setContent(content: String?) { contentTextView.text = content } } diff --git a/pass/Views/TextViewTableViewCell.xib b/pass/Views/TextViewTableViewCell.xib index bfed387..83178bb 100644 --- a/pass/Views/TextViewTableViewCell.xib +++ b/pass/Views/TextViewTableViewCell.xib @@ -1,5 +1,5 @@ - + @@ -20,10 +20,10 @@ - + - + @@ -32,8 +32,8 @@ - - + +