DisplayOrderTableViewController.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2017-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 UIKit
  21. class DisplayOrderTableViewController: ThemedTableViewController {
  22. var sortings:[String] = []
  23. var selectedIndexPath:IndexPath? = nil
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. sortings = ["Firstname", "Lastname"]
  27. }
  28. override func viewWillAppear(_ animated: Bool) {
  29. super.viewWillAppear(animated)
  30. tableView.reloadData()
  31. }
  32. // MARK: - Table view data source
  33. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  34. return sortings.count
  35. }
  36. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  37. let cell = tableView.dequeueReusableCell(withIdentifier: "DisplayOrderCell", for: indexPath)
  38. let orderKey = "SortOrder_\(sortings[indexPath.row])"
  39. cell.textLabel?.text = NSLocalizedString(orderKey, comment: "")
  40. if (UserSettings.shared().displayOrderFirstName && indexPath.row == 0) || (!UserSettings.shared().displayOrderFirstName && indexPath.row == 1) {
  41. selectedIndexPath = indexPath
  42. cell.accessoryType = .checkmark
  43. } else {
  44. cell.accessoryType = .none
  45. }
  46. return cell
  47. }
  48. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  49. UserSettings.shared().displayOrderFirstName = indexPath.row == 0 ? true : false
  50. if selectedIndexPath != nil {
  51. tableView.cellForRow(at: selectedIndexPath!)?.accessoryType = .none
  52. }
  53. tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
  54. tableView.deselectRow(at: indexPath, animated: true)
  55. selectedIndexPath = indexPath
  56. }
  57. }