[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

//
// AttachmentsView.swift
// elpha-ios
//
// Created by Dwayne Harris on 11/28/18.
// Copyright © 2018 Elpha. All rights reserved.
//
import UIKit
protocol AttachmentsViewDelegate: class {
func attachmentTapped(_ sender: Any, index: Int)
}
class AttachmentsView: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet var attachmentsCollectionView: UICollectionView!
var attachments: [AttachmentMO]?
weak var delegate: AttachmentsViewDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
private func setup() {
Bundle.main.loadNibNamed("AttachmentsView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleWidth]
attachmentsCollectionView.dataSource = self
attachmentsCollectionView.delegate = self
attachmentsCollectionView.register(UINib(nibName: "ImageAttachmentCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ImageAttachmentCollectionViewCell")
attachmentsCollectionView.register(UINib(nibName: "VideoAttachmentCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "VideoAttachmentCollectionViewCell")
}
func update(withAttachments attachments: [AttachmentMO]) {
self.attachments = Array(attachments.prefix(4))
attachmentsCollectionView.reloadData()
attachmentsCollectionView.collectionViewLayout.invalidateLayout()
}
}
extension AttachmentsView: UICollectionViewDelegate, UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return attachments?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let attachment = attachments?[indexPath.row] else {
fatalError("")
}
let attachmentType = AttachmentType(rawValue: attachment.type!)!
switch attachmentType {
case .gifv, .video:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "VideoAttachmentCollectionViewCell", for: indexPath) as! VideoAttachmentCollectionViewCell
cell.update(withAttachment: attachment)
return cell
default:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageAttachmentCollectionViewCell", for: indexPath) as! ImageAttachmentCollectionViewCell
cell.update(withAttachment: attachment)
return cell
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.attachmentTapped(self, index: indexPath.row)
}
}
extension AttachmentsView: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
print("collectionView sizeForItemAt indexPath: \(indexPath)")
let halfWidth = (frame.size.width / 2) - 2
let halfHeight = (frame.size.height / 2) - 2
let tallSize = CGSize(width: halfWidth, height: frame.size.height)
let smallSize = CGSize(width: halfWidth, height: halfHeight)
switch attachments?.count ?? 0 {
case 1: return CGSize(width: frame.size.width, height: frame.size.height)
case 2: return tallSize
case 3: return indexPath.row == 0 ? tallSize : smallSize
case 4: return smallSize
default: return CGSize.zero
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 4
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets.zero
}
}