EditToolbar.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 model: MediaLibraryBaseModel
  21. private var stackView: UIStackView = {
  22. let stackView = UIStackView()
  23. stackView.translatesAutoresizingMaskIntoConstraints = false
  24. stackView.distribution = .equalSpacing
  25. return stackView
  26. }()
  27. private var rightStackView: UIStackView = {
  28. let rightStackView = UIStackView()
  29. rightStackView.translatesAutoresizingMaskIntoConstraints = false
  30. return rightStackView
  31. }()
  32. private var addToPlaylistButton: UIButton = {
  33. let addToPlaylistButton = UIButton(type: .system)
  34. addToPlaylistButton.setTitle(NSLocalizedString("ADD_TO_PLAYLIST", comment: ""), for: .normal)
  35. addToPlaylistButton.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .medium)
  36. addToPlaylistButton.contentHorizontalAlignment = .left
  37. addToPlaylistButton.addTarget(self, action: #selector(addToPlaylist), for: .touchUpInside)
  38. addToPlaylistButton.tintColor = .orange
  39. addToPlaylistButton.accessibilityLabel = NSLocalizedString("ADD_TO_PLAYLIST", comment: "")
  40. addToPlaylistButton.accessibilityHint = NSLocalizedString("ADD_TO_PLAYLIST_HINT", comment: "")
  41. return addToPlaylistButton
  42. }()
  43. @objc func addToPlaylist() {
  44. delegate?.editToolbarDidAddToPlaylist(self)
  45. }
  46. @objc func deleteSelection() {
  47. delegate?.editToolbarDidDelete(self)
  48. }
  49. @objc func rename() {
  50. delegate?.editToolbarDidRename(self)
  51. }
  52. @objc func share() {
  53. delegate?.editToolbarDidShare(self)
  54. }
  55. private func setupRightStackView() {
  56. var buttonList = EditButtonsFactory.buttonList(for: model.anyfiles.first)
  57. // For now we remove the first button which is Add to playlist since it is not in the same group
  58. if buttonList.contains(.addToPlaylist) {
  59. if let index = buttonList.firstIndex(of: .addToPlaylist) {
  60. buttonList.remove(at: index)
  61. }
  62. }
  63. let buttons = EditButtonsFactory.generate(buttons: buttonList)
  64. for button in buttons {
  65. switch button.identifier {
  66. case .addToPlaylist:
  67. rightStackView.addArrangedSubview(button.button(#selector(addToPlaylist)))
  68. case .rename:
  69. rightStackView.addArrangedSubview(button.button(#selector(rename)))
  70. case .delete:
  71. rightStackView.addArrangedSubview(button.button(#selector(deleteSelection)))
  72. case .share:
  73. rightStackView.addArrangedSubview(button.button(#selector(share)))
  74. }
  75. }
  76. }
  77. private func setupStackView() {
  78. stackView.addArrangedSubview(addToPlaylistButton)
  79. stackView.addArrangedSubview(rightStackView)
  80. addSubview(stackView)
  81. NSLayoutConstraint.activate([
  82. stackView.topAnchor.constraint(equalTo: topAnchor),
  83. stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20),
  84. stackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10),
  85. stackView.bottomAnchor.constraint(equalTo: bottomAnchor)
  86. ])
  87. }
  88. private func setupView() {
  89. layer.shadowOpacity = 0.1
  90. layer.shadowOffset = CGSize(width: 0, height: -1)
  91. backgroundColor = PresentationTheme.current.colors.background
  92. }
  93. init(model: MediaLibraryBaseModel) {
  94. self.model = model
  95. super.init(frame: .zero)
  96. setupView()
  97. setupRightStackView()
  98. setupStackView()
  99. }
  100. required init(coder: NSCoder) {
  101. fatalError("init(coder:) has not been implemented")
  102. }
  103. }