VLCEditController.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 placeHolder: String
  39. var confirmActionTitle: String
  40. init(alertTitle: String = "",
  41. placeHolder: String = "",
  42. confirmActionTitle: String = NSLocalizedString("BUTTON_DONE", comment: "")) {
  43. self.alertTitle = alertTitle
  44. self.placeHolder = placeHolder
  45. self.confirmActionTitle = confirmActionTitle
  46. }
  47. }
  48. private func presentTextFieldAlert(with info: TextFieldAlertInfo,
  49. completionHandler: @escaping (String) -> Void) {
  50. let alertController = UIAlertController(title: info.alertTitle,
  51. message: "",
  52. preferredStyle: .alert)
  53. alertController.addTextField(configurationHandler: {
  54. textField in
  55. textField.placeholder = info.placeHolder
  56. })
  57. let cancelButton = UIAlertAction(title: NSLocalizedString("BUTTON_CANCEL", comment: ""),
  58. style: .default)
  59. let confirmAction = UIAlertAction(title: info.confirmActionTitle, style: .default) {
  60. [weak alertController] _ in
  61. guard let alertController = alertController,
  62. let textField = alertController.textFields?.first else { return }
  63. completionHandler(textField.text ?? "")
  64. }
  65. alertController.addAction(cancelButton)
  66. alertController.addAction(confirmAction)
  67. UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
  68. }
  69. }
  70. // MARK: - VLCEditToolbarDelegate
  71. extension VLCEditController: VLCEditToolbarDelegate {
  72. func createPlaylist() {
  73. if let model = model as? PlaylistModel {
  74. let alertInfo = TextFieldAlertInfo(alertTitle: NSLocalizedString("PLAYLISTS", comment: ""),
  75. placeHolder: "NEW_PLAYLIST")
  76. presentTextFieldAlert(with: alertInfo, completionHandler: {
  77. text -> Void in
  78. model.create(name: text)
  79. })
  80. } else if let model = model as? VideoModel {
  81. let alertInfo = TextFieldAlertInfo(alertTitle: NSLocalizedString("PLAYLISTS", comment: ""),
  82. placeHolder: "NEW_PLAYLIST")
  83. presentTextFieldAlert(with: alertInfo, completionHandler: {
  84. [selectedCellIndexPaths, model] text -> Void in
  85. let playlist = model.medialibrary.createPlaylist(with: text)
  86. for indexPath in selectedCellIndexPaths {
  87. if let media = model.anyfiles[indexPath.row] as? VLCMLMedia {
  88. playlist.appendMedia(withIdentifier: media.identifier())
  89. }
  90. }
  91. })
  92. }
  93. }
  94. func delete() {
  95. var objectsToDelete = [VLCMLObject]()
  96. for indexPath in selectedCellIndexPaths {
  97. objectsToDelete.append(model.anyfiles[indexPath.row])
  98. }
  99. let cancelButton = VLCAlertButton(title: NSLocalizedString("BUTTON_CANCEL", comment: ""))
  100. let deleteButton = VLCAlertButton(title: NSLocalizedString("BUTTON_DELETE", comment: ""),
  101. style: .destructive,
  102. action: {
  103. [weak self] action in
  104. self?.model.delete(objectsToDelete)
  105. self?.selectedCellIndexPaths.removeAll()
  106. self?.resetAllVisibleCell()
  107. })
  108. VLCAlertViewController.alertViewManager(title: NSLocalizedString("DELETE_TITLE", comment: ""),
  109. errorMessage: NSLocalizedString("DELETE_MESSAGE", comment: ""),
  110. viewController: (UIApplication.shared.keyWindow?.rootViewController)!,
  111. buttonsAction: [cancelButton,
  112. deleteButton])
  113. }
  114. func share() {
  115. assertionFailure("Implement me")
  116. }
  117. func rename() {
  118. // FIXME: Multiple renaming of files(multiple alert can get unfriendly if too many files)
  119. for indexPath in selectedCellIndexPaths {
  120. if let media = model.anyfiles[indexPath.row] as? VLCMLMedia {
  121. // Not using VLCAlertViewController to have more customization in text fields
  122. let alertInfo = TextFieldAlertInfo(alertTitle: String(format: NSLocalizedString("RENAME_MEDIA_TO", comment: ""), media.title),
  123. placeHolder: "NEW_NAME",
  124. confirmActionTitle: NSLocalizedString("BUTTON_RENAME", comment: ""))
  125. presentTextFieldAlert(with: alertInfo, completionHandler: {
  126. [weak self] text -> Void in
  127. guard text != "" else {
  128. VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_RENAME_FAILED", comment: ""),
  129. errorMessage: NSLocalizedString("ERROR_EMPTY_NAME", comment: ""),
  130. viewController: (UIApplication.shared.keyWindow?.rootViewController)!)
  131. return
  132. }
  133. media.updateTitle(text)
  134. self?.resetCell(at: indexPath)
  135. })
  136. }
  137. }
  138. }
  139. }
  140. // MARK: - UICollectionViewDataSource
  141. extension VLCEditController: UICollectionViewDataSource {
  142. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  143. return model.anyfiles.count
  144. }
  145. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  146. guard let editCell = (model as? EditableMLModel)?.editCellType() else {
  147. assertionFailure("The category either doesn't implement EditableMLModel or doesn't have a editcellType defined")
  148. return UICollectionViewCell()
  149. }
  150. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: editCell.defaultReuseIdentifier,
  151. for: indexPath) as? MediaEditCell {
  152. cell.media = model.anyfiles[indexPath.row]
  153. cell.isChecked = selectedCellIndexPaths.contains(indexPath)
  154. return cell
  155. } else {
  156. assertionFailure("We couldn't dequeue a reusable cell, the cell might not be registered or is not a MediaEditCell")
  157. return UICollectionViewCell()
  158. }
  159. }
  160. }
  161. // MARK: - UICollectionViewDelegate
  162. extension VLCEditController: UICollectionViewDelegate {
  163. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  164. if let cell = collectionView.cellForItem(at: indexPath) as? MediaEditCell {
  165. cell.isChecked = !cell.isChecked
  166. if cell.isChecked {
  167. // cell selected, saving indexPath
  168. selectedCellIndexPaths.insert(indexPath)
  169. } else {
  170. selectedCellIndexPaths.remove(indexPath)
  171. }
  172. }
  173. }
  174. }
  175. // MARK: - UICollectionViewDelegateFlowLayout
  176. extension VLCEditController: UICollectionViewDelegateFlowLayout {
  177. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  178. let contentInset = collectionView.contentInset
  179. // FIXME: 5 should be cell padding, but not usable maybe static?
  180. let insetToRemove = contentInset.left + contentInset.right + (5 * 2)
  181. return CGSize(width: collectionView.frame.width - insetToRemove, height: MediaEditCell.height)
  182. }
  183. }