Skip to content

Commit 60234d4

Browse files
authored
Add handling of poll events (#125)
* Handle all poll events for PollList * Handle poll events for ActivityList * Handle PollDeleted for PollVoteList
1 parent b53a3db commit 60234d4

File tree

15 files changed

+454
-34
lines changed

15 files changed

+454
-34
lines changed

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/client/FeedsClientImpl.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ internal class FeedsClientImpl(
330330
override fun pollList(query: PollsQuery): PollList =
331331
PollListImpl(
332332
query = query,
333+
currentUserId = user.id,
333334
pollsRepository = pollsRepository,
334335
subscriptionManager = stateEventsSubscriptionManager,
335336
)

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/ActivityListStateImpl.kt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import io.getstream.feeds.android.client.api.model.BookmarkData
2020
import io.getstream.feeds.android.client.api.model.CommentData
2121
import io.getstream.feeds.android.client.api.model.FeedsReactionData
2222
import io.getstream.feeds.android.client.api.model.PaginationData
23+
import io.getstream.feeds.android.client.api.model.PollData
24+
import io.getstream.feeds.android.client.api.model.PollVoteData
2325
import io.getstream.feeds.android.client.api.state.ActivityListState
2426
import io.getstream.feeds.android.client.api.state.query.ActivitiesQuery
2527
import io.getstream.feeds.android.client.api.state.query.ActivitiesSort
@@ -28,10 +30,13 @@ import io.getstream.feeds.android.client.internal.model.deleteBookmark
2830
import io.getstream.feeds.android.client.internal.model.removeComment
2931
import io.getstream.feeds.android.client.internal.model.removeCommentReaction
3032
import io.getstream.feeds.android.client.internal.model.removeReaction
33+
import io.getstream.feeds.android.client.internal.model.removeVote
34+
import io.getstream.feeds.android.client.internal.model.update
3135
import io.getstream.feeds.android.client.internal.model.upsertBookmark
3236
import io.getstream.feeds.android.client.internal.model.upsertComment
3337
import io.getstream.feeds.android.client.internal.model.upsertCommentReaction
3438
import io.getstream.feeds.android.client.internal.model.upsertReaction
39+
import io.getstream.feeds.android.client.internal.model.upsertVote
3540
import io.getstream.feeds.android.client.internal.state.query.ActivitiesQueryConfig
3641
import io.getstream.feeds.android.client.internal.utils.mergeSorted
3742
import io.getstream.feeds.android.client.internal.utils.updateIf
@@ -147,6 +152,36 @@ internal class ActivityListStateImpl(
147152
}
148153
}
149154

155+
override fun onPollDeleted(pollId: String) {
156+
_activities.update { current ->
157+
current.updateIf({ it.poll?.id == pollId }) { it.copy(poll = null) }
158+
}
159+
}
160+
161+
override fun onPollUpdated(poll: PollData) {
162+
_activities.update { current ->
163+
current.updateIf({ it.poll?.id == poll.id }) { activity ->
164+
activity.copy(poll = activity.poll?.update(poll))
165+
}
166+
}
167+
}
168+
169+
override fun onPollVoteRemoved(pollId: String, vote: PollVoteData) {
170+
_activities.update { current ->
171+
current.updateIf({ it.poll?.id == pollId }) { activity ->
172+
activity.copy(poll = activity.poll?.removeVote(vote, currentUserId))
173+
}
174+
}
175+
}
176+
177+
override fun onPollVoteUpserted(pollId: String, vote: PollVoteData) {
178+
_activities.update { current ->
179+
current.updateIf({ it.poll?.id == pollId }) { activity ->
180+
activity.copy(poll = activity.poll?.upsertVote(vote, currentUserId))
181+
}
182+
}
183+
}
184+
150185
override fun onReactionUpserted(reaction: FeedsReactionData, activity: ActivityData) {
151186
_activities.update { current ->
152187
current.updateIf({ it.id == reaction.activityId }) {
@@ -244,6 +279,36 @@ internal interface ActivityListStateUpdates {
244279
*/
245280
fun onCommentReactionUpserted(comment: CommentData, reaction: FeedsReactionData)
246281

282+
/**
283+
* Called when a poll is deleted.
284+
*
285+
* @param pollId The ID of the deleted poll.
286+
*/
287+
fun onPollDeleted(pollId: String)
288+
289+
/**
290+
* Called when a poll is updated.
291+
*
292+
* @param poll The updated poll.
293+
*/
294+
fun onPollUpdated(poll: PollData)
295+
296+
/**
297+
* Called when a vote is removed from a poll.
298+
*
299+
* @param pollId The ID of the poll associated with the vote.
300+
* @param vote The vote that was removed.
301+
*/
302+
fun onPollVoteRemoved(pollId: String, vote: PollVoteData)
303+
304+
/**
305+
* Called when a vote is added to or updated in a poll.
306+
*
307+
* @param pollId The ID of the poll associated with the vote.
308+
* @param vote The vote that was added or updated.
309+
*/
310+
fun onPollVoteUpserted(pollId: String, vote: PollVoteData)
311+
247312
/**
248313
* Called when a reaction is added to or updated in an activity.
249314
*

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/PollListImpl.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@ import io.getstream.feeds.android.client.internal.subscribe.StateUpdateEventList
4040
*/
4141
internal class PollListImpl(
4242
override val query: PollsQuery,
43+
private val currentUserId: String,
4344
private val pollsRepository: PollsRepository,
4445
private val subscriptionManager: StreamSubscriptionManager<StateUpdateEventListener>,
45-
private val _state: PollListStateImpl = PollListStateImpl(query),
4646
) : PollList {
47+
private val _state: PollListStateImpl = PollListStateImpl(currentUserId = currentUserId, query)
4748

4849
override val state: PollListState
4950
get() = _state
5051

51-
private val eventHandler = PollListEventHandler(_state)
52+
private val eventHandler = PollListEventHandler(query.filter, _state)
5253

5354
init {
5455
subscriptionManager.subscribe(eventHandler)

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/PollListStateImpl.kt

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ package io.getstream.feeds.android.client.internal.state
1717

1818
import io.getstream.feeds.android.client.api.model.PaginationData
1919
import io.getstream.feeds.android.client.api.model.PollData
20+
import io.getstream.feeds.android.client.api.model.PollVoteData
2021
import io.getstream.feeds.android.client.api.state.PollListState
2122
import io.getstream.feeds.android.client.api.state.query.PollsQuery
2223
import io.getstream.feeds.android.client.api.state.query.PollsSort
2324
import io.getstream.feeds.android.client.internal.model.PaginationResult
25+
import io.getstream.feeds.android.client.internal.model.removeVote
26+
import io.getstream.feeds.android.client.internal.model.update
27+
import io.getstream.feeds.android.client.internal.model.upsertVote
2428
import io.getstream.feeds.android.client.internal.state.query.PollsQueryConfig
2529
import io.getstream.feeds.android.client.internal.utils.mergeSorted
30+
import io.getstream.feeds.android.client.internal.utils.updateIf
2631
import kotlinx.coroutines.flow.MutableStateFlow
2732
import kotlinx.coroutines.flow.StateFlow
2833
import kotlinx.coroutines.flow.asStateFlow
@@ -36,7 +41,10 @@ import kotlinx.coroutines.flow.update
3641
*
3742
* @property query The query used to fetch the polls.
3843
*/
39-
internal class PollListStateImpl(override val query: PollsQuery) : PollListMutableState {
44+
internal class PollListStateImpl(
45+
private val currentUserId: String,
46+
override val query: PollsQuery,
47+
) : PollListMutableState {
4048

4149
private val _polls: MutableStateFlow<List<PollData>> = MutableStateFlow(emptyList())
4250

@@ -65,16 +73,23 @@ internal class PollListStateImpl(override val query: PollsQuery) : PollListMutab
6573
_polls.update { current -> current.mergeSorted(result.models, PollData::id, pollsSorting) }
6674
}
6775

76+
override fun onPollDeleted(pollId: String) {
77+
_polls.update { current -> current.filterNot { it.id == pollId } }
78+
}
79+
6880
override fun onPollUpdated(poll: PollData) {
81+
_polls.update { current -> current.updateIf({ it.id == poll.id }) { it.update(poll) } }
82+
}
83+
84+
override fun onPollVoteUpserted(pollId: String, vote: PollVoteData) {
85+
_polls.update { current ->
86+
current.updateIf({ it.id == pollId }) { it.upsertVote(vote, currentUserId) }
87+
}
88+
}
89+
90+
override fun onPollVoteRemoved(pollId: String, vote: PollVoteData) {
6991
_polls.update { current ->
70-
current.map {
71-
if (it.id == poll.id) {
72-
// Update the existing poll with the new data
73-
poll
74-
} else {
75-
it
76-
}
77-
}
92+
current.updateIf({ it.id == pollId }) { it.removeVote(vote, currentUserId) }
7893
}
7994
}
8095
}
@@ -98,10 +113,33 @@ internal interface PollListStateUpdates {
98113
*/
99114
fun onQueryMorePolls(result: PaginationResult<PollData>, queryConfig: PollsQueryConfig)
100115

116+
/**
117+
* Called when a poll is deleted.
118+
*
119+
* @param pollId The ID of the deleted poll.
120+
*/
121+
fun onPollDeleted(pollId: String)
122+
101123
/**
102124
* Called when a poll is updated.
103125
*
104126
* @param poll The updated poll data.
105127
*/
106128
fun onPollUpdated(poll: PollData)
129+
130+
/**
131+
* Called when a poll vote is added or updated.
132+
*
133+
* @param pollId The ID of the poll.
134+
* @param vote The vote that was added or updated.
135+
*/
136+
fun onPollVoteUpserted(pollId: String, vote: PollVoteData)
137+
138+
/**
139+
* Called when a poll vote is removed.
140+
*
141+
* @param pollId The ID of the poll.
142+
* @param vote The vote that was removed.
143+
*/
144+
fun onPollVoteRemoved(pollId: String, vote: PollVoteData)
107145
}

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/PollVoteListStateImpl.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ internal class PollVoteListStateImpl(override val query: PollVotesQuery) :
5353
override val pagination: PaginationData?
5454
get() = _pagination
5555

56+
override fun onPollDeleted() {
57+
_votes.update { emptyList() }
58+
}
59+
5660
override fun onQueryMorePollVotes(
5761
result: PaginationResult<PollVoteData>,
5862
queryConfig: PollVotesQueryConfig,
@@ -86,6 +90,9 @@ internal interface PollVoteListMutableState : PollVoteListState, PollVoteListSta
8690
/** Interface for handling updates to the poll vote list state. */
8791
internal interface PollVoteListStateUpdates {
8892

93+
/** Handles the deletion of the parent poll. */
94+
fun onPollDeleted()
95+
8996
/**
9097
* Handles updates to the poll vote list state when new poll votes are fetched.
9198
*

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/event/handler/ActivityListEventHandler.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ internal class ActivityListEventHandler(
6565
is StateUpdateEvent.CommentReactionUpdated ->
6666
state.onCommentReactionUpserted(event.comment, event.reaction)
6767

68+
is StateUpdateEvent.PollDeleted -> state.onPollDeleted(event.pollId)
69+
is StateUpdateEvent.PollUpdated -> state.onPollUpdated(event.poll)
70+
is StateUpdateEvent.PollVoteCasted -> state.onPollVoteUpserted(event.pollId, event.vote)
71+
72+
is StateUpdateEvent.PollVoteChanged ->
73+
state.onPollVoteUpserted(event.pollId, event.vote)
74+
75+
is StateUpdateEvent.PollVoteRemoved -> state.onPollVoteRemoved(event.pollId, event.vote)
76+
6877
else -> {
6978
// No action needed for other event types
7079
}

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/event/handler/PollListEventHandler.kt

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,44 @@
1515
*/
1616
package io.getstream.feeds.android.client.internal.state.event.handler
1717

18+
import io.getstream.feeds.android.client.api.state.query.PollsFilter
1819
import io.getstream.feeds.android.client.internal.state.PollListStateUpdates
1920
import io.getstream.feeds.android.client.internal.state.event.StateUpdateEvent
21+
import io.getstream.feeds.android.client.internal.state.query.matches
2022
import io.getstream.feeds.android.client.internal.subscribe.StateUpdateEventListener
2123

2224
/**
2325
* Handles events related to poll updates in the poll list state.
2426
*
2527
* @property state The instance that manages updates to the poll list state.
2628
*/
27-
internal class PollListEventHandler(private val state: PollListStateUpdates) :
28-
StateUpdateEventListener {
29+
internal class PollListEventHandler(
30+
private val filter: PollsFilter?,
31+
private val state: PollListStateUpdates,
32+
) : StateUpdateEventListener {
2933

3034
override fun onEvent(event: StateUpdateEvent) {
3135
when (event) {
36+
is StateUpdateEvent.PollDeleted -> {
37+
state.onPollDeleted(event.pollId)
38+
}
39+
3240
is StateUpdateEvent.PollUpdated -> {
33-
state.onPollUpdated(event.poll)
41+
if (event.poll matches filter) {
42+
state.onPollUpdated(event.poll)
43+
}
44+
}
45+
46+
is StateUpdateEvent.PollVoteCasted -> {
47+
state.onPollVoteUpserted(event.pollId, event.vote)
48+
}
49+
50+
is StateUpdateEvent.PollVoteChanged -> {
51+
state.onPollVoteUpserted(event.pollId, event.vote)
52+
}
53+
54+
is StateUpdateEvent.PollVoteRemoved -> {
55+
state.onPollVoteRemoved(event.pollId, event.vote)
3456
}
3557

3658
else -> {}

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/event/handler/PollVoteListEventHandler.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ internal class PollVoteListEventHandler(
3232

3333
override fun onEvent(event: StateUpdateEvent) {
3434
when (event) {
35+
is StateUpdateEvent.PollDeleted -> {
36+
if (event.pollId != pollId) return
37+
state.onPollDeleted()
38+
}
39+
3540
is StateUpdateEvent.PollVoteCasted -> {
3641
if (event.pollId != pollId) return
3742
state.pollVoteUpserted(event.vote)

0 commit comments

Comments
 (0)