@@ -176,10 +176,10 @@ extension PollDataMutations on PollData {
176176 /// exists (by ID), it will be updated.
177177 ///
178178 /// Returns a new [PollData] instance with the updated options.
179- PollData addOption (PollOptionData option) {
179+ PollData upsertOption (PollOptionData option) {
180180 final updatedOptions = options.upsert (
181181 option,
182- key: (it) => it.id == option.id ,
182+ key: (it) => it.id,
183183 );
184184
185185 return copyWith (options: updatedOptions);
@@ -191,44 +191,97 @@ extension PollDataMutations on PollData {
191191 ///
192192 /// Returns a new [PollData] instance with the updated options.
193193 PollData removeOption (String optionId) {
194- final updatedOptions = options.where ((it) => it.id != optionId).toList ();
194+ final updatedOptions = options.where ((it) {
195+ return it.id != optionId;
196+ }).toList ();
195197
196198 return copyWith (options: updatedOptions);
197199 }
198200
199- /// Updates an existing option in this poll.
201+ /// Adds or updates a vote in this poll.
200202 ///
201- /// Updates the options list by replacing the option with the same ID as [option]
202- /// with the new [option] data .
203+ /// Updates the own votes and answers list by adding or updating [vote] . Only adds votes
204+ /// that belong to [currentUserId] .
203205 ///
204- /// Returns a new [PollData] instance with the updated options.
205- PollData updateOption (PollOptionData option) {
206- final updatedOptions = options.map ((it) {
207- if (it.id != option.id) return it;
208- return option;
209- }).toList ();
206+ /// Returns a new [PollData] instance with the updated own votes and answers.
207+ PollData upsertVote (
208+ PollData updatedPoll,
209+ PollVoteData vote,
210+ String currentUserId,
211+ ) {
212+ final updatedOwnVotesAndAnswers = ownVotesAndAnswers.let ((it) {
213+ if (vote.userId != currentUserId) return it;
214+ return it.upsert (vote, key: (it) => it.id);
215+ });
210216
211- return copyWith (options: updatedOptions);
217+ return updateWith (
218+ updatedPoll,
219+ ownVotesAndAnswers: updatedOwnVotesAndAnswers,
220+ );
212221 }
213222
214- /// Casts an answer to this poll.
223+ /// Adds or updates an answer in this poll.
215224 ///
216- /// Updates the latest answers and own votes/ answers lists by adding or updating [answer] .
217- /// Only adds answers that belong to [currentUserId] to the own votes/answers list .
225+ /// Updates the own votes and answers list by adding or updating [answer] . Only adds answers
226+ /// that belong to [currentUserId] .
218227 ///
219- /// Returns a new [PollData] instance with the updated answers.
220- PollData castAnswer (PollVoteData answer, String currentUserId) {
221- final updatedLatestAnswers = latestAnswers.let ((it) {
222- return it.upsert (answer, key: (it) => it.id == answer.id);
228+ /// Returns a new [PollData] instance with the updated own votes and answers.
229+ PollData upsertAnswer (
230+ PollData updatedPoll,
231+ PollVoteData answer,
232+ String currentUserId,
233+ ) {
234+ final updatedOwnVotesAndAnswers = ownVotesAndAnswers.let ((it) {
235+ if (answer.userId != currentUserId) return it;
236+ return it.upsert (answer, key: (it) => it.id);
223237 });
224238
239+ return updateWith (
240+ updatedPoll,
241+ ownVotesAndAnswers: updatedOwnVotesAndAnswers,
242+ );
243+ }
244+
245+ /// Removes a vote from this poll.
246+ ///
247+ /// Updates the own votes and answers list by removing [vote] . Only removes votes
248+ /// that belong to [currentUserId] .
249+ ///
250+ /// Returns a new [PollData] instance with the updated own votes and answers.
251+ PollData removeVote (
252+ PollData updatedPoll,
253+ PollVoteData vote,
254+ String currentUserId,
255+ ) {
256+ final updatedOwnVotesAndAnswers = ownVotesAndAnswers.let ((it) {
257+ if (vote.userId != currentUserId) return it;
258+ return it.where ((it) => it.id != vote.id).toList ();
259+ });
260+
261+ return updateWith (
262+ updatedPoll,
263+ ownVotesAndAnswers: updatedOwnVotesAndAnswers,
264+ );
265+ }
266+
267+ /// Removes an answer from this poll.
268+ ///
269+ /// Updates the own votes and answers list by removing [answer] . Only removes answers
270+ /// that belong to [currentUserId] .
271+ ///
272+ /// Returns a new [PollData] instance with the updated own votes and answers.
273+ PollData removeAnswer (
274+ PollData updatedPoll,
275+ PollVoteData answer,
276+ String currentUserId,
277+ ) {
225278 final updatedOwnVotesAndAnswers = ownVotesAndAnswers.let ((it) {
226279 if (answer.userId != currentUserId) return it;
227- return it.upsert (answer, key : (it) => it.id == answer.id);
280+ return it.where ( (it) => it.id != answer.id). toList ( );
228281 });
229282
230- return copyWith (
231- latestAnswers : updatedLatestAnswers ,
283+ return updateWith (
284+ updatedPoll ,
232285 ownVotesAndAnswers: updatedOwnVotesAndAnswers,
233286 );
234287 }
0 commit comments