VLCEditController.swift 9.7 KB

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