VLCEditController.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 createPlaylist() {
  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 delete() {
  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 share() {
  118. assertionFailure("Implement me")
  119. }
  120. func rename() {
  121. // FIXME: Multiple renaming of files(multiple alert can get unfriendly if too many files)
  122. for indexPath in selectedCellIndexPaths {
  123. if let media = model.anyfiles[indexPath.row] as? VLCMLMedia {
  124. // Not using VLCAlertViewController to have more customization in text fields
  125. let alertInfo = TextFieldAlertInfo(alertTitle: String(format: NSLocalizedString("RENAME_MEDIA_TO", comment: ""), media.title),
  126. placeHolder: "NEW_NAME",
  127. confirmActionTitle: NSLocalizedString("BUTTON_RENAME", comment: ""))
  128. presentTextFieldAlert(with: alertInfo, completionHandler: {
  129. [weak self] text -> Void in
  130. guard text != "" else {
  131. VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_RENAME_FAILED", comment: ""),
  132. errorMessage: NSLocalizedString("ERROR_EMPTY_NAME", comment: ""),
  133. viewController: (UIApplication.shared.keyWindow?.rootViewController)!)
  134. return
  135. }
  136. media.updateTitle(text)
  137. self?.resetCell(at: indexPath)
  138. })
  139. }
  140. }
  141. }
  142. }
  143. // MARK: - UICollectionViewDataSource
  144. extension VLCEditController: UICollectionViewDataSource {
  145. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  146. return model.anyfiles.count
  147. }
  148. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  149. guard let editCell = (model as? EditableMLModel)?.editCellType() else {
  150. assertionFailure("The category either doesn't implement EditableMLModel or doesn't have a editcellType defined")
  151. return UICollectionViewCell()
  152. }
  153. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: editCell.defaultReuseIdentifier,
  154. for: indexPath) as? MediaEditCell {
  155. cell.media = model.anyfiles[indexPath.row]
  156. cell.isChecked = selectedCellIndexPaths.contains(indexPath)
  157. return cell
  158. } else {
  159. assertionFailure("We couldn't dequeue a reusable cell, the cell might not be registered or is not a MediaEditCell")
  160. return UICollectionViewCell()
  161. }
  162. }
  163. }
  164. // MARK: - UICollectionViewDelegate
  165. extension VLCEditController: UICollectionViewDelegate {
  166. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  167. if let cell = collectionView.cellForItem(at: indexPath) as? MediaEditCell {
  168. cell.isChecked = !cell.isChecked
  169. if cell.isChecked {
  170. // cell selected, saving indexPath
  171. selectedCellIndexPaths.insert(indexPath)
  172. } else {
  173. selectedCellIndexPaths.remove(indexPath)
  174. }
  175. }
  176. }
  177. }
  178. // MARK: - UICollectionViewDelegateFlowLayout
  179. extension VLCEditController: UICollectionViewDelegateFlowLayout {
  180. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  181. let contentInset = collectionView.contentInset
  182. // FIXME: 5 should be cell padding, but not usable maybe static?
  183. let insetToRemove = contentInset.left + contentInset.right + (5 * 2)
  184. return CGSize(width: collectionView.frame.width - insetToRemove, height: MediaEditCell.height)
  185. }
  186. }