SettingsClassicViewController.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import UIKit
  2. class SettingsClassicViewController: UITableViewController {
  3. var dcContext: DcContext
  4. var options: [Int]
  5. var staticCells: [UITableViewCell] {
  6. return options.map({
  7. let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  8. cell.textLabel?.text = SettingsClassicViewController.getValString(val: $0)
  9. cell.selectionStyle = .none
  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. let oldSelectedCell = tableView.cellForRow(at: IndexPath.init(row: DcConfig.showEmails, section: 0))
  47. oldSelectedCell?.accessoryType = .none
  48. let newSelectedCell = tableView.cellForRow(at: IndexPath.init(row: indexPath.row, section: 0))
  49. newSelectedCell?.accessoryType = .checkmark
  50. DcConfig.showEmails = indexPath.row
  51. }
  52. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  53. let cell = staticCells[indexPath.row]
  54. if options[indexPath.row] == DcConfig.showEmails {
  55. cell.accessoryType = .checkmark
  56. }
  57. return cell
  58. }
  59. }