MediaQualityController.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import UIKit
  2. import DcCore
  3. class MediaQualityController: UITableViewController {
  4. private var dcContext: DcContext
  5. private var options: [Int]
  6. private lazy var staticCells: [UITableViewCell] = {
  7. return options.map({
  8. let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  9. cell.textLabel?.text = MediaQualityController.getValString(val: $0)
  10. return cell
  11. })
  12. }()
  13. init(dcContext: DcContext) {
  14. self.dcContext = dcContext
  15. self.options = [Int(DC_MEDIA_QUALITY_BALANCED), Int(DC_MEDIA_QUALITY_WORSE)]
  16. super.init(style: .grouped)
  17. self.title = String.localized("pref_outgoing_media_quality")
  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 func getValString(val: Int) -> String {
  27. switch Int32(val) {
  28. case DC_MEDIA_QUALITY_BALANCED:
  29. return String.localized("pref_outgoing_balanced")
  30. case DC_MEDIA_QUALITY_WORSE:
  31. return String.localized("pref_outgoing_worse")
  32. default:
  33. return "Err"
  34. }
  35. }
  36. // MARK: - Table view data source
  37. override func numberOfSections(in tableView: UITableView) -> Int {
  38. return 1
  39. }
  40. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  41. return options.count
  42. }
  43. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  44. tableView.deselectRow(at: indexPath, animated: true) // animated as no other elements pop up
  45. let oldSelectedCell = tableView.cellForRow(at: IndexPath.init(row: dcContext.getConfigInt("media_quality"), section: 0))
  46. oldSelectedCell?.accessoryType = .none
  47. let newSelectedCell = tableView.cellForRow(at: IndexPath.init(row: indexPath.row, section: 0))
  48. newSelectedCell?.accessoryType = .checkmark
  49. dcContext.setConfigInt("media_quality", indexPath.row)
  50. }
  51. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  52. let cell = staticCells[indexPath.row]
  53. if options[indexPath.row] == dcContext.getConfigInt("media_quality") {
  54. cell.accessoryType = .checkmark
  55. }
  56. return cell
  57. }
  58. }