EditToolbar.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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, presentFrom button: UIButton)
  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 lazy var shareButton: UIButton = {
  33. let shareButton = UIButton(type: .system)
  34. shareButton.addTarget(self, action: #selector(share), for: .touchUpInside)
  35. shareButton.setImage(UIImage(named: "share"), for: .normal)
  36. shareButton.tintColor = .orange
  37. shareButton.widthAnchor.constraint(equalToConstant: 44).isActive = true
  38. shareButton.accessibilityLabel = NSLocalizedString("SHARE_LABEL", comment: "")
  39. shareButton.accessibilityHint = NSLocalizedString("SHARE_HINT", comment: "")
  40. return shareButton
  41. }()
  42. private lazy var renameButton: UIButton = {
  43. let renameButton = UIButton(type: .system)
  44. renameButton.addTarget(self, action: #selector(rename), for: .touchUpInside)
  45. renameButton.setImage(UIImage(named: "rename"), for: .normal)
  46. renameButton.tintColor = .orange
  47. renameButton.widthAnchor.constraint(equalToConstant: 44).isActive = true
  48. renameButton.accessibilityLabel = NSLocalizedString("BUTTON_RENAME", comment: "")
  49. renameButton.accessibilityHint = NSLocalizedString("RENAME_HINT", comment: "")
  50. return renameButton
  51. }()
  52. private lazy var deleteButton: UIButton = {
  53. let deleteButton = UIButton(type: .system)
  54. deleteButton.addTarget(self, action: #selector(deleteSelection), for: .touchUpInside)
  55. deleteButton.setImage(UIImage(named: "delete"), for: .normal)
  56. deleteButton.tintColor = .orange
  57. deleteButton.widthAnchor.constraint(equalToConstant: 44).isActive = true
  58. deleteButton.accessibilityLabel = NSLocalizedString("BUTTON_DELETE", comment: "")
  59. deleteButton.accessibilityHint = NSLocalizedString("DELETE_HINT", comment: "")
  60. return deleteButton
  61. }()
  62. private var addToPlaylistButton: UIButton = {
  63. let addToPlaylistButton = UIButton(type: .system)
  64. addToPlaylistButton.setTitle(NSLocalizedString("ADD_TO_PLAYLIST", comment: ""), for: .normal)
  65. addToPlaylistButton.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .medium)
  66. addToPlaylistButton.contentHorizontalAlignment = .left
  67. addToPlaylistButton.addTarget(self, action: #selector(addToPlaylist), for: .touchUpInside)
  68. addToPlaylistButton.tintColor = .orange
  69. addToPlaylistButton.accessibilityLabel = NSLocalizedString("ADD_TO_PLAYLIST", comment: "")
  70. addToPlaylistButton.accessibilityHint = NSLocalizedString("ADD_TO_PLAYLIST_HINT", comment: "")
  71. return addToPlaylistButton
  72. }()
  73. @objc func addToPlaylist() {
  74. delegate?.editToolbarDidAddToPlaylist(self)
  75. }
  76. @objc func deleteSelection() {
  77. delegate?.editToolbarDidDelete(self)
  78. }
  79. @objc func rename() {
  80. delegate?.editToolbarDidRename(self)
  81. }
  82. @objc func share() {
  83. delegate?.editToolbarDidShare(self, presentFrom: shareButton)
  84. }
  85. private func setupRightStackView() {
  86. let file = model.anyfiles.first
  87. if !(file is VLCMLArtist) && !(file is VLCMLGenre) && !(file is VLCMLAlbum)
  88. && !(file is VLCMLVideoGroup) {
  89. rightStackView.addArrangedSubview(renameButton)
  90. }
  91. if !(file is VLCMLVideoGroup) {
  92. rightStackView.addArrangedSubview(deleteButton)
  93. }
  94. rightStackView.addArrangedSubview(shareButton)
  95. }
  96. private func setupStackView() {
  97. stackView.addArrangedSubview(addToPlaylistButton)
  98. stackView.addArrangedSubview(rightStackView)
  99. addSubview(stackView)
  100. NSLayoutConstraint.activate([
  101. stackView.topAnchor.constraint(equalTo: topAnchor),
  102. stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20),
  103. stackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10),
  104. stackView.bottomAnchor.constraint(equalTo: bottomAnchor)
  105. ])
  106. }
  107. private func setupView() {
  108. layer.shadowOpacity = 0.1
  109. layer.shadowOffset = CGSize(width: 0, height: -1)
  110. backgroundColor = PresentationTheme.current.colors.background
  111. }
  112. init(model: MediaLibraryBaseModel) {
  113. self.model = model
  114. super.init(frame: .zero)
  115. setupView()
  116. setupRightStackView()
  117. setupStackView()
  118. }
  119. required init(coder: NSCoder) {
  120. fatalError("init(coder:) has not been implemented")
  121. }
  122. }