Slather logo

Coverage for "ContainerCollectionViewCell.swift" : 0.00%

(0 of 53 relevant lines covered)

ChatLayout/Classes/Extras/ContainerCollectionViewCell.swift

1
//
2
// ChatLayout
3
// ContainerCollectionViewCell.swift
4
// https://github.com/ekazaev/ChatLayout
5
//
6
// Created by Eugene Kazaev in 2020-2022.
7
// Distributed under the MIT license.
8
//
9
// Become a sponsor:
10
// https://github.com/sponsors/ekazaev
11
//
12
13
import Foundation
14
import UIKit
15
16
/// A container `UICollectionViewCell` that constraints its contained view to its margins.
17
public final class ContainerCollectionViewCell<CustomView: UIView>: UICollectionViewCell {
18
19
    /// Default reuse identifier is set with the class name.
20
    public static var reuseIdentifier: String {
!
21
        String(describing: self)
!
22
    }
!
23
24
    /// Contained view.
25
    public lazy var customView = CustomView(frame: bounds)
26
27
    /// An instance of `ContainerCollectionViewCellDelegate`
28
    public weak var delegate: ContainerCollectionViewCellDelegate?
29
30
    /// Initializes and returns a newly allocated view object with the specified frame rectangle.
31
    /// - Parameter frame: The frame rectangle for the view, measured in points. The origin of the frame is relative
32
    ///   to the superview in which you plan to add it.
33
    override init(frame: CGRect) {
!
34
        super.init(frame: frame)
!
35
        setupSubviews()
!
36
    }
!
37
38
    @available(*, unavailable, message: "Use init(reuseIdentifier:) instead.")
39
    /// This constructor is unavailable.
40
    public required init?(coder aDecoder: NSCoder) {
!
41
        fatalError("init(coder:) has not been implemented.")
!
42
    }
!
43
44
    /// Performs any clean up necessary to prepare the view for use again.
45
    public override func prepareForReuse() {
!
46
        super.prepareForReuse()
!
47
        delegate?.prepareForReuse()
!
48
    }
!
49
50
    /// Gives the cell a chance to modify the attributes provided by the layout object.
51
    /// - Parameter layoutAttributes: The attributes provided by the layout object. These attributes represent the values that the layout intends to apply to the cell.
52
    /// - Returns: Modified `UICollectionViewLayoutAttributes`
53
    public override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
!
54
        guard let chatLayoutAttributes = layoutAttributes as? ChatLayoutAttributes else {
!
55
            return super.preferredLayoutAttributesFitting(layoutAttributes)
!
56
        }
!
57
        delegate?.apply(chatLayoutAttributes)
!
58
        let resultingLayoutAttributes: ChatLayoutAttributes
!
59
        if let preferredLayoutAttributes = delegate?.preferredLayoutAttributesFitting(chatLayoutAttributes) {
!
60
            resultingLayoutAttributes = preferredLayoutAttributes
!
61
        } else if let chatLayoutAttributes = super.preferredLayoutAttributesFitting(chatLayoutAttributes) as? ChatLayoutAttributes {
!
62
            delegate?.modifyPreferredLayoutAttributesFitting(chatLayoutAttributes)
!
63
            resultingLayoutAttributes = chatLayoutAttributes
!
64
        } else {
!
65
            resultingLayoutAttributes = chatLayoutAttributes
!
66
        }
!
67
        return resultingLayoutAttributes
!
68
    }
!
69
70
    /// Applies the specified layout attributes to the view.
71
    /// - Parameter layoutAttributes: The layout attributes to apply.
72
    public override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
!
73
        guard let chatLayoutAttributes = layoutAttributes as? ChatLayoutAttributes else {
!
74
            return
!
75
        }
!
76
        super.apply(layoutAttributes)
!
77
        delegate?.apply(chatLayoutAttributes)
!
78
    }
!
79
80
    private func setupSubviews() {
!
81
        contentView.addSubview(customView)
!
82
        insetsLayoutMarginsFromSafeArea = false
!
83
        layoutMargins = .zero
!
84
!
85
        contentView.insetsLayoutMarginsFromSafeArea = false
!
86
        contentView.layoutMargins = .zero
!
87
!
88
        customView.translatesAutoresizingMaskIntoConstraints = false
!
89
        NSLayoutConstraint.activate([
!
90
            customView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
!
91
            customView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor),
!
92
            customView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
!
93
            customView.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor)
!
94
        ])
!
95
    }
!
96
97
}