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, at sectionIndex: Int) -> Bool
|
|
33 |
|
|
34 |
/// `ChatLayout` will call this method to ask if it should present the footer in the current layout.
|
|
35 |
/// - Parameters:
|
|
36 |
/// - chatLayout: ChatLayout reference.
|
|
37 |
/// - sectionIndex: Index of the section.
|
|
38 |
/// - Returns: `Bool`.
|
|
39 |
func shouldPresentFooter(_ chatLayout: ChatLayout, at sectionIndex: Int) -> Bool
|
|
40 |
|
|
41 |
/// `ChatLayout` will call this method to ask what size the item should have.
|
|
42 |
/// - Parameters:
|
|
43 |
/// - chatLayout: ChatLayout reference.
|
|
44 |
/// - kind: Type of element represented by `ItemKind`.
|
|
45 |
/// - indexPath: Index path of the item.
|
|
46 |
/// - Returns: `ItemSize`.
|
|
47 |
func sizeForItem(_ chatLayout: ChatLayout, of kind: ItemKind, at indexPath: IndexPath) -> ItemSize
|
|
48 |
|
|
49 |
/// `ChatLayout` will call this method to ask what type of alignment the item should have.
|
|
50 |
/// - Parameters:
|
|
51 |
/// - chatLayout: ChatLayout reference.
|
|
52 |
/// - kind: Type of element represented by `ItemKind`.
|
|
53 |
/// - indexPath: Index path of the item.
|
|
54 |
/// - Returns: `ChatItemAlignment`.
|
|
55 |
func alignmentForItem(_ chatLayout: ChatLayout, of kind: ItemKind, at indexPath: IndexPath) -> ChatItemAlignment
|
|
56 |
|
|
57 |
/// Asks the delegate to modify a layout attributes instance so that it represents the initial visual state of an item
|
|
58 |
/// being inserted.
|
|
59 |
///
|
|
60 |
/// The `originalAttributes` instance is a reference type, and therefore can be modified directly.
|
|
61 |
///
|
|
62 |
/// - Parameters:
|
|
63 |
/// - chatLayout: ChatLayout reference.
|
|
64 |
/// - kind: Type of element represented by `ItemKind`.
|
|
65 |
/// - indexPath: Index path of the item.
|
|
66 |
/// - originalAttributes: `ChatLayoutAttributes` that the `ChatLayout` is going to use.
|
|
67 |
/// - state: `InitialAttributesRequestType` instance. Represents when is this method being called.
|
|
68 |
func initialLayoutAttributesForInsertedItem(_ chatLayout: ChatLayout,
|
|
69 |
of kind: ItemKind,
|
|
70 |
at indexPath: IndexPath,
|
|
71 |
modifying originalAttributes: ChatLayoutAttributes,
|
|
72 |
on state: InitialAttributesRequestType)
|
|
73 |
|
|
74 |
/// Asks the delegate to modify a layout attributes instance so that it represents the final visual state of an item
|
|
75 |
/// being removed via `UICollectionView.deleteSections(_:)`.
|
|
76 |
///
|
|
77 |
/// The `originalAttributes` instance is a reference type, and therefore can be modified directly.
|
|
78 |
///
|
|
79 |
/// - Parameters:
|
|
80 |
/// - chatLayout: ChatLayout reference.
|
|
81 |
/// - kind: Type of element represented by `ItemKind`.
|
|
82 |
/// - indexPath: Index path of the item.
|
|
83 |
/// - originalAttributes: `ChatLayoutAttributes` that the `ChatLayout` is going to use.
|
|
84 |
func finalLayoutAttributesForDeletedItem(_ chatLayout: ChatLayout,
|
|
85 |
of kind: ItemKind,
|
|
86 |
at indexPath: IndexPath,
|
|
87 |
modifying originalAttributes: ChatLayoutAttributes)
|
|
88 |
|
|
89 |
}
|
|
90 |
|
|
91 |
/// Default extension.
|
|
92 |
public extension ChatLayoutDelegate {
|
|
93 |
|
|
94 |
/// Default implementation returns: `false`.
|
|
95 |
func shouldPresentHeader(_ chatLayout: ChatLayout, at sectionIndex: Int) -> Bool {
|
! |
96 |
return false
|
! |
97 |
}
|
! |
98 |
|
|
99 |
/// Default implementation returns: `false`.
|
|
100 |
func shouldPresentFooter(_ chatLayout: ChatLayout, at sectionIndex: Int) -> Bool {
|
! |
101 |
return false
|
! |
102 |
}
|
! |
103 |
|
|
104 |
/// Default implementation returns: `ItemSize.auto`.
|
|
105 |
func sizeForItem(_ chatLayout: ChatLayout, of kind: ItemKind, at indexPath: IndexPath) -> ItemSize {
|
! |
106 |
return .auto
|
! |
107 |
}
|
! |
108 |
|
|
109 |
/// Default implementation returns: `ChatItemAlignment.fullWidth`.
|
|
110 |
func alignmentForItem(_ chatLayout: ChatLayout, of kind: ItemKind, at indexPath: IndexPath) -> ChatItemAlignment {
|
! |
111 |
return .fullWidth
|
! |
112 |
}
|
! |
113 |
|
|
114 |
/// Default implementation sets a `ChatLayoutAttributes.alpha` to zero.
|
|
115 |
func initialLayoutAttributesForInsertedItem(_ chatLayout: ChatLayout,
|
|
116 |
of kind: ItemKind,
|
|
117 |
at indexPath: IndexPath,
|
|
118 |
modifying originalAttributes: ChatLayoutAttributes,
|
|
119 |
on state: InitialAttributesRequestType) {
|
! |
120 |
originalAttributes.alpha = 0
|
! |
121 |
}
|
! |
122 |
|
|
123 |
/// Default implementation sets a `ChatLayoutAttributes.alpha` to zero.
|
|
124 |
func finalLayoutAttributesForDeletedItem(_ chatLayout: ChatLayout,
|
|
125 |
of kind: ItemKind,
|
|
126 |
at indexPath: IndexPath,
|
|
127 |
modifying originalAttributes: ChatLayoutAttributes) {
|
! |
128 |
originalAttributes.alpha = 0
|
! |
129 |
}
|
! |
130 |
|
|
131 |
}
|
|