VLCEditController.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. }
  42. extension VLCEditController: VLCEditControllerDataSource {
  43. func toolbarNeedsUpdate(editing: Bool) {
  44. editToolbar.isHidden = !editing
  45. if !editing {
  46. // not in editing mode anymore should reset
  47. selectedCellIndexPaths.removeAll(keepingCapacity: false)
  48. }
  49. }
  50. }
  51. extension VLCEditController: VLCEditToolbarDelegate {
  52. func createPlaylist() {
  53. }
  54. func delete() {
  55. }
  56. func rename() {
  57. for indexPath in selectedCellIndexPaths {
  58. if let media = category.anyfiles[indexPath.row] as? VLCMLMedia {
  59. // Not using VLCAlertViewController to have more customization in text fields
  60. let alertController = UIAlertController(title: String(format: NSLocalizedString("RENAME_MEDIA_TO", comment: ""), media.title),
  61. message: "",
  62. preferredStyle: .alert)
  63. alertController.addTextField(configurationHandler: {
  64. textField in
  65. textField.placeholder = NSLocalizedString("NEW_NAME", comment: "")
  66. })
  67. let cancelButton = UIAlertAction(title: NSLocalizedString("BUTTON_CANCEL", comment: ""),
  68. style: .default)
  69. let confirmAction = UIAlertAction(title: NSLocalizedString("BUTTON_DONE", comment: ""), style: .default) {
  70. [weak alertController, weak self] _ in
  71. guard let alertController = alertController,
  72. let textField = alertController.textFields?.first else { return }
  73. media.updateTitle(textField.text)
  74. self?.resetCell(at: indexPath)
  75. }
  76. alertController.addAction(cancelButton)
  77. alertController.addAction(confirmAction)
  78. UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
  79. }
  80. }
  81. }
  82. }
  83. extension VLCEditController: UICollectionViewDataSource {
  84. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  85. return category.anyfiles.count
  86. }
  87. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  88. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: VLCMediaViewEditCell.identifier,
  89. for: indexPath) as? VLCMediaViewEditCell {
  90. if let media = category.anyfiles[indexPath.row] as? VLCMLMedia {
  91. cell.titleLabel.text = media.title
  92. cell.subInfoLabel.text = media.formatDuration()
  93. cell.sizeLabel.text = media.formatSize()
  94. }
  95. return cell
  96. }
  97. return UICollectionViewCell()
  98. }
  99. }
  100. extension VLCEditController: UICollectionViewDelegate {
  101. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  102. if let cell = collectionView.cellForItem(at: indexPath) as? VLCMediaViewEditCell {
  103. cell.checkView.isEnabled = !cell.checkView.isEnabled
  104. if cell.checkView.isEnabled {
  105. // cell selected, saving indexPath
  106. selectedCellIndexPaths.insert(indexPath)
  107. } else {
  108. selectedCellIndexPaths.remove(indexPath)
  109. }
  110. }
  111. }
  112. }
  113. extension VLCEditController: UICollectionViewDelegateFlowLayout {
  114. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  115. let contentInset = collectionView.contentInset
  116. // FIXME: 5 should be cell padding, but not usable maybe static?
  117. let insetToRemove = contentInset.left + contentInset.right + (5 * 2)
  118. return CGSize(width: collectionView.frame.width - insetToRemove, height: VLCMediaViewEditCell.height)
  119. }
  120. }