VLCEditController.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 let collectionView: UICollectionView
  16. private let category: MediaLibraryBaseModel
  17. private lazy var editToolbar: VLCEditToolbar = {
  18. let editToolbar = VLCEditToolbar(frame: CGRect(x: 0, y: 550,
  19. width: collectionView.frame.width, height: 50))
  20. editToolbar.isHidden = true
  21. editToolbar.delegate = self
  22. return editToolbar
  23. }()
  24. init(collectionView: UICollectionView, category: MediaLibraryBaseModel) {
  25. self.collectionView = collectionView
  26. self.category = category
  27. super.init()
  28. collectionView.addSubview(editToolbar)
  29. collectionView.bringSubview(toFront: editToolbar)
  30. }
  31. }
  32. extension VLCEditController: VLCEditControllerDataSource {
  33. func toolbarNeedsUpdate(editing: Bool) {
  34. editToolbar.isHidden = !editing
  35. }
  36. }
  37. extension VLCEditController: VLCEditToolbarDelegate {
  38. func createPlaylist() {
  39. }
  40. func delete() {
  41. }
  42. func rename() {
  43. }
  44. }
  45. extension VLCEditController: UICollectionViewDataSource {
  46. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  47. return category.anyfiles.count
  48. }
  49. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  50. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: VLCMediaViewEditCell.identifier,
  51. for: indexPath) as? VLCMediaViewEditCell {
  52. if let media = category.anyfiles[indexPath.row] as? VLCMLMedia {
  53. cell.titleLabel.text = media.title
  54. cell.subInfoLabel.text = media.formatDuration()
  55. cell.sizeLabel.text = media.formatSize()
  56. }
  57. return cell
  58. }
  59. return UICollectionViewCell()
  60. }
  61. }
  62. extension VLCEditController: UICollectionViewDelegate {
  63. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  64. if let cell = collectionView.cellForItem(at: indexPath) as? VLCMediaViewEditCell {
  65. cell.checkView.isEnabled = !cell.checkView.isEnabled
  66. }
  67. }
  68. }
  69. extension VLCEditController: UICollectionViewDelegateFlowLayout {
  70. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  71. let contentInset = collectionView.contentInset
  72. // FIXME: 5 should be cell padding, but not usable maybe static?
  73. let insetToRemove = contentInset.left + contentInset.right + (5 * 2)
  74. return CGSize(width: collectionView.frame.width - insetToRemove, height: VLCMediaViewEditCell.height)
  75. }
  76. }