-
Notifications
You must be signed in to change notification settings - Fork 0
Handle missing events for various state objects #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
53a4c94
Handle CommentDeletedEvent for CommentList
gpunto b54e650
Handle BookmarkDeletedEvent for BookmarkList
gpunto cec9fe3
Filter events handled by MemberListEventHandler by fid
gpunto 6331d8d
Handle FeedMemberAddedEvent for MemberList
gpunto 36fee26
Handle ActivityReactionAddedEvent for ActivityReactionList
gpunto 372602d
Handle ActivityRemovedFromFeedEvent for Feed
gpunto 3ce4737
Update FeedsReactionData id to include comment id and type
gpunto 678a438
Update addReaction logic to increment the count when the reaction was…
gpunto 22e56e2
Unify add reaction logic
gpunto a9160ef
Update removeReaction logic to decrement the count when the reaction …
gpunto 396c024
Unify remove reaction logic
gpunto 06289cc
Handle ActivityReactionAdded, ActivityReactionDeleted, ActivityUpdate…
gpunto ff3727f
Reduce duplication in ActivityEventHandlerTest
gpunto 081a827
Extend ActivityEventHandlerTest to cover new events
gpunto 65d47b3
Handle FollowDeletedEvent for FollowList
gpunto 927ff82
Handle FeedDeletedEvent for FeedList
gpunto 38110f7
Notify fees state after successfully adding/removing a reaction
gpunto bd30ecf
Handler PollVoteCastedFeedEvent for PollVoteList
gpunto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...roid-client/src/main/kotlin/io/getstream/feeds/android/client/internal/model/Reactions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-feeds-android/blob/main/LICENSE | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.getstream.feeds.android.client.internal.model | ||
|
||
import io.getstream.feeds.android.client.api.model.FeedsReactionData | ||
import io.getstream.feeds.android.client.api.model.ReactionGroupData | ||
import io.getstream.feeds.android.client.api.model.decrement | ||
import io.getstream.feeds.android.client.api.model.increment | ||
import io.getstream.feeds.android.client.api.model.isEmpty | ||
import io.getstream.feeds.android.client.internal.utils.upsert | ||
|
||
internal inline fun <T> addReaction( | ||
VelikovPetar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ownReactions: List<FeedsReactionData>, | ||
latestReactions: List<FeedsReactionData>, | ||
reactionGroups: Map<String, ReactionGroupData>, | ||
reaction: FeedsReactionData, | ||
currentUserId: String, | ||
update: | ||
( | ||
latestReactions: List<FeedsReactionData>, | ||
reactionGroups: Map<String, ReactionGroupData>, | ||
reactionCount: Int, | ||
ownReactions: List<FeedsReactionData>, | ||
) -> T, | ||
): T { | ||
val ownReaction = reaction.user.id == currentUserId | ||
val updatedOwnReactions = | ||
if (ownReaction) { | ||
ownReactions.upsert(reaction, FeedsReactionData::id) | ||
} else { | ||
ownReactions | ||
} | ||
val updatedLatestReactions = latestReactions.upsert(reaction, FeedsReactionData::id) | ||
val inserted = | ||
updatedOwnReactions.size > ownReactions.size || | ||
updatedLatestReactions.size > latestReactions.size | ||
val reactionGroup = | ||
reactionGroups[reaction.type] | ||
?: ReactionGroupData(count = 1, reaction.createdAt, reaction.createdAt) | ||
// Increment the count if the reaction was inserted (not an update of existing) | ||
val updatedReactionGroup = | ||
if (inserted) { | ||
reactionGroup.increment(reaction.createdAt) | ||
} else { | ||
reactionGroup | ||
} | ||
val updatedReactionGroups = | ||
reactionGroups.toMutableMap().apply { this[reaction.type] = updatedReactionGroup } | ||
val updatedReactionCount = updatedReactionGroups.values.sumOf(ReactionGroupData::count) | ||
return update( | ||
updatedLatestReactions, | ||
updatedReactionGroups, | ||
updatedReactionCount, | ||
updatedOwnReactions, | ||
) | ||
} | ||
|
||
internal inline fun <T> removeReaction( | ||
ownReactions: List<FeedsReactionData>, | ||
latestReactions: List<FeedsReactionData>, | ||
reactionGroups: Map<String, ReactionGroupData>, | ||
reaction: FeedsReactionData, | ||
currentUserId: String, | ||
update: | ||
( | ||
latestReactions: List<FeedsReactionData>, | ||
reactionGroups: Map<String, ReactionGroupData>?, | ||
reactionCount: Int?, | ||
ownReactions: List<FeedsReactionData>, | ||
) -> T, | ||
): T { | ||
val ownReaction = reaction.user.id == currentUserId | ||
val updatedOwnReactions = | ||
if (ownReaction) { | ||
ownReactions.filter { it.id != reaction.id } | ||
} else { | ||
ownReactions | ||
} | ||
val updatedLatestReactions = latestReactions.filter { it.id != reaction.id } | ||
val removed = | ||
updatedOwnReactions.size < ownReactions.size || | ||
updatedLatestReactions.size < latestReactions.size | ||
val reactionGroup = reactionGroups[reaction.type] | ||
if (reactionGroup == null) { | ||
// If there is no reaction group for this type, just update latest and own reactions. | ||
// Note: This is only a hypothetical case, as we should always have a reaction group. | ||
return update(updatedLatestReactions, null, null, updatedOwnReactions) | ||
} | ||
// Decrement the count if the reaction was actually removed | ||
val updatedReactionGroup = | ||
if (removed) { | ||
reactionGroup.decrement(reaction.createdAt) | ||
} else { | ||
reactionGroup | ||
} | ||
val updatedReactionGroups = | ||
if (updatedReactionGroup.isEmpty) { | ||
reactionGroups - reaction.type // Remove empty group | ||
} else { | ||
reactionGroups.toMutableMap().apply { this[reaction.type] = updatedReactionGroup } | ||
} | ||
val updatedReactionCount = updatedReactionGroups.values.sumOf(ReactionGroupData::count) | ||
return update( | ||
updatedLatestReactions, | ||
updatedReactionGroups, | ||
updatedReactionCount, | ||
updatedOwnReactions, | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.