Slather logo

Coverage for "ItemModel.swift" : 0.00%

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