-
Notifications
You must be signed in to change notification settings - Fork 255
/
ChatEventHandler.kt
147 lines (135 loc) · 5.54 KB
/
ChatEventHandler.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2014-2022 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.getstream.chat.android.state.event.handler.chat
import io.getstream.chat.android.client.events.ChannelDeletedEvent
import io.getstream.chat.android.client.events.ChannelHiddenEvent
import io.getstream.chat.android.client.events.ChannelVisibleEvent
import io.getstream.chat.android.client.events.ChatEvent
import io.getstream.chat.android.client.events.CidEvent
import io.getstream.chat.android.client.events.HasChannel
import io.getstream.chat.android.client.events.NotificationChannelDeletedEvent
import io.getstream.chat.android.models.Channel
import io.getstream.chat.android.models.FilterObject
/**
* Handler responsible for deciding whether the set of channels should be updated after receiving the particular event.
*
* @see [EventHandlingResult]
*/
public fun interface ChatEventHandler {
/**
* Computes the event handling result.
*
* @param event [ChatEvent] that may contain updates for the set of channels.
* @param filter [FilterObject] associated with the set of channels. Can be used to define the result of handling.
* @param cachedChannel optional cached [Channel] object.
*
* @return [EventHandlingResult] Result of handling.
*/
public fun handleChatEvent(event: ChatEvent, filter: FilterObject, cachedChannel: Channel?): EventHandlingResult
}
/**
* Represent possible outcomes of handling a chat event.
*/
public sealed class EventHandlingResult {
/**
* Add a channel to a query channels collection.
*
* @param channel Channel to be added.
*/
public data class Add(public val channel: Channel) : EventHandlingResult()
/**
* Call watch and add the channel to a query channels collection.
*
* @param cid cid of the channel to watch and add.
*/
public data class WatchAndAdd(public val cid: String) : EventHandlingResult()
/**
* Remove a channel from a query channels collection.
*
* @param cid cid of channel to remove.
*
*/
public data class Remove(public val cid: String) : EventHandlingResult()
/**
* Skip the event.
*/
public object Skip : EventHandlingResult() {
override fun toString(): String = "Skip"
}
}
/**
* More specific [ChatEventHandler] implementation that gives you a separation
* between [CidEvent] and [HasChannel] events.
*
* The channel will be removed from the set after receiving
* [ChannelDeletedEvent], [NotificationChannelDeletedEvent], [ChannelHiddenEvent] events.
*
* The channel will be watched and added to the set after receiving [ChannelVisibleEvent] event.
*
* Other events will be skipped.
*/
public abstract class BaseChatEventHandler : ChatEventHandler {
/**
* Handles [HasChannel] event which contains specific [Channel] object.
*
* @param event [ChatEvent] that may contain updates for the set of channels.
* @param filter [FilterObject] associated with the set of channels. Can be used to define the result of handling.
* @return [EventHandlingResult] Result of handling.
*/
public open fun handleChannelEvent(event: HasChannel, filter: FilterObject): EventHandlingResult {
return when (event) {
is ChannelDeletedEvent -> EventHandlingResult.Remove(event.cid)
is NotificationChannelDeletedEvent -> EventHandlingResult.Remove(event.cid)
else -> EventHandlingResult.Skip
}
}
/**
* Handles [CidEvent] event which is associated with a specific [Channel] which can be tracked using [CidEvent.cid].
*
* @param event [ChatEvent] that may contain updates for the set of channels.
* @param filter [FilterObject] associated with the set of channels. Can be used to define the result of handling.
* @param cachedChannel optional cached [Channel] object if exists.
*
* @return [EventHandlingResult] Result of handling.
*/
public open fun handleCidEvent(
event: CidEvent,
filter: FilterObject,
cachedChannel: Channel?,
): EventHandlingResult {
return when (event) {
is ChannelHiddenEvent -> EventHandlingResult.Remove(event.cid)
is ChannelVisibleEvent -> EventHandlingResult.WatchAndAdd(event.cid)
else -> EventHandlingResult.Skip
}
}
/**
* Computes the event handling result.
*
* @param event [ChatEvent] that may contain updates for the set of channels.
* @param filter [FilterObject] associated with the set of channels. Can be used to define the result of handling.
* @param cachedChannel optional cached [Channel] object.
*
* @return [EventHandlingResult] Result of handling.
*/
override fun handleChatEvent(event: ChatEvent, filter: FilterObject, cachedChannel: Channel?): EventHandlingResult {
return when (event) {
is HasChannel -> handleChannelEvent(event, filter)
is CidEvent -> handleCidEvent(event, filter, cachedChannel)
else -> EventHandlingResult.Skip
}
}
}