Slather logo

Coverage for "ChatLayoutDelegate.swift" : 0.00%

(0 of 18 relevant lines covered)

ChatLayout/Classes/Core/ChatLayoutDelegate.swift

1
//
2
// ChatLayout
3
// ChatLayoutDelegate.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
/// Represents the point in time `ChatLayout` when chat layout asks about layout attributes modification.
14
public enum InitialAttributesRequestType {
15
16
    /// `UICollectionView` initially asks about the layout of an item.
17
    case initial
18
19
    /// An item is being invalidated.
20
    case invalidation
21
22
}
23
24
/// `ChatLayout` delegate
25
public protocol ChatLayoutDelegate: AnyObject {
26
27
    /// `ChatLayout` will call this method to ask if it should present the header in the current layout.
28
    /// - Parameters:
29
    ///   - chatLayout: ChatLayout reference.
30
    ///   - sectionIndex: Index of the section.
31
    /// - Returns: `Bool`.
32
    func shouldPresentHeader(_ chatLayout: ChatLayout,
33
                             at sectionIndex: Int) -> Bool
34
35
    /// `ChatLayout` will call this method to ask if it should present the footer in the current layout.
36
    /// - Parameters:
37
    ///   - chatLayout: ChatLayout reference.
38
    ///   - sectionIndex: Index of the section.
39
    /// - Returns: `Bool`.
40
    func shouldPresentFooter(_ chatLayout: ChatLayout,
41
                             at sectionIndex: Int) -> Bool
42
43
    /// `ChatLayout` will call this method to ask what size the item should have.
44
    ///
45
    /// **NB:**
46
    ///
47
    /// If you are trying to speed up the layout process by returning exact item sizes in this method -
48
    /// do not forget to change `UICollectionReusableView.preferredLayoutAttributesFitting(...)` method and do not
49
    /// call `super.preferredLayoutAttributesFitting(...)` there as it will measure the `UIView`
50
    /// using Autolayout Engine anyway.
51
    ///
52
    /// - Parameters:
53
    ///   - chatLayout: ChatLayout reference.
54
    ///   - kind: Type of element represented by `ItemKind`.
55
    ///   - indexPath: Index path of the item.
56
    /// - Returns: `ItemSize`.
57
    func sizeForItem(_ chatLayout: ChatLayout,
58
                     of kind: ItemKind,
59
                     at indexPath: IndexPath) -> ItemSize
60
61
    /// `ChatLayout` will call this method to ask what type of alignment the item should have.
62
    /// - Parameters:
63
    ///   - chatLayout: ChatLayout reference.
64
    ///   - kind: Type of element represented by `ItemKind`.
65
    ///   - indexPath: Index path of the item.
66
    /// - Returns: `ChatItemAlignment`.
67
    func alignmentForItem(_ chatLayout: ChatLayout,
68
                          of kind: ItemKind,
69
                          at indexPath: IndexPath) -> ChatItemAlignment
70
71
    ///   Asks the delegate to modify a layout attributes instance so that it represents the initial visual state of an item
72
    ///   being inserted.
73
    ///
74
    ///   The `originalAttributes` instance is a reference type, and therefore can be modified directly.
75
    ///
76
    /// - Parameters:
77
    ///   - chatLayout: ChatLayout reference.
78
    ///   - kind: Type of element represented by `ItemKind`.
79
    ///   - indexPath: Index path of the item.
80
    ///   - originalAttributes: `ChatLayoutAttributes` that the `ChatLayout` is going to use.
81
    ///   - state: `InitialAttributesRequestType` instance. Represents when is this method being called.
82
    func initialLayoutAttributesForInsertedItem(_ chatLayout: ChatLayout,
83
                                                of kind: ItemKind,
84
                                                at indexPath: IndexPath,
85
                                                modifying originalAttributes: ChatLayoutAttributes,
86
                                                on state: InitialAttributesRequestType)
87
88
    ///   Asks the delegate to modify a layout attributes instance so that it represents the final visual state of an item
89
    ///   being removed via `UICollectionView.deleteSections(_:)`.
90
    ///
91
    ///   The `originalAttributes` instance is a reference type, and therefore can be modified directly.
92
    ///
93
    /// - Parameters:
94
    ///   - chatLayout: ChatLayout reference.
95
    ///   - kind: Type of element represented by `ItemKind`.
96
    ///   - indexPath: Index path of the item.
97
    ///   - originalAttributes: `ChatLayoutAttributes` that the `ChatLayout` is going to use.
98
    func finalLayoutAttributesForDeletedItem(_ chatLayout: ChatLayout,
99
                                             of kind: ItemKind,
100
                                             at indexPath: IndexPath,
101
                                             modifying originalAttributes: ChatLayoutAttributes)
102
103
}
104
105
/// Default extension.
106
public extension ChatLayoutDelegate {
107
108
    /// Default implementation returns: `false`.
109
    func shouldPresentHeader(_ chatLayout: ChatLayout,
110
                             at sectionIndex: Int) -> Bool {
!
111
        return false
!
112
    }
!
113
114
    /// Default implementation returns: `false`.
115
    func shouldPresentFooter(_ chatLayout: ChatLayout,
116
                             at sectionIndex: Int) -> Bool {
!
117
        return false
!
118
    }
!
119
120
    /// Default implementation returns: `ItemSize.auto`.
121
    func sizeForItem(_ chatLayout: ChatLayout,
122
                     of kind: ItemKind,
123
                     at indexPath: IndexPath) -> ItemSize {
!
124
        return .auto
!
125
    }
!
126
127
    /// Default implementation returns: `ChatItemAlignment.fullWidth`.
128
    func alignmentForItem(_ chatLayout: ChatLayout,
129
                          of kind: ItemKind,
130
                          at indexPath: IndexPath) -> ChatItemAlignment {
!
131
        return .fullWidth
!
132
    }
!
133
134
    /// Default implementation sets a `ChatLayoutAttributes.alpha` to zero.
135
    func initialLayoutAttributesForInsertedItem(_ chatLayout: ChatLayout,
136
                                                of kind: ItemKind,
137
                                                at indexPath: IndexPath,
138
                                                modifying originalAttributes: ChatLayoutAttributes,
139
                                                on state: InitialAttributesRequestType) {
!
140
        originalAttributes.alpha = 0
!
141
    }
!
142
143
    /// Default implementation sets a `ChatLayoutAttributes.alpha` to zero.
144
    func finalLayoutAttributesForDeletedItem(_ chatLayout: ChatLayout,
145
                                             of kind: ItemKind,
146
                                             at indexPath: IndexPath,
147
                                             modifying originalAttributes: ChatLayoutAttributes) {
!
148
        originalAttributes.alpha = 0
!
149
    }
!
150
151
}