metatext/Views/Status/StatusAttachmentsView.swift

92 lines
3.4 KiB
Swift
Raw Normal View History

2020-08-28 19:56:28 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import UIKit
2020-09-01 07:33:49 +00:00
import ViewModels
2020-08-28 19:56:28 +00:00
2020-10-11 21:03:25 +00:00
final class StatusAttachmentsView: UIView {
2020-08-28 19:56:28 +00:00
private let containerStackView = UIStackView()
private let leftStackView = UIStackView()
private let rightStackView = UIStackView()
2020-10-12 05:37:34 +00:00
private var aspectRatioConstraint: NSLayoutConstraint?
2020-08-28 19:56:28 +00:00
2020-10-13 20:18:49 +00:00
var viewModel: StatusViewModel? {
2020-08-28 19:56:28 +00:00
didSet {
for stackView in [leftStackView, rightStackView] {
for view in stackView.arrangedSubviews {
stackView.removeArrangedSubview(view)
view.removeFromSuperview()
}
}
2020-10-13 20:18:49 +00:00
let attachmentViewModels = viewModel?.attachmentViewModels ?? []
2020-08-28 19:56:28 +00:00
let attachmentCount = attachmentViewModels.count
rightStackView.isHidden = attachmentCount == 1
for (index, viewModel) in attachmentViewModels.enumerated() {
2020-10-11 21:03:25 +00:00
let attachmentView = StatusAttachmentView(viewModel: viewModel)
2020-08-28 19:56:28 +00:00
if attachmentCount == 2 && index == 1
|| attachmentCount == 3 && index != 0
|| attachmentCount > 3 && index % 2 != 0 {
2020-10-11 21:03:25 +00:00
rightStackView.addArrangedSubview(attachmentView)
2020-08-28 19:56:28 +00:00
} else {
2020-10-11 21:03:25 +00:00
leftStackView.addArrangedSubview(attachmentView)
2020-08-28 19:56:28 +00:00
}
}
2020-10-12 05:37:34 +00:00
let newAspectRatio: CGFloat
2020-10-13 20:18:49 +00:00
if attachmentCount == 1, let aspectRatio = attachmentViewModels.first?.aspectRatio {
2020-10-12 05:37:34 +00:00
newAspectRatio = max(CGFloat(aspectRatio), 16 / 9)
} else {
newAspectRatio = 16 / 9
}
aspectRatioConstraint?.isActive = false
aspectRatioConstraint = widthAnchor.constraint(equalTo: heightAnchor, multiplier: newAspectRatio)
aspectRatioConstraint?.priority = .justBelowMax
aspectRatioConstraint?.isActive = true
2020-08-28 19:56:28 +00:00
}
}
override init(frame: CGRect) {
super.init(frame: frame)
2020-10-13 20:11:27 +00:00
initialSetup()
2020-08-28 19:56:28 +00:00
}
2020-10-13 20:11:27 +00:00
@available(*, unavailable)
2020-08-28 19:56:28 +00:00
required init?(coder: NSCoder) {
2020-10-13 20:11:27 +00:00
fatalError("init(coder:) has not been implemented")
2020-08-28 19:56:28 +00:00
}
}
2020-10-11 21:03:25 +00:00
private extension StatusAttachmentsView {
2020-10-13 20:11:27 +00:00
func initialSetup() {
2020-08-28 19:56:28 +00:00
backgroundColor = .clear
layoutMargins = .zero
clipsToBounds = true
2020-09-29 01:32:28 +00:00
layer.cornerRadius = .defaultCornerRadius
2020-08-28 19:56:28 +00:00
addSubview(containerStackView)
containerStackView.translatesAutoresizingMaskIntoConstraints = false
containerStackView.distribution = .fillEqually
2020-09-29 01:32:28 +00:00
containerStackView.spacing = .compactSpacing
2020-08-28 19:56:28 +00:00
leftStackView.distribution = .fillEqually
2020-09-29 01:32:28 +00:00
leftStackView.spacing = .compactSpacing
2020-08-28 19:56:28 +00:00
leftStackView.axis = .vertical
rightStackView.distribution = .fillEqually
2020-09-29 01:32:28 +00:00
rightStackView.spacing = .compactSpacing
2020-08-28 19:56:28 +00:00
rightStackView.axis = .vertical
containerStackView.addArrangedSubview(leftStackView)
containerStackView.addArrangedSubview(rightStackView)
2020-09-29 01:32:28 +00:00
NSLayoutConstraint.activate([
containerStackView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
containerStackView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
containerStackView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
containerStackView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
])
2020-08-28 19:56:28 +00:00
}
}