VLCEditController.swift 5.4 KB

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