Skip to content

Commit 519dc03

Browse files
authored
Simplify handling of poll events (#112)
* Use PollUpdated instead of the specific PollClosed event * Merge handling of PollVoteCasted & PollVoteChanged
1 parent f008a76 commit 519dc03

File tree

15 files changed

+67
-315
lines changed

15 files changed

+67
-315
lines changed

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package io.getstream.feeds.android.client.internal.model
1818
import io.getstream.feeds.android.client.api.model.PollData
1919
import io.getstream.feeds.android.client.api.model.PollOptionData
2020
import io.getstream.feeds.android.client.api.model.PollVoteData
21-
import io.getstream.feeds.android.client.api.model.toModel
2221
import io.getstream.feeds.android.client.internal.utils.upsert
2322
import io.getstream.feeds.android.network.models.PollResponseData
2423
import kotlin.math.max
@@ -70,14 +69,6 @@ internal fun PollData.update(
7069
*/
7170
internal fun PollData.update(updated: PollData): PollData = updated.copy(ownVotes = ownVotes)
7271

73-
/**
74-
* Mark the poll as closed. This function creates a new [PollData] instance with the `isClosed`
75-
* property set to `true`.
76-
*
77-
* @return A new [PollData] instance with the poll marked as closed.
78-
*/
79-
internal fun PollData.setClosed(): PollData = copy(isClosed = true)
80-
8172
/**
8273
* Extension function to add a new option to the poll. This function creates a new [PollData]
8374
* instance with the new option added to the existing options.
@@ -130,7 +121,7 @@ internal fun PollData.updateOption(option: PollOptionData): PollData {
130121
* @param currentUserId The ID of the current user, used to determine if the vote belongs to them.
131122
* @return A new [PollData] instance with the specified vote added or updated.
132123
*/
133-
internal fun PollData.castVote(vote: PollVoteData, currentUserId: String): PollData {
124+
internal fun PollData.upsertVote(vote: PollVoteData, currentUserId: String): PollData {
134125
// Answers and option votes are mutually exclusive, so if the vote is an answer, we don't need
135126
// to update the option votes
136127
if (vote.isAnswer == true) {

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

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ import io.getstream.feeds.android.client.api.model.PollVoteData
2323
import io.getstream.feeds.android.client.api.model.ThreadedCommentData
2424
import io.getstream.feeds.android.client.api.state.ActivityCommentListState
2525
import io.getstream.feeds.android.client.api.state.ActivityState
26-
import io.getstream.feeds.android.client.internal.model.castVote
2726
import io.getstream.feeds.android.client.internal.model.deleteBookmark
2827
import io.getstream.feeds.android.client.internal.model.removeReaction
2928
import io.getstream.feeds.android.client.internal.model.removeVote
30-
import io.getstream.feeds.android.client.internal.model.setClosed
3129
import io.getstream.feeds.android.client.internal.model.update
3230
import io.getstream.feeds.android.client.internal.model.upsertBookmark
3331
import io.getstream.feeds.android.client.internal.model.upsertReaction
32+
import io.getstream.feeds.android.client.internal.model.upsertVote
3433
import kotlinx.coroutines.flow.MutableStateFlow
3534
import kotlinx.coroutines.flow.StateFlow
3635
import kotlinx.coroutines.flow.asStateFlow
@@ -90,10 +89,6 @@ internal class ActivityStateImpl(
9089
_activity.update { current -> current?.upsertBookmark(bookmark, currentUserId) }
9190
}
9291

93-
override fun onPollClosed(poll: PollData) {
94-
updatePoll(poll.id, PollData::setClosed)
95-
}
96-
9792
override fun onPollDeleted(pollId: String) {
9893
updatePoll(pollId) { null }
9994
}
@@ -102,18 +97,14 @@ internal class ActivityStateImpl(
10297
updatePoll(poll.id) { update(poll) }
10398
}
10499

105-
override fun onPollVoteCasted(vote: PollVoteData, pollId: String) {
106-
updatePoll(pollId) { castVote(vote, currentUserId) }
107-
}
108-
109-
override fun onPollVoteChanged(vote: PollVoteData, pollId: String) {
110-
updatePoll(pollId) { castVote(vote, currentUserId) }
111-
}
112-
113100
override fun onPollVoteRemoved(vote: PollVoteData, pollId: String) {
114101
updatePoll(pollId) { removeVote(vote, currentUserId) }
115102
}
116103

104+
override fun onPollVoteUpserted(vote: PollVoteData, pollId: String) {
105+
updatePoll(pollId) { upsertVote(vote, currentUserId) }
106+
}
107+
117108
private fun updatePoll(pollId: String, update: PollData.() -> PollData?) {
118109
if (_poll.value?.id != pollId) return
119110

@@ -177,13 +168,6 @@ internal interface ActivityStateUpdates {
177168
*/
178169
fun onBookmarkUpserted(bookmark: BookmarkData)
179170

180-
/**
181-
* Called when the associated poll is closed.
182-
*
183-
* @param poll The updated poll data.
184-
*/
185-
fun onPollClosed(poll: PollData)
186-
187171
/**
188172
* Called when the associated poll is deleted.
189173
*
@@ -198,27 +182,19 @@ internal interface ActivityStateUpdates {
198182
*/
199183
fun onPollUpdated(poll: PollData)
200184

201-
/**
202-
* Called when a vote is casted on the poll.
203-
*
204-
* @param vote The vote that was casted.
205-
* @param pollId The ID of the poll associated with the vote.
206-
*/
207-
fun onPollVoteCasted(vote: PollVoteData, pollId: String)
208-
209-
/**
210-
* Called when a vote is changed on the poll.
211-
*
212-
* @param vote The updated vote data.
213-
* @param pollId The ID of the poll associated with the changed vote.
214-
*/
215-
fun onPollVoteChanged(vote: PollVoteData, pollId: String)
216-
217185
/**
218186
* Called when a vote is removed from the poll.
219187
*
220188
* @param vote The vote that was removed.
221189
* @param pollId The ID of the poll associated with the removed vote.
222190
*/
223191
fun onPollVoteRemoved(vote: PollVoteData, pollId: String)
192+
193+
/**
194+
* Called when a vote is casted or changed in the poll.
195+
*
196+
* @param vote The vote.
197+
* @param pollId The ID of the poll associated with the vote.
198+
*/
199+
fun onPollVoteUpserted(vote: PollVoteData, pollId: String)
224200
}

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

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import io.getstream.feeds.android.client.api.state.query.FeedQuery
3636
import io.getstream.feeds.android.client.internal.model.PaginationResult
3737
import io.getstream.feeds.android.client.internal.model.QueryConfiguration
3838
import io.getstream.feeds.android.client.internal.model.addComment
39-
import io.getstream.feeds.android.client.internal.model.castVote
4039
import io.getstream.feeds.android.client.internal.model.deleteBookmark
4140
import io.getstream.feeds.android.client.internal.model.isFollowRequest
4241
import io.getstream.feeds.android.client.internal.model.isFollowerOf
@@ -45,11 +44,11 @@ import io.getstream.feeds.android.client.internal.model.removeComment
4544
import io.getstream.feeds.android.client.internal.model.removeCommentReaction
4645
import io.getstream.feeds.android.client.internal.model.removeReaction
4746
import io.getstream.feeds.android.client.internal.model.removeVote
48-
import io.getstream.feeds.android.client.internal.model.setClosed
4947
import io.getstream.feeds.android.client.internal.model.update
5048
import io.getstream.feeds.android.client.internal.model.upsertBookmark
5149
import io.getstream.feeds.android.client.internal.model.upsertCommentReaction
5250
import io.getstream.feeds.android.client.internal.model.upsertReaction
51+
import io.getstream.feeds.android.client.internal.model.upsertVote
5352
import io.getstream.feeds.android.client.internal.repository.GetOrCreateInfo
5453
import io.getstream.feeds.android.client.internal.state.query.ActivitiesQueryConfig
5554
import io.getstream.feeds.android.client.internal.utils.mergeSorted
@@ -273,12 +272,6 @@ internal class FeedStateImpl(
273272
}
274273
}
275274

276-
override fun onPollClosed(id: String) {
277-
updateActivitiesWhere({ it.poll?.id == id }) { activity ->
278-
activity.copy(poll = activity.poll?.setClosed())
279-
}
280-
}
281-
282275
override fun onPollDeleted(id: String) {
283276
updateActivitiesWhere({ it.poll?.id == id }) { activity -> activity.copy(poll = null) }
284277
}
@@ -289,21 +282,15 @@ internal class FeedStateImpl(
289282
}
290283
}
291284

292-
override fun onPollVoteCasted(vote: PollVoteData, pollId: String) {
293-
updateActivitiesWhere({ it.poll?.id == pollId }) { activity ->
294-
activity.copy(poll = activity.poll?.castVote(vote, currentUserId))
295-
}
296-
}
297-
298-
override fun onPollVoteChanged(vote: PollVoteData, pollId: String) {
285+
override fun onPollVoteRemoved(vote: PollVoteData, pollId: String) {
299286
updateActivitiesWhere({ it.poll?.id == pollId }) { activity ->
300-
activity.copy(poll = activity.poll?.castVote(vote, currentUserId))
287+
activity.copy(poll = activity.poll?.removeVote(vote, currentUserId))
301288
}
302289
}
303290

304-
override fun onPollVoteRemoved(vote: PollVoteData, pollId: String) {
291+
override fun onPollVoteUpserted(vote: PollVoteData, pollId: String) {
305292
updateActivitiesWhere({ it.poll?.id == pollId }) { activity ->
306-
activity.copy(poll = activity.poll?.removeVote(vote, currentUserId))
293+
activity.copy(poll = activity.poll?.upsertVote(vote, currentUserId))
307294
}
308295
}
309296

@@ -436,24 +423,18 @@ internal interface FeedStateUpdates {
436423
/** Handles updates to the feed state when a reaction is removed. */
437424
fun onReactionRemoved(reaction: FeedsReactionData, activity: ActivityData)
438425

439-
/** Handles updates to the feed state when a poll is closed. */
440-
fun onPollClosed(id: String)
441-
442426
/** Handles updates to the feed state when a poll is deleted. */
443427
fun onPollDeleted(id: String)
444428

445429
/** Handles updates to the feed state when a poll is updated. */
446430
fun onPollUpdated(poll: PollData)
447431

448-
/** Handles updates to the feed state when a poll vote is casted. */
449-
fun onPollVoteCasted(vote: PollVoteData, pollId: String)
450-
451-
/** Handles updates to the feed state when a poll vote is changed. */
452-
fun onPollVoteChanged(vote: PollVoteData, pollId: String)
453-
454432
/** Handles updates to the feed state when a poll vote is removed. */
455433
fun onPollVoteRemoved(vote: PollVoteData, pollId: String)
456434

435+
/** Handles updates to the feed state when a poll vote is casted or changed. */
436+
fun onPollVoteUpserted(vote: PollVoteData, pollId: String)
437+
457438
/**
458439
* Handles updates to a notification feed.
459440
*

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

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,13 @@ internal class PollVoteListStateImpl(override val query: PollVotesQuery) :
6666
}
6767
}
6868

69-
override fun pollVoteAdded(vote: PollVoteData) {
69+
override fun pollVoteUpserted(vote: PollVoteData) {
7070
_votes.update { current -> current.upsertSorted(vote, PollVoteData::id, votesSorting) }
7171
}
7272

7373
override fun pollVoteRemoved(voteId: String) {
7474
_votes.update { current -> current.filter { it.id != voteId } }
7575
}
76-
77-
override fun pollVoteUpdated(vote: PollVoteData) {
78-
_votes.update { current ->
79-
current.map {
80-
if (it.id == vote.id) {
81-
// Update the existing vote with the new data
82-
vote
83-
} else {
84-
it
85-
}
86-
}
87-
}
88-
}
8976
}
9077

9178
/**
@@ -110,16 +97,9 @@ internal interface PollVoteListStateUpdates {
11097
queryConfig: PollVotesQueryConfig,
11198
)
11299

113-
/** Handles the addition of a new poll vote to the list. */
114-
fun pollVoteAdded(vote: PollVoteData)
115-
116100
/** Handles the removal of a poll vote from the list. */
117101
fun pollVoteRemoved(voteId: String)
118102

119-
/**
120-
* Handles updates to an existing poll vote in the list.
121-
*
122-
* @param vote The updated poll vote data.
123-
*/
124-
fun pollVoteUpdated(vote: PollVoteData)
103+
/** Handles the addition of a new poll vote to the list. */
104+
fun pollVoteUpserted(vote: PollVoteData)
125105
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,17 @@ internal sealed interface StateUpdateEvent {
161161
val notificationStatus: NotificationStatusResponse?,
162162
) : StateUpdateEvent
163163

164-
data class PollClosed(val fid: String, val poll: PollData) : StateUpdateEvent
165-
166164
data class PollDeleted(val fid: String, val pollId: String) : StateUpdateEvent
167165

168166
data class PollUpdated(val fid: String, val poll: PollData) : StateUpdateEvent
169167

170-
data class PollVoteCasted(val fid: String, val pollId: String, val vote: PollVoteData) :
168+
data class PollVoteRemoved(val fid: String, val pollId: String, val vote: PollVoteData) :
171169
StateUpdateEvent
172170

173-
data class PollVoteChanged(val fid: String, val pollId: String, val vote: PollVoteData) :
171+
data class PollVoteCasted(val fid: String, val pollId: String, val vote: PollVoteData) :
174172
StateUpdateEvent
175173

176-
data class PollVoteRemoved(val fid: String, val pollId: String, val vote: PollVoteData) :
174+
data class PollVoteChanged(val fid: String, val pollId: String, val vote: PollVoteData) :
177175
StateUpdateEvent
178176
}
179177

@@ -252,7 +250,7 @@ internal fun WSEvent.toModel(): StateUpdateEvent? =
252250

253251
is FeedMemberUpdatedEvent -> StateUpdateEvent.FeedMemberUpdated(fid, member.toModel())
254252

255-
is PollClosedFeedEvent -> StateUpdateEvent.PollClosed(fid, poll.toModel())
253+
is PollClosedFeedEvent -> StateUpdateEvent.PollUpdated(fid, poll.toModel())
256254

257255
is PollDeletedFeedEvent -> StateUpdateEvent.PollDeleted(fid, poll.id)
258256

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ internal class ActivityEventHandler(
7979
state.onBookmarkUpserted(event.bookmark)
8080
}
8181

82-
is StateUpdateEvent.PollClosed -> {
83-
if (event.fid != fid.rawValue) return
84-
state.onPollClosed(event.poll)
85-
}
86-
8782
is StateUpdateEvent.PollDeleted -> {
8883
if (event.fid != fid.rawValue) return
8984
state.onPollDeleted(event.pollId)
@@ -96,12 +91,12 @@ internal class ActivityEventHandler(
9691

9792
is StateUpdateEvent.PollVoteCasted -> {
9893
if (event.fid != fid.rawValue) return
99-
state.onPollVoteCasted(event.vote, event.pollId)
94+
state.onPollVoteUpserted(event.vote, event.pollId)
10095
}
10196

10297
is StateUpdateEvent.PollVoteChanged -> {
10398
if (event.fid != fid.rawValue) return
104-
state.onPollVoteChanged(event.vote, event.pollId)
99+
state.onPollVoteUpserted(event.vote, event.pollId)
105100
}
106101

107102
is StateUpdateEvent.PollVoteRemoved -> {

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,6 @@ internal class FeedEventHandler(private val fid: FeedId, private val state: Feed
180180
}
181181
}
182182

183-
is StateUpdateEvent.PollClosed -> {
184-
if (event.fid == fid.rawValue) {
185-
state.onPollClosed(event.poll.id)
186-
}
187-
}
188-
189183
is StateUpdateEvent.PollDeleted -> {
190184
if (event.fid == fid.rawValue) {
191185
state.onPollDeleted(event.pollId)
@@ -200,13 +194,13 @@ internal class FeedEventHandler(private val fid: FeedId, private val state: Feed
200194

201195
is StateUpdateEvent.PollVoteCasted -> {
202196
if (event.fid == fid.rawValue) {
203-
state.onPollVoteCasted(event.vote, event.pollId)
197+
state.onPollVoteUpserted(event.vote, event.pollId)
204198
}
205199
}
206200

207201
is StateUpdateEvent.PollVoteChanged -> {
208202
if (event.fid == fid.rawValue) {
209-
state.onPollVoteChanged(event.vote, event.pollId)
203+
state.onPollVoteUpserted(event.vote, event.pollId)
210204
}
211205
}
212206

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,18 @@ internal class PollVoteListEventHandler(
3434
when (event) {
3535
is StateUpdateEvent.PollVoteCasted -> {
3636
if (event.pollId != pollId) return
37-
state.pollVoteAdded(event.vote)
37+
state.pollVoteUpserted(event.vote)
3838
}
3939

4040
is StateUpdateEvent.PollVoteChanged -> {
4141
if (event.pollId != pollId) return
42-
state.pollVoteUpdated(event.vote)
42+
state.pollVoteUpserted(event.vote)
4343
}
4444

4545
is StateUpdateEvent.PollVoteRemoved -> {
4646
if (event.pollId != pollId) return
4747
state.pollVoteRemoved(event.vote.id)
4848
}
49-
5049
else -> {}
5150
}
5251
}

0 commit comments

Comments
 (0)