EditController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*****************************************************************************
  2. * EditController.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 EditControllerDelegate: class {
  12. func editController(editController: EditController, cellforItemAt indexPath: IndexPath) -> MediaEditCell?
  13. func editController(editController: EditController, present viewController: UIViewController)
  14. }
  15. class EditController: 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: EditControllerDelegate?
  25. override func loadView() {
  26. let editToolbar = EditToolbar(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()
  40. }
  41. }
  42. // MARK: - Helpers
  43. private extension EditController {
  44. private struct TextFieldAlertInfo {
  45. var alertTitle: String
  46. var alertDescription: String
  47. var placeHolder: String
  48. var textfieldText: String
  49. var confirmActionTitle: String
  50. init(alertTitle: String = "",
  51. alertDescription: String = "",
  52. placeHolder: String = "",
  53. textfieldText: String = "",
  54. confirmActionTitle: String = NSLocalizedString("BUTTON_DONE", comment: "")) {
  55. self.alertTitle = alertTitle
  56. self.alertDescription = alertDescription
  57. self.placeHolder = placeHolder
  58. self.textfieldText = textfieldText
  59. self.confirmActionTitle = confirmActionTitle
  60. }
  61. }
  62. private func presentTextFieldAlert(with info: TextFieldAlertInfo,
  63. completionHandler: @escaping (String) -> Void) {
  64. let alertController = UIAlertController(title: info.alertTitle,
  65. message: info.alertDescription,
  66. preferredStyle: .alert)
  67. alertController.addTextField(configurationHandler: {
  68. textField in
  69. textField.text = info.textfieldText
  70. textField.placeholder = info.placeHolder
  71. })
  72. let cancelButton = UIAlertAction(title: NSLocalizedString("BUTTON_CANCEL", comment: ""),
  73. style: .default)
  74. let confirmAction = UIAlertAction(title: info.confirmActionTitle, style: .default) {
  75. [weak alertController] _ in
  76. guard let alertController = alertController,
  77. let textField = alertController.textFields?.first else { return }
  78. completionHandler(textField.text ?? "")
  79. }
  80. alertController.addAction(cancelButton)
  81. alertController.addAction(confirmAction)
  82. present(alertController, animated: true, completion: nil)
  83. }
  84. private func createPlaylist(_ name: String) {
  85. guard let playlist = mediaLibraryService.createPlaylist(with: name) else {
  86. assertionFailure("MediaModel: createPlaylist: Failed to create a playlist.")
  87. DispatchQueue.main.async {
  88. VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_PLAYLIST_CREATION",
  89. comment: ""),
  90. viewController: self)
  91. }
  92. return
  93. }
  94. // In the case of Video, Tracks
  95. if let files = model.anyfiles as? [VLCMLMedia] {
  96. for index in selectedCellIndexPaths where index.row < files.count {
  97. playlist.appendMedia(withIdentifier: files[index.row].identifier())
  98. }
  99. } else if let mediaCollectionArray = model.anyfiles as? [MediaCollectionModel] {
  100. for index in selectedCellIndexPaths where index.row < mediaCollectionArray.count {
  101. guard let tracks = mediaCollectionArray[index.row].files() else {
  102. assertionFailure("EditController: Fail to retrieve tracks.")
  103. DispatchQueue.main.async {
  104. VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_PLAYLIST_TRACKS",
  105. comment: ""),
  106. viewController: self)
  107. }
  108. return
  109. }
  110. tracks.forEach() {
  111. playlist.appendMedia(withIdentifier: $0.identifier())
  112. }
  113. }
  114. }
  115. selectedCellIndexPaths.removeAll()
  116. }
  117. }
  118. // MARK: - VLCEditToolbarDelegate
  119. extension EditController: EditToolbarDelegate {
  120. func addToNewPlaylist() {
  121. let alertInfo = TextFieldAlertInfo(alertTitle: NSLocalizedString("PLAYLISTS", comment: ""),
  122. placeHolder: NSLocalizedString("PLAYLIST_PLACEHOLDER",
  123. comment: ""))
  124. presentTextFieldAlert(with: alertInfo) {
  125. [unowned self] text -> Void in
  126. guard text != "" else {
  127. DispatchQueue.main.async {
  128. VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_EMPTY_NAME",
  129. comment: ""),
  130. viewController: self)
  131. }
  132. return
  133. }
  134. self.createPlaylist(text)
  135. }
  136. }
  137. func editToolbarDidAddToPlaylist(_ editToolbar: EditToolbar) {
  138. if !mediaLibraryService.playlists().isEmpty && !selectedCellIndexPaths.isEmpty {
  139. addToPlaylistViewController.playlists = mediaLibraryService.playlists()
  140. delegate?.editController(editController: self,
  141. present: addToPlaylistViewController)
  142. } else {
  143. addToNewPlaylist()
  144. }
  145. }
  146. func editToolbarDidDelete(_ editToolbar: EditToolbar) {
  147. var objectsToDelete = [VLCMLObject]()
  148. for indexPath in selectedCellIndexPaths {
  149. objectsToDelete.append(model.anyfiles[indexPath.row])
  150. }
  151. let cancelButton = VLCAlertButton(title: NSLocalizedString("BUTTON_CANCEL", comment: ""))
  152. let deleteButton = VLCAlertButton(title: NSLocalizedString("BUTTON_DELETE", comment: ""),
  153. style: .destructive,
  154. action: {
  155. [weak self] action in
  156. self?.model.delete(objectsToDelete)
  157. self?.selectedCellIndexPaths.removeAll()
  158. })
  159. VLCAlertViewController.alertViewManager(title: NSLocalizedString("DELETE_TITLE", comment: ""),
  160. errorMessage: NSLocalizedString("DELETE_MESSAGE", comment: ""),
  161. viewController: (UIApplication.shared.keyWindow?.rootViewController)!,
  162. buttonsAction: [cancelButton,
  163. deleteButton])
  164. }
  165. func editToolbarDidShare(_ editToolbar: EditToolbar, presentFrom button: UIButton) {
  166. UIApplication.shared.beginIgnoringInteractionEvents()
  167. let rootViewController = UIApplication.shared.keyWindow?.rootViewController
  168. guard let controller = VLCActivityViewControllerVendor.activityViewController(forFiles: fileURLsFromSelection(), presenting: button, presenting: rootViewController) else {
  169. UIApplication.shared.endIgnoringInteractionEvents()
  170. return
  171. }
  172. controller.popoverPresentationController?.sourceView = editToolbar
  173. rootViewController?.present(controller, animated: true) {
  174. UIApplication.shared.endIgnoringInteractionEvents()
  175. }
  176. }
  177. func fileURLsFromSelection() -> [URL] {
  178. var fileURLS = [URL]()
  179. for indexPath in selectedCellIndexPaths {
  180. let file = model.anyfiles[indexPath.row]
  181. if let collection = file as? MediaCollectionModel,
  182. let files = collection.files() {
  183. files.forEach {
  184. if let mainFile = $0.mainFile() {
  185. fileURLS.append(mainFile.mrl)
  186. }
  187. }
  188. } else if let media = file as? VLCMLMedia, let mainFile = media.mainFile() {
  189. fileURLS.append(mainFile.mrl)
  190. } else {
  191. assertionFailure("we're trying to share something that doesn't have an mrl")
  192. return fileURLS
  193. }
  194. }
  195. return fileURLS
  196. }
  197. func editToolbarDidRename(_ editToolbar: EditToolbar) {
  198. guard let indexPath = selectedCellIndexPaths.first else {
  199. assertionFailure("EditController: Rename called without selection.")
  200. return
  201. }
  202. if let media = model.anyfiles[indexPath.row] as? VLCMLMedia {
  203. // Not using VLCAlertViewController to have more customization in text fields
  204. let alertInfo = TextFieldAlertInfo(alertTitle: String(format: NSLocalizedString("RENAME_MEDIA_TO", comment: ""), media.title),
  205. textfieldText: media.title,
  206. confirmActionTitle: NSLocalizedString("BUTTON_RENAME", comment: ""))
  207. presentTextFieldAlert(with: alertInfo, completionHandler: {
  208. [weak self] text -> Void in
  209. guard text != "" else {
  210. VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_RENAME_FAILED", comment: ""),
  211. errorMessage: NSLocalizedString("ERROR_EMPTY_NAME", comment: ""),
  212. viewController: (UIApplication.shared.keyWindow?.rootViewController)!)
  213. return
  214. }
  215. media.updateTitle(text)
  216. guard let strongself = self else {
  217. return
  218. }
  219. strongself.delegate?.editController(editController: strongself, cellforItemAt: indexPath)?.isChecked = false
  220. strongself.selectedCellIndexPaths.remove(indexPath)
  221. //call until all items are renamed
  222. if !strongself.selectedCellIndexPaths.isEmpty {
  223. strongself.editToolbarDidRename(editToolbar)
  224. }
  225. })
  226. }
  227. }
  228. }
  229. // MARK: - UICollectionViewDataSource
  230. extension EditController: UICollectionViewDataSource {
  231. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  232. return model.anyfiles.count
  233. }
  234. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  235. guard let editCell = (model as? EditableMLModel)?.editCellType() else {
  236. assertionFailure("The category either doesn't implement EditableMLModel or doesn't have a editcellType defined")
  237. return UICollectionViewCell()
  238. }
  239. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: editCell.defaultReuseIdentifier,
  240. for: indexPath) as? MediaEditCell {
  241. cell.media = model.anyfiles[indexPath.row]
  242. cell.isChecked = selectedCellIndexPaths.contains(indexPath)
  243. cell.isAccessibilityElement = true
  244. return cell
  245. } else {
  246. assertionFailure("We couldn't dequeue a reusable cell, the cell might not be registered or is not a MediaEditCell")
  247. return UICollectionViewCell()
  248. }
  249. }
  250. func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
  251. guard let collectionModel = model as? CollectionModel, let playlist = collectionModel.mediaCollection as? VLCMLPlaylist else {
  252. assertionFailure("can Move should've been false")
  253. return
  254. }
  255. playlist.moveMedia(fromPosition: UInt32(sourceIndexPath.row), toDestination: UInt32(destinationIndexPath.row))
  256. }
  257. func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
  258. if let collectionModel = model as? CollectionModel, collectionModel.mediaCollection is VLCMLPlaylist {
  259. return true
  260. }
  261. return false
  262. }
  263. }
  264. // MARK: - UICollectionViewDelegate
  265. extension EditController: UICollectionViewDelegate {
  266. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  267. if let cell = collectionView.cellForItem(at: indexPath) as? MediaEditCell {
  268. cell.isChecked = !cell.isChecked
  269. if cell.isChecked {
  270. // cell selected, saving indexPath
  271. selectedCellIndexPaths.insert(indexPath)
  272. } else {
  273. selectedCellIndexPaths.remove(indexPath)
  274. }
  275. }
  276. }
  277. }
  278. // MARK: - UICollectionViewDelegateFlowLayout
  279. extension EditController: UICollectionViewDelegateFlowLayout {
  280. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  281. let contentInset = collectionView.contentInset
  282. // FIXME: 5 should be cell padding, but not usable maybe static?
  283. let insetToRemove = contentInset.left + contentInset.right + (5 * 2)
  284. var width = collectionView.frame.width
  285. if #available(iOS 11.0, *) {
  286. width = collectionView.safeAreaLayoutGuide.layoutFrame.width
  287. }
  288. return MediaEditCell.cellSizeForWidth(width - insetToRemove)
  289. }
  290. }
  291. extension EditController: AddToPlaylistViewControllerDelegate {
  292. func addToPlaylistViewController(_ addToPlaylistViewController: AddToPlaylistViewController,
  293. didSelectPlaylist playlist: VLCMLPlaylist) {
  294. let files = model.anyfiles
  295. var mediaObjects = [VLCMLObject]()
  296. for index in selectedCellIndexPaths where index.row < files.count {
  297. if let mediaCollection = files[index.row] as? MediaCollectionModel {
  298. mediaObjects += mediaCollection.files() ?? []
  299. } else {
  300. mediaObjects.append(files[index.row])
  301. }
  302. }
  303. for media in mediaObjects {
  304. if !playlist.appendMedia(withIdentifier: media.identifier()) {
  305. assertionFailure("EditController: AddToPlaylistViewControllerDelegate: Failed to add item.")
  306. }
  307. }
  308. resetSelections()
  309. addToPlaylistViewController.dismiss(animated: true, completion: nil)
  310. }
  311. func addToPlaylistViewController(_ addToPlaylistViewController: AddToPlaylistViewController,
  312. newPlaylistWithName name: String) {
  313. createPlaylist(name)
  314. addToPlaylistViewController.dismiss(animated: true, completion: nil)
  315. }
  316. }