@@ -170,16 +170,7 @@ internal class FeedStateImpl(
170170 override fun onActivityUpdated (activity : ActivityData ) {
171171 if (feedQuery.activityFilter?.matches(activity) == false ) return
172172
173- // Update the activities list
174- _activities .update { current ->
175- current.updateIf({ it.id == activity.id }) { it.update(activity) }
176- }
177- // Update the pinned activities if the activity is pinned
178- _pinnedActivities .update { current ->
179- current.updateIf({ it.activity.id == activity.id }) { pin ->
180- pin.copy(activity = pin.activity.update(activity))
181- }
182- }
173+ updateActivitiesWhere({ it.id == activity.id }) { it.update(activity) }
183174 }
184175
185176 override fun onActivityRemoved (activityId : String ) {
@@ -197,46 +188,38 @@ internal class FeedStateImpl(
197188 }
198189
199190 override fun onBookmarkRemoved (bookmark : BookmarkData ) {
200- _activities .update { current -> current.deleteBookmark(bookmark, currentUserId) }
191+ updateActivitiesWhere({ it.id == bookmark.activity.id }) { activity ->
192+ activity.deleteBookmark(bookmark, currentUserId)
193+ }
201194 }
202195
203196 override fun onBookmarkUpserted (bookmark : BookmarkData ) {
204- _activities .update { current -> current.upsertBookmark(bookmark, currentUserId) }
197+ updateActivitiesWhere({ it.id == bookmark.activity.id }) { activity ->
198+ activity.upsertBookmark(bookmark, currentUserId)
199+ }
205200 }
206201
207202 override fun onCommentAdded (comment : CommentData ) {
208- _activities .update { current ->
209- current.map {
210- if (it.id == comment.objectId) {
211- it.addComment(comment)
212- } else {
213- it
214- }
215- }
203+ updateActivitiesWhere({ it.id == comment.objectId }) { activity ->
204+ activity.addComment(comment)
216205 }
217206 }
218207
219208 override fun onCommentRemoved (comment : CommentData ) {
220- _activities .update { current ->
221- current.map {
222- if (it.id == comment.objectId) {
223- it.removeComment(comment)
224- } else {
225- it
226- }
227- }
209+ updateActivitiesWhere({ it.id == comment.objectId }) { activity ->
210+ activity.removeComment(comment)
228211 }
229212 }
230213
231214 override fun onCommentReactionRemoved (comment : CommentData , reaction : FeedsReactionData ) {
232- _activities .update { current ->
233- current .removeCommentReaction(comment, reaction, currentUserId)
215+ updateActivitiesWhere({ it.id == comment.objectId }) { activity ->
216+ activity .removeCommentReaction(comment, reaction, currentUserId)
234217 }
235218 }
236219
237220 override fun onCommentReactionUpserted (comment : CommentData , reaction : FeedsReactionData ) {
238- _activities .update { current ->
239- current .upsertCommentReaction(comment, reaction, currentUserId)
221+ updateActivitiesWhere({ it.id == comment.objectId }) { activity ->
222+ activity .upsertCommentReaction(comment, reaction, currentUserId)
240223 }
241224 }
242225
@@ -279,64 +262,48 @@ internal class FeedStateImpl(
279262 }
280263
281264 override fun onReactionUpserted (reaction : FeedsReactionData , activity : ActivityData ) {
282- _activities .update { current ->
283- current.updateIf({ it.id == reaction.activityId }) { currentActivity ->
284- currentActivity.upsertReaction(activity, reaction, currentUserId)
285- }
265+ updateActivitiesWhere({ it.id == reaction.activityId }) { currentActivity ->
266+ currentActivity.upsertReaction(activity, reaction, currentUserId)
286267 }
287268 }
288269
289270 override fun onReactionRemoved (reaction : FeedsReactionData , activity : ActivityData ) {
290- _activities .update { current ->
291- current.updateIf({ it.id == reaction.activityId }) { currentActivity ->
292- currentActivity.removeReaction(activity, reaction, currentUserId)
293- }
271+ updateActivitiesWhere({ it.id == reaction.activityId }) { currentActivity ->
272+ currentActivity.removeReaction(activity, reaction, currentUserId)
294273 }
295274 }
296275
297276 override fun onPollClosed (id : String ) {
298- _activities .update { current ->
299- current.updateIf({ it.poll?.id == id }) { activity ->
300- activity.copy(poll = activity.poll?.setClosed())
301- }
277+ updateActivitiesWhere({ it.poll?.id == id }) { activity ->
278+ activity.copy(poll = activity.poll?.setClosed())
302279 }
303280 }
304281
305282 override fun onPollDeleted (id : String ) {
306- _activities .update { current ->
307- current.updateIf({ it.poll?.id == id }) { activity -> activity.copy(poll = null ) }
308- }
283+ updateActivitiesWhere({ it.poll?.id == id }) { activity -> activity.copy(poll = null ) }
309284 }
310285
311286 override fun onPollUpdated (poll : PollData ) {
312- _activities .update { current ->
313- current.updateIf({ it.poll?.id == poll.id }) { activity ->
314- activity.copy(poll = activity.poll?.update(poll))
315- }
287+ updateActivitiesWhere({ it.poll?.id == poll.id }) { activity ->
288+ activity.copy(poll = activity.poll?.update(poll))
316289 }
317290 }
318291
319292 override fun onPollVoteCasted (vote : PollVoteData , pollId : String ) {
320- _activities .update { current ->
321- current.updateIf({ it.poll?.id == pollId }) { activity ->
322- activity.copy(poll = activity.poll?.castVote(vote, currentUserId))
323- }
293+ updateActivitiesWhere({ it.poll?.id == pollId }) { activity ->
294+ activity.copy(poll = activity.poll?.castVote(vote, currentUserId))
324295 }
325296 }
326297
327298 override fun onPollVoteChanged (vote : PollVoteData , pollId : String ) {
328- _activities .update { current ->
329- current.updateIf({ it.poll?.id == pollId }) { activity ->
330- activity.copy(poll = activity.poll?.castVote(vote, currentUserId))
331- }
299+ updateActivitiesWhere({ it.poll?.id == pollId }) { activity ->
300+ activity.copy(poll = activity.poll?.castVote(vote, currentUserId))
332301 }
333302 }
334303
335304 override fun onPollVoteRemoved (vote : PollVoteData , pollId : String ) {
336- _activities .update { current ->
337- current.updateIf({ it.poll?.id == pollId }) { activity ->
338- activity.copy(poll = activity.poll?.removeVote(vote, currentUserId))
339- }
305+ updateActivitiesWhere({ it.poll?.id == pollId }) { activity ->
306+ activity.copy(poll = activity.poll?.removeVote(vote, currentUserId))
340307 }
341308 }
342309
@@ -369,6 +336,19 @@ internal class FeedStateImpl(
369336 removeFollow(follow)
370337 addFollow(follow)
371338 }
339+
340+ private fun updateActivitiesWhere (
341+ filter : (ActivityData ) -> Boolean ,
342+ update : (ActivityData ) -> ActivityData ,
343+ ) {
344+ _activities .update { current -> current.updateIf(filter = filter, update = update) }
345+ _pinnedActivities .update { current ->
346+ current.updateIf(
347+ filter = { filter(it.activity) },
348+ update = { pin -> pin.copy(activity = update(pin.activity)) },
349+ )
350+ }
351+ }
372352}
373353
374354/* *
0 commit comments