VLCEditToolbar.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*****************************************************************************
  2. * VLCEditToolbar.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 VLCEditToolbarDelegate: class {
  12. func editToolbarDidDelete(_ editToolbar: VLCEditToolbar)
  13. func editToolbarDidAddToPlaylist(_ editToolbar: VLCEditToolbar)
  14. func editToolbarDidRename(_ editToolbar: VLCEditToolbar)
  15. func editToolbarDidShare(_ editToolbar: VLCEditToolbar, presentFrom button: UIButton)
  16. }
  17. class VLCEditToolbar: UIView {
  18. static let height: CGFloat = 60
  19. weak var delegate: VLCEditToolbarDelegate?
  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. return shareButton
  29. }()
  30. private var renameButton: UIButton = {
  31. let renameButton = UIButton(type: .system)
  32. renameButton.addTarget(self, action: #selector(rename), for: .touchUpInside)
  33. renameButton.setImage(UIImage(named: "rename"), for: .normal)
  34. renameButton.tintColor = .orange
  35. renameButton.widthAnchor.constraint(equalToConstant: 44).isActive = true
  36. return renameButton
  37. }()
  38. private var deleteButton: UIButton = {
  39. let deleteButton = UIButton(type: .system)
  40. deleteButton.addTarget(self, action: #selector(deleteSelection), for: .touchUpInside)
  41. deleteButton.setImage(UIImage(named: "delete"), for: .normal)
  42. deleteButton.tintColor = .orange
  43. deleteButton.widthAnchor.constraint(equalToConstant: 44).isActive = true
  44. return deleteButton
  45. }()
  46. private var addToPlaylistButton: UIButton = {
  47. let addToPlaylistButton = UIButton(type: .system)
  48. addToPlaylistButton.setTitle(NSLocalizedString("ADD_TO_PLAYLIST", comment: ""), for: .normal)
  49. addToPlaylistButton.titleLabel?.font = UIFont.systemFont(ofSize: 17)
  50. addToPlaylistButton.contentHorizontalAlignment = .left
  51. addToPlaylistButton.addTarget(self, action: #selector(addToPlaylist), for: .touchUpInside)
  52. addToPlaylistButton.tintColor = .orange
  53. return addToPlaylistButton
  54. }()
  55. @objc func addToPlaylist() {
  56. delegate?.editToolbarDidAddToPlaylist(self)
  57. }
  58. @objc func deleteSelection() {
  59. delegate?.editToolbarDidDelete(self)
  60. }
  61. @objc func rename() {
  62. delegate?.editToolbarDidRename(self)
  63. }
  64. @objc func share() {
  65. delegate?.editToolbarDidShare(self, presentFrom: shareButton)
  66. }
  67. private func setupStackView() {
  68. let stackView = UIStackView(arrangedSubviews: [addToPlaylistButton])
  69. let file = category.anyfiles.first
  70. if !(file is VLCMLArtist) && !(file is VLCMLGenre) && !(file is VLCMLAlbum) {
  71. stackView.addArrangedSubview(deleteButton)
  72. }
  73. stackView.addArrangedSubview(shareButton)
  74. if !(file is VLCMLAlbum) && !(file is VLCMLArtist) && !(file is VLCMLGenre) {
  75. stackView.addArrangedSubview(renameButton)
  76. }
  77. stackView.translatesAutoresizingMaskIntoConstraints = false
  78. addSubview(stackView)
  79. NSLayoutConstraint.activate([
  80. stackView.topAnchor.constraint(equalTo: topAnchor),
  81. stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20),
  82. stackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20),
  83. stackView.bottomAnchor.constraint(equalTo: bottomAnchor)
  84. ])
  85. }
  86. private func setupView() {
  87. layer.shadowOpacity = 0.1
  88. layer.shadowOffset = CGSize(width: 0, height: -1)
  89. backgroundColor = PresentationTheme.current.colors.background
  90. }
  91. init(category: MediaLibraryBaseModel) {
  92. self.category = category
  93. super.init(frame: .zero)
  94. setupView()
  95. setupStackView()
  96. }
  97. required init(coder: NSCoder) {
  98. fatalError("init(coder:) has not been implemented")
  99. }
  100. }