Slather logo

Coverage for "LayoutModel.swift" : 74.19%

(92 of 124 relevant lines covered)

ChatLayout/Classes/Core/Model/LayoutModel.swift

1
//
2
// ChatLayout
3
// LayoutModel.swift
4
// https://github.com/ekazaev/ChatLayout
5
//
6
// Created by Eugene Kazaev in 2020-2021.
7
// Distributed under the MIT license.
8
//
9
10
import Foundation
11
import UIKit
12
13
struct LayoutModel {
14
15
    private struct ItemUUIDKey: Hashable {
16
17
        let kind: ItemKind
18
19
        let id: UUID
20
21
    }
22
23
    private(set) var sections: [SectionModel]
24
25
    private unowned var collectionLayout: ChatLayoutRepresentation
26
27
    private var sectionIndexByIdentifierCache: [UUID: Int]?
28
29
    private var itemPathByIdentifierCache: [ItemUUIDKey: ItemPath]?
30
31
    init(sections: [SectionModel], collectionLayout: ChatLayoutRepresentation) {
88x
32
        self.sections = sections
88x
33
        self.collectionLayout = collectionLayout
88x
34
    }
88x
35
36
    mutating func assembleLayout() {
65x
37
        var offset: CGFloat = collectionLayout.settings.additionalInsets.top
65x
38
65x
39
        var sectionIndexByIdentifierCache = [UUID: Int](minimumCapacity: sections.count)
65x
40
        var itemPathByIdentifierCache = [ItemUUIDKey: ItemPath]()
65x
41
65x
42
        for sectionIndex in 0..<sections.count {
115x
43
            sectionIndexByIdentifierCache[sections[sectionIndex].id] = sectionIndex
115x
44
            sections[sectionIndex].offsetY = offset
115x
45
            offset += sections[sectionIndex].height + collectionLayout.settings.interSectionSpacing
115x
46
            if let header = sections[sectionIndex].header {
115x
47
                itemPathByIdentifierCache[ItemUUIDKey(kind: .header, id: header.id)] = ItemPath(item: 0, section: sectionIndex)
112x
48
            }
115x
49
            for itemIndex in 0..<sections[sectionIndex].items.count {
50
                itemPathByIdentifierCache[ItemUUIDKey(kind: .cell, id: sections[sectionIndex].items[itemIndex].id)] = ItemPath(item: itemIndex, section: sectionIndex)
51
            }
52
            if let footer = sections[sectionIndex].footer {
115x
53
                itemPathByIdentifierCache[ItemUUIDKey(kind: .footer, id: footer.id)] = ItemPath(item: 0, section: sectionIndex)
114x
54
            }
115x
55
        }
115x
56
        self.itemPathByIdentifierCache = itemPathByIdentifierCache
65x
57
        self.sectionIndexByIdentifierCache = sectionIndexByIdentifierCache
65x
58
    }
65x
59
60
    // MARK: To use when its is important to make the correct insertion
61
62
    mutating func setAndAssemble(header: ItemModel, sectionIndex: Int) {
3x
63
        guard sectionIndex < sections.count else {
3x
64
            assertionFailure("Internal inconsistency")
!
65
            return
!
66
        }
3x
67
3x
68
        let oldSection = sections[sectionIndex]
3x
69
        sections[sectionIndex].setAndAssemble(header: header)
3x
70
        let heightDiff = sections[sectionIndex].height - oldSection.height
3x
71
        offsetEverything(below: sectionIndex, by: heightDiff)
3x
72
    }
3x
73
74
    mutating func setAndAssemble(item: ItemModel, sectionIndex: Int, itemIndex: Int) {
10000x
75
        guard sectionIndex < sections.count else {
10000x
76
            assertionFailure("Internal inconsistency")
!
77
            return
!
78
        }
10000x
79
        let oldSection = sections[sectionIndex]
10000x
80
        sections[sectionIndex].setAndAssemble(item: item, at: itemIndex)
10000x
81
        let heightDiff = sections[sectionIndex].height - oldSection.height
10000x
82
        offsetEverything(below: sectionIndex, by: heightDiff)
10000x
83
    }
10000x
84
85
    mutating func setAndAssemble(footer: ItemModel, sectionIndex: Int) {
3x
86
        guard sectionIndex < sections.count else {
3x
87
            assertionFailure("Internal inconsistency")
!
88
            return
!
89
        }
3x
90
        let oldSection = sections[sectionIndex]
3x
91
        sections[sectionIndex].setAndAssemble(footer: footer)
3x
92
        let heightDiff = sections[sectionIndex].height - oldSection.height
3x
93
        offsetEverything(below: sectionIndex, by: heightDiff)
3x
94
    }
3x
95
96
    func sectionIndex(by sectionId: UUID) -> Int? {
5x
97
        guard let sectionIndexByIdentifierCache = sectionIndexByIdentifierCache else {
5x
98
            assertionFailure("Internal inconsistency")
!
99
            return sections.firstIndex(where: { $0.id == sectionId })
!
100
        }
5x
101
        return sectionIndexByIdentifierCache[sectionId]
5x
102
    }
5x
103
104
    func itemPath(by itemId: UUID, kind: ItemKind) -> ItemPath? {
10300x
105
        guard let itemPathByIdentifierCache = itemPathByIdentifierCache else {
10300x
106
            assertionFailure("Internal inconsistency")
!
107
            for (sectionIndex, section) in sections.enumerated() {
!
108
                switch kind {
!
109
                case .header:
!
110
                    if itemId == section.header?.id {
!
111
                        return ItemPath(item: 0, section: sectionIndex)
!
112
                    }
!
113
                case .footer:
!
114
                    if itemId == section.footer?.id {
!
115
                        return ItemPath(item: 0, section: sectionIndex)
!
116
                    }
!
117
                case .cell:
!
118
                    if let itemIndex = section.items.firstIndex(where: { $0.id == itemId }) {
!
119
                        return ItemPath(item: itemIndex, section: sectionIndex)
!
120
                    }
!
121
                }
!
122
            }
!
123
            return nil
!
124
        }
10300x
125
        return itemPathByIdentifierCache[ItemUUIDKey(kind: kind, id: itemId)]
10300x
126
    }
10300x
127
128
    private mutating func offsetEverything(below index: Int, by heightDiff: CGFloat) {
10000x
129
        guard heightDiff != 0 else {
10000x
130
            return
9000x
131
        }
9000x
132
        if index < sections.count - 1 {
1000x
133
            for index in (index + 1)..<sections.count {
16x
134
                sections[index].offsetY += heightDiff
16x
135
            }
16x
136
        }
1000x
137
    }
1000x
138
139
    // MARK: To use only withing process(updateItems:)
140
141
    mutating func insertSection(_ section: SectionModel, at sectionIndex: Int) {
9x
142
        var sections = self.sections
9x
143
        guard sectionIndex <= sections.count else {
9x
144
            assertionFailure("Internal inconsistency")
!
145
            return
!
146
        }
9x
147
9x
148
        sections.insert(section, at: sectionIndex)
9x
149
        self.sections = sections
9x
150
        resetCache()
9x
151
    }
9x
152
153
    mutating func removeSection(by sectionIdentifier: UUID) {
4x
154
        guard let sectionIndex = sections.firstIndex(where: { $0.id == sectionIdentifier }) else {
7x
155
            assertionFailure("Internal inconsistency")
!
156
            return
!
157
        }
4x
158
        sections.remove(at: sectionIndex)
4x
159
        resetCache()
4x
160
    }
4x
161
162
    mutating func removeSection(for sectionIndex: Int) {
5x
163
        sections.remove(at: sectionIndex)
5x
164
        resetCache()
5x
165
    }
5x
166
167
    mutating func insertItem(_ item: ItemModel, at indexPath: IndexPath) {
168
        sections[indexPath.section].insert(item, at: indexPath.item)
169
        resetCache()
170
    }
171
172
    mutating func replaceItem(_ item: ItemModel, at indexPath: IndexPath) {
10300x
173
        sections[indexPath.section].replace(item, at: indexPath.item)
10300x
174
        resetCache()
10300x
175
    }
10300x
176
177
    mutating func removeItem(by itemId: UUID) {
178
        var itemPath: ItemPath?
179
        for (sectionIndex, section) in sections.enumerated() {
180
            if let itemIndex = section.items.firstIndex(where: { $0.id == itemId }) {
181
                itemPath = ItemPath(item: itemIndex, section: sectionIndex)
182
                break
183
            }
184
        }
185
        guard let path = itemPath else {
186
            assertionFailure("Internal inconsistency")
!
187
            return
!
188
        }
189
        sections[path.section].remove(at: path.item)
190
        resetCache()
191
    }
192
193
    private mutating func resetCache() {
194
        itemPathByIdentifierCache = nil
195
        sectionIndexByIdentifierCache = nil
196
    }
197
198
}