CompanyDirectoryCategoryViewController.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. class CompanyDirectoryCategoryViewController: ThemedTableViewController {
  22. var companyDirectoryViewController: CompanyDirectoryViewController?
  23. private var categoryDict: [String:String] = MyIdentityStore.shared().directoryCategories as! [String:String]
  24. private let categoryIdArray: [String] = MyIdentityStore.shared().directoryCategoryIdsSortedByName() as! [String]
  25. override func viewWillAppear(_ animated: Bool) {
  26. super.viewWillAppear(animated)
  27. refresh()
  28. }
  29. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  30. return categoryIdArray.count + 1
  31. }
  32. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  33. let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for: indexPath)
  34. if indexPath.row == 0 {
  35. cell.textLabel?.text = BundleUtil.localizedString(forKey: "all")
  36. cell.accessoryType = companyDirectoryViewController!.filterArray.count == 0 ? .checkmark : .none
  37. } else {
  38. let catId = categoryIdArray[indexPath.row - 1]
  39. cell.textLabel?.text = categoryDict[catId]
  40. cell.accessoryType = companyDirectoryViewController!.filterArray.contains(catId) ? .checkmark : .none
  41. }
  42. return cell
  43. }
  44. override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  45. Colors.update(cell)
  46. }
  47. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  48. if indexPath.row == 0 {
  49. companyDirectoryViewController!.filterArray.removeAll()
  50. } else {
  51. let catId = categoryIdArray[indexPath.row - 1]
  52. if companyDirectoryViewController!.filterArray.contains(catId) {
  53. let index = companyDirectoryViewController!.filterArray.firstIndex(of: catId)
  54. companyDirectoryViewController!.filterArray.remove(at: index!)
  55. } else {
  56. companyDirectoryViewController!.filterArray.append(catId)
  57. }
  58. }
  59. refresh()
  60. }
  61. }