VLCEditController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*****************************************************************************
  2. * VLCEditController.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. class VLCEditController: NSObject {
  12. private var selectedCellIndexPaths = Set<IndexPath>()
  13. private let collectionView: UICollectionView
  14. private let model: MediaLibraryBaseModel
  15. init(collectionView: UICollectionView, model: MediaLibraryBaseModel) {
  16. self.collectionView = collectionView
  17. self.model = model
  18. super.init()
  19. }
  20. func resetSelections() {
  21. selectedCellIndexPaths.removeAll(keepingCapacity: false)
  22. }
  23. }
  24. // MARK: - Helpers
  25. private extension VLCEditController {
  26. private func resetCell(at indexPath: IndexPath) {
  27. if let cell = collectionView.cellForItem(at: indexPath) as? MediaEditCell {
  28. cell.isChecked = false
  29. }
  30. }
  31. private func resetAllVisibleCell() {
  32. for case let cell as MediaEditCell in collectionView.visibleCells {
  33. cell.isChecked = false
  34. }
  35. }
  36. private struct TextFieldAlertInfo {
  37. var alertTitle: String
  38. var alertDescription: String
  39. var placeHolder: String
  40. var confirmActionTitle: String
  41. init(alertTitle: String = "",
  42. alertDescription: String = "",
  43. placeHolder: String = "",
  44. confirmActionTitle: String = NSLocalizedString("BUTTON_DONE", comment: "")) {
  45. self.alertTitle = alertTitle
  46. self.alertDescription = alertDescription
  47. self.placeHolder = placeHolder
  48. self.confirmActionTitle = confirmActionTitle
  49. }
  50. }
  51. private func presentTextFieldAlert(with info: TextFieldAlertInfo,
  52. completionHandler: @escaping (String) -> Void) {
  53. let alertController = UIAlertController(title: info.alertTitle,
  54. message: info.alertDescription,
  55. preferredStyle: .alert)
  56. alertController.addTextField(configurationHandler: {
  57. textField in
  58. textField.placeholder = info.placeHolder
  59. })
  60. let cancelButton = UIAlertAction(title: NSLocalizedString("BUTTON_CANCEL", comment: ""),
  61. style: .default)
  62. let confirmAction = UIAlertAction(title: info.confirmActionTitle, style: .default) {
  63. [weak alertController] _ in
  64. guard let alertController = alertController,
  65. let textField = alertController.textFields?.first else { return }
  66. completionHandler(textField.text ?? "")
  67. }
  68. alertController.addAction(cancelButton)
  69. alertController.addAction(confirmAction)
  70. UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
  71. }
  72. }
  73. // MARK: - VLCEditToolbarDelegate
  74. extension VLCEditController: VLCEditToolbarDelegate {
  75. func editToolbarDidAddToPlaylist(_ editToolbar: VLCEditToolbar) {
  76. if let model = model as? PlaylistModel {
  77. let alertInfo = TextFieldAlertInfo(alertTitle: NSLocalizedString("PLAYLISTS", comment: ""),
  78. placeHolder: "NEW_PLAYLIST")
  79. presentTextFieldAlert(with: alertInfo, completionHandler: {
  80. text -> Void in
  81. model.create(name: text)
  82. })
  83. } else if let model = model as? VideoModel {
  84. let alertInfo = TextFieldAlertInfo(alertTitle: NSLocalizedString("PLAYLISTS", comment: ""),
  85. placeHolder: "NEW_PLAYLIST")
  86. presentTextFieldAlert(with: alertInfo, completionHandler: {
  87. [selectedCellIndexPaths, model] text -> Void in
  88. let playlist = model.medialibrary.createPlaylist(with: text)
  89. for indexPath in selectedCellIndexPaths {
  90. if let media = model.anyfiles[indexPath.row] as? VLCMLMedia {
  91. playlist.appendMedia(withIdentifier: media.identifier())
  92. }
  93. }
  94. })
  95. }
  96. }
  97. func editToolbarDidDelete(_ editToolbar: VLCEditToolbar) {
  98. var objectsToDelete = [VLCMLObject]()
  99. for indexPath in selectedCellIndexPaths {
  100. objectsToDelete.append(model.anyfiles[indexPath.row])
  101. }
  102. let cancelButton = VLCAlertButton(title: NSLocalizedString("BUTTON_CANCEL", comment: ""))
  103. let deleteButton = VLCAlertButton(title: NSLocalizedString("BUTTON_DELETE", comment: ""),
  104. style: .destructive,
  105. action: {
  106. [weak self] action in
  107. self?.model.delete(objectsToDelete)
  108. self?.selectedCellIndexPaths.removeAll()
  109. self?.resetAllVisibleCell()
  110. })
  111. VLCAlertViewController.alertViewManager(title: NSLocalizedString("DELETE_TITLE", comment: ""),
  112. errorMessage: NSLocalizedString("DELETE_MESSAGE", comment: ""),
  113. viewController: (UIApplication.shared.keyWindow?.rootViewController)!,
  114. buttonsAction: [cancelButton,
  115. deleteButton])
  116. }
  117. func editToolbarDidShare(_ editToolbar: VLCEditToolbar, presentFrom button: UIButton) {
  118. UIApplication.shared.beginIgnoringInteractionEvents()
  119. let rootViewController = UIApplication.shared.keyWindow?.rootViewController
  120. guard let controller = VLCActivityViewControllerVendor.activityViewController(forFiles: fileURLsFromSelection(), presenting: button, presenting: rootViewController) else {
  121. UIApplication.shared.endIgnoringInteractionEvents()
  122. return
  123. }
  124. controller.popoverPresentationController?.sourceView = editToolbar
  125. rootViewController?.present(controller, animated: true) {
  126. UIApplication.shared.endIgnoringInteractionEvents()
  127. }
  128. }
  129. func fileURLsFromSelection() -> [URL] {
  130. var fileURLS = [URL]()
  131. for indexPath in selectedCellIndexPaths {
  132. guard let file = model.anyfiles[indexPath.row] as? VLCMLMedia else {
  133. assertionFailure("we're trying to share something that doesn't have an mrl")
  134. return fileURLS
  135. }
  136. fileURLS.append(file.mainFile().mrl)
  137. }
  138. return fileURLS
  139. }
  140. func editToolbarDidRename(_ editToolbar: VLCEditToolbar) {
  141. // FIXME: Multiple renaming of files(multiple alert can get unfriendly if too many files)
  142. for indexPath in selectedCellIndexPaths {
  143. if let media = model.anyfiles[indexPath.row] as? VLCMLMedia {
  144. // Not using VLCAlertViewController to have more customization in text fields
  145. let alertInfo = TextFieldAlertInfo(alertTitle: String(format: NSLocalizedString("RENAME_MEDIA_TO", comment: ""), media.title),
  146. placeHolder: NSLocalizedString("RENAME_PLACEHOLDER", comment: ""),
  147. confirmActionTitle: NSLocalizedString("BUTTON_RENAME", comment: ""))
  148. presentTextFieldAlert(with: alertInfo, completionHandler: {
  149. [weak self] text -> Void in
  150. guard text != "" else {
  151. VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_RENAME_FAILED", comment: ""),
  152. errorMessage: NSLocalizedString("ERROR_EMPTY_NAME", comment: ""),
  153. viewController: (UIApplication.shared.keyWindow?.rootViewController)!)
  154. return
  155. }
  156. media.updateTitle(text)
  157. self?.resetCell(at: indexPath)
  158. })
  159. }
  160. }
  161. }
  162. }
  163. // MARK: - UICollectionViewDataSource
  164. extension VLCEditController: UICollectionViewDataSource {
  165. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  166. return model.anyfiles.count
  167. }
  168. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  169. guard let editCell = (model as? EditableMLModel)?.editCellType() else {
  170. assertionFailure("The category either doesn't implement EditableMLModel or doesn't have a editcellType defined")
  171. return UICollectionViewCell()
  172. }
  173. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: editCell.defaultReuseIdentifier,
  174. for: indexPath) as? MediaEditCell {
  175. cell.media = model.anyfiles[indexPath.row]
  176. cell.isChecked = selectedCellIndexPaths.contains(indexPath)
  177. return cell
  178. } else {
  179. assertionFailure("We couldn't dequeue a reusable cell, the cell might not be registered or is not a MediaEditCell")
  180. return UICollectionViewCell()
  181. }
  182. }
  183. }
  184. // MARK: - UICollectionViewDelegate
  185. extension VLCEditController: UICollectionViewDelegate {
  186. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  187. if let cell = collectionView.cellForItem(at: indexPath) as? MediaEditCell {
  188. cell.isChecked = !cell.isChecked
  189. if cell.isChecked {
  190. // cell selected, saving indexPath
  191. selectedCellIndexPaths.insert(indexPath)
  192. } else {
  193. selectedCellIndexPaths.remove(indexPath)
  194. }
  195. }
  196. }
  197. }
  198. // MARK: - UICollectionViewDelegateFlowLayout
  199. extension VLCEditController: UICollectionViewDelegateFlowLayout {
  200. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  201. let contentInset = collectionView.contentInset
  202. // FIXME: 5 should be cell padding, but not usable maybe static?
  203. let insetToRemove = contentInset.left + contentInset.right + (5 * 2)
  204. return CGSize(width: collectionView.frame.width - insetToRemove, height: MediaEditCell.height)
  205. }
  206. }