From f75f949ab122f3d04bb64c06c0c6f54db8a69a63 Mon Sep 17 00:00:00 2001 From: Bob Sun Date: Thu, 2 Mar 2017 15:01:52 +0800 Subject: [PATCH] Add switch to turn on/off showing folders --- .../GeneralSettingsTableViewController.swift | 26 +++++++++++++++++-- .../Controllers/PasswordsViewController.swift | 9 ++++++- pass/Helpers/DefaultsKeys.swift | 1 + 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/pass/Controllers/GeneralSettingsTableViewController.swift b/pass/Controllers/GeneralSettingsTableViewController.swift index 8b29182..f80a3c8 100644 --- a/pass/Controllers/GeneralSettingsTableViewController.swift +++ b/pass/Controllers/GeneralSettingsTableViewController.swift @@ -18,6 +18,7 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController { uiSwitch.addTarget(self, action: #selector(hideUnknownSwitchAction(_:)), for: UIControlEvents.valueChanged) return uiSwitch }() + let rememberPassphraseSwitch: UISwitch = { let uiSwitch = UISwitch() uiSwitch.onTintColor = Globals.blue @@ -26,6 +27,15 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController { uiSwitch.isOn = Defaults[.isRememberPassphraseOn] return uiSwitch }() + + let showFolderSwitch: UISwitch = { + let uiSwitch = UISwitch() + uiSwitch.onTintColor = Globals.blue + uiSwitch.sizeToFit() + uiSwitch.addTarget(self, action: #selector(showFolderSwitchAction(_:)), for: UIControlEvents.valueChanged) + uiSwitch.isOn = Defaults[.isShowFolderOn] + return uiSwitch + }() override func viewDidLoad() { navigationItemTitle = "General" @@ -36,6 +46,7 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController { // section 1 [ [.title: "Remember Phassphrase", .action: "none",], + [.title: "Show Folder", .action: "none",], [.title: "Hide Unknown Fields", .action: "none",], ], @@ -46,7 +57,8 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController { override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = super.tableView(tableView, cellForRowAt: indexPath) - if cell.textLabel?.text == "Hide Unknown Fields" { + switch cell.textLabel!.text! { + case "Hide Unknown Fields": cell.accessoryType = .none let detailButton = UIButton(type: .detailDisclosure) hideUnknownSwitch.frame = CGRect(x: detailButton.bounds.width+10, y: 0, width: hideUnknownSwitch.bounds.width, height: hideUnknownSwitch.bounds.height) @@ -58,10 +70,15 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController { cell.accessoryView = accessoryView cell.selectionStyle = .none hideUnknownSwitch.isOn = Defaults[.isHideUnknownOn] - } else if cell.textLabel?.text == "Remember Phassphrase" { + case "Remember Phassphrase": cell.accessoryType = .none cell.selectionStyle = .none cell.accessoryView = rememberPassphraseSwitch + case "Show Folder": + cell.accessoryType = .none + cell.selectionStyle = .none + cell.accessoryView = showFolderSwitch + default: break } return cell } @@ -83,4 +100,9 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController { } } + func showFolderSwitchAction(_ sender: Any?) { + Defaults[.isShowFolderOn] = showFolderSwitch.isOn + NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated"))) + } + } diff --git a/pass/Controllers/PasswordsViewController.swift b/pass/Controllers/PasswordsViewController.swift index 02e6636..222e5ac 100644 --- a/pass/Controllers/PasswordsViewController.swift +++ b/pass/Controllers/PasswordsViewController.swift @@ -29,7 +29,14 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV private func initPasswordsTableEntries() { passwordsTableEntries.removeAll() filteredPasswordsTableEntries.removeAll() - passwordsTableEntries = PasswordStore.shared.fetchPasswordEntityCoreData(parent: parentPasswordEntity).map { + var passwordEntities = [PasswordEntity]() + if Defaults[.isShowFolderOn] { + passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData(parent: parentPasswordEntity) + } else { + passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData(withDir: false) + + } + passwordsTableEntries = passwordEntities.map { PasswordsTableEntry(title: $0.name!, isDir: $0.isDir, passwordEntity: $0) } } diff --git a/pass/Helpers/DefaultsKeys.swift b/pass/Helpers/DefaultsKeys.swift index b77c685..29ddfe4 100644 --- a/pass/Helpers/DefaultsKeys.swift +++ b/pass/Helpers/DefaultsKeys.swift @@ -34,6 +34,7 @@ extension DefaultsKeys { static let isHideUnknownOn = DefaultsKey("isHideUnknownOn") static let isRememberPassphraseOn = DefaultsKey("isRememberPassphraseOn") + static let isShowFolderOn = DefaultsKey("isShowFolderOn") static let passwordGenerationMethod = DefaultsKey("passwordGenerationMethod")