From a4b1f87b56d459c68c3d38a2e9de303c4eeacb45 Mon Sep 17 00:00:00 2001 From: Mingshen Sun Date: Sun, 17 Nov 2019 23:25:30 -0800 Subject: [PATCH] Tap title to change between all and unsynced passwords --- .../Controllers/PasswordsViewController.swift | 59 +++++++++++++++++-- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/pass/Controllers/PasswordsViewController.swift b/pass/Controllers/PasswordsViewController.swift index 51276e8..f98d60c 100644 --- a/pass/Controllers/PasswordsViewController.swift +++ b/pass/Controllers/PasswordsViewController.swift @@ -32,10 +32,15 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV private let keychain = AppKeychain.shared private var tapTabBarTime: TimeInterval = 0 + private var tapNavigationBarGestureRecognizer: UITapGestureRecognizer! private var sections = [(title: String, entries: [PasswordsTableEntry])]() private var searchActive : Bool = false + private enum PasswordLabel { + case all + case unsynced + } private lazy var searchController: UISearchController = { let uiSearchController = UISearchController(searchResultsController: nil) @@ -248,6 +253,8 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV view.addSubview(searchBarView!) } navigationItem.title = "PasswordStore".localize() + tapNavigationBarGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTapNavigationBar)) + tableView.refreshControl = syncControl SVProgressHUD.setDefaultMaskType(.black) tableView.register(UINib(nibName: "PasswordWithFolderTableViewCell", bundle: nil), forCellReuseIdentifier: "passwordWithFolderTableViewCell") @@ -267,12 +274,48 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV self.view.addGestureRecognizer(swipeRight) } + @objc func didTapNavigationBar(_ sender: UITapGestureRecognizer) { + let location = sender.location(in: self.navigationController?.navigationBar) + let hitView = self.navigationController?.navigationBar.hitTest(location, with: nil) + guard !(hitView is UIControl) else { return } + guard (passwordStore.numberOfLocalCommits != 0) else { return } + + let ac = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) + let allAction = UIAlertAction(title: "All Passwords", style: .default) { _ in + self.reloadTableView(parent: nil, label: .all) + } + let unsyncedAction = UIAlertAction(title: "Unsynced Passwords", style: .default) { _ in + self.filteredPasswordsTableEntries = self.passwordsTableEntries.filter { entry in + return !entry.passwordEntity!.synced + } + self.reloadTableView(data: self.filteredPasswordsTableEntries, label: .unsynced) + } + let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) + + ac.addAction(allAction) + ac.addAction(unsyncedAction) + ac.addAction(cancelAction) + + self.present(ac, animated: true, completion: nil) + } + override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) tabBarController!.delegate = self if let path = tableView.indexPathForSelectedRow { tableView.deselectRow(at: path, animated: false) } + + // Add gesture recognizer to the navigation bar when the view is about to appear + self.navigationController?.navigationBar.addGestureRecognizer(tapNavigationBarGestureRecognizer) + + // This allows controlls in the navigation bar to continue receiving touches + tapNavigationBarGestureRecognizer.cancelsTouchesInView = false + } + + override func viewWillDisappear(_ animated: Bool) { + // Remove gesture recognizer from navigation bar when view is about to disappear + self.navigationController?.navigationBar.removeGestureRecognizer(tapNavigationBarGestureRecognizer) } override func viewWillLayoutSubviews() { @@ -548,7 +591,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV } - private func reloadTableView(data: [PasswordsTableEntry], anim: CAAnimation? = nil) { + private func reloadTableView(data: [PasswordsTableEntry], label: PasswordLabel = .all, anim: CAAnimation? = nil) { // set navigation item if let numberOfLocalCommits = passwordStore.numberOfLocalCommits, numberOfLocalCommits != 0 { navigationController?.tabBarItem.badgeValue = "\(numberOfLocalCommits)" @@ -561,12 +604,19 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV if #available(iOS 11, *) { navigationController?.navigationBar.prefersLargeTitles = false } + self.navigationController?.navigationBar.removeGestureRecognizer(tapNavigationBarGestureRecognizer) } else { navigationItem.leftBarButtonItem = nil - navigationItem.title = "PasswordStore".localize() + switch label { + case .all: + navigationItem.title = "PasswordStore".localize() + case .unsynced: + navigationItem.title = "Unsynced" + } if #available(iOS 11, *) { navigationController?.navigationBar.prefersLargeTitles = true } + self.navigationController?.navigationBar.addGestureRecognizer(tapNavigationBarGestureRecognizer) } navigationItem.rightBarButtonItem = addPasswordUIBarButtonItem @@ -593,9 +643,9 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV return formatter.string(from: date) } - private func reloadTableView(parent: PasswordEntity?, anim: CAAnimation? = nil) { + private func reloadTableView(parent: PasswordEntity?, label: PasswordLabel = .all, anim: CAAnimation? = nil) { initPasswordsTableEntries(parent: parent) - reloadTableView(data: passwordsTableEntries, anim: anim) + reloadTableView(data: passwordsTableEntries, label: label, anim: anim) } @objc func actOnReloadTableViewRelatedNotification() { @@ -635,7 +685,6 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV updateSearchResults(for: searchController) } - func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool { // set the default search scope to "all" if SharedDefaults[.isShowFolderOn] && SharedDefaults[.searchDefault] == .all {