VLCEditController.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. protocol VLCEditControllerDataSource {
  12. func toolbarNeedsUpdate(editing: Bool)
  13. }
  14. class VLCEditController: NSObject {
  15. private var selectedCellIndexPaths = Set<IndexPath>()
  16. private let collectionView: UICollectionView
  17. private let category: MediaLibraryBaseModel
  18. private lazy var editToolbar: VLCEditToolbar = {
  19. let editToolbar = VLCEditToolbar(frame: CGRect(x: 0, y: 400,
  20. width: collectionView.frame.width, height: 50))
  21. editToolbar.isHidden = true
  22. editToolbar.delegate = self
  23. return editToolbar
  24. }()
  25. init(collectionView: UICollectionView, category: MediaLibraryBaseModel) {
  26. self.collectionView = collectionView
  27. self.category = category
  28. super.init()
  29. collectionView.addSubview(editToolbar)
  30. collectionView.bringSubview(toFront: editToolbar)
  31. }
  32. }
  33. // MARK: - Helpers
  34. private extension VLCEditController {
  35. private func resetCell(at indexPath: IndexPath) {
  36. if let cell = collectionView.cellForItem(at: indexPath) as? MediaEditCell {
  37. cell.isChecked = false
  38. }
  39. }
  40. private func resetAllVisibleCell() {
  41. for case let cell as MediaEditCell in collectionView.visibleCells {
  42. cell.isChecked = false
  43. }
  44. }
  45. private struct TextFieldAlertInfo {
  46. var alertTitle: String
  47. var placeHolder: String
  48. var confirmActionTitle: String
  49. init(alertTitle: String = "",
  50. placeHolder: String = "",
  51. confirmActionTitle: String = NSLocalizedString("BUTTON_DONE", comment: "")) {
  52. self.alertTitle = alertTitle
  53. self.placeHolder = placeHolder
  54. self.confirmActionTitle = confirmActionTitle
  55. }
  56. }
  57. private func presentTextFieldAlert(with info: TextFieldAlertInfo,
  58. completionHandler: @escaping (String) -> Void) {
  59. let alertController = UIAlertController(title: info.alertTitle,
  60. message: "",
  61. preferredStyle: .alert)
  62. alertController.addTextField(configurationHandler: {
  63. textField in
  64. textField.placeholder = info.placeHolder
  65. })
  66. let cancelButton = UIAlertAction(title: NSLocalizedString("BUTTON_CANCEL", comment: ""),
  67. style: .default)
  68. let confirmAction = UIAlertAction(title: info.confirmActionTitle, style: .default) {
  69. [weak alertController] _ in
  70. guard let alertController = alertController,
  71. let textField = alertController.textFields?.first else { return }
  72. completionHandler(textField.text ?? "")
  73. }
  74. alertController.addAction(cancelButton)
  75. alertController.addAction(confirmAction)
  76. UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
  77. }
  78. }
  79. // MARK: - VLCEditControllerDataSource
  80. extension VLCEditController: VLCEditControllerDataSource {
  81. func toolbarNeedsUpdate(editing: Bool) {
  82. // editToolbar.isHidden = !editing
  83. if !editing {
  84. // not in editing mode anymore should reset
  85. selectedCellIndexPaths.removeAll(keepingCapacity: false)
  86. }
  87. }
  88. }
  89. // MARK: - VLCEditToolbarDelegate
  90. extension VLCEditController: VLCEditToolbarDelegate {
  91. func createPlaylist() {
  92. if let model = category as? PlaylistModel {
  93. let alertInfo = TextFieldAlertInfo(alertTitle: NSLocalizedString("VIDEO_PLAYLISTS", comment: ""),
  94. placeHolder: "NEW_PLAYLIST")
  95. presentTextFieldAlert(with: alertInfo, completionHandler: {
  96. text -> Void in
  97. model.create(name: text)
  98. })
  99. } else if let model = category as? VideoModel {
  100. let alertInfo = TextFieldAlertInfo(alertTitle: NSLocalizedString("VIDEO_PLAYLISTS", comment: ""),
  101. placeHolder: "NEW_PLAYLIST")
  102. presentTextFieldAlert(with: alertInfo, completionHandler: {
  103. [selectedCellIndexPaths, category] text -> Void in
  104. let playlist = model.medialibrary.createPlaylist(with: text)
  105. for indexPath in selectedCellIndexPaths {
  106. if let media = category.anyfiles[indexPath.row] as? VLCMLMedia {
  107. playlist.appendMedia(withIdentifier: media.identifier())
  108. }
  109. }
  110. })
  111. }
  112. }
  113. func delete() {
  114. var objectsToDelete = [VLCMLObject]()
  115. for indexPath in selectedCellIndexPaths {
  116. objectsToDelete.append(category.anyfiles[indexPath.row])
  117. }
  118. let cancelButton = VLCAlertButton(title: NSLocalizedString("BUTTON_CANCEL", comment: ""))
  119. let deleteButton = VLCAlertButton(title: NSLocalizedString("BUTTON_DELETE", comment: ""),
  120. style: .destructive,
  121. action: {
  122. [weak self] action in
  123. self?.category.delete(objectsToDelete)
  124. self?.selectedCellIndexPaths.removeAll()
  125. self?.resetAllVisibleCell()
  126. })
  127. VLCAlertViewController.alertViewManager(title: NSLocalizedString("DELETE_TITLE", comment: ""),
  128. errorMessage: NSLocalizedString("DELETE_MESSAGE", comment: ""),
  129. viewController: (UIApplication.shared.keyWindow?.rootViewController)!,
  130. buttonsAction: [cancelButton,
  131. deleteButton])
  132. }
  133. func rename() {
  134. // FIXME: Multiple renaming of files(multiple alert can get unfriendly if too many files)
  135. for indexPath in selectedCellIndexPaths {
  136. if let media = category.anyfiles[indexPath.row] as? VLCMLMedia {
  137. // Not using VLCAlertViewController to have more customization in text fields
  138. let alertInfo = TextFieldAlertInfo(alertTitle: String(format: NSLocalizedString("RENAME_MEDIA_TO", comment: ""), media.title),
  139. placeHolder: "NEW_NAME",
  140. confirmActionTitle: NSLocalizedString("BUTTON_RENAME", comment: ""))
  141. presentTextFieldAlert(with: alertInfo, completionHandler: {
  142. [weak self] text -> Void in
  143. guard text != "" else {
  144. VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_RENAME_FAILED", comment: ""),
  145. errorMessage: NSLocalizedString("ERROR_EMPTY_NAME", comment: ""),
  146. viewController: (UIApplication.shared.keyWindow?.rootViewController)!)
  147. return
  148. }
  149. media.updateTitle(text)
  150. self?.resetCell(at: indexPath)
  151. })
  152. }
  153. }
  154. }
  155. }
  156. // MARK: - UICollectionViewDataSource
  157. extension VLCEditController: UICollectionViewDataSource {
  158. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  159. return category.anyfiles.count
  160. }
  161. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  162. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: category.editCellType.defaultReuseIdentifier,
  163. for: indexPath) as? BaseCollectionViewCell {
  164. cell.media = category.anyfiles[indexPath.row]
  165. return cell
  166. }
  167. return UICollectionViewCell()
  168. }
  169. }
  170. // MARK: - UICollectionViewDelegate
  171. extension VLCEditController: UICollectionViewDelegate {
  172. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  173. if let cell = collectionView.cellForItem(at: indexPath) as? MediaEditCell {
  174. cell.isChecked = !cell.isChecked
  175. if cell.isChecked {
  176. // cell selected, saving indexPath
  177. selectedCellIndexPaths.insert(indexPath)
  178. } else {
  179. selectedCellIndexPaths.remove(indexPath)
  180. }
  181. }
  182. }
  183. }
  184. // MARK: - UICollectionViewDelegateFlowLayout
  185. extension VLCEditController: UICollectionViewDelegateFlowLayout {
  186. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  187. let contentInset = collectionView.contentInset
  188. // FIXME: 5 should be cell padding, but not usable maybe static?
  189. let insetToRemove = contentInset.left + contentInset.right + (5 * 2)
  190. return CGSize(width: collectionView.frame.width - insetToRemove, height: MediaEditCell.height)
  191. }
  192. }