[ABANDONED] Mastodon iOS client.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
4.4 KiB

6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
  1. //
  2. // AttachmentsView.swift
  3. // elpha-ios
  4. //
  5. // Created by Dwayne Harris on 11/28/18.
  6. // Copyright © 2018 Elpha. All rights reserved.
  7. //
  8. import UIKit
  9. protocol AttachmentsViewDelegate: class {
  10. func attachmentTapped(_ sender: Any, index: Int)
  11. }
  12. class AttachmentsView: UIView {
  13. @IBOutlet var contentView: UIView!
  14. @IBOutlet var attachmentsCollectionView: UICollectionView!
  15. var attachments: [AttachmentMO]?
  16. weak var delegate: AttachmentsViewDelegate?
  17. override init(frame: CGRect) {
  18. super.init(frame: frame)
  19. setup()
  20. }
  21. required init?(coder aDecoder: NSCoder) {
  22. super.init(coder: aDecoder)
  23. setup()
  24. }
  25. private func setup() {
  26. Bundle.main.loadNibNamed("AttachmentsView", owner: self, options: nil)
  27. addSubview(contentView)
  28. contentView.frame = self.bounds
  29. contentView.autoresizingMask = [.flexibleWidth]
  30. attachmentsCollectionView.dataSource = self
  31. attachmentsCollectionView.delegate = self
  32. attachmentsCollectionView.register(UINib(nibName: "ImageAttachmentCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ImageAttachmentCollectionViewCell")
  33. attachmentsCollectionView.register(UINib(nibName: "VideoAttachmentCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "VideoAttachmentCollectionViewCell")
  34. }
  35. func update(withAttachments attachments: [AttachmentMO]) {
  36. self.attachments = Array(attachments.prefix(4))
  37. attachmentsCollectionView.reloadData()
  38. attachmentsCollectionView.collectionViewLayout.invalidateLayout()
  39. }
  40. }
  41. extension AttachmentsView: UICollectionViewDelegate, UICollectionViewDataSource {
  42. func numberOfSections(in collectionView: UICollectionView) -> Int {
  43. return 1
  44. }
  45. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  46. return attachments?.count ?? 0
  47. }
  48. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  49. guard let attachment = attachments?[indexPath.row] else {
  50. fatalError("")
  51. }
  52. let attachmentType = AttachmentType(rawValue: attachment.type!)!
  53. switch attachmentType {
  54. case .gifv, .video:
  55. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "VideoAttachmentCollectionViewCell", for: indexPath) as! VideoAttachmentCollectionViewCell
  56. cell.update(withAttachment: attachment)
  57. return cell
  58. default:
  59. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageAttachmentCollectionViewCell", for: indexPath) as! ImageAttachmentCollectionViewCell
  60. cell.update(withAttachment: attachment)
  61. return cell
  62. }
  63. }
  64. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  65. delegate?.attachmentTapped(self, index: indexPath.row)
  66. }
  67. }
  68. extension AttachmentsView: UICollectionViewDelegateFlowLayout {
  69. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  70. print("collectionView sizeForItemAt indexPath: \(indexPath)")
  71. let halfWidth = (frame.size.width / 2) - 2
  72. let halfHeight = (frame.size.height / 2) - 2
  73. let tallSize = CGSize(width: halfWidth, height: frame.size.height)
  74. let smallSize = CGSize(width: halfWidth, height: halfHeight)
  75. switch attachments?.count ?? 0 {
  76. case 1: return CGSize(width: frame.size.width, height: frame.size.height)
  77. case 2: return tallSize
  78. case 3: return indexPath.row == 0 ? tallSize : smallSize
  79. case 4: return smallSize
  80. default: return CGSize.zero
  81. }
  82. }
  83. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  84. return 0
  85. }
  86. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  87. return 4
  88. }
  89. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  90. return UIEdgeInsets.zero
  91. }
  92. }