Skip to content

Commit 28e0b45

Browse files
committed
Implement missing state update methods
1 parent ef0ff2d commit 28e0b45

File tree

6 files changed

+204
-80
lines changed

6 files changed

+204
-80
lines changed

packages/stream_feeds/lib/src/state/activity_state.dart

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import 'dart:math' as math;
2+
13
import 'package:freezed_annotation/freezed_annotation.dart';
24
import 'package:state_notifier/state_notifier.dart';
35
import 'package:stream_core/stream_core.dart';
46

57
import '../models/activity_data.dart';
6-
import '../models/activity_pin_data.dart';
78
import '../models/comment_data.dart';
89
import '../models/feeds_reaction_data.dart';
9-
import '../models/mark_activity_data.dart';
1010
import '../models/pagination_data.dart';
1111
import '../models/poll_data.dart';
1212
import '../models/poll_vote_data.dart';
@@ -20,7 +20,8 @@ part 'activity_state.freezed.dart';
2020
///
2121
/// Provides methods to update the activity state in response to data changes
2222
/// and real-time events from the Stream Feeds API.
23-
class ActivityStateNotifier extends StateNotifier<ActivityState> implements StateWithUpdatableActivity {
23+
class ActivityStateNotifier extends StateNotifier<ActivityState>
24+
implements StateWithUpdatableActivity {
2425
ActivityStateNotifier({
2526
required ActivityState initialState,
2627
required this.currentUserId,
@@ -45,6 +46,7 @@ class ActivityStateNotifier extends StateNotifier<ActivityState> implements Stat
4546
}
4647

4748
/// Handles the update of an activity.
49+
@override
4850
void onActivityUpdated(ActivityData activity) {
4951
state = state.copyWith(
5052
activity: activity,
@@ -53,6 +55,7 @@ class ActivityStateNotifier extends StateNotifier<ActivityState> implements Stat
5355
}
5456

5557
/// Handles when a poll is closed.
58+
@override
5659
void onPollClosed(PollData poll) {
5760
if (state.poll?.id != poll.id) return;
5861

@@ -61,12 +64,14 @@ class ActivityStateNotifier extends StateNotifier<ActivityState> implements Stat
6164
}
6265

6366
/// Handles when a poll is deleted.
67+
@override
6468
void onPollDeleted(String pollId) {
6569
if (state.poll?.id != pollId) return;
6670
state = state.copyWith(poll: null);
6771
}
6872

6973
/// Handles when a poll is updated.
74+
@override
7075
void onPollUpdated(PollData poll) {
7176
final currentPoll = state.poll;
7277
if (currentPoll == null || currentPoll.id != poll.id) return;
@@ -83,6 +88,7 @@ class ActivityStateNotifier extends StateNotifier<ActivityState> implements Stat
8388
}
8489

8590
/// Handles when a poll answer is casted.
91+
@override
8692
void onPollAnswerCasted(PollVoteData answer, PollData poll) {
8793
final currentPoll = state.poll;
8894
if (currentPoll == null || currentPoll.id != poll.id) return;
@@ -105,11 +111,13 @@ class ActivityStateNotifier extends StateNotifier<ActivityState> implements Stat
105111
}
106112

107113
/// Handles when a poll vote is casted (with poll data).
114+
@override
108115
void onPollVoteCasted(PollVoteData vote, PollData poll) {
109116
return onPollVoteChanged(vote, poll);
110117
}
111118

112119
/// Handles when a poll vote is changed.
120+
@override
113121
void onPollVoteChanged(PollVoteData vote, PollData poll) {
114122
final currentPoll = state.poll;
115123
if (currentPoll == null || currentPoll.id != poll.id) return;
@@ -129,6 +137,7 @@ class ActivityStateNotifier extends StateNotifier<ActivityState> implements Stat
129137
}
130138

131139
/// Handles when a poll answer is removed (with poll data).
140+
@override
132141
void onPollAnswerRemoved(PollVoteData answer, PollData poll) {
133142
final currentPoll = state.poll;
134143
if (currentPoll == null || currentPoll.id != poll.id) return;
@@ -150,6 +159,7 @@ class ActivityStateNotifier extends StateNotifier<ActivityState> implements Stat
150159
}
151160

152161
/// Handles when a poll vote is removed (with poll data).
162+
@override
153163
void onPollVoteRemoved(PollVoteData vote, PollData poll) {
154164
final currentPoll = state.poll;
155165
if (currentPoll == null || currentPoll.id != poll.id) return;
@@ -167,45 +177,63 @@ class ActivityStateNotifier extends StateNotifier<ActivityState> implements Stat
167177
state = state.copyWith(poll: updatedPoll);
168178
}
169179

170-
@override
171-
void dispose() {
172-
_removeCommentListListener?.call();
173-
super.dispose();
174-
}
175-
176-
@override
177-
void onActivityMarked(MarkActivityData activityMark) {
178-
// TODO: implement onActivityMarked
179-
}
180-
181-
@override
182-
void onActivityPinned(ActivityPinData activityPin) {
183-
// TODO: implement onActivityPinned
184-
}
185-
186-
@override
187-
void onActivityUnpinned(String activityId) {
188-
// TODO: implement onActivityUnpinned
189-
}
190-
191180
@override
192181
void onCommentAdded(CommentData comment) {
193-
// TODO: implement onCommentAdded
182+
// The comments are stored in the comment list, but that doesn't contain the total count.
183+
if (state.activity case final activity?) {
184+
state = state.copyWith(
185+
activity: activity.copyWith(
186+
commentCount: math.max(0, activity.commentCount + 1),
187+
),
188+
);
189+
}
190+
commentList.onCommentAdded(ThreadedCommentData.fromComment(comment));
194191
}
195192

196193
@override
197194
void onCommentRemoved(CommentData comment) {
198-
// TODO: implement onCommentRemoved
195+
// The comments are stored in the comment list, but that doesn't contain the total count.
196+
if (state.activity case final activity?) {
197+
state = state.copyWith(
198+
activity: activity.copyWith(
199+
commentCount: math.max(0, activity.commentCount - 1),
200+
),
201+
);
202+
}
203+
commentList.onCommentRemoved(comment.id);
199204
}
200205

201206
@override
202207
void onReactionAdded(FeedsReactionData reaction) {
203-
// TODO: implement onReactionAdded
208+
final activity = state.activity;
209+
if (activity == null || reaction.activityId != activity.id) return;
210+
if (reaction.commentId case final commentId?) {
211+
commentList.onCommentReactionAdded(commentId, reaction);
212+
} else {
213+
state = state.copyWith(
214+
activity: activity.addReaction(reaction, currentUserId),
215+
);
216+
}
204217
}
205218

206219
@override
207220
void onReactionRemoved(FeedsReactionData reaction) {
208-
// TODO: implement onReactionRemoved
221+
final activity = state.activity;
222+
if (activity == null || reaction.activityId != activity.id) return;
223+
224+
if (reaction.commentId case final commentId?) {
225+
commentList.onCommentReactionRemoved(commentId, reaction);
226+
} else {
227+
state = state.copyWith(
228+
activity: activity.removeReaction(reaction, currentUserId),
229+
);
230+
}
231+
}
232+
233+
@override
234+
void dispose() {
235+
_removeCommentListListener?.call();
236+
super.dispose();
209237
}
210238
}
211239

packages/stream_feeds/lib/src/state/event/feed_event_handler.dart

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import 'package:stream_core/stream_core.dart';
22

33
import '../../generated/api/models.dart' as api;
4-
import '../../models/activity_data.dart';
54
import '../../models/activity_pin_data.dart';
65
import '../../models/aggregated_activity_data.dart';
7-
import '../../models/bookmark_data.dart';
8-
import '../../models/comment_data.dart';
96
import '../../models/feed_data.dart';
10-
import '../../models/feeds_reaction_data.dart';
117
import '../../models/follow_data.dart';
128
import '../../models/mark_activity_data.dart';
139
import '../../repository/capabilities_repository.dart';
@@ -54,6 +50,26 @@ class FeedEventHandler with FeedCapabilitiesMixin implements StateEventHandler {
5450

5551
final fid = query.fid;
5652

53+
if (event is api.ActivityPinnedEvent) {
54+
if (event.fid != fid.rawValue) return;
55+
56+
state.onActivityPinned(event.pinnedActivity.toModel());
57+
return;
58+
}
59+
60+
if (event is api.ActivityUnpinnedEvent) {
61+
if (event.fid != fid.rawValue) return;
62+
63+
state.onActivityUnpinned(event.pinnedActivity.activity.id);
64+
return;
65+
}
66+
67+
if (event is api.ActivityMarkEvent) {
68+
if (event.fid != fid.rawValue) return;
69+
state.onActivityMarked(event.toModel());
70+
return;
71+
}
72+
5773
if (event is api.NotificationFeedUpdatedEvent) {
5874
if (event.fid != fid.rawValue) return;
5975
return state.onNotificationFeedUpdated(

packages/stream_feeds/lib/src/state/event/partial_activity_event_handler.dart

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import '../../../stream_feeds.dart';
22
import '../../../stream_feeds.dart' as api;
3-
import '../../models/activity_pin_data.dart';
43
import '../../models/comment_data.dart';
54
import '../../models/feeds_reaction_data.dart';
6-
import '../../models/mark_activity_data.dart';
75
import '../../repository/capabilities_repository.dart';
86
import '../../resolvers/resolvers.dart';
97
import 'feed_capabilities_mixin.dart';
@@ -20,7 +18,6 @@ class PartialActivityEventHandler with FeedCapabilitiesMixin {
2018
@override
2119
final CapabilitiesRepository capabilitiesRepository;
2220

23-
@override
2421
Future<bool> handleEvent(WsEvent event) async {
2522
if (event is api.ActivityUpdatedEvent) {
2623
if (event.fid != fid.rawValue) return false;
@@ -43,25 +40,6 @@ class PartialActivityEventHandler with FeedCapabilitiesMixin {
4340
state.onReactionRemoved(event.reaction.toModel());
4441
return true;
4542
}
46-
if (event is api.ActivityPinnedEvent) {
47-
if (event.fid != fid.rawValue) return false;
48-
49-
state.onActivityPinned(event.pinnedActivity.toModel());
50-
return true;
51-
}
52-
53-
if (event is api.ActivityUnpinnedEvent) {
54-
if (event.fid != fid.rawValue) return false;
55-
56-
state.onActivityUnpinned(event.pinnedActivity.activity.id);
57-
return true;
58-
}
59-
60-
if (event is api.ActivityMarkEvent) {
61-
if (event.fid != fid.rawValue) return false;
62-
state.onActivityMarked(event.toModel());
63-
return true;
64-
}
6543

6644
if (event is api.CommentAddedEvent) {
6745
if (event.fid != fid.rawValue) return false;
@@ -107,7 +85,6 @@ class PartialActivityEventHandler with FeedCapabilitiesMixin {
10785

10886
if (event is api.PollVoteChangedFeedEvent) {
10987
// Only handle events for this specific feed
110-
if (event.fid != fid.rawValue) return false;
11188
final vote = event.pollVote.toModel();
11289
final poll = event.poll.toModel();
11390
state.onPollVoteChanged(vote, poll);
@@ -136,9 +113,6 @@ abstract interface class StateWithUpdatableActivity {
136113
void onActivityUpdated(ActivityData activity);
137114
void onReactionAdded(FeedsReactionData reaction);
138115
void onReactionRemoved(FeedsReactionData reaction);
139-
void onActivityPinned(ActivityPinData activityPin);
140-
void onActivityUnpinned(String activityId);
141-
void onActivityMarked(MarkActivityData activityMark);
142116
void onCommentAdded(CommentData comment);
143117
void onCommentRemoved(CommentData comment);
144118

0 commit comments

Comments
 (0)