metatext-app-ios-iphone-ipad/Views/Attachments/AttachmentsView.swift

77 lines
2.7 KiB
Swift
Raw Normal View History

2020-08-28 21:56:28 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import UIKit
2020-09-01 09:33:49 +02:00
import ViewModels
2020-08-28 21:56:28 +02:00
2020-09-08 04:35:28 +02:00
final class AttachmentsView: UIView {
2020-08-28 21:56:28 +02:00
private let containerStackView = UIStackView()
private let leftStackView = UIStackView()
private let rightStackView = UIStackView()
var attachmentViewModels = [AttachmentViewModel]() {
didSet {
for stackView in [leftStackView, rightStackView] {
for view in stackView.arrangedSubviews {
stackView.removeArrangedSubview(view)
view.removeFromSuperview()
}
}
let attachmentCount = attachmentViewModels.count
rightStackView.isHidden = attachmentCount == 1
for (index, viewModel) in attachmentViewModels.enumerated() {
if attachmentCount == 2 && index == 1
|| attachmentCount == 3 && index != 0
|| attachmentCount > 3 && index % 2 != 0 {
rightStackView.addArrangedSubview(AttachmentView(viewModel: viewModel))
} else {
leftStackView.addArrangedSubview(AttachmentView(viewModel: viewModel))
}
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
initializationActions()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
initializationActions()
}
}
private extension AttachmentsView {
func initializationActions() {
backgroundColor = .clear
layoutMargins = .zero
clipsToBounds = true
2020-09-29 03:32:28 +02:00
layer.cornerRadius = .defaultCornerRadius
2020-08-28 21:56:28 +02:00
addSubview(containerStackView)
containerStackView.translatesAutoresizingMaskIntoConstraints = false
containerStackView.distribution = .fillEqually
2020-09-29 03:32:28 +02:00
containerStackView.spacing = .compactSpacing
2020-08-28 21:56:28 +02:00
leftStackView.distribution = .fillEqually
2020-09-29 03:32:28 +02:00
leftStackView.spacing = .compactSpacing
2020-08-28 21:56:28 +02:00
leftStackView.axis = .vertical
rightStackView.distribution = .fillEqually
2020-09-29 03:32:28 +02:00
rightStackView.spacing = .compactSpacing
2020-08-28 21:56:28 +02:00
rightStackView.axis = .vertical
containerStackView.addArrangedSubview(leftStackView)
containerStackView.addArrangedSubview(rightStackView)
2020-09-29 03:32:28 +02: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 21:56:28 +02:00
}
}