MainCollectionViewController.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. private let imageReuseIdentifier = "ImagePreviewCell"
  22. private let videoReuseIdentifier = "VideoPreviewCell"
  23. class MainCollectionViewController: NSObject, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  24. var delegate : MediaPreviewViewController
  25. init(delegate : MediaPreviewViewController) {
  26. self.delegate = delegate
  27. }
  28. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  29. return self.delegate.mediaData.count
  30. }
  31. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  32. return 0.0
  33. }
  34. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  35. return 0.0
  36. }
  37. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  38. return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
  39. }
  40. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  41. var cell : UICollectionViewCell?
  42. if self.delegate.mediaData[indexPath.item] is VideoPreviewItem {
  43. cell = prepareVideoItem(indexPath: indexPath, collectionView: collectionView)
  44. } else {
  45. cell = prepareImageCell(indexPath: indexPath, collectionView: collectionView)
  46. }
  47. return cell!
  48. }
  49. func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
  50. guard let indexPath = self.delegate.getCurrentlyVisibleItem() else {
  51. return
  52. }
  53. guard let cell = collectionView.cellForItem(at: indexPath) as? ImagePreviewCollectionViewCell else {
  54. return
  55. }
  56. cell.scrollView.setZoomScale(cell.scrollView.minimumZoomScale, animated: true)
  57. }
  58. var lastContentOffset : CGFloat = 0.0
  59. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  60. self.lastContentOffset = scrollView.contentOffset.x
  61. }
  62. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  63. let section = self.delegate.currentItem.section
  64. let size = self.delegate.largeCollectionView.frame.width
  65. let items = scrollView.contentOffset.x / size
  66. self.delegate.currentItem = IndexPath(item: Int(items), section: section)
  67. self.delegate.updateSelection()
  68. }
  69. func prepareImageCell(indexPath : IndexPath, collectionView : UICollectionView) -> ImagePreviewCollectionViewCell {
  70. let cell = collectionView
  71. .dequeueReusableCell(withReuseIdentifier: imageReuseIdentifier, for: indexPath) as! ImagePreviewCollectionViewCell
  72. cell.indexPath = indexPath
  73. DispatchQueue.main.async {
  74. cell.setColors()
  75. cell.addAccessibilityLabels()
  76. cell.showLoadingScreen()
  77. if cell.indexPath != indexPath {
  78. return
  79. }
  80. guard let item = self.delegate.mediaData[indexPath.item] as? ImagePreviewItem else {
  81. return
  82. }
  83. guard let data = item.getItem() else {
  84. return
  85. }
  86. if UTIConverter.isGifMimeType(UTIConverter.mimeType(fromUTI: item.uti!)) {
  87. cell.updateImageTo(data: data)
  88. } else {
  89. self.updateImageCell(data: data, cell: cell)
  90. }
  91. }
  92. return cell
  93. }
  94. func updateImageCell(data : Data, cell : ImagePreviewCollectionViewCell) {
  95. guard let image = UIImage(data: data) else {
  96. return
  97. }
  98. cell.updateImageTo(image: image)
  99. }
  100. func prepareVideoItem(indexPath : IndexPath, collectionView : UICollectionView) -> VideoImageCell {
  101. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: videoReuseIdentifier, for: indexPath) as! VideoImageCell
  102. cell.indexPath = indexPath
  103. cell.addAccessibilityLabels()
  104. cell.showLoadingScreen()
  105. self.delegate.mediaFetchQueue.async {
  106. let videoItem = self.delegate.mediaData[indexPath.item] as! VideoPreviewItem
  107. guard let asset: AVAsset = videoItem.getItem() else {
  108. cell.loadingVideoText.text = String(format: NSLocalizedString("loading_video_failed", comment: ""))
  109. cell.loadingVideoText.isHidden = false
  110. return
  111. }
  112. DispatchQueue.main.async {
  113. if cell.indexPath != indexPath {
  114. return
  115. }
  116. cell.updateVideoWithAsset(asset: asset)
  117. }
  118. }
  119. return cell
  120. }
  121. }