VLCEditController.swift 6.9 KB

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