Skip to content

Commit b53a3db

Browse files
authored
Handle CommentUpdated and ActivityDeleted events (#124)
* Handle CommentUpdated for Feed * Handle CommentUpdated for Activity * Handle ActivityDeleted for ActivityCommentList * Handle ActivityDeleted for ActivityReactionList
1 parent 387f241 commit b53a3db

24 files changed

+142
-38
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ internal fun ActivityData.update(
9898
* @param comment The comment to be added.
9999
* @return A new [ActivityData] instance with the updated comments and comment count.
100100
*/
101-
internal fun ActivityData.addComment(comment: CommentData): ActivityData {
101+
internal fun ActivityData.upsertComment(comment: CommentData): ActivityData {
102102
val updatedComments = this.comments.upsert(comment, CommentData::id)
103103
val updatedCommentCount =
104104
if (updatedComments.size > this.comments.size) {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ internal class ActivityCommentListStateImpl(
6161
override val pagination: PaginationData?
6262
get() = _pagination
6363

64+
override fun onActivityRemoved() {
65+
_comments.update { emptyList() }
66+
}
67+
6468
override fun onQueryMoreComments(result: PaginationResult<ThreadedCommentData>) {
6569
_pagination = result.pagination
6670
_comments.update { current ->
@@ -179,6 +183,9 @@ internal interface ActivityCommentListMutableState :
179183
/** An interface that defines the methods for updating the state of an activity comment list. */
180184
internal interface ActivityCommentListStateUpdates {
181185

186+
/** Handles the deletion of the parent activity. */
187+
fun onActivityRemoved()
188+
182189
/**
183190
* Handles the result of a query for comments.
184191
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ internal class ActivityImpl(
108108

109109
override suspend fun getComment(commentId: String): Result<CommentData> {
110110
return commentsRepository.getComment(commentId).onSuccess { comment ->
111-
subscriptionManager.onEvent(StateUpdateEvent.CommentUpdated(comment))
111+
subscriptionManager.onEvent(StateUpdateEvent.CommentUpdated(fid.rawValue, comment))
112112
}
113113
}
114114

@@ -152,7 +152,7 @@ internal class ActivityImpl(
152152
request: UpdateCommentRequest,
153153
): Result<CommentData> {
154154
return commentsRepository.updateComment(commentId, request).onSuccess {
155-
subscriptionManager.onEvent(StateUpdateEvent.CommentUpdated(it))
155+
subscriptionManager.onEvent(StateUpdateEvent.CommentUpdated(fid.rawValue, it))
156156
}
157157
}
158158

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import io.getstream.feeds.android.client.api.state.ActivityListState
2424
import io.getstream.feeds.android.client.api.state.query.ActivitiesQuery
2525
import io.getstream.feeds.android.client.api.state.query.ActivitiesSort
2626
import io.getstream.feeds.android.client.internal.model.PaginationResult
27-
import io.getstream.feeds.android.client.internal.model.addComment
2827
import io.getstream.feeds.android.client.internal.model.deleteBookmark
2928
import io.getstream.feeds.android.client.internal.model.removeComment
3029
import io.getstream.feeds.android.client.internal.model.removeCommentReaction
3130
import io.getstream.feeds.android.client.internal.model.removeReaction
3231
import io.getstream.feeds.android.client.internal.model.upsertBookmark
32+
import io.getstream.feeds.android.client.internal.model.upsertComment
3333
import io.getstream.feeds.android.client.internal.model.upsertCommentReaction
3434
import io.getstream.feeds.android.client.internal.model.upsertReaction
3535
import io.getstream.feeds.android.client.internal.state.query.ActivitiesQueryConfig
@@ -111,14 +111,10 @@ internal class ActivityListStateImpl(
111111
}
112112
}
113113

114-
override fun onCommentAdded(comment: CommentData) {
114+
override fun onCommentUpserted(comment: CommentData) {
115115
_activities.update { current ->
116-
current.map { activity ->
117-
if (activity.id == comment.objectId) {
118-
activity.addComment(comment)
119-
} else {
120-
activity
121-
}
116+
current.updateIf({ it.id == comment.objectId }) { activity ->
117+
activity.upsertComment(comment)
122118
}
123119
}
124120
}
@@ -219,11 +215,11 @@ internal interface ActivityListStateUpdates {
219215
fun onBookmarkUpserted(bookmark: BookmarkData)
220216

221217
/**
222-
* Called when a comment is added to an activity.
218+
* Called when a comment is added to or updated in the activity.
223219
*
224-
* @param comment The comment that was added.
220+
* @param comment The comment that was added or updated.
225221
*/
226-
fun onCommentAdded(comment: CommentData)
222+
fun onCommentUpserted(comment: CommentData)
227223

228224
/**
229225
* Called when a comment is removed from an activity.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ internal class ActivityReactionListStateImpl(override val query: ActivityReactio
5858
override val pagination: PaginationData?
5959
get() = _pagination
6060

61+
override fun onActivityRemoved() {
62+
_reactions.update { emptyList() }
63+
}
64+
6165
override fun onQueryMoreActivityReactions(
6266
result: PaginationResult<FeedsReactionData>,
6367
queryConfig: ActivityReactionsQueryConfig,
@@ -93,6 +97,9 @@ internal interface ActivityReactionListMutableState :
9397
/** Interface for handling updates to the state of activity reactions in a list. */
9498
internal interface ActivityReactionListStateUpdates {
9599

100+
/** Handles the deletion of the parent activity. */
101+
fun onActivityRemoved()
102+
96103
/**
97104
* Handles the loading of activity reactions.
98105
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ internal class FeedImpl(
260260

261261
override suspend fun getComment(commentId: String): Result<CommentData> {
262262
return commentsRepository.getComment(commentId).onSuccess {
263-
subscriptionManager.onEvent(StateUpdateEvent.CommentUpdated(it))
263+
subscriptionManager.onEvent(StateUpdateEvent.CommentUpdated(fid.rawValue, it))
264264
}
265265
}
266266

@@ -278,7 +278,7 @@ internal class FeedImpl(
278278
request: UpdateCommentRequest,
279279
): Result<CommentData> {
280280
return commentsRepository.updateComment(commentId, request).onSuccess {
281-
subscriptionManager.onEvent(StateUpdateEvent.CommentUpdated(it))
281+
subscriptionManager.onEvent(StateUpdateEvent.CommentUpdated(fid.rawValue, it))
282282
}
283283
}
284284

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import io.getstream.feeds.android.client.api.state.query.ActivitiesSort
3434
import io.getstream.feeds.android.client.api.state.query.FeedQuery
3535
import io.getstream.feeds.android.client.internal.model.PaginationResult
3636
import io.getstream.feeds.android.client.internal.model.QueryConfiguration
37-
import io.getstream.feeds.android.client.internal.model.addComment
3837
import io.getstream.feeds.android.client.internal.model.deleteBookmark
3938
import io.getstream.feeds.android.client.internal.model.isFollowRequest
4039
import io.getstream.feeds.android.client.internal.model.isFollowerOf
@@ -45,6 +44,7 @@ import io.getstream.feeds.android.client.internal.model.removeReaction
4544
import io.getstream.feeds.android.client.internal.model.removeVote
4645
import io.getstream.feeds.android.client.internal.model.update
4746
import io.getstream.feeds.android.client.internal.model.upsertBookmark
47+
import io.getstream.feeds.android.client.internal.model.upsertComment
4848
import io.getstream.feeds.android.client.internal.model.upsertCommentReaction
4949
import io.getstream.feeds.android.client.internal.model.upsertReaction
5050
import io.getstream.feeds.android.client.internal.model.upsertVote
@@ -195,9 +195,9 @@ internal class FeedStateImpl(
195195
}
196196
}
197197

198-
override fun onCommentAdded(comment: CommentData) {
198+
override fun onCommentUpserted(comment: CommentData) {
199199
updateActivitiesWhere({ it.id == comment.objectId }) { activity ->
200-
activity.addComment(comment)
200+
activity.upsertComment(comment)
201201
}
202202
}
203203

@@ -389,7 +389,7 @@ internal interface FeedStateUpdates {
389389
fun onBookmarkUpserted(bookmark: BookmarkData)
390390

391391
/** Handles updates to the feed state when a comment is added or removed. */
392-
fun onCommentAdded(comment: CommentData)
392+
fun onCommentUpserted(comment: CommentData)
393393

394394
/** Handles updates to the feed state when a comment is removed. */
395395
fun onCommentRemoved(comment: CommentData)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ internal sealed interface StateUpdateEvent {
121121

122122
data class CommentDeleted(val fid: String, val comment: CommentData) : StateUpdateEvent
123123

124-
data class CommentUpdated(val comment: CommentData) : StateUpdateEvent
124+
data class CommentUpdated(val fid: String, val comment: CommentData) : StateUpdateEvent
125125

126126
data class CommentReactionAdded(
127127
val fid: String,
@@ -222,7 +222,7 @@ internal fun WSEvent.toModel(): StateUpdateEvent? =
222222

223223
is CommentAddedEvent -> StateUpdateEvent.CommentAdded(fid, comment.toModel())
224224

225-
is CommentUpdatedEvent -> StateUpdateEvent.CommentUpdated(comment.toModel())
225+
is CommentUpdatedEvent -> StateUpdateEvent.CommentUpdated(fid, comment.toModel())
226226

227227
is CommentDeletedEvent -> StateUpdateEvent.CommentDeleted(fid, comment.toModel())
228228

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ internal class ActivityCommentListEventHandler(
2828

2929
override fun onEvent(event: StateUpdateEvent) {
3030
when (event) {
31+
is StateUpdateEvent.ActivityDeleted -> {
32+
if (event.activityId == objectId) {
33+
state.onActivityRemoved()
34+
}
35+
}
36+
3137
is StateUpdateEvent.CommentAdded -> {
3238
if (event.comment.objectId == objectId && event.comment.objectType == objectType) {
3339
state.onCommentAdded(ThreadedCommentData(event.comment))

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ internal class ActivityListEventHandler(
5353
is StateUpdateEvent.BookmarkAdded -> state.onBookmarkUpserted(event.bookmark)
5454
is StateUpdateEvent.BookmarkDeleted -> state.onBookmarkRemoved(event.bookmark)
5555
is StateUpdateEvent.BookmarkUpdated -> state.onBookmarkUpserted(event.bookmark)
56-
is StateUpdateEvent.CommentAdded -> state.onCommentAdded(event.comment)
56+
is StateUpdateEvent.CommentAdded -> state.onCommentUpserted(event.comment)
5757
is StateUpdateEvent.CommentDeleted -> state.onCommentRemoved(event.comment)
58+
is StateUpdateEvent.CommentUpdated -> state.onCommentUpserted(event.comment)
5859
is StateUpdateEvent.CommentReactionAdded ->
5960
state.onCommentReactionUpserted(event.comment, event.reaction)
6061

0 commit comments

Comments
 (0)