VLCEditController.swift 7.4 KB

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