VLCEditController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. protocol VLCEditControllerDelegate: class {
  12. func editController(editController: VLCEditController, cellforItemAt indexPath: IndexPath) -> MediaEditCell?
  13. func editController(editController: VLCEditController, present viewController: UIViewController)
  14. }
  15. class VLCEditController: UIViewController {
  16. private var selectedCellIndexPaths = Set<IndexPath>()
  17. private let model: MediaLibraryBaseModel
  18. private let mediaLibraryService: MediaLibraryService
  19. private lazy var addToPlaylistViewController: AddToPlaylistViewController = {
  20. var addToPlaylistViewController = AddToPlaylistViewController(playlists: mediaLibraryService.playlists())
  21. addToPlaylistViewController.delegate = self
  22. return addToPlaylistViewController
  23. }()
  24. weak var delegate: VLCEditControllerDelegate?
  25. override func loadView() {
  26. let editToolbar = VLCEditToolbar(category: model)
  27. editToolbar.delegate = self
  28. self.view = editToolbar
  29. }
  30. init(mediaLibraryService: MediaLibraryService, model: MediaLibraryBaseModel) {
  31. self.mediaLibraryService = mediaLibraryService
  32. self.model = model
  33. super.init(nibName: nil, bundle: nil)
  34. }
  35. required init?(coder aDecoder: NSCoder) {
  36. fatalError("init(coder:) has not been implemented")
  37. }
  38. func resetSelections() {
  39. selectedCellIndexPaths.removeAll(keepingCapacity: false)
  40. }
  41. }
  42. // MARK: - Helpers
  43. private extension VLCEditController {
  44. private struct TextFieldAlertInfo {
  45. var alertTitle: String
  46. var alertDescription: String
  47. var placeHolder: String
  48. var confirmActionTitle: String
  49. init(alertTitle: String = "",
  50. alertDescription: String = "",
  51. placeHolder: String = "",
  52. confirmActionTitle: String = NSLocalizedString("BUTTON_DONE", comment: "")) {
  53. self.alertTitle = alertTitle
  54. self.alertDescription = alertDescription
  55. self.placeHolder = placeHolder
  56. self.confirmActionTitle = confirmActionTitle
  57. }
  58. }
  59. private func presentTextFieldAlert(with info: TextFieldAlertInfo,
  60. completionHandler: @escaping (String) -> Void) {
  61. let alertController = UIAlertController(title: info.alertTitle,
  62. message: info.alertDescription,
  63. preferredStyle: .alert)
  64. alertController.addTextField(configurationHandler: {
  65. textField in
  66. textField.placeholder = info.placeHolder
  67. })
  68. let cancelButton = UIAlertAction(title: NSLocalizedString("BUTTON_CANCEL", comment: ""),
  69. style: .default)
  70. let confirmAction = UIAlertAction(title: info.confirmActionTitle, style: .default) {
  71. [weak alertController] _ in
  72. guard let alertController = alertController,
  73. let textField = alertController.textFields?.first else { return }
  74. completionHandler(textField.text ?? "")
  75. }
  76. alertController.addAction(cancelButton)
  77. alertController.addAction(confirmAction)
  78. present(alertController, animated: true, completion: nil)
  79. }
  80. }
  81. // MARK: - VLCEditToolbarDelegate
  82. extension VLCEditController: VLCEditToolbarDelegate {
  83. func addToNewPlaylist() {
  84. let alertInfo = TextFieldAlertInfo(alertTitle: NSLocalizedString("PLAYLISTS", comment: ""),
  85. placeHolder: NSLocalizedString("PLAYLIST_PLACEHOLDER",
  86. comment: ""))
  87. presentTextFieldAlert(with: alertInfo,
  88. completionHandler: {
  89. [unowned self] text -> Void in
  90. guard text != "" else {
  91. DispatchQueue.main.async {
  92. VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_EMPTY_NAME",
  93. comment: ""),
  94. viewController: self)
  95. }
  96. return
  97. }
  98. self.model.createPlaylist(text, self.selectedCellIndexPaths)
  99. self.selectedCellIndexPaths.removeAll()
  100. })
  101. }
  102. func editToolbarDidAddToPlaylist(_ editToolbar: VLCEditToolbar) {
  103. if !mediaLibraryService.playlists().isEmpty && !selectedCellIndexPaths.isEmpty {
  104. addToPlaylistViewController.playlists = mediaLibraryService.playlists()
  105. delegate?.editController(editController: self,
  106. present: addToPlaylistViewController)
  107. } else {
  108. addToNewPlaylist()
  109. }
  110. }
  111. func editToolbarDidDelete(_ editToolbar: VLCEditToolbar) {
  112. var objectsToDelete = [VLCMLObject]()
  113. for indexPath in selectedCellIndexPaths {
  114. objectsToDelete.append(model.anyfiles[indexPath.row])
  115. }
  116. let cancelButton = VLCAlertButton(title: NSLocalizedString("BUTTON_CANCEL", comment: ""))
  117. let deleteButton = VLCAlertButton(title: NSLocalizedString("BUTTON_DELETE", comment: ""),
  118. style: .destructive,
  119. action: {
  120. [weak self] action in
  121. self?.model.delete(objectsToDelete)
  122. self?.selectedCellIndexPaths.removeAll()
  123. })
  124. VLCAlertViewController.alertViewManager(title: NSLocalizedString("DELETE_TITLE", comment: ""),
  125. errorMessage: NSLocalizedString("DELETE_MESSAGE", comment: ""),
  126. viewController: (UIApplication.shared.keyWindow?.rootViewController)!,
  127. buttonsAction: [cancelButton,
  128. deleteButton])
  129. }
  130. func editToolbarDidShare(_ editToolbar: VLCEditToolbar, presentFrom button: UIButton) {
  131. UIApplication.shared.beginIgnoringInteractionEvents()
  132. let rootViewController = UIApplication.shared.keyWindow?.rootViewController
  133. guard let controller = VLCActivityViewControllerVendor.activityViewController(forFiles: fileURLsFromSelection(), presenting: button, presenting: rootViewController) else {
  134. UIApplication.shared.endIgnoringInteractionEvents()
  135. return
  136. }
  137. controller.popoverPresentationController?.sourceView = editToolbar
  138. rootViewController?.present(controller, animated: true) {
  139. UIApplication.shared.endIgnoringInteractionEvents()
  140. }
  141. }
  142. func fileURLsFromSelection() -> [URL] {
  143. var fileURLS = [URL]()
  144. for indexPath in selectedCellIndexPaths {
  145. let file = model.anyfiles[indexPath.row]
  146. if let collection = file as? MediaCollectionModel {
  147. collection.files().forEach {
  148. fileURLS.append($0.mainFile().mrl)
  149. }
  150. } else if let media = file as? VLCMLMedia {
  151. fileURLS.append(media.mainFile().mrl)
  152. } else {
  153. assertionFailure("we're trying to share something that doesn't have an mrl")
  154. return fileURLS
  155. }
  156. }
  157. return fileURLS
  158. }
  159. func editToolbarDidRename(_ editToolbar: VLCEditToolbar) {
  160. // FIXME: Multiple renaming of files(multiple alert can get unfriendly if too many files)
  161. for indexPath in selectedCellIndexPaths {
  162. if let media = model.anyfiles[indexPath.row] as? VLCMLMedia {
  163. // Not using VLCAlertViewController to have more customization in text fields
  164. let alertInfo = TextFieldAlertInfo(alertTitle: String(format: NSLocalizedString("RENAME_MEDIA_TO", comment: ""), media.title),
  165. placeHolder: NSLocalizedString("RENAME_PLACEHOLDER", comment: ""),
  166. confirmActionTitle: NSLocalizedString("BUTTON_RENAME", comment: ""))
  167. presentTextFieldAlert(with: alertInfo, completionHandler: {
  168. [weak self] text -> Void in
  169. guard text != "" else {
  170. VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_RENAME_FAILED", comment: ""),
  171. errorMessage: NSLocalizedString("ERROR_EMPTY_NAME", comment: ""),
  172. viewController: (UIApplication.shared.keyWindow?.rootViewController)!)
  173. return
  174. }
  175. media.updateTitle(text)
  176. if let strongself = self {
  177. strongself.delegate?.editController(editController: strongself, cellforItemAt: indexPath)?.isChecked = false
  178. }
  179. })
  180. }
  181. }
  182. }
  183. }
  184. // MARK: - UICollectionViewDataSource
  185. extension VLCEditController: UICollectionViewDataSource {
  186. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  187. return model.anyfiles.count
  188. }
  189. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  190. guard let editCell = (model as? EditableMLModel)?.editCellType() else {
  191. assertionFailure("The category either doesn't implement EditableMLModel or doesn't have a editcellType defined")
  192. return UICollectionViewCell()
  193. }
  194. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: editCell.defaultReuseIdentifier,
  195. for: indexPath) as? MediaEditCell {
  196. cell.media = model.anyfiles[indexPath.row]
  197. cell.isChecked = selectedCellIndexPaths.contains(indexPath)
  198. return cell
  199. } else {
  200. assertionFailure("We couldn't dequeue a reusable cell, the cell might not be registered or is not a MediaEditCell")
  201. return UICollectionViewCell()
  202. }
  203. }
  204. func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
  205. guard let collectionModel = model as? CollectionModel, let playlist = collectionModel.mediaCollection as? VLCMLPlaylist else {
  206. assertionFailure("can Move should've been false")
  207. return
  208. }
  209. playlist.moveMedia(fromPosition: UInt32(sourceIndexPath.row), toDestination: UInt32(destinationIndexPath.row))
  210. }
  211. func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
  212. if let collectionModel = model as? CollectionModel, collectionModel.mediaCollection is VLCMLPlaylist {
  213. return true
  214. }
  215. return false
  216. }
  217. }
  218. // MARK: - UICollectionViewDelegate
  219. extension VLCEditController: UICollectionViewDelegate {
  220. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  221. if let cell = collectionView.cellForItem(at: indexPath) as? MediaEditCell {
  222. cell.isChecked = !cell.isChecked
  223. if cell.isChecked {
  224. // cell selected, saving indexPath
  225. selectedCellIndexPaths.insert(indexPath)
  226. } else {
  227. selectedCellIndexPaths.remove(indexPath)
  228. }
  229. }
  230. }
  231. }
  232. // MARK: - UICollectionViewDelegateFlowLayout
  233. extension VLCEditController: UICollectionViewDelegateFlowLayout {
  234. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  235. let contentInset = collectionView.contentInset
  236. // FIXME: 5 should be cell padding, but not usable maybe static?
  237. let insetToRemove = contentInset.left + contentInset.right + (5 * 2)
  238. return CGSize(width: collectionView.frame.width - insetToRemove, height: MediaEditCell.height)
  239. }
  240. }
  241. extension VLCEditController: AddToPlaylistViewControllerDelegate {
  242. func addToPlaylistViewController(_ addToPlaylistViewController: AddToPlaylistViewController,
  243. didSelectPlaylist playlist: VLCMLPlaylist) {
  244. let files = model.anyfiles
  245. for index in selectedCellIndexPaths where index.row < files.count {
  246. if !playlist.appendMedia(withIdentifier: files[index.row].identifier()) {
  247. assertionFailure("VLCEditController: AddToPlaylistViewControllerDelegate: Failed to add item.")
  248. }
  249. }
  250. addToPlaylistViewController.dismiss(animated: true, completion: nil)
  251. }
  252. func addToPlaylistViewController(_ addToPlaylistViewController: AddToPlaylistViewController,
  253. newPlaylistWithName name: String) {
  254. model.createPlaylist(name, self.selectedCellIndexPaths)
  255. selectedCellIndexPaths.removeAll()
  256. addToPlaylistViewController.dismiss(animated: true, completion: nil)
  257. }
  258. }