metatext-app-ios-iphone-ipad/Views/UIKit/ConversationAvatarsView.swift

89 lines
2.9 KiB
Swift
Raw Normal View History

2020-10-29 07:03:45 +01:00
// Copyright © 2020 Metabolist. All rights reserved.
2021-02-23 00:59:33 +01:00
import SDWebImage
2020-10-29 07:03:45 +01:00
import UIKit
import ViewModels
final class ConversationAvatarsView: UIView {
private let leftStackView = UIStackView()
private let rightStackView = UIStackView()
var viewModel: ConversationViewModel? {
didSet {
for stackView in [leftStackView, rightStackView] {
for view in stackView.arrangedSubviews {
stackView.removeArrangedSubview(view)
view.removeFromSuperview()
}
}
let accountViewModels = viewModel?.accountViewModels ?? []
let accountCount = accountViewModels.count
rightStackView.isHidden = accountCount == 1
for (index, accountViewModel) in accountViewModels.enumerated() {
2021-02-23 00:59:33 +01:00
let imageView = SDAnimatedImageView()
2020-10-29 07:03:45 +01:00
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
2021-02-23 00:59:33 +01:00
imageView.sd_setImage(with: accountViewModel.avatarURL())
2020-10-29 07:03:45 +01:00
if accountCount == 2 && index == 1
|| accountCount == 3 && index != 0
|| accountCount > 3 && index % 2 != 0 {
rightStackView.addArrangedSubview(imageView)
} else {
leftStackView.addArrangedSubview(imageView)
}
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
initialSetup()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = bounds.height / 2
}
}
private extension ConversationAvatarsView {
func initialSetup() {
backgroundColor = .clear
clipsToBounds = true
let containerStackView = UIStackView()
addSubview(containerStackView)
containerStackView.translatesAutoresizingMaskIntoConstraints = false
containerStackView.distribution = .fillEqually
containerStackView.spacing = .ultraCompactSpacing
leftStackView.distribution = .fillEqually
leftStackView.spacing = .ultraCompactSpacing
leftStackView.axis = .vertical
rightStackView.distribution = .fillEqually
rightStackView.spacing = .ultraCompactSpacing
rightStackView.axis = .vertical
containerStackView.addArrangedSubview(leftStackView)
containerStackView.addArrangedSubview(rightStackView)
NSLayoutConstraint.activate([
containerStackView.leadingAnchor.constraint(equalTo: leadingAnchor),
containerStackView.trailingAnchor.constraint(equalTo: trailingAnchor),
containerStackView.topAnchor.constraint(equalTo: topAnchor),
containerStackView.bottomAnchor.constraint(equalTo: bottomAnchor)
])
}
}