VLCEditController.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. }
  49. }
  50. extension VLCEditController: UICollectionViewDataSource {
  51. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  52. return category.anyfiles.count
  53. }
  54. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  55. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: VLCMediaViewEditCell.identifier,
  56. for: indexPath) as? VLCMediaViewEditCell {
  57. if let media = category.anyfiles[indexPath.row] as? VLCMLMedia {
  58. cell.titleLabel.text = media.title
  59. cell.subInfoLabel.text = media.formatDuration()
  60. cell.sizeLabel.text = media.formatSize()
  61. }
  62. return cell
  63. }
  64. return UICollectionViewCell()
  65. }
  66. }
  67. extension VLCEditController: UICollectionViewDelegate {
  68. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  69. if let cell = collectionView.cellForItem(at: indexPath) as? VLCMediaViewEditCell {
  70. cell.checkView.isEnabled = !cell.checkView.isEnabled
  71. if cell.checkView.isEnabled {
  72. // cell selected, saving indexPath
  73. selectedCellIndexPaths.insert(indexPath)
  74. } else {
  75. selectedCellIndexPaths.remove(indexPath)
  76. }
  77. }
  78. }
  79. }
  80. extension VLCEditController: UICollectionViewDelegateFlowLayout {
  81. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  82. let contentInset = collectionView.contentInset
  83. // FIXME: 5 should be cell padding, but not usable maybe static?
  84. let insetToRemove = contentInset.left + contentInset.right + (5 * 2)
  85. return CGSize(width: collectionView.frame.width - insetToRemove, height: VLCMediaViewEditCell.height)
  86. }
  87. }