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 size: CGSize {
|
1020000x |
41 |
guard let calculatedSize = calculatedSize else {
|
1020000x |
42 |
return preferredSize
|
! |
43 |
}
|
1020000x |
44 |
|
1020000x |
45 |
return calculatedSize
|
1020000x |
46 |
}
|
1020000x |
47 |
|
|
48 |
var frame: CGRect {
|
|
49 |
CGRect(origin: CGPoint(x: 0, y: offsetY), size: size)
|
|
50 |
}
|
|
51 |
|
|
52 |
init(id: UUID = UUID(), with configuration: Configuration) {
|
|
53 |
self.id = id
|
|
54 |
alignment = configuration.alignment
|
|
55 |
preferredSize = configuration.preferredSize
|
|
56 |
calculatedSize = configuration.calculatedSize
|
|
57 |
calculatedOnce = configuration.calculatedSize != nil
|
|
58 |
}
|
|
59 |
|
|
60 |
// We are just resetting `calculatedSize` if needed as the actual size will be found in invalidationContext(forPreferredLayoutAttributes:, withOriginalAttributes:)
|
|
61 |
// It is important for the rotation to keep previous frame size.
|
|
62 |
mutating func resetSize() {
|
! |
63 |
guard let calculatedSize = calculatedSize else {
|
! |
64 |
return
|
! |
65 |
}
|
! |
66 |
self.calculatedSize = nil
|
! |
67 |
preferredSize = calculatedSize
|
! |
68 |
}
|
! |
69 |
|
|
70 |
}
|
|