Slather logo

Coverage for "MessageContainerView.swift" : 0.00%

(0 of 47 relevant lines covered)

ChatLayout/Classes/Extras/MessageContainerView.swift

1
//
2
// ChatLayout
3
// MessageContainerView.swift
4
// https://github.com/ekazaev/ChatLayout
5
//
6
// Created by Eugene Kazaev in 2020-2022.
7
// Distributed under the MIT license.
8
//
9
10
import Foundation
11
import UIKit
12
13
/// A container view that helps to layout the message view and its accessory
14
public final class MessageContainerView<AccessoryViewFactory: StaticViewFactory, MainView: UIView>: UIView {
15
16
    private lazy var stackView = UIStackView(frame: bounds)
17
18
    /// An accessory view.
19
    public lazy var accessoryView: AccessoryViewFactory.View? = AccessoryViewFactory.buildView(within: bounds)
20
21
    /// Main view.
22
    public var customView: MainView {
!
23
        return internalContentView.customView
!
24
    }
!
25
26
    /// An alignment of the contained views within the `MessageContainerView`,
27
    public var alignment: ChatItemAlignment = .fullWidth {
!
28
        didSet {
!
29
            switch alignment {
!
30
            case .leading:
!
31
                internalContentView.flexibleEdges = [.trailing]
!
32
            case .trailing:
!
33
                internalContentView.flexibleEdges = [.leading]
!
34
            case .center:
!
35
                internalContentView.flexibleEdges = [.leading, .trailing]
!
36
            case .fullWidth:
!
37
                internalContentView.flexibleEdges = []
!
38
            }
!
39
        }
!
40
    }
41
42
    private lazy var internalContentView = EdgeAligningView<MainView>(frame: bounds)
43
44
    /// Initializes and returns a newly allocated view object with the specified frame rectangle.
45
    /// - Parameter frame: The frame rectangle for the view, measured in points. The origin of the frame is relative
46
    ///   to the superview in which you plan to add it.
47
    public override init(frame: CGRect) {
!
48
        super.init(frame: frame)
!
49
        setupSubviews()
!
50
    }
!
51
52
    /// Returns an object initialized from data in a given unarchiver.
53
    /// - Parameter coder: An unarchiver object.
54
    public required init?(coder: NSCoder) {
!
55
        super.init(coder: coder)
!
56
        setupSubviews()
!
57
    }
!
58
59
    private func setupSubviews() {
!
60
        translatesAutoresizingMaskIntoConstraints = false
!
61
        insetsLayoutMarginsFromSafeArea = false
!
62
        layoutMargins = .zero
!
63
        addSubview(stackView)
!
64
!
65
        stackView.translatesAutoresizingMaskIntoConstraints = false
!
66
        stackView.axis = .horizontal
!
67
        stackView.spacing = .zero
!
68
!
69
        stackView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true
!
70
        stackView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor).isActive = true
!
71
        stackView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true
!
72
        stackView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor).isActive = true
!
73
!
74
        if let accessoryView = accessoryView {
!
75
            stackView.addArrangedSubview(accessoryView)
!
76
            accessoryView.translatesAutoresizingMaskIntoConstraints = false
!
77
        }
!
78
!
79
        internalContentView.translatesAutoresizingMaskIntoConstraints = false
!
80
        stackView.addArrangedSubview(internalContentView)
!
81
    }
!
82
83
}