SettingsClassicViewController.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import UIKit
  2. import DcCore
  3. class SettingsClassicViewController: UITableViewController {
  4. var dcContext: DcContext
  5. var options: [Int]
  6. var staticCells: [UITableViewCell] {
  7. return options.map({
  8. let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  9. cell.textLabel?.text = SettingsClassicViewController.getValString(val: $0)
  10. return cell
  11. })
  12. }
  13. init(dcContext: DcContext) {
  14. self.dcContext = dcContext
  15. self.options = [Int(DC_SHOW_EMAILS_OFF), Int(DC_SHOW_EMAILS_ACCEPTED_CONTACTS), Int(DC_SHOW_EMAILS_ALL)]
  16. super.init(style: .grouped)
  17. self.title = String.localized("pref_show_emails")
  18. hidesBottomBarWhenPushed = true
  19. }
  20. required init?(coder aDecoder: NSCoder) {
  21. fatalError("init(coder:) has not been implemented")
  22. }
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. }
  26. static public func getValString(val: Int) -> String {
  27. switch Int32(val) {
  28. case DC_SHOW_EMAILS_OFF:
  29. return String.localized("pref_show_emails_no")
  30. case DC_SHOW_EMAILS_ACCEPTED_CONTACTS:
  31. return String.localized("pref_show_emails_accepted_contacts")
  32. case DC_SHOW_EMAILS_ALL:
  33. return String.localized("pref_show_emails_all")
  34. default:
  35. return "Err"
  36. }
  37. }
  38. // MARK: - Table view data source
  39. override func numberOfSections(in tableView: UITableView) -> Int {
  40. return 1
  41. }
  42. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  43. return options.count
  44. }
  45. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  46. tableView.deselectRow(at: indexPath, animated: true) // animated as no other elements pop up
  47. let oldSelectedCell = tableView.cellForRow(at: IndexPath.init(row: dcContext.showEmails, section: 0))
  48. oldSelectedCell?.accessoryType = .none
  49. let newSelectedCell = tableView.cellForRow(at: IndexPath.init(row: indexPath.row, section: 0))
  50. newSelectedCell?.accessoryType = .checkmark
  51. dcContext.showEmails = indexPath.row
  52. }
  53. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  54. let cell = staticCells[indexPath.row]
  55. if options[indexPath.row] == dcContext.showEmails {
  56. cell.accessoryType = .checkmark
  57. }
  58. return cell
  59. }
  60. }