add refresh control without function
This commit is contained in:
parent
a473e9f4f2
commit
8755575779
1 changed files with 34 additions and 8 deletions
|
|
@ -16,6 +16,13 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
|
||||||
var sections : [(index: Int, length :Int, title: String)] = Array()
|
var sections : [(index: Int, length :Int, title: String)] = Array()
|
||||||
var searchActive : Bool = false
|
var searchActive : Bool = false
|
||||||
let searchController = UISearchController(searchResultsController: nil)
|
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))
|
||||||
|
|
||||||
@IBOutlet weak var tableView: UITableView!
|
@IBOutlet weak var tableView: UITableView!
|
||||||
|
|
||||||
|
|
@ -34,8 +41,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
|
||||||
}
|
}
|
||||||
print("pull success")
|
print("pull success")
|
||||||
self.passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
|
self.passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
|
||||||
self.generateSections(item: self.passwordEntities!)
|
self.reloadTableView(data: self.passwordEntities!)
|
||||||
self.tableView.reloadData()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -51,9 +57,9 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
|
||||||
searchController.dimsBackgroundDuringPresentation = false
|
searchController.dimsBackgroundDuringPresentation = false
|
||||||
searchController.searchBar.sizeToFit()
|
searchController.searchBar.sizeToFit()
|
||||||
definesPresentationContext = true
|
definesPresentationContext = true
|
||||||
let searchBarView = UIView(frame: CGRect(x: 0, y: 64, width: searchController.searchBar.frame.size.width, height: 44))
|
|
||||||
searchBarView.addSubview(searchController.searchBar)
|
searchBarView.addSubview(searchController.searchBar)
|
||||||
view.addSubview(searchBarView)
|
view.addSubview(searchBarView)
|
||||||
|
tableView.insertSubview(refreshControl, at: 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
override func viewWillAppear(_ animated: Bool) {
|
override func viewWillAppear(_ animated: Bool) {
|
||||||
|
|
@ -96,6 +102,19 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
|
||||||
return index
|
return index
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
|
||||||
|
let index = sections[indexPath.section].index + indexPath.row
|
||||||
|
let password: PasswordEntity
|
||||||
|
if searchController.isActive && searchController.searchBar.text != "" {
|
||||||
|
password = filteredPasswordEntities[index]
|
||||||
|
} else {
|
||||||
|
password = passwordEntities![index]
|
||||||
|
}
|
||||||
|
let decryptedPassword = password.decrypt()!
|
||||||
|
UIPasteboard.general.string = decryptedPassword.password
|
||||||
|
}
|
||||||
|
|
||||||
func generateSections(item: [PasswordEntity]) {
|
func generateSections(item: [PasswordEntity]) {
|
||||||
sections.removeAll()
|
sections.removeAll()
|
||||||
if item.count == 0 {
|
if item.count == 0 {
|
||||||
|
|
@ -120,8 +139,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
|
||||||
|
|
||||||
func actOnPasswordUpdatedNotification() {
|
func actOnPasswordUpdatedNotification() {
|
||||||
passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
|
passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
|
||||||
generateSections(item: passwordEntities!)
|
reloadTableView(data: passwordEntities!)
|
||||||
self.tableView.reloadData()
|
|
||||||
print("actOnPasswordUpdatedNotification")
|
print("actOnPasswordUpdatedNotification")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -132,7 +150,6 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
|
||||||
let index = sections[selectedIndexPath.section].index + selectedIndexPath.row
|
let index = sections[selectedIndexPath.section].index + selectedIndexPath.row
|
||||||
let password: PasswordEntity
|
let password: PasswordEntity
|
||||||
if searchController.isActive && searchController.searchBar.text != "" {
|
if searchController.isActive && searchController.searchBar.text != "" {
|
||||||
|
|
||||||
password = filteredPasswordEntities[index]
|
password = filteredPasswordEntities[index]
|
||||||
} else {
|
} else {
|
||||||
password = passwordEntities![index]
|
password = passwordEntities![index]
|
||||||
|
|
@ -142,17 +159,26 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func filterContentForSearchText(searchText: String, scope: String = "All") {
|
func filterContentForSearchText(searchText: String, scope: String = "All") {
|
||||||
filteredPasswordEntities = passwordEntities!.filter { password in
|
filteredPasswordEntities = passwordEntities!.filter { password in
|
||||||
return password.name!.lowercased().contains(searchText.lowercased())
|
return password.name!.lowercased().contains(searchText.lowercased())
|
||||||
}
|
}
|
||||||
if searchController.isActive && searchController.searchBar.text != "" {
|
if searchController.isActive && searchController.searchBar.text != "" {
|
||||||
generateSections(item: filteredPasswordEntities)
|
reloadTableView(data: filteredPasswordEntities)
|
||||||
} else {
|
} else {
|
||||||
generateSections(item: passwordEntities!)
|
reloadTableView(data: passwordEntities!)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func reloadTableView (data: [PasswordEntity]) {
|
||||||
|
generateSections(item: data)
|
||||||
tableView.reloadData()
|
tableView.reloadData()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleRefresh(_ refreshControl: UIRefreshControl) {
|
||||||
|
refreshControl.endRefreshing()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension PasswordsViewController: UISearchResultsUpdating {
|
extension PasswordsViewController: UISearchResultsUpdating {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue