EditToolbar.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*****************************************************************************
  2. * EditToolbar.swift
  3. *
  4. * Copyright © 2018 VLC authors and VideoLAN
  5. * Copyright © 2018 Videolabs
  6. *
  7. * Authors: Soomin Lee <bubu@mikan.io>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. protocol EditToolbarDelegate: class {
  12. func editToolbarDidDelete(_ editToolbar: EditToolbar)
  13. func editToolbarDidAddToPlaylist(_ editToolbar: EditToolbar)
  14. func editToolbarDidRename(_ editToolbar: EditToolbar)
  15. func editToolbarDidShare(_ editToolbar: EditToolbar)
  16. }
  17. class EditToolbar: UIView {
  18. static let height: CGFloat = 60
  19. weak var delegate: EditToolbarDelegate?
  20. private var stackView: UIStackView = {
  21. let stackView = UIStackView()
  22. stackView.translatesAutoresizingMaskIntoConstraints = false
  23. stackView.distribution = .equalSpacing
  24. return stackView
  25. }()
  26. private var rightStackView: UIStackView = {
  27. let rightStackView = UIStackView()
  28. rightStackView.translatesAutoresizingMaskIntoConstraints = false
  29. return rightStackView
  30. }()
  31. private var addToPlaylistButton: UIButton = {
  32. let addToPlaylistButton = UIButton(type: .system)
  33. addToPlaylistButton.setTitle(NSLocalizedString("ADD_TO_PLAYLIST", comment: ""), for: .normal)
  34. addToPlaylistButton.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .medium)
  35. addToPlaylistButton.contentHorizontalAlignment = .left
  36. addToPlaylistButton.addTarget(self, action: #selector(addToPlaylist), for: .touchUpInside)
  37. addToPlaylistButton.tintColor = .orange
  38. addToPlaylistButton.accessibilityLabel = NSLocalizedString("ADD_TO_PLAYLIST", comment: "")
  39. addToPlaylistButton.accessibilityHint = NSLocalizedString("ADD_TO_PLAYLIST_HINT", comment: "")
  40. return addToPlaylistButton
  41. }()
  42. private var renameButton: UIButton!
  43. private var deleteButton: UIButton!
  44. private var shareButton: UIButton!
  45. @objc func addToPlaylist() {
  46. delegate?.editToolbarDidAddToPlaylist(self)
  47. }
  48. @objc func deleteSelection() {
  49. delegate?.editToolbarDidDelete(self)
  50. }
  51. @objc func rename() {
  52. delegate?.editToolbarDidRename(self)
  53. }
  54. @objc func share() {
  55. delegate?.editToolbarDidShare(self)
  56. }
  57. func updateEditToolbar(for model: MediaLibraryBaseModel) {
  58. var buttonList = EditButtonsFactory.buttonList(for: model.anyfiles.first)
  59. // For now we remove the first button which is Add to playlist since it is not in the same group
  60. if buttonList.contains(.addToPlaylist) {
  61. if let index = buttonList.firstIndex(of: .addToPlaylist) {
  62. buttonList.remove(at: index)
  63. }
  64. }
  65. // Hide all buttons and show depending on model
  66. renameButton.isHidden = true
  67. deleteButton.isHidden = true
  68. shareButton.isHidden = true
  69. for buttonType in buttonList {
  70. switch buttonType {
  71. case .addToPlaylist:
  72. addToPlaylistButton.isHidden = false
  73. case .rename:
  74. renameButton.isHidden = false
  75. case .delete:
  76. deleteButton.isHidden = false
  77. case .share:
  78. shareButton.isHidden = false
  79. }
  80. }
  81. }
  82. private func setupRightStackView() {
  83. let buttons = EditButtonsFactory.generate(buttons: [.rename, .delete, .share])
  84. for button in buttons {
  85. switch button.identifier {
  86. case .addToPlaylist:
  87. rightStackView.addArrangedSubview(button.button(#selector(addToPlaylist)))
  88. case .rename:
  89. renameButton = button.button(#selector(rename))
  90. rightStackView.addArrangedSubview(renameButton)
  91. case .delete:
  92. deleteButton = button.button(#selector(deleteSelection))
  93. rightStackView.addArrangedSubview(deleteButton)
  94. case .share:
  95. shareButton = button.button(#selector(share))
  96. rightStackView.addArrangedSubview(shareButton)
  97. }
  98. }
  99. }
  100. private func setupStackView() {
  101. stackView.addArrangedSubview(addToPlaylistButton)
  102. stackView.addArrangedSubview(rightStackView)
  103. addSubview(stackView)
  104. NSLayoutConstraint.activate([
  105. stackView.topAnchor.constraint(equalTo: topAnchor),
  106. stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20),
  107. stackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10),
  108. stackView.bottomAnchor.constraint(equalTo: bottomAnchor)
  109. ])
  110. }
  111. private func setupView() {
  112. backgroundColor = PresentationTheme.current.colors.background
  113. }
  114. init() {
  115. super.init(frame: .zero)
  116. setupView()
  117. setupRightStackView()
  118. setupStackView()
  119. }
  120. required init(coder: NSCoder) {
  121. fatalError("init(coder:) has not been implemented")
  122. }
  123. }