Slather logo

Coverage for "ItemModel.swift" : 100.00%

(11 of 11 relevant lines covered)

ChatLayout/Classes/Core/Model/ItemModel.swift

1
//
2
// ChatLayout
3
// ItemModel.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
struct ItemModel {
17
18
    struct Configuration {
19
20
        let alignment: ChatItemAlignment
21
22
        let preferredSize: CGSize
23
24
        let calculatedSize: CGSize?
25
26
    }
27
28
    let id: UUID
29
30
    var preferredSize: CGSize
31
32
    var offsetY: CGFloat = .zero
33
34
    var calculatedSize: CGSize?
35
36
    var calculatedOnce: Bool = false
37
38
    var alignment: ChatItemAlignment
39
40
    var origin: CGPoint {
41
        CGPoint(x: 0, y: offsetY)
42
    }
43
44
    var height: CGFloat {
45
        size.height
46
    }
47
48
    var locationHeight: CGFloat {
2x
49
        offsetY + height
2x
50
    }
2x
51
52
    var size: CGSize {
53
        guard let calculatedSize = calculatedSize else {
54
            return preferredSize
21200x
55
        }
56
57
        return calculatedSize
58
    }
59
60
    var frame: CGRect {
61
        CGRect(origin: origin, size: size)
62
    }
63
64
    init(id: UUID = UUID(), with configuration: Configuration) {
65
        self.id = id
66
        alignment = configuration.alignment
67
        preferredSize = configuration.preferredSize
68
        calculatedSize = configuration.calculatedSize
69
        calculatedOnce = configuration.calculatedSize != nil
70
    }
71
72
    // We are just resetting `calculatedSize` if needed as the actual size will be found in invalidationContext(forPreferredLayoutAttributes:, withOriginalAttributes:)
73
    // It is important for the rotation to keep previous frame size.
74
    mutating func resetSize() {
10800x
75
        guard let calculatedSize = calculatedSize else {
10800x
76
            return
200x
77
        }
10600x
78
        self.calculatedSize = nil
10600x
79
        preferredSize = calculatedSize
10600x
80
    }
10600x
81
82
}