EditController.swift 12 KB

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