VLCSettingsTableViewCell.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*****************************************************************************
  2. * VLCSettingsTableViewCell.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Mike JS. Choi <mkchoi212 # icloud.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. import UIKit
  13. class VLCSettingsTableViewCell: UITableViewCell {
  14. @objc fileprivate func themeDidChange() {
  15. backgroundColor = PresentationTheme.current.colors.background
  16. selectedBackgroundView?.backgroundColor = PresentationTheme.current.colors.mediaCategorySeparatorColor
  17. textLabel?.textColor = PresentationTheme.current.colors.cellTextColor
  18. detailTextLabel?.textColor = PresentationTheme.current.colors.cellDetailTextColor
  19. }
  20. @objc init(reuseIdentifier: String, target: IASKAppSettingsViewController) {
  21. super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
  22. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  23. themeDidChange()
  24. switch reuseIdentifier {
  25. case kIASKPSToggleSwitchSpecifier:
  26. let toggle = IASKSwitch(frame: .zero)
  27. toggle.addTarget(target, action: #selector(target.toggledValue(_:)), for: .valueChanged)
  28. accessoryView = toggle
  29. case kIASKOpenURLSpecifier:
  30. accessoryType = .disclosureIndicator
  31. case kIASKPSMultiValueSpecifier:
  32. accessoryType = .none
  33. case kIASKButtonSpecifier:
  34. accessoryType = .none
  35. default:
  36. assertionFailure("\(reuseIdentifier) has not been defined for VLCSettingsTableViewCell")
  37. }
  38. }
  39. @available(*, unavailable, message: "use init(reuseIdentifier: String)")
  40. required init?(coder aDecoder: NSCoder) {
  41. fatalError("init(coder:) has not been implemented")
  42. }
  43. @objc func configure(specifier: IASKSpecifier, settingsValue: Any?) {
  44. textLabel?.text = specifier.title()
  45. textLabel?.numberOfLines = 0
  46. detailTextLabel?.text = specifier.subtitle()
  47. switch specifier.type() {
  48. case kIASKPSToggleSwitchSpecifier:
  49. configureToggle(specifier, settingsValue)
  50. case kIASKPSMultiValueSpecifier:
  51. configureMultiValue(specifier, settingsValue)
  52. case kIASKOpenURLSpecifier:
  53. break
  54. case kIASKButtonSpecifier:
  55. break
  56. default:
  57. assertionFailure("\(specifier.type() ?? "nil") has not been defined for VLCSettingsTableViewCell")
  58. }
  59. }
  60. fileprivate func configureToggle(_ specifier: IASKSpecifier, _ settingsValue: Any?) {
  61. assert(specifier.type() == kIASKPSToggleSwitchSpecifier, "configureToggle should only be called for kIASKPSToggleSwitchSpecifier")
  62. assert(settingsValue is Bool?, "settingsValue should be Bool?")
  63. assert(accessoryView is IASKSwitch, "the accessory should be a switch")
  64. var state = specifier.defaultBoolValue()
  65. if let currentValue = settingsValue as? Bool {
  66. switch currentValue {
  67. case specifier.trueValue() as? Bool:
  68. state = true
  69. case specifier.falseValue() as? Bool:
  70. state = false
  71. default:
  72. state = currentValue
  73. }
  74. }
  75. if let toggle = accessoryView as? IASKSwitch {
  76. toggle.isOn = state
  77. toggle.key = specifier.key()
  78. }
  79. selectionStyle = .none
  80. }
  81. fileprivate func configureMultiValue(_ specifier: IASKSpecifier, _ value: Any?) {
  82. assert(specifier.type() == kIASKPSMultiValueSpecifier, "configureMultiValue should only be called for kIASKPSMultiValueSpecifier")
  83. detailTextLabel?.text = specifier.title(forCurrentValue: value ?? specifier.defaultValue())
  84. }
  85. }