1+ import 'dart:math' as math;
2+
13import 'package:freezed_annotation/freezed_annotation.dart' ;
24import 'package:state_notifier/state_notifier.dart' ;
35import 'package:stream_core/stream_core.dart' ;
46
57import '../models/activity_data.dart' ;
6- import '../models/activity_pin_data.dart' ;
78import '../models/comment_data.dart' ;
89import '../models/feeds_reaction_data.dart' ;
9- import '../models/mark_activity_data.dart' ;
1010import '../models/pagination_data.dart' ;
1111import '../models/poll_data.dart' ;
1212import '../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
0 commit comments