EditToolbar.swift 5.1 KB

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