Polish and simplify code of PasswordView

This commit is contained in:
Bob Sun 2017-03-02 16:46:23 +08:00
parent e1b5c0d524
commit 90c468acef
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4

View file

@ -26,6 +26,34 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
private var filteredPasswordsTableEntries: [PasswordsTableEntry] = []
private var parentPasswordEntity: PasswordEntity? = nil
var sections : [(index: Int, length :Int, title: String)] = Array()
var searchActive : Bool = false
lazy var searchController: UISearchController = {
let uiSearchController = UISearchController(searchResultsController: nil)
uiSearchController.searchResultsUpdater = self
uiSearchController.dimsBackgroundDuringPresentation = false
uiSearchController.searchBar.isTranslucent = false
uiSearchController.searchBar.backgroundColor = UIColor.gray
uiSearchController.searchBar.sizeToFit()
return uiSearchController
}()
lazy var refreshControl: UIRefreshControl = {
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(PasswordsViewController.handleRefresh(_:)), for: UIControlEvents.valueChanged)
return refreshControl
}()
lazy var searchBarView: UIView = {
let uiView = UIView(frame: CGRect(x: 0, y: 64, width: UIScreen.main.bounds.width, height: 44))
uiView.addSubview(self.searchController.searchBar)
return uiView
}()
lazy var backUIBarButtonItem: UIBarButtonItem = {
let backUIBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(self.backAction(_:)))
return backUIBarButtonItem
}()
@IBOutlet weak var tableView: UITableView!
private func initPasswordsTableEntries() {
passwordsTableEntries.removeAll()
filteredPasswordsTableEntries.removeAll()
@ -34,29 +62,13 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
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)
}
}
var sections : [(index: Int, length :Int, title: String)] = Array()
var searchActive : Bool = false
let searchController = UISearchController(searchResultsController: nil)
lazy var refreshControl: UIRefreshControl = {
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(PasswordsViewController.handleRefresh(_:)), for: UIControlEvents.valueChanged)
return refreshControl
}()
let searchBarView = UIView(frame: CGRect(x: 0, y: 64, width: UIScreen.main.bounds.width, height: 44))
lazy var backUIBarButtonItem: UIBarButtonItem = {
let backUIBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(self.backAction(_:)))
return backUIBarButtonItem
}()
@IBOutlet weak var tableView: UITableView!
@IBAction func cancelAddPassword(segue: UIStoryboardSegue) {
}
@ -135,13 +147,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
generateSections(item: passwordsTableEntries)
tableView.delegate = self
tableView.dataSource = self
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.isTranslucent = false
searchController.searchBar.backgroundColor = UIColor.gray
searchController.searchBar.sizeToFit()
definesPresentationContext = true
searchBarView.addSubview(searchController.searchBar)
view.addSubview(searchBarView)
tableView.insertSubview(refreshControl, at: 0)
SVProgressHUD.setDefaultMaskType(.black)