EditButtons.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*****************************************************************************
  2. * EditButtons.swift
  3. *
  4. * Copyright © 2019 VLC authors and VideoLAN
  5. *
  6. * Authors: Edgar Fouillet <vlc # edgar.fouillet.eu>
  7. *
  8. * Refer to the COPYING file of the official project for license.
  9. *****************************************************************************/
  10. enum EditButtonType {
  11. case addToPlaylist
  12. case rename
  13. case delete
  14. case share
  15. }
  16. class EditButton {
  17. var identifier: EditButtonType
  18. var title: String
  19. var image: String
  20. var accessibilityLabel: String
  21. var accessibilityHint: String
  22. init(identifier: EditButtonType, title: String, image: String, accessibilityLabel: String, accessibilityHint: String) {
  23. self.identifier = identifier
  24. self.title = title
  25. self.image = image
  26. self.accessibilityLabel = accessibilityLabel
  27. self.accessibilityHint = accessibilityHint
  28. }
  29. func button(_ selector: Selector) -> UIButton {
  30. let generatedButton = UIButton(type: .system)
  31. generatedButton.setImage(UIImage(named: image), for: .normal)
  32. generatedButton.addTarget(self, action: selector, for: .touchUpInside)
  33. generatedButton.widthAnchor.constraint(equalToConstant: 44).isActive = true
  34. generatedButton.tintColor = .orange
  35. generatedButton.accessibilityLabel = accessibilityLabel
  36. generatedButton.accessibilityHint = accessibilityHint
  37. return generatedButton
  38. }
  39. @available(iOS 13.0, *)
  40. func action(_ handler: @escaping (UIAction) -> Void) -> UIAction {
  41. let generatedAction = UIAction(title: title,
  42. image: UIImage(named: image),
  43. identifier: UIAction.Identifier(rawValue: image),
  44. handler: handler)
  45. generatedAction.accessibilityLabel = accessibilityLabel
  46. generatedAction.accessibilityHint = accessibilityHint
  47. return generatedAction
  48. }
  49. }
  50. class EditButtonsFactory {
  51. static func buttonList(for file: VLCMLObject?) -> [EditButtonType] {
  52. var actionList = [EditButtonType]()
  53. if let file = file {
  54. actionList.append(.addToPlaylist)
  55. if !(file is VLCMLArtist) && !(file is VLCMLGenre) && !(file is VLCMLAlbum) && !(file is VLCMLVideoGroup) {
  56. actionList.append(.rename)
  57. }
  58. if !(file is VLCMLVideoGroup) {
  59. actionList.append(.delete)
  60. }
  61. actionList.append(.share)
  62. }
  63. return actionList
  64. }
  65. static func generate(buttons: [EditButtonType]) -> [EditButton] {
  66. var editButtons = [EditButton]()
  67. for button in buttons {
  68. switch button {
  69. case .addToPlaylist:
  70. editButtons.append(EditButton(identifier: button,
  71. title: NSLocalizedString("ADD_TO_PLAYLIST", comment: ""),
  72. image: "addToPlaylist",
  73. accessibilityLabel: NSLocalizedString("ADD_TO_PLAYLIST", comment: ""),
  74. accessibilityHint: NSLocalizedString("ADD_TO_PLAYLIST_HINT", comment: "")))
  75. case .rename:
  76. editButtons.append(EditButton(identifier: button,
  77. title: NSLocalizedString("BUTTON_RENAME", comment: ""),
  78. image: "rename",
  79. accessibilityLabel: NSLocalizedString("BUTTON_RENAME", comment: ""),
  80. accessibilityHint: NSLocalizedString("RENAME_HINT", comment: "")))
  81. case .delete:
  82. editButtons.append(EditButton(identifier: button,
  83. title: NSLocalizedString("BUTTON_DELETE", comment: ""),
  84. image: "delete",
  85. accessibilityLabel: NSLocalizedString("BUTTON_DELETE", comment: ""),
  86. accessibilityHint: NSLocalizedString("DELETE_HINT", comment: "")))
  87. case .share:
  88. editButtons.append(EditButton(identifier: button,
  89. title: NSLocalizedString("SHARE_LABEL", comment: ""),
  90. image: "share",
  91. accessibilityLabel: NSLocalizedString("SHARE_LABEL", comment: ""),
  92. accessibilityHint: NSLocalizedString("SHARE_HINT", comment: "")))
  93. }
  94. }
  95. return editButtons
  96. }
  97. }