ThumbnailCollectionViewController.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import UIKit
  21. class ThumbnailCollectionViewController: NSObject, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
  22. var parent: MediaPreviewViewController?
  23. let mediaFetchQueue = DispatchQueue(label: "MediaDataFetchQueue",
  24. qos: .userInitiated,
  25. attributes: .concurrent,
  26. autoreleaseFrequency: .inherit,
  27. target: nil)
  28. private let reuseIdentifier = "thumbnailCell"
  29. private let collectionViewInsets : CGFloat = 10
  30. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  31. return parent?.mediaData.count ?? 0
  32. }
  33. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  34. return 5.0
  35. }
  36. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  37. return 5.0
  38. }
  39. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  40. let collectionViewHeight = collectionView.frame.height
  41. let width = collectionViewHeight - collectionViewInsets
  42. let height = collectionViewHeight - collectionViewInsets
  43. return CGSize(width: width, height: height)
  44. }
  45. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  46. let cell = collectionView
  47. .dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ThumbnailCollectionViewCell
  48. cell.startLoading()
  49. cell.identifier = indexPath
  50. self.mediaFetchQueue.async {
  51. self.parent?.mediaData[indexPath.item].getThumbnail(onCompletion: { image in
  52. self.imageHandler(image: image, cell : cell, indexPath: indexPath)
  53. })
  54. }
  55. return cell
  56. }
  57. func imageHandler(image : UIImage, cell : ThumbnailCollectionViewCell, indexPath : IndexPath) {
  58. if cell.identifier != indexPath {
  59. // Cell has been reused do not update image
  60. return
  61. }
  62. DispatchQueue.main.async {
  63. cell.loadImage(image: image)
  64. }
  65. }
  66. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  67. parent!.largeCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
  68. parent!.updateTextForIndex(indexPath: indexPath, animated: true)
  69. parent!.currentItem = indexPath
  70. collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
  71. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kMediaPreviewPauseVideo), object: nil)
  72. }
  73. func removeItem(at indexPath: IndexPath) {
  74. _ = parent?.mediaData.remove(at: indexPath.item)
  75. parent?.largeCollectionView.reloadData()
  76. parent?.updateSelection()
  77. }
  78. func insertItem(_ item: MediaPreviewItem, at indexPath: IndexPath) {
  79. parent?.mediaData.insert(item, at: indexPath.item)
  80. parent?.largeCollectionView.reloadData()
  81. parent?.updateSelection()
  82. }
  83. }
  84. @available(iOS 11.0, *)
  85. extension ThumbnailCollectionViewController: UICollectionViewDragDelegate {
  86. func collectionView(_ collectionView: UICollectionView, dragSessionIsRestrictedToDraggingApplication session: UIDragSession) -> Bool {
  87. return true
  88. }
  89. func collectionView(_ collectionView: UICollectionView,
  90. itemsForBeginning session: UIDragSession,
  91. at indexPath: IndexPath) -> [UIDragItem] {
  92. let cell = collectionView.cellForItem(at: indexPath) as! ThumbnailCollectionViewCell
  93. guard let image = cell.imageView.image else {
  94. return []
  95. }
  96. let item = NSItemProvider(object: image)
  97. let dragItem = UIDragItem(itemProvider: item)
  98. return [dragItem]
  99. }
  100. }
  101. @available(iOS 11.0, *)
  102. extension ThumbnailCollectionViewController: UICollectionViewDropDelegate {
  103. func collectionView(
  104. _ collectionView: UICollectionView,
  105. dropSessionDidUpdate session: UIDropSession,
  106. withDestinationIndexPath destinationIndexPath: IndexPath?)
  107. -> UICollectionViewDropProposal {
  108. return UICollectionViewDropProposal(
  109. operation: .move,
  110. intent: .insertAtDestinationIndexPath)
  111. }
  112. func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
  113. guard let destinationIndexPath = coordinator.destinationIndexPath else {
  114. return
  115. }
  116. coordinator.items.forEach { dropItem in
  117. guard let sourceIndexPath = dropItem.sourceIndexPath else {
  118. return
  119. }
  120. collectionView.performBatchUpdates({
  121. let item = parent?.mediaData[sourceIndexPath.item]
  122. removeItem(at: sourceIndexPath)
  123. insertItem(item!, at: destinationIndexPath)
  124. collectionView.deleteItems(at: [sourceIndexPath])
  125. collectionView.insertItems(at: [destinationIndexPath])
  126. }, completion: { _ in
  127. coordinator.drop(dropItem.dragItem,
  128. toItemAt: destinationIndexPath)
  129. })
  130. if self.parent?.currentItem == sourceIndexPath {
  131. self.parent?.currentItem = destinationIndexPath
  132. } else {
  133. if destinationIndexPath < self.parent!.currentItem && sourceIndexPath > self.parent!.currentItem {
  134. self.parent?.currentItem = IndexPath(item: min(self.parent!.currentItem.item + 1, self.parent!.mediaData.count - 1), section: 0)
  135. } else if destinationIndexPath > self.parent!.currentItem && sourceIndexPath < self.parent!.currentItem {
  136. self.parent?.currentItem = IndexPath(item: max(self.parent!.currentItem.item - 1, 0), section: 0)
  137. } else if destinationIndexPath == self.parent!.currentItem {
  138. if sourceIndexPath < self.parent!.currentItem {
  139. self.parent?.currentItem = IndexPath(item: max(self.parent!.currentItem.item - 1, 0), section: 0)
  140. } else {
  141. self.parent?.currentItem = IndexPath(item: min(self.parent!.currentItem.item + 1, self.parent!.mediaData.count - 1), section: 0)
  142. }
  143. }
  144. }
  145. self.parent?.largeCollectionView.selectItem(at: self.parent?.currentItem, animated: true, scrollPosition: .centeredHorizontally)
  146. self.parent?.updateSelection()
  147. self.parent?.shouldScrollTo(indexPath: self.parent!.currentItem)
  148. }
  149. }
  150. func collectionView(_ collectionView: UICollectionView,
  151. canHandle session: UIDropSession) -> Bool {
  152. return true
  153. }
  154. }