EditToolbar.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. stackView.addArrangedSubview(deleteButton)
  80. }
  81. stackView.addArrangedSubview(shareButton)
  82. if !(file is VLCMLAlbum) && !(file is VLCMLArtist) && !(file is VLCMLGenre) {
  83. stackView.addArrangedSubview(renameButton)
  84. }
  85. stackView.translatesAutoresizingMaskIntoConstraints = false
  86. addSubview(stackView)
  87. NSLayoutConstraint.activate([
  88. stackView.topAnchor.constraint(equalTo: topAnchor),
  89. stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20),
  90. stackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20),
  91. stackView.bottomAnchor.constraint(equalTo: bottomAnchor)
  92. ])
  93. }
  94. private func setupView() {
  95. layer.shadowOpacity = 0.1
  96. layer.shadowOffset = CGSize(width: 0, height: -1)
  97. backgroundColor = PresentationTheme.current.colors.background
  98. }
  99. init(category: MediaLibraryBaseModel) {
  100. self.category = category
  101. super.init(frame: .zero)
  102. setupView()
  103. setupStackView()
  104. }
  105. required init(coder: NSCoder) {
  106. fatalError("init(coder:) has not been implemented")
  107. }
  108. }