2019-06-16 23:02:18 +00:00
|
|
|
//
|
|
|
|
// AttachmentsContainerView.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 6/16/19.
|
|
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Pachyderm
|
|
|
|
|
|
|
|
class AttachmentsContainerView: UIView {
|
|
|
|
|
|
|
|
var delegate: AttachmentViewDelegate?
|
|
|
|
|
2019-06-17 02:39:46 +00:00
|
|
|
let attachmentViews: NSHashTable<AttachmentView> = .weakObjects()
|
|
|
|
|
2019-06-16 23:02:18 +00:00
|
|
|
override func awakeFromNib() {
|
|
|
|
super.awakeFromNib()
|
|
|
|
|
|
|
|
self.isUserInteractionEnabled = true
|
|
|
|
}
|
|
|
|
|
2019-06-17 02:39:46 +00:00
|
|
|
func getAttachmentView(for attachment: Attachment) -> AttachmentView? {
|
|
|
|
return attachmentViews.allObjects.first { $0.attachment.id == attachment.id }
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - User Interaface
|
|
|
|
|
2019-06-16 23:02:18 +00:00
|
|
|
func updateUI(status: Status) {
|
|
|
|
let attachments = status.attachments.filter { $0.kind == .image }
|
|
|
|
|
2019-06-17 02:39:46 +00:00
|
|
|
attachmentViews.removeAllObjects()
|
|
|
|
subviews.forEach { $0.removeFromSuperview() }
|
|
|
|
|
2019-06-16 23:02:18 +00:00
|
|
|
if attachments.count > 0 {
|
|
|
|
self.isHidden = false
|
|
|
|
|
|
|
|
switch attachments.count {
|
|
|
|
case 1:
|
2019-06-17 02:39:46 +00:00
|
|
|
makeMainView(createAttachmentView(attachments[0]))
|
2019-06-16 23:02:18 +00:00
|
|
|
case 2:
|
2019-06-17 02:39:46 +00:00
|
|
|
let left = createAttachmentView(attachments[0])
|
|
|
|
makeMainView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [
|
|
|
|
left,
|
2019-06-16 23:02:18 +00:00
|
|
|
createAttachmentView(attachments[1])
|
2019-06-17 02:39:46 +00:00
|
|
|
]))
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
left.halfWidth()
|
2019-06-16 23:02:18 +00:00
|
|
|
])
|
|
|
|
case 3:
|
2019-06-17 02:39:46 +00:00
|
|
|
let left = createAttachmentView(attachments[0])
|
|
|
|
let topRight = createAttachmentView(attachments[1])
|
|
|
|
makeMainView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [
|
|
|
|
left,
|
2019-06-16 23:02:18 +00:00
|
|
|
createAttachmentsStack(axis: .vertical, arrangedSubviews: [
|
2019-06-17 02:39:46 +00:00
|
|
|
topRight,
|
2019-06-16 23:02:18 +00:00
|
|
|
createAttachmentView(attachments[2])
|
|
|
|
])
|
2019-06-17 02:39:46 +00:00
|
|
|
]))
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
left.halfWidth(),
|
|
|
|
topRight.halfHeight(),
|
2019-06-16 23:02:18 +00:00
|
|
|
])
|
|
|
|
case 4:
|
2019-06-17 02:39:46 +00:00
|
|
|
let topLeft = createAttachmentView(attachments[0])
|
|
|
|
let left = createAttachmentsStack(axis: .vertical, arrangedSubviews: [
|
|
|
|
topLeft,
|
|
|
|
createAttachmentView(attachments[2])
|
|
|
|
])
|
|
|
|
let topRight = createAttachmentView(attachments[1])
|
|
|
|
makeMainView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [
|
|
|
|
left,
|
2019-06-16 23:02:18 +00:00
|
|
|
createAttachmentsStack(axis: .vertical, arrangedSubviews: [
|
2019-06-17 02:39:46 +00:00
|
|
|
topRight,
|
2019-06-16 23:02:18 +00:00
|
|
|
createAttachmentView(attachments[3])
|
|
|
|
])
|
2019-06-17 02:39:46 +00:00
|
|
|
]))
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
left.halfWidth(),
|
|
|
|
topLeft.halfHeight(),
|
|
|
|
topRight.halfHeight(),
|
2019-06-16 23:02:18 +00:00
|
|
|
])
|
|
|
|
default:
|
|
|
|
fatalError("Too many attachments")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.isHidden = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func createAttachmentView(_ attachment: Attachment) -> AttachmentView {
|
|
|
|
let attachmentView = AttachmentView(attachment: attachment)
|
|
|
|
attachmentView.delegate = delegate
|
|
|
|
attachmentView.translatesAutoresizingMaskIntoConstraints = false
|
2019-06-17 02:39:46 +00:00
|
|
|
attachmentViews.add(attachmentView)
|
2019-06-16 23:02:18 +00:00
|
|
|
return attachmentView
|
|
|
|
}
|
|
|
|
|
|
|
|
private func createAttachmentsStack(axis: NSLayoutConstraint.Axis, arrangedSubviews: [UIView]) -> UIStackView {
|
|
|
|
let stack = UIStackView(arrangedSubviews: arrangedSubviews)
|
|
|
|
stack.axis = axis
|
2019-06-17 02:39:46 +00:00
|
|
|
stack.spacing = 4
|
2019-06-16 23:02:18 +00:00
|
|
|
stack.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
return stack
|
|
|
|
}
|
2019-06-17 02:39:46 +00:00
|
|
|
|
|
|
|
private func makeMainView(_ view: UIView) {
|
|
|
|
addSubview(view)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
view.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
|
|
view.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
|
|
view.topAnchor.constraint(equalTo: topAnchor),
|
|
|
|
view.bottomAnchor.constraint(equalTo: bottomAnchor)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-06-16 23:02:18 +00:00
|
|
|
|
2019-06-17 02:39:46 +00:00
|
|
|
fileprivate extension UIView {
|
|
|
|
enum RelativeSize {
|
|
|
|
case full, half
|
|
|
|
|
|
|
|
var multiplier: CGFloat {
|
|
|
|
switch self {
|
|
|
|
case .full:
|
|
|
|
return 1
|
|
|
|
case .half:
|
|
|
|
return 0.5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func halfWidth(spacing: CGFloat = 4) -> NSLayoutConstraint {
|
|
|
|
return widthAnchor.constraint(equalTo: superview!.widthAnchor, multiplier: 0.5, constant: -spacing / 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func halfHeight(spacing: CGFloat = 4) -> NSLayoutConstraint {
|
|
|
|
return heightAnchor.constraint(equalTo: superview!.heightAnchor, multiplier: 0.5, constant: -spacing / 2)
|
|
|
|
}
|
2019-06-16 23:02:18 +00:00
|
|
|
}
|