Skip to content

Commit 7fc1798

Browse files
committed
Merge poll logic
1 parent 28e0b45 commit 7fc1798

File tree

5 files changed

+115
-95
lines changed

5 files changed

+115
-95
lines changed

packages/stream_feeds/lib/src/models.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export 'models/feed_member_data.dart';
77
export 'models/feed_member_request_data.dart';
88
export 'models/feeds_config.dart';
99
export 'models/follow_data.dart';
10-
export 'models/poll_data.dart';
10+
export 'models/poll_data.dart' show PollData;
1111
export 'models/poll_option_data.dart';
1212
export 'models/poll_vote_data.dart';
1313
export 'models/push_notifications_config.dart';

packages/stream_feeds/lib/src/models/poll_data.dart

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,19 @@ extension PollDataMutations on PollData {
175175
return copyWith(options: updatedOptions);
176176
}
177177

178-
PollData castAnswer(PollVoteData answer, String currentUserId) {
179-
final updatedLatestAnswers = latestAnswers.let((it) {
178+
PollData castAnswer(
179+
PollVoteData answer,
180+
String currentUserId, {
181+
List<PollVoteData>? currentLatestAnswers,
182+
List<PollVoteData>? currentOwnVotesAndAnswers,
183+
}) {
184+
final updatedLatestAnswers =
185+
(currentLatestAnswers ?? latestAnswers).let((it) {
180186
return it.upsert(answer, key: (it) => it.id == answer.id);
181187
});
182188

183-
final updatedOwnVotesAndAnswers = ownVotesAndAnswers.let((it) {
189+
final updatedOwnVotesAndAnswers =
190+
(currentOwnVotesAndAnswers ?? ownVotesAndAnswers).let((it) {
184191
if (answer.userId != currentUserId) return it;
185192
return it.upsert(answer, key: (it) => it.id == answer.id);
186193
});
@@ -190,6 +197,65 @@ extension PollDataMutations on PollData {
190197
ownVotesAndAnswers: updatedOwnVotesAndAnswers,
191198
);
192199
}
200+
201+
PollData changeVote(
202+
PollVoteData vote,
203+
String currentUserId, {
204+
List<PollVoteData>? currentLatestVotes,
205+
List<PollVoteData>? currentOwnVotesAndAnswers,
206+
}) {
207+
final latestAnswers = currentLatestVotes ?? latestVotes;
208+
final ownVotesAndAnswers =
209+
(currentOwnVotesAndAnswers ?? this.ownVotesAndAnswers).let((it) {
210+
if (vote.userId != currentUserId) return it;
211+
return it.upsert(vote, key: (it) => it.id == vote.id);
212+
});
213+
214+
return copyWith(
215+
latestAnswers: latestAnswers,
216+
ownVotesAndAnswers: ownVotesAndAnswers,
217+
);
218+
}
219+
220+
PollData removeAnswer(
221+
PollVoteData answer,
222+
String currentUserId, {
223+
List<PollVoteData>? currentLatestAnswers,
224+
List<PollVoteData>? currentOwnVotesAndAnswers,
225+
}) {
226+
final latestAnswers =
227+
(currentLatestAnswers ?? this.latestAnswers).where((it) {
228+
return it.id != answer.id;
229+
}).toList();
230+
231+
final ownVotesAndAnswers =
232+
(currentOwnVotesAndAnswers ?? this.ownVotesAndAnswers).where((it) {
233+
return it.id != answer.id;
234+
}).toList();
235+
236+
return copyWith(
237+
latestAnswers: latestAnswers,
238+
ownVotesAndAnswers: ownVotesAndAnswers,
239+
);
240+
}
241+
242+
PollData removeVote(
243+
PollVoteData vote,
244+
String currentUserId, {
245+
List<PollVoteData>? currentLatestVotes,
246+
List<PollVoteData>? currentOwnVotesAndAnswers,
247+
}) {
248+
final latestAnswers = currentLatestVotes ?? latestVotes;
249+
final ownVotesAndAnswers =
250+
(currentOwnVotesAndAnswers ?? this.ownVotesAndAnswers).where((it) {
251+
return it.id != vote.id;
252+
}).toList();
253+
254+
return copyWith(
255+
latestAnswers: latestAnswers,
256+
ownVotesAndAnswers: ownVotesAndAnswers,
257+
);
258+
}
193259
}
194260

195261
/// Extension function to convert a [PollResponseData] to a [PollData] model.

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

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'dart:math' as math;
22

33
import 'package:freezed_annotation/freezed_annotation.dart';
44
import 'package:state_notifier/state_notifier.dart';
5-
import 'package:stream_core/stream_core.dart';
65

76
import '../models/activity_data.dart';
87
import '../models/comment_data.dart';
@@ -76,12 +75,9 @@ class ActivityStateNotifier extends StateNotifier<ActivityState>
7675
final currentPoll = state.poll;
7776
if (currentPoll == null || currentPoll.id != poll.id) return;
7877

79-
final latestAnswers = currentPoll.latestAnswers;
80-
final ownVotesAndAnswers = currentPoll.ownVotesAndAnswers;
81-
8278
final updatedPoll = poll.copyWith(
83-
latestAnswers: latestAnswers,
84-
ownVotesAndAnswers: ownVotesAndAnswers,
79+
latestAnswers: currentPoll.latestAnswers,
80+
ownVotesAndAnswers: currentPoll.ownVotesAndAnswers,
8581
);
8682

8783
state = state.copyWith(poll: updatedPoll);
@@ -93,18 +89,11 @@ class ActivityStateNotifier extends StateNotifier<ActivityState>
9389
final currentPoll = state.poll;
9490
if (currentPoll == null || currentPoll.id != poll.id) return;
9591

96-
final latestAnswers = currentPoll.latestAnswers.let((it) {
97-
return it.upsert(answer, key: (it) => it.id == answer.id);
98-
});
99-
100-
final ownVotesAndAnswers = currentPoll.ownVotesAndAnswers.let((it) {
101-
if (answer.userId != currentUserId) return it;
102-
return it.upsert(answer, key: (it) => it.id == answer.id);
103-
});
104-
105-
final updatedPoll = poll.copyWith(
106-
latestAnswers: latestAnswers,
107-
ownVotesAndAnswers: ownVotesAndAnswers,
92+
final updatedPoll = poll.castAnswer(
93+
answer,
94+
currentUserId,
95+
currentLatestAnswers: currentPoll.latestAnswers,
96+
currentOwnVotesAndAnswers: currentPoll.ownVotesAndAnswers,
10897
);
10998

11099
state = state.copyWith(poll: updatedPoll);
@@ -122,15 +111,11 @@ class ActivityStateNotifier extends StateNotifier<ActivityState>
122111
final currentPoll = state.poll;
123112
if (currentPoll == null || currentPoll.id != poll.id) return;
124113

125-
final latestAnswers = currentPoll.latestAnswers;
126-
final ownVotesAndAnswers = currentPoll.ownVotesAndAnswers.let((it) {
127-
if (vote.userId != currentUserId) return it;
128-
return it.upsert(vote, key: (it) => it.id == vote.id);
129-
});
130-
131-
final updatedPoll = poll.copyWith(
132-
latestAnswers: latestAnswers,
133-
ownVotesAndAnswers: ownVotesAndAnswers,
114+
final updatedPoll = poll.changeVote(
115+
vote,
116+
currentUserId,
117+
currentLatestVotes: currentPoll.latestVotes,
118+
currentOwnVotesAndAnswers: currentPoll.ownVotesAndAnswers,
134119
);
135120

136121
state = state.copyWith(poll: updatedPoll);
@@ -142,17 +127,11 @@ class ActivityStateNotifier extends StateNotifier<ActivityState>
142127
final currentPoll = state.poll;
143128
if (currentPoll == null || currentPoll.id != poll.id) return;
144129

145-
final latestAnswers = currentPoll.latestAnswers.where((it) {
146-
return it.id != answer.id;
147-
}).toList();
148-
149-
final ownVotesAndAnswers = currentPoll.ownVotesAndAnswers.where((it) {
150-
return it.id != answer.id;
151-
}).toList();
152-
153-
final updatedPoll = poll.copyWith(
154-
latestAnswers: latestAnswers,
155-
ownVotesAndAnswers: ownVotesAndAnswers,
130+
final updatedPoll = poll.removeAnswer(
131+
answer,
132+
currentUserId,
133+
currentLatestAnswers: currentPoll.latestAnswers,
134+
currentOwnVotesAndAnswers: currentPoll.ownVotesAndAnswers,
156135
);
157136

158137
state = state.copyWith(poll: updatedPoll);
@@ -164,14 +143,11 @@ class ActivityStateNotifier extends StateNotifier<ActivityState>
164143
final currentPoll = state.poll;
165144
if (currentPoll == null || currentPoll.id != poll.id) return;
166145

167-
final latestAnswers = currentPoll.latestAnswers;
168-
final ownVotesAndAnswers = currentPoll.ownVotesAndAnswers.where((it) {
169-
return it.id != vote.id;
170-
}).toList();
171-
172-
final updatedPoll = poll.copyWith(
173-
latestAnswers: latestAnswers,
174-
ownVotesAndAnswers: ownVotesAndAnswers,
146+
final updatedPoll = poll.removeVote(
147+
vote,
148+
currentUserId,
149+
currentLatestVotes: currentPoll.latestVotes,
150+
currentOwnVotesAndAnswers: currentPoll.ownVotesAndAnswers,
175151
);
176152

177153
state = state.copyWith(poll: updatedPoll);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import '../../../stream_feeds.dart';
22
import '../../../stream_feeds.dart' as api;
33
import '../../models/comment_data.dart';
44
import '../../models/feeds_reaction_data.dart';
5+
import '../../models/poll_data.dart';
56
import '../../repository/capabilities_repository.dart';
67
import '../../resolvers/resolvers.dart';
78
import 'feed_capabilities_mixin.dart';

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

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,9 @@ class FeedStateNotifier extends StateNotifier<FeedState>
531531

532532
final currentPoll = activity.poll!;
533533

534-
final latestAnswers = currentPoll.latestAnswers;
535-
final ownVotesAndAnswers = currentPoll.ownVotesAndAnswers;
536-
537534
final updatedPoll = poll.copyWith(
538-
latestAnswers: latestAnswers,
539-
ownVotesAndAnswers: ownVotesAndAnswers,
535+
latestAnswers: currentPoll.latestAnswers,
536+
ownVotesAndAnswers: currentPoll.ownVotesAndAnswers,
540537
);
541538

542539
_updateActivityInState(activity.copyWith(poll: updatedPoll));
@@ -550,18 +547,11 @@ class FeedStateNotifier extends StateNotifier<FeedState>
550547

551548
final currentPoll = activity.poll!;
552549

553-
final latestAnswers = currentPoll.latestAnswers.let((it) {
554-
return it.upsert(answer, key: (it) => it.id == answer.id);
555-
});
556-
557-
final ownVotesAndAnswers = currentPoll.ownVotesAndAnswers.let((it) {
558-
if (answer.userId != currentUserId) return it;
559-
return it.upsert(answer, key: (it) => it.id == answer.id);
560-
});
561-
562-
final updatedPoll = poll.copyWith(
563-
latestAnswers: latestAnswers,
564-
ownVotesAndAnswers: ownVotesAndAnswers,
550+
final updatedPoll = poll.castAnswer(
551+
answer,
552+
currentUserId,
553+
currentLatestAnswers: currentPoll.latestAnswers,
554+
currentOwnVotesAndAnswers: currentPoll.ownVotesAndAnswers,
565555
);
566556

567557
_updateActivityInState(activity.copyWith(poll: updatedPoll));
@@ -581,15 +571,11 @@ class FeedStateNotifier extends StateNotifier<FeedState>
581571

582572
final currentPoll = activity.poll!;
583573

584-
final latestAnswers = currentPoll.latestAnswers;
585-
final ownVotesAndAnswers = currentPoll.ownVotesAndAnswers.let((it) {
586-
if (vote.userId != currentUserId) return it;
587-
return it.upsert(vote, key: (it) => it.id == vote.id);
588-
});
589-
590-
final updatedPoll = poll.copyWith(
591-
latestAnswers: latestAnswers,
592-
ownVotesAndAnswers: ownVotesAndAnswers,
574+
final updatedPoll = poll.changeVote(
575+
vote,
576+
currentUserId,
577+
currentLatestVotes: currentPoll.latestVotes,
578+
currentOwnVotesAndAnswers: currentPoll.ownVotesAndAnswers,
593579
);
594580

595581
_updateActivityInState(activity.copyWith(poll: updatedPoll));
@@ -603,17 +589,11 @@ class FeedStateNotifier extends StateNotifier<FeedState>
603589

604590
final currentPoll = activity.poll!;
605591

606-
final latestAnswers = currentPoll.latestAnswers.where((it) {
607-
return it.id != answer.id;
608-
}).toList();
609-
610-
final ownVotesAndAnswers = currentPoll.ownVotesAndAnswers.where((it) {
611-
return it.id != answer.id;
612-
}).toList();
613-
614-
final updatedPoll = poll.copyWith(
615-
latestAnswers: latestAnswers,
616-
ownVotesAndAnswers: ownVotesAndAnswers,
592+
final updatedPoll = poll.removeAnswer(
593+
answer,
594+
currentUserId,
595+
currentLatestAnswers: currentPoll.latestAnswers,
596+
currentOwnVotesAndAnswers: currentPoll.ownVotesAndAnswers,
617597
);
618598

619599
_updateActivityInState(activity.copyWith(poll: updatedPoll));
@@ -627,14 +607,11 @@ class FeedStateNotifier extends StateNotifier<FeedState>
627607

628608
final currentPoll = activity.poll!;
629609

630-
final latestAnswers = currentPoll.latestAnswers;
631-
final ownVotesAndAnswers = currentPoll.ownVotesAndAnswers.where((it) {
632-
return it.id != vote.id;
633-
}).toList();
634-
635-
final updatedPoll = poll.copyWith(
636-
latestAnswers: latestAnswers,
637-
ownVotesAndAnswers: ownVotesAndAnswers,
610+
final updatedPoll = poll.removeVote(
611+
vote,
612+
currentUserId,
613+
currentLatestVotes: currentPoll.latestVotes,
614+
currentOwnVotesAndAnswers: currentPoll.ownVotesAndAnswers,
638615
);
639616

640617
_updateActivityInState(activity.copyWith(poll: updatedPoll));

0 commit comments

Comments
 (0)