Slather logo

Coverage for "ContainerCollectionReusableView.swift" : 0.00%

(0 of 51 relevant lines covered)

ChatLayout/Classes/Extras/ContainerCollectionReusableView.swift

1
//
2
// ChatLayout
3
// ContainerCollectionReusableView.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 `UICollectionReusableView` that constraints its contained view to its margins.
17
public final class ContainerCollectionReusableView<CustomView: UIView>: UICollectionReusableView {
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(frame:) 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
!
59
        let resultingLayoutAttributes: ChatLayoutAttributes
!
60
        if let preferredLayoutAttributes = delegate?.preferredLayoutAttributesFitting(chatLayoutAttributes) {
!
61
            resultingLayoutAttributes = preferredLayoutAttributes
!
62
        } else if let chatLayoutAttributes = super.preferredLayoutAttributesFitting(chatLayoutAttributes) as? ChatLayoutAttributes {
!
63
            delegate?.modifyPreferredLayoutAttributesFitting(chatLayoutAttributes)
!
64
            resultingLayoutAttributes = chatLayoutAttributes
!
65
        } else {
!
66
            resultingLayoutAttributes = chatLayoutAttributes
!
67
        }
!
68
        return resultingLayoutAttributes
!
69
    }
!
70
71
    /// Applies the specified layout attributes to the view.
72
    /// - Parameter layoutAttributes: The layout attributes to apply.
73
    public override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
!
74
        guard let chatLayoutAttributes = layoutAttributes as? ChatLayoutAttributes else {
!
75
            return
!
76
        }
!
77
        super.apply(layoutAttributes)
!
78
        delegate?.apply(chatLayoutAttributes)
!
79
    }
!
80
81
    private func setupSubviews() {
!
82
        addSubview(customView)
!
83
        insetsLayoutMarginsFromSafeArea = false
!
84
        layoutMargins = .zero
!
85
!
86
        customView.translatesAutoresizingMaskIntoConstraints = false
!
87
        NSLayoutConstraint.activate([
!
88
            customView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
!
89
            customView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
!
90
            customView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
!
91
            customView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor)
!
92
        ])
!
93
    }
!
94
95
}