git.iko.soy/dashared/hookah-tau
on develop help sign in
Сортировочка клиентов по имени/телефону подъехала
commit61df54551fe6496fa8eb944675e03353aa5af4e9 authorDarya Rednikina <rednikina.com@yandex.ru> dateThu 9 Jan 2020, 16:18 (7 years ago) parent7220254
1 file changed+50−5
Hookah Tau/Controllers/Admin/Clients/ClientsViewController.swift+50−5
Hookah Tau/Controllers/Admin/Clients/ClientsViewController.swift+50−5
@@ -18,16 +18,29 @@ class ClientsViewController: BaseTableViewController {
1818 }
1919 }
2020
21+ var filteredDataSource: [Client] = []
22+
2123 var clientsService: ClientsService?
2224
2325 weak var coordinator: ClientsCoordinator?
2426
27+ var searchControl: UISearchController?
28+
29+ var isFiltering: Bool {
30+ return searchControl?.isActive ?? false && !searchBarIsEmpty()
31+ }
32+
33+ func searchBarIsEmpty() -> Bool {
34+ return searchControl?.searchBar.text?.isEmpty ?? true
35+ }
36+
2537 // MARK: - Lifecycle
2638
2739 override func viewDidLoad() {
2840 super.viewDidLoad()
2941
3042 setupNavigationBar()
43+ setupSearchController()
3144 clientsService = ClientsService(apiClient: APIClient.shared)
3245 }
3346
@@ -49,6 +62,15 @@ class ClientsViewController: BaseTableViewController {
4962 navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareCSVFile))
5063 }
5164
65+ func setupSearchController() {
66+ searchControl = UISearchController(searchResultsController: nil)
67+ navigationItem.searchController = searchControl
68+ navigationItem.hidesSearchBarWhenScrolling = false
69+ searchControl?.obscuresBackgroundDuringPresentation = false
70+ definesPresentationContext = true
71+ searchControl?.searchResultsUpdater = self
72+ }
73+
5274 // MARK: - Api calls
5375
5476 /// Загружаем клиентов кальянки
@@ -100,7 +122,17 @@ class ClientsViewController: BaseTableViewController {
100122 if res {
101123 self.tableView?.beginUpdates()
102124 self.tableView?.reloadRows(at: [indexPath], with: .automatic)
103− self.dataSource[indexPath.row].isBlocked = !wasBlocked
125+ if self.isFiltering {
126+ self.filteredDataSource[indexPath.row].isBlocked = !wasBlocked
127+ let changed = self.filteredDataSource[indexPath.row]
128+ let filtered = self.dataSource.map { (one: Client) -> Client in
129+ if one.uuid == changed.uuid {
130+ return changed
131+ } else { return one } }
132+ self.dataSource = filtered
133+ } else {
134+ self.dataSource[indexPath.row].isBlocked = !wasBlocked
135+ }
104136 self.tableView?.endUpdates()
105137 } else {
106138 self.displayAlert(with: "Не удалось \(wasBlocked ? "удалить" : "добавить") пользователя \(wasBlocked ? "из" : "в") чс! Попробуйте еще раз!")
@@ -117,19 +149,20 @@ extension ClientsViewController {
117149 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
118150 let cell = tableView.dequeueReusableCell(withIdentifier: clientTableViewCellId, for: indexPath) as! ClientTableViewCell
119151
120− let userModel = dataSource[indexPath.row]
152+ let userModel = isFiltering ? filteredDataSource[indexPath.row] : dataSource[indexPath.row]
121153 cell.bind(withModel: userModel)
122154
123155 return cell
124156 }
125157
126158 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
127− return dataSource.count
159+ return isFiltering ? filteredDataSource.count : dataSource.count
128160 }
129161
130162 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
131− let user = dataSource[indexPath.row]
163+ let user = isFiltering ? filteredDataSource[indexPath.row] : dataSource[indexPath.row]
132164 coordinator?.openUserProfile(user)
165+ searchControl?.dismiss(animated: true, completion: nil)
133166 }
134167
135168 // Update
@@ -140,7 +173,7 @@ extension ClientsViewController {
140173
141174 override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
142175
143− let user = dataSource[indexPath.row]
176+ let user = isFiltering ? filteredDataSource[indexPath.row] : dataSource[indexPath.row]
144177 let blockButton = UITableViewRowAction(style: .normal, title: "\(!user.isBlocked ? "🙅🏻‍♀️" : "👍🏻")") { _,_ in
145178
146179 self.updateUserBlockStatus(wasBlocked: user.isBlocked, phone: user.phoneNumber, indexPath: indexPath)
@@ -151,3 +184,15 @@ extension ClientsViewController {
151184 return [blockButton]
152185 }
153186}
187+
188+extension ClientsViewController: UISearchResultsUpdating {
189+ func filterContentForSearchText(_ searchText: String, scope: String = "All")
190+ {
191+ filteredDataSource = dataSource.filter { ($0.name?.lowercased().contains(searchText.lowercased()) ?? false) || ($0.phoneNumber.lowercased().contains(searchText.lowercased())) }
192+ tableView.reloadData()
193+ }
194+
195+ func updateSearchResults(for searchController: UISearchController) {
196+ filterContentForSearchText(searchController.searchBar.text ?? "")
197+ }
198+}