VLCSettingsTableViewCell.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. textLabel?.textColor = PresentationTheme.current.colors.cellTextColor
  17. detailTextLabel?.textColor = PresentationTheme.current.colors.cellDetailTextColor
  18. }
  19. @objc init(reuseIdentifier: String, target: IASKAppSettingsViewController) {
  20. super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
  21. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  22. themeDidChange()
  23. switch reuseIdentifier {
  24. case kIASKPSToggleSwitchSpecifier:
  25. let toggle = IASKSwitch(frame: .zero)
  26. toggle.addTarget(target, action: #selector(target.toggledValue(_:)), for: .valueChanged)
  27. accessoryView = toggle
  28. selectionStyle = .none
  29. case kIASKOpenURLSpecifier, kIASKPSMultiValueSpecifier:
  30. accessoryType = .disclosureIndicator
  31. selectionStyle = .default
  32. default:
  33. assertionFailure("\(reuseIdentifier) has not been defined for VLCSettingsTableViewCell")
  34. }
  35. }
  36. @available(*, unavailable, message: "use init(reuseIdentifier: String)")
  37. required init?(coder aDecoder: NSCoder) {
  38. fatalError("init(coder:) has not been implemented")
  39. }
  40. @objc func configure(specifier: IASKSpecifier, settingsValue: Any?) {
  41. textLabel?.text = specifier.title()
  42. textLabel?.numberOfLines = 0
  43. detailTextLabel?.text = specifier.subtitle()
  44. switch specifier.type() {
  45. case kIASKPSToggleSwitchSpecifier:
  46. configureToggle(specifier, settingsValue)
  47. case kIASKPSMultiValueSpecifier:
  48. configureMultiValue(specifier, settingsValue)
  49. case kIASKOpenURLSpecifier:
  50. break
  51. default:
  52. assertionFailure("\(specifier.type()) has not been defined for VLCSettingsTableViewCell")
  53. }
  54. }
  55. fileprivate func configureToggle(_ specifier: IASKSpecifier, _ settingsValue: Any?) {
  56. assert(specifier.type() == kIASKPSToggleSwitchSpecifier, "configureToggle should only be called for kIASKPSToggleSwitchSpecifier")
  57. assert(settingsValue is Bool?, "settingsValue should be Bool?")
  58. assert(accessoryView is IASKSwitch, "the accessory should be a switch")
  59. var state = specifier.defaultBoolValue()
  60. if let currentValue = settingsValue as? Bool {
  61. switch currentValue {
  62. case specifier.trueValue() as? Bool:
  63. state = true
  64. case specifier.falseValue() as? Bool:
  65. state = false
  66. default:
  67. state = currentValue
  68. }
  69. }
  70. if let toggle = accessoryView as? IASKSwitch {
  71. toggle.isOn = state
  72. toggle.key = specifier.key()
  73. }
  74. }
  75. fileprivate func configureMultiValue(_ specifier: IASKSpecifier, _ value: Any?) {
  76. assert(specifier.type() == kIASKPSMultiValueSpecifier, "configureMultiValue should only be called for kIASKPSMultiValueSpecifier")
  77. detailTextLabel?.text = specifier.title(forCurrentValue: value ?? specifier.defaultValue())
  78. }
  79. }