@@ -25,15 +25,17 @@ import io.getstream.feeds.android.client.api.model.PollData
25
25
import io.getstream.feeds.android.client.api.model.PollOptionData
26
26
import io.getstream.feeds.android.client.api.model.PollVoteData
27
27
import io.getstream.feeds.android.client.api.model.ThreadedCommentData
28
+ import io.getstream.feeds.android.client.api.model.addOption
29
+ import io.getstream.feeds.android.client.api.model.removeOption
28
30
import io.getstream.feeds.android.client.api.model.request.ActivityAddCommentRequest
31
+ import io.getstream.feeds.android.client.api.model.updateOption
29
32
import io.getstream.feeds.android.client.api.state.Activity
30
33
import io.getstream.feeds.android.client.api.state.ActivityState
31
34
import io.getstream.feeds.android.client.internal.repository.ActivitiesRepository
32
35
import io.getstream.feeds.android.client.internal.repository.CommentsRepository
33
36
import io.getstream.feeds.android.client.internal.repository.PollsRepository
34
37
import io.getstream.feeds.android.client.internal.state.event.StateUpdateEvent
35
38
import io.getstream.feeds.android.client.internal.state.event.handler.ActivityEventHandler
36
- import io.getstream.feeds.android.client.internal.subscribe.FeedsEventListener
37
39
import io.getstream.feeds.android.client.internal.subscribe.StateUpdateEventListener
38
40
import io.getstream.feeds.android.client.internal.subscribe.onEvent
39
41
import io.getstream.feeds.android.client.internal.utils.flatMap
@@ -62,8 +64,6 @@ import io.getstream.feeds.android.network.models.UpdatePollRequest
62
64
* @property pollsRepository The repository used to fetch and manage polls.
63
65
* @property commentList The list of comments associated with this activity.
64
66
* @property subscriptionManager The manager for state update subscriptions.
65
- * @property socketSubscriptionManager The manager for WebSocket subscriptions to receive real-time
66
- * updates.
67
67
*/
68
68
internal class ActivityImpl (
69
69
override val activityId : String ,
@@ -74,7 +74,6 @@ internal class ActivityImpl(
74
74
private val pollsRepository : PollsRepository ,
75
75
private val commentList : ActivityCommentListImpl ,
76
76
private val subscriptionManager : StreamSubscriptionManager <StateUpdateEventListener >,
77
- socketSubscriptionManager : StreamSubscriptionManager <FeedsEventListener >,
78
77
) : Activity {
79
78
80
79
private val _state : ActivityStateImpl = ActivityStateImpl (currentUserId, commentList.state)
@@ -83,15 +82,17 @@ internal class ActivityImpl(
83
82
ActivityEventHandler (fid = fid, activityId = activityId, state = _state )
84
83
85
84
init {
86
- socketSubscriptionManager .subscribe(eventHandler)
85
+ subscriptionManager .subscribe(eventHandler)
87
86
}
88
87
89
88
override val state: ActivityState
90
89
get() = _state
91
90
92
91
override suspend fun get (): Result <ActivityData > {
93
92
val activity =
94
- activitiesRepository.getActivity(activityId).onSuccess { _state .onActivityUpdated(it) }
93
+ activitiesRepository.getActivity(activityId).onSuccess {
94
+ subscriptionManager.onEvent(StateUpdateEvent .ActivityUpdated (fid.rawValue, it))
95
+ }
95
96
// Query the comments as well (state will be updated automatically)
96
97
queryComments()
97
98
return activity
@@ -135,7 +136,9 @@ internal class ActivityImpl(
135
136
.deleteComment(commentId, hardDelete)
136
137
.onSuccess { (comment, activity) ->
137
138
subscriptionManager.onEvent(StateUpdateEvent .CommentDeleted (comment))
138
- _state .onActivityUpdated(activity)
139
+ subscriptionManager.onEvent(
140
+ StateUpdateEvent .ActivityUpdated (fid.rawValue, activity)
141
+ )
139
142
}
140
143
.map {}
141
144
}
@@ -180,105 +183,133 @@ internal class ActivityImpl(
180
183
override suspend fun pin (): Result <Unit > {
181
184
return activitiesRepository
182
185
.pin(activityId, fid)
183
- .onSuccess { _state .onActivityUpdated(it) }
186
+ .onSuccess {
187
+ subscriptionManager.onEvent(StateUpdateEvent .ActivityUpdated (fid.rawValue, it))
188
+ }
184
189
.map { Unit }
185
190
}
186
191
187
192
override suspend fun unpin (): Result <Unit > {
188
193
return activitiesRepository
189
194
.unpin(activityId, fid)
190
- .onSuccess { _state .onActivityUpdated(it) }
195
+ .onSuccess {
196
+ subscriptionManager.onEvent(StateUpdateEvent .ActivityUpdated (fid.rawValue, it))
197
+ }
191
198
.map { Unit }
192
199
}
193
200
194
201
override suspend fun closePoll (): Result <PollData > {
195
- return pollId().flatMap { pollId ->
196
- pollsRepository.closePoll(pollId = pollId).onSuccess { _state .onPollUpdated(it) }
202
+ return poll().flatMap { poll ->
203
+ pollsRepository.closePoll(pollId = poll.id).onSuccess {
204
+ subscriptionManager.onEvent(StateUpdateEvent .PollUpdated (fid.rawValue, it))
205
+ }
197
206
}
198
207
}
199
208
200
209
override suspend fun deletePoll (userId : String? ): Result <Unit > {
201
- return pollId ().flatMap { pollId ->
202
- pollsRepository.deletePoll(pollId = pollId , userId = userId).onSuccess {
203
- _state .onPollDeleted(pollId )
210
+ return poll ().flatMap { poll ->
211
+ pollsRepository.deletePoll(pollId = poll.id , userId = userId).onSuccess {
212
+ subscriptionManager.onEvent( StateUpdateEvent . PollDeleted (fid.rawValue, poll.id) )
204
213
}
205
214
}
206
215
}
207
216
208
217
override suspend fun getPoll (userId : String? ): Result <PollData > {
209
- return pollId ().flatMap { pollId ->
210
- pollsRepository.getPoll(pollId = pollId , userId = userId).onSuccess {
211
- _state .onPollUpdated(it )
218
+ return poll ().flatMap { poll ->
219
+ pollsRepository.getPoll(pollId = poll.id , userId = userId).onSuccess {
220
+ subscriptionManager.onEvent( StateUpdateEvent . PollUpdated (fid.rawValue, it) )
212
221
}
213
222
}
214
223
}
215
224
216
225
override suspend fun updatePollPartial (request : UpdatePollPartialRequest ): Result <PollData > {
217
- return pollId ().flatMap { pollId ->
218
- pollsRepository.updatePollPartial(pollId , request).onSuccess {
219
- _state .onPollUpdated(it )
226
+ return poll ().flatMap { poll ->
227
+ pollsRepository.updatePollPartial(poll.id , request).onSuccess {
228
+ subscriptionManager.onEvent( StateUpdateEvent . PollUpdated (fid.rawValue, it) )
220
229
}
221
230
}
222
231
}
223
232
224
233
override suspend fun updatePoll (request : UpdatePollRequest ): Result <PollData > {
225
- return pollsRepository.updatePoll(request).onSuccess { _state .onPollUpdated(it) }
234
+ return pollsRepository.updatePoll(request).onSuccess {
235
+ subscriptionManager.onEvent(StateUpdateEvent .PollUpdated (fid.rawValue, it))
236
+ }
226
237
}
227
238
228
239
override suspend fun createPollOption (
229
240
request : CreatePollOptionRequest
230
241
): Result <PollOptionData > {
231
- return pollId().flatMap { pollId ->
232
- pollsRepository.createPollOption(pollId, request).onSuccess {
233
- _state .onOptionCreated(it)
242
+ return poll().flatMap { poll ->
243
+ pollsRepository.createPollOption(poll.id, request).onSuccess {
244
+ val newPoll = poll.addOption(it)
245
+ subscriptionManager.onEvent(StateUpdateEvent .PollUpdated (fid.rawValue, newPoll))
234
246
}
235
247
}
236
248
}
237
249
238
250
override suspend fun deletePollOption (optionId : String , userId : String? ): Result <Unit > {
239
- return pollId ().flatMap { pollId ->
251
+ return poll ().flatMap { poll ->
240
252
pollsRepository
241
- .deletePollOption(pollId = pollId, optionId = optionId, userId = userId)
242
- .onSuccess { _state .onOptionDeleted(optionId) }
253
+ .deletePollOption(pollId = poll.id, optionId = optionId, userId = userId)
254
+ .onSuccess {
255
+ val newPoll = poll.removeOption(optionId)
256
+ subscriptionManager.onEvent(StateUpdateEvent .PollUpdated (fid.rawValue, newPoll))
257
+ }
243
258
}
244
259
}
245
260
246
261
override suspend fun getPollOption (optionId : String , userId : String? ): Result <PollOptionData > {
247
- return pollId ().flatMap { pollId ->
262
+ return poll ().flatMap { poll ->
248
263
pollsRepository
249
- .getPollOption(pollId = pollId, optionId = optionId, userId = userId)
250
- .onSuccess { _state .onOptionUpdated(it) }
264
+ .getPollOption(pollId = poll.id, optionId = optionId, userId = userId)
265
+ .onSuccess {
266
+ val newPoll = poll.updateOption(it)
267
+ subscriptionManager.onEvent(StateUpdateEvent .PollUpdated (fid.rawValue, newPoll))
268
+ }
251
269
}
252
270
}
253
271
254
272
override suspend fun updatePollOption (
255
273
request : UpdatePollOptionRequest
256
274
): Result <PollOptionData > {
257
- return pollId().flatMap { pollId ->
258
- pollsRepository.updatePollOption(pollId, request).onSuccess {
259
- _state .onOptionUpdated(it)
275
+ return poll().flatMap { poll ->
276
+ pollsRepository.updatePollOption(poll.id, request).onSuccess {
277
+ val newPoll = poll.updateOption(it)
278
+ subscriptionManager.onEvent(StateUpdateEvent .PollUpdated (fid.rawValue, newPoll))
260
279
}
261
280
}
262
281
}
263
282
264
283
override suspend fun castPollVote (request : CastPollVoteRequest ): Result <PollVoteData ?> {
265
- return pollId ().flatMap { pollId ->
284
+ return poll ().flatMap { poll ->
266
285
pollsRepository
267
- .castPollVote(activityId = activityId, pollId = pollId, request = request)
268
- .onSuccess { it?.let { vote -> _state .onPollVoteCasted(vote, pollId) } }
286
+ .castPollVote(activityId = activityId, pollId = poll.id, request = request)
287
+ .onSuccess {
288
+ it?.let { vote ->
289
+ subscriptionManager.onEvent(
290
+ StateUpdateEvent .PollVoteCasted (fid.rawValue, poll.id, vote)
291
+ )
292
+ }
293
+ }
269
294
}
270
295
}
271
296
272
297
override suspend fun deletePollVote (voteId : String , userId : String? ): Result <PollVoteData ?> {
273
- return pollId ().flatMap { pollId ->
298
+ return poll ().flatMap { poll ->
274
299
pollsRepository
275
300
.deletePollVote(
276
301
activityId = activityId,
277
- pollId = pollId ,
302
+ pollId = poll.id ,
278
303
voteId = voteId,
279
304
userId = userId,
280
305
)
281
- .onSuccess { it?.let { vote -> _state .onPollVoteRemoved(vote, pollId) } }
306
+ .onSuccess {
307
+ it?.let { vote ->
308
+ subscriptionManager.onEvent(
309
+ StateUpdateEvent .PollVoteRemoved (fid.rawValue, poll.id, vote)
310
+ )
311
+ }
312
+ }
282
313
}
283
314
}
284
315
@@ -291,11 +322,11 @@ internal class ActivityImpl(
291
322
}
292
323
}
293
324
294
- private suspend fun pollId (): Result <String > {
325
+ private suspend fun poll (): Result <PollData > {
295
326
return ensureActivityLoaded().flatMap {
296
327
val poll = it.poll
297
328
if (poll != null ) {
298
- Result .success(poll.id )
329
+ Result .success(poll)
299
330
} else {
300
331
Result .failure(IllegalStateException (" Activity does not have a poll" ))
301
332
}
0 commit comments