ActionSheetSortSectionHeader.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*****************************************************************************
  2. * ActionSheetSortSectionHeader.swift
  3. *
  4. * Copyright © 2019 VLC authors and VideoLAN
  5. *
  6. * Authors: Soomin Lee <bubu@mikan.io>
  7. *
  8. * Refer to the COPYING file of the official project for license.
  9. *****************************************************************************/
  10. protocol ActionSheetSortSectionHeaderDelegate: class {
  11. func actionSheetSortSectionHeader(_ header: ActionSheetSortSectionHeader,
  12. onSwitchIsOnChange: Bool)
  13. }
  14. class ActionSheetSortSectionHeader: ActionSheetSectionHeader {
  15. override var cellHeight: CGFloat {
  16. return 100
  17. }
  18. let descendingStackView: UIStackView = {
  19. let descendingStackView = UIStackView()
  20. descendingStackView.spacing = 0
  21. descendingStackView.alignment = .center
  22. descendingStackView.translatesAutoresizingMaskIntoConstraints = false
  23. return descendingStackView
  24. }()
  25. let descendingLabel: UILabel = {
  26. let descendingLabel = UILabel()
  27. descendingLabel.textColor = PresentationTheme.current.colors.cellTextColor
  28. descendingLabel.text = NSLocalizedString("DESCENDING_LABEL", comment: "")
  29. descendingLabel.font = UIFont.systemFont(ofSize: 15, weight: .medium)
  30. descendingLabel.translatesAutoresizingMaskIntoConstraints = false
  31. return descendingLabel
  32. }()
  33. let actionSwitch: UISwitch = {
  34. let actionSwitch = UISwitch()
  35. actionSwitch.addTarget(self, action: #selector(handleSwitch(_:)), for: .valueChanged)
  36. actionSwitch.accessibilityLabel = NSLocalizedString("DESCENDING_SWITCH_LABEL", comment: "")
  37. actionSwitch.accessibilityHint = NSLocalizedString("DESCENDING_SWITCH_HINT", comment: "")
  38. actionSwitch.translatesAutoresizingMaskIntoConstraints = false
  39. return actionSwitch
  40. }()
  41. weak var delegate: ActionSheetSortSectionHeaderDelegate?
  42. override init(frame: CGRect) {
  43. super.init(frame: frame)
  44. translatesAutoresizingMaskIntoConstraints = false
  45. setupStackView()
  46. updateTheme()
  47. NotificationCenter.default.addObserver(self, selector: #selector(updateTheme),
  48. name: .VLCThemeDidChangeNotification, object: nil)
  49. }
  50. required init?(coder aDecoder: NSCoder) {
  51. fatalError("init(coder:) has not been implemented")
  52. }
  53. @objc private func updateTheme() {
  54. backgroundColor = PresentationTheme.current.colors.background
  55. descendingLabel.textColor = PresentationTheme.current.colors.cellTextColor
  56. }
  57. @objc func handleSwitch(_ sender: UISwitch) {
  58. delegate?.actionSheetSortSectionHeader(self, onSwitchIsOnChange: sender.isOn)
  59. }
  60. private func setupStackView() {
  61. descendingStackView.addArrangedSubview(descendingLabel)
  62. descendingStackView.addArrangedSubview(actionSwitch)
  63. addSubview(descendingStackView)
  64. NSLayoutConstraint.activate([
  65. descendingStackView.leadingAnchor.constraint(equalTo: guide.leadingAnchor, constant: 20),
  66. descendingStackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20),
  67. descendingStackView.topAnchor.constraint(equalTo: title.bottomAnchor, constant: 15),
  68. ])
  69. }
  70. }