Slather logo

Coverage for "SectionModel.swift" : 0.00%

(0 of 132 relevant lines covered)

ChatLayout/Classes/Core/Model/SectionModel.swift

1
//
2
// ChatLayout
3
// SectionModel.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 SectionModel {
14
15
    let id: UUID
16
17
    private(set) var header: ItemModel?
18
19
    private(set) var footer: ItemModel?
20
21
    private(set) var items: [ItemModel]
22
23
    var offsetY: CGFloat = 0
24
25
    private unowned var collectionLayout: ChatLayoutRepresentation
26
27
    var count: Int {
!
28
        return items.count
!
29
    }
!
30
31
    var frame: CGRect {
!
32
        let additionalInsets = collectionLayout.settings.additionalInsets
!
33
        return CGRect(x: 0,
!
34
                      y: offsetY,
!
35
                      width: collectionLayout.visibleBounds.width - additionalInsets.left - additionalInsets.right,
!
36
                      height: height)
!
37
    }
!
38
39
    var height: CGFloat {
!
40
        if let footer = footer {
!
41
            return footer.frame.maxY
!
42
        } else {
!
43
            guard let lastItem = items.last else {
!
44
                return header?.frame.maxY ?? .zero
!
45
            }
!
46
            return lastItem.locationHeight
!
47
        }
!
48
    }
!
49
50
    var locationHeight: CGFloat {
!
51
        return offsetY + height
!
52
    }
!
53
54
    init(id: UUID = UUID(),
55
         header: ItemModel?,
56
         footer: ItemModel?,
57
         items: [ItemModel] = [],
58
         collectionLayout: ChatLayoutRepresentation) {
!
59
        self.id = id
!
60
        self.items = items
!
61
        self.collectionLayout = collectionLayout
!
62
        self.header = header
!
63
        self.footer = footer
!
64
    }
!
65
66
    mutating func assembleLayout() {
!
67
        var offsetY: CGFloat = 0
!
68
!
69
        if header != nil {
!
70
            header?.offsetY = 0
!
71
            offsetY += header?.frame.height ?? 0
!
72
        }
!
73
!
74
        for rowIndex in 0..<items.count {
!
75
            items[rowIndex].offsetY = offsetY
!
76
            offsetY += items[rowIndex].height + collectionLayout.settings.interItemSpacing
!
77
        }
!
78
!
79
        if footer != nil {
!
80
            footer?.offsetY = offsetY
!
81
        }
!
82
    }
!
83
84
    // MARK: To use when its is important to make the correct insertion
85
86
    mutating func setAndAssemble(header: ItemModel) {
!
87
        guard let oldHeader = self.header else {
!
88
            self.header = header
!
89
            offsetEverything(below: -1, by: header.height)
!
90
            return
!
91
        }
!
92
        #if DEBUG
!
93
        if header.id != oldHeader.id {
!
94
            assertionFailure("Internal inconsistency")
!
95
        }
!
96
        #endif
!
97
        self.header = header
!
98
        let heightDiff = header.height - oldHeader.height
!
99
        offsetEverything(below: -1, by: heightDiff)
!
100
    }
!
101
102
    mutating func setAndAssemble(item: ItemModel, at index: Int) {
!
103
        guard index < count else {
!
104
            assertionFailure("Incorrect item index.")
!
105
            return
!
106
        }
!
107
        let oldItem = items[index]
!
108
        #if DEBUG
!
109
        if item.id != oldItem.id {
!
110
            assertionFailure("Internal inconsistency")
!
111
        }
!
112
        #endif
!
113
        items[index] = item
!
114
!
115
        let heightDiff = item.height - oldItem.height
!
116
        offsetEverything(below: index, by: heightDiff)
!
117
    }
!
118
119
    mutating func setAndAssemble(footer: ItemModel) {
!
120
        #if DEBUG
!
121
        if let oldFooter = self.footer,
!
122
           footer.id != oldFooter.id {
!
123
            assertionFailure("Internal inconsistency")
!
124
        }
!
125
        #endif
!
126
        self.footer = footer
!
127
    }
!
128
129
    // MARK: Just updaters
130
131
    mutating func set(header: ItemModel?) {
!
132
        self.header = header
!
133
    }
!
134
135
    mutating func set(items: [ItemModel]) {
!
136
        self.items = items
!
137
    }
!
138
139
    mutating func set(footer: ItemModel?) {
!
140
        guard let _ = self.footer, let _ = footer else {
!
141
            self.footer = footer
!
142
            return
!
143
        }
!
144
        self.footer = footer
!
145
    }
!
146
147
    private mutating func offsetEverything(below index: Int, by heightDiff: CGFloat) {
!
148
        guard heightDiff != 0 else {
!
149
            return
!
150
        }
!
151
        if index < items.count - 1 {
!
152
            for index in (index + 1)..<items.count {
!
153
                items[index].offsetY += heightDiff
!
154
            }
!
155
        }
!
156
        footer?.offsetY += heightDiff
!
157
    }
!
158
159
    // MARK: To use only withing process(updateItems:)
160
161
    mutating func insert(_ item: ItemModel, at index: Int) {
!
162
        guard index <= count else {
!
163
            assertionFailure("Incorrect item index.")
!
164
            return
!
165
        }
!
166
        items.insert(item, at: index)
!
167
    }
!
168
169
    mutating func replace(_ item: ItemModel, at index: Int) {
!
170
        guard index <= count else {
!
171
            assertionFailure("Incorrect item index.")
!
172
            return
!
173
        }
!
174
        items[index] = item
!
175
    }
!
176
177
    mutating func remove(at index: Int) {
!
178
        guard index < count else {
!
179
            assertionFailure("Incorrect item index.")
!
180
            return
!
181
        }
!
182
        items.remove(at: index)
!
183
    }
!
184
185
}