EditController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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) -> BaseCollectionViewCell?
  13. func editController(editController: EditController, present viewController: UIViewController)
  14. func editControllerDidFinishEditing(editController: EditController?)
  15. }
  16. class EditController: UIViewController {
  17. // Cache selectedIndexPath separately to indexPathsForSelectedItems in order to have persistance
  18. private var selectedCellIndexPaths = Set<IndexPath>()
  19. private let model: MediaLibraryBaseModel
  20. private let mediaLibraryService: MediaLibraryService
  21. private let presentingView: UICollectionView
  22. private(set) var editActions: EditActions
  23. weak var delegate: EditControllerDelegate?
  24. override func loadView() {
  25. let editToolbar = EditToolbar(model: model)
  26. editToolbar.delegate = self
  27. self.view = editToolbar
  28. }
  29. init(mediaLibraryService: MediaLibraryService,
  30. model: MediaLibraryBaseModel,
  31. presentingView: UICollectionView) {
  32. self.mediaLibraryService = mediaLibraryService
  33. self.model = model
  34. self.presentingView = presentingView
  35. self.editActions = EditActions(model: model, mediaLibraryService: mediaLibraryService)
  36. super.init(nibName: nil, bundle: nil)
  37. }
  38. required init?(coder aDecoder: NSCoder) {
  39. fatalError("init(coder:) has not been implemented")
  40. }
  41. func resetSelections(resetUI: Bool) {
  42. for indexPath in selectedCellIndexPaths {
  43. presentingView.deselectItem(at: indexPath, animated: true)
  44. if resetUI {
  45. collectionView(presentingView, didDeselectItemAt: indexPath)
  46. }
  47. }
  48. selectedCellIndexPaths.removeAll()
  49. }
  50. }
  51. // MARK: - Helpers
  52. private extension EditController {
  53. private struct TextFieldAlertInfo {
  54. var alertTitle: String
  55. var alertDescription: String
  56. var placeHolder: String
  57. var textfieldText: String
  58. var confirmActionTitle: String
  59. init(alertTitle: String = "",
  60. alertDescription: String = "",
  61. placeHolder: String = "",
  62. textfieldText: String = "",
  63. confirmActionTitle: String = NSLocalizedString("BUTTON_DONE", comment: "")) {
  64. self.alertTitle = alertTitle
  65. self.alertDescription = alertDescription
  66. self.placeHolder = placeHolder
  67. self.textfieldText = textfieldText
  68. self.confirmActionTitle = confirmActionTitle
  69. }
  70. }
  71. private func presentTextFieldAlert(with info: TextFieldAlertInfo,
  72. completionHandler: @escaping (String) -> Void) {
  73. let alertController = UIAlertController(title: info.alertTitle,
  74. message: info.alertDescription,
  75. preferredStyle: .alert)
  76. alertController.addTextField(configurationHandler: {
  77. textField in
  78. textField.text = info.textfieldText
  79. textField.placeholder = info.placeHolder
  80. })
  81. let cancelButton = UIAlertAction(title: NSLocalizedString("BUTTON_CANCEL", comment: ""),
  82. style: .cancel)
  83. let confirmAction = UIAlertAction(title: info.confirmActionTitle, style: .default) {
  84. [weak alertController] _ in
  85. guard let alertController = alertController,
  86. let textField = alertController.textFields?.first else { return }
  87. completionHandler(textField.text ?? "")
  88. }
  89. alertController.addAction(cancelButton)
  90. alertController.addAction(confirmAction)
  91. present(alertController, animated: true, completion: nil)
  92. }
  93. }
  94. // MARK: - VLCEditToolbarDelegate
  95. extension EditController: EditToolbarDelegate {
  96. private func getSelectedObjects() {
  97. let files = model.anyfiles
  98. for index in selectedCellIndexPaths where index.row < files.count {
  99. if let mediaCollection = files[index.row] as? MediaCollectionModel {
  100. guard let files = mediaCollection.files() else {
  101. assertionFailure("EditController: Fail to retrieve tracks.")
  102. DispatchQueue.main.async {
  103. VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_PLAYLIST_TRACKS",
  104. comment: ""),
  105. viewController: self)
  106. }
  107. return
  108. }
  109. editActions.objects += files
  110. } else {
  111. editActions.objects.append(files[index.row])
  112. }
  113. }
  114. }
  115. func editToolbarDidAddToPlaylist(_ editToolbar: EditToolbar) {
  116. guard !selectedCellIndexPaths.isEmpty else {
  117. assertionFailure("EditController: Add to playlist called without selection")
  118. return
  119. }
  120. editActions.objects.removeAll()
  121. getSelectedObjects()
  122. editActions.addToPlaylist({
  123. [weak self] state in
  124. if state == .success || state == .fail {
  125. self?.resetSelections(resetUI: false)
  126. self?.delegate?.editControllerDidFinishEditing(editController: self)
  127. }
  128. })
  129. }
  130. func editToolbarDidDelete(_ editToolbar: EditToolbar) {
  131. guard !selectedCellIndexPaths.isEmpty else {
  132. assertionFailure("EditController: Delete called without selection")
  133. return
  134. }
  135. editActions.objects.removeAll()
  136. for indexPath in selectedCellIndexPaths.sorted(by: { $0 > $1 }) {
  137. editActions.objects.append(model.anyfiles[indexPath.row])
  138. }
  139. editActions.delete({
  140. [weak self] state in
  141. if state == .success || state == .fail {
  142. self?.resetSelections(resetUI: false)
  143. self?.delegate?.editControllerDidFinishEditing(editController: self)
  144. }
  145. })
  146. }
  147. func editToolbarDidShare(_ editToolbar: EditToolbar) {
  148. guard !selectedCellIndexPaths.isEmpty else {
  149. assertionFailure("EditController: Share called without selection")
  150. return
  151. }
  152. editActions.objects.removeAll()
  153. getSelectedObjects()
  154. editActions.share({
  155. [weak self] state in
  156. if state == .success || state == .fail {
  157. self?.resetSelections(resetUI: false)
  158. self?.delegate?.editControllerDidFinishEditing(editController: self)
  159. }
  160. })
  161. }
  162. func editToolbarDidRename(_ editToolbar: EditToolbar) {
  163. guard !selectedCellIndexPaths.isEmpty else {
  164. assertionFailure("EditController: Rename called without selection")
  165. return
  166. }
  167. editActions.objects.removeAll()
  168. for indexPath in selectedCellIndexPaths.sorted(by: { $0 > $1 }) {
  169. editActions.objects.append(model.anyfiles[indexPath.row])
  170. }
  171. editActions.rename({
  172. [weak self] state in
  173. if state == .success || state == .fail {
  174. self?.resetSelections(resetUI: true)
  175. self?.delegate?.editControllerDidFinishEditing(editController: self)
  176. }
  177. })
  178. }
  179. }
  180. // MARK: - UICollectionViewDelegate
  181. extension EditController: UICollectionViewDelegate {
  182. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  183. selectedCellIndexPaths.insert(indexPath)
  184. // Isolate selectionViewOverlay changes inside EditController
  185. if let cell = collectionView.cellForItem(at: indexPath) as? BaseCollectionViewCell {
  186. cell.selectionViewOverlay?.isHidden = false
  187. }
  188. }
  189. func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
  190. selectedCellIndexPaths.remove(indexPath)
  191. if let cell = collectionView.cellForItem(at: indexPath) as? BaseCollectionViewCell {
  192. cell.selectionViewOverlay?.isHidden = true
  193. }
  194. }
  195. }
  196. // MARK: - UICollectionViewDataSource
  197. extension EditController: UICollectionViewDataSource {
  198. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  199. return model.anyfiles.count
  200. }
  201. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  202. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: model.cellType.defaultReuseIdentifier,
  203. for: indexPath) as? BaseCollectionViewCell {
  204. cell.media = model.anyfiles[indexPath.row]
  205. cell.isSelected = selectedCellIndexPaths.contains(indexPath)
  206. cell.isAccessibilityElement = true
  207. cell.checkImageView?.isHidden = false
  208. if let cell = cell as? MediaCollectionViewCell,
  209. let collectionModel = model as? CollectionModel, collectionModel.mediaCollection is VLCMLPlaylist {
  210. cell.dragIndicatorImageView.isHidden = false
  211. }
  212. if cell.isSelected {
  213. cell.selectionViewOverlay?.isHidden = false
  214. }
  215. return cell
  216. } else {
  217. assertionFailure("We couldn't dequeue a reusable cell, the cell might not be registered or is not a MediaEditCell")
  218. return UICollectionViewCell()
  219. }
  220. }
  221. func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
  222. guard let collectionModel = model as? CollectionModel, let playlist = collectionModel.mediaCollection as? VLCMLPlaylist else {
  223. assertionFailure("can Move should've been false")
  224. return
  225. }
  226. playlist.moveMedia(fromPosition: UInt32(sourceIndexPath.row), toDestination: UInt32(destinationIndexPath.row))
  227. }
  228. func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
  229. if let collectionModel = model as? CollectionModel, collectionModel.mediaCollection is VLCMLPlaylist {
  230. return true
  231. }
  232. return false
  233. }
  234. }
  235. // MARK: - UICollectionViewDelegateFlowLayout
  236. extension EditController: UICollectionViewDelegateFlowLayout {
  237. func collectionView(_ collectionView: UICollectionView,
  238. layout collectionViewLayout: UICollectionViewLayout,
  239. sizeForItemAt indexPath: IndexPath) -> CGSize {
  240. var toWidth = collectionView.frame.size.width
  241. if #available(iOS 11.0, *) {
  242. toWidth = collectionView.safeAreaLayoutGuide.layoutFrame.width
  243. }
  244. return model.cellType.cellSizeForWidth(toWidth)
  245. }
  246. func collectionView(_ collectionView: UICollectionView,
  247. layout collectionViewLayout: UICollectionViewLayout,
  248. insetForSectionAt section: Int) -> UIEdgeInsets {
  249. return UIEdgeInsets(top: model.cellType.edgePadding,
  250. left: model.cellType.edgePadding,
  251. bottom: model.cellType.edgePadding,
  252. right: model.cellType.edgePadding)
  253. }
  254. func collectionView(_ collectionView: UICollectionView,
  255. layout collectionViewLayout: UICollectionViewLayout,
  256. minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  257. return model.cellType.edgePadding
  258. }
  259. func collectionView(_ collectionView: UICollectionView,
  260. layout collectionViewLayout: UICollectionViewLayout,
  261. minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  262. return model.cellType.interItemPadding
  263. }
  264. }