metatext/Views/CompositionAttachmentView.swift

113 lines
4.6 KiB
Swift
Raw Normal View History

2020-12-19 06:30:19 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Kingfisher
import UIKit
import ViewModels
class CompositionAttachmentView: UIView {
let imageView = UIImageView()
2021-01-03 01:22:17 +00:00
let removeButton = UIButton()
let editButton = UIButton()
2020-12-19 06:30:19 +00:00
private var compositionAttachmentConfiguration: CompositionAttachmentContentConfiguration
2021-01-03 01:22:17 +00:00
private var aspectRatioConstraint: NSLayoutConstraint
2020-12-19 06:30:19 +00:00
init(configuration: CompositionAttachmentContentConfiguration) {
self.compositionAttachmentConfiguration = configuration
2021-01-03 01:22:17 +00:00
aspectRatioConstraint = imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor, multiplier: 2)
2020-12-19 06:30:19 +00:00
super.init(frame: .zero)
initialSetup()
applyCompositionAttachmentConfiguration()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension CompositionAttachmentView: UIContentView {
var configuration: UIContentConfiguration {
get { compositionAttachmentConfiguration }
set {
guard let compositionAttachmentConfiguration = newValue as? CompositionAttachmentContentConfiguration
else { return }
self.compositionAttachmentConfiguration = compositionAttachmentConfiguration
applyCompositionAttachmentConfiguration()
}
}
}
private extension CompositionAttachmentView {
2021-01-03 01:22:17 +00:00
// swiftlint:disable:next function_body_length
2020-12-19 06:30:19 +00:00
func initialSetup() {
2021-01-03 01:22:17 +00:00
backgroundColor = .secondarySystemBackground
layer.cornerRadius = .defaultCornerRadius
clipsToBounds = true
2020-12-19 06:30:19 +00:00
addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
2021-01-03 01:22:17 +00:00
imageView.kf.indicatorType = .activity
addSubview(removeButton)
removeButton.translatesAutoresizingMaskIntoConstraints = false
removeButton.setImage(
UIImage(
systemName: "xmark.circle.fill",
withConfiguration: UIImage.SymbolConfiguration(scale: .large)),
for: .normal)
removeButton.showsMenuAsPrimaryAction = true
removeButton.menu = UIMenu(
children: [
UIAction(
2021-01-03 21:55:04 +00:00
title: NSLocalizedString("remove", comment: ""),
image: UIImage(systemName: "trash"),
2021-01-03 01:22:17 +00:00
attributes: .destructive, handler: { [weak self] _ in
guard let self = self else { return }
self.compositionAttachmentConfiguration.parentViewModel.remove(
attachmentViewModel: self.compositionAttachmentConfiguration.viewModel)
})])
addSubview(editButton)
editButton.translatesAutoresizingMaskIntoConstraints = false
editButton.setImage(
UIImage(
systemName: "pencil.circle.fill",
withConfiguration: UIImage.SymbolConfiguration(scale: .large)),
for: .normal)
editButton.addAction(UIAction { [weak self] _ in }, for: .touchUpInside)
2020-12-19 06:30:19 +00:00
NSLayoutConstraint.activate([
2021-01-03 01:22:17 +00:00
aspectRatioConstraint,
2020-12-19 06:30:19 +00:00
imageView.leadingAnchor.constraint(equalTo: leadingAnchor),
imageView.topAnchor.constraint(equalTo: topAnchor),
imageView.trailingAnchor.constraint(equalTo: trailingAnchor),
2021-01-03 01:22:17 +00:00
imageView.bottomAnchor.constraint(equalTo: bottomAnchor),
removeButton.topAnchor.constraint(equalTo: topAnchor),
removeButton.trailingAnchor.constraint(equalTo: trailingAnchor),
removeButton.heightAnchor.constraint(equalToConstant: .minimumButtonDimension),
removeButton.widthAnchor.constraint(equalToConstant: .minimumButtonDimension),
editButton.trailingAnchor.constraint(equalTo: trailingAnchor),
editButton.bottomAnchor.constraint(equalTo: bottomAnchor),
editButton.heightAnchor.constraint(equalToConstant: .minimumButtonDimension),
editButton.widthAnchor.constraint(equalToConstant: .minimumButtonDimension)
2020-12-19 06:30:19 +00:00
])
}
func applyCompositionAttachmentConfiguration() {
imageView.kf.setImage(with: compositionAttachmentConfiguration.viewModel.attachment.previewUrl)
2021-01-03 01:22:17 +00:00
aspectRatioConstraint.isActive = false
aspectRatioConstraint = imageView.widthAnchor.constraint(
equalTo: imageView.heightAnchor,
multiplier: CGFloat(compositionAttachmentConfiguration.viewModel.attachment.aspectRatio ?? 1))
aspectRatioConstraint.priority = .justBelowMax
aspectRatioConstraint.isActive = true
2020-12-19 06:30:19 +00:00
}
}