VLCEditController.swift 8.5 KB

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