VLCSettingsTableViewCell.swift 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. override func prepareForReuse() {
  15. super.prepareForReuse()
  16. setupCell()
  17. }
  18. fileprivate func setupCell() {
  19. backgroundColor = PresentationTheme.current.colors.background
  20. textLabel?.textColor = PresentationTheme.current.colors.cellTextColor
  21. detailTextLabel?.textColor = PresentationTheme.current.colors.cellDetailTextColor
  22. }
  23. @objc static func cell(identifier: String, target: IASKAppSettingsViewController) -> VLCSettingsTableViewCell? {
  24. let cell = VLCSettingsTableViewCell(style: .subtitle, reuseIdentifier: identifier)
  25. cell.setupCell()
  26. switch identifier {
  27. case kIASKPSToggleSwitchSpecifier:
  28. let toggle = IASKSwitch(frame: .zero)
  29. toggle.addTarget(target, action: #selector(target.toggledValue(_:)), for: .valueChanged)
  30. cell.accessoryView = toggle
  31. cell.selectionStyle = .none
  32. case kIASKOpenURLSpecifier, kIASKPSMultiValueSpecifier:
  33. cell.accessoryType = .disclosureIndicator
  34. cell.selectionStyle = .default
  35. default:
  36. assertionFailure("\(identifier) has not been defined for VLCSettingsTableViewCell")
  37. }
  38. return cell
  39. }
  40. @objc func configure(specifier: IASKSpecifier, value: Any?) {
  41. textLabel?.text = specifier.title()
  42. switch specifier.type() {
  43. case kIASKPSToggleSwitchSpecifier:
  44. configureToggle(specifier, value)
  45. case kIASKPSMultiValueSpecifier:
  46. configureMultiValue(specifier, value)
  47. case kIASKOpenURLSpecifier:
  48. configureOpenURL(specifier)
  49. default:
  50. assertionFailure("\(specifier.type()) has not been defined for VLCSettingsTableViewCell")
  51. }
  52. }
  53. fileprivate func configureToggle(_ specifier: IASKSpecifier, _ value: Any?) {
  54. detailTextLabel?.text = specifier.subtitle()
  55. var state: Bool
  56. if let currentValue = value as? Bool {
  57. switch currentValue {
  58. case specifier.trueValue() as? Bool:
  59. state = true
  60. case specifier.falseValue() as? Bool:
  61. state = false
  62. default:
  63. state = currentValue
  64. }
  65. } else {
  66. state = specifier.defaultBoolValue()
  67. }
  68. if let toggle = accessoryView as? IASKSwitch {
  69. toggle.isOn = state
  70. toggle.key = specifier.key()
  71. }
  72. }
  73. fileprivate func configureMultiValue(_ specifier: IASKSpecifier, _ value: Any?) {
  74. if let currentValue = value {
  75. detailTextLabel?.text = specifier.title(forCurrentValue: currentValue)
  76. } else {
  77. detailTextLabel?.text = specifier.title(forCurrentValue: specifier.defaultValue())
  78. }
  79. }
  80. fileprivate func configureOpenURL(_ specifier: IASKSpecifier) {
  81. detailTextLabel?.text = specifier.subtitle()
  82. }
  83. }