/// <summary> /// Wrapper for handling replace task, but without adding to merge records. /// </summary> /// <param name="vote">The vote being modified.</param> /// <param name="task">The new task to apply to the vote.</param> /// <returns>Returns true if the task replacement was successfully completed.</returns> private bool ReplaceTaskImplWrapper(VoteLineBlock vote, string task) { if (!VoteStorage.TryGetValue(vote, out var supporters)) { return(false); } // Incoming parameter may be an entry in storage, or a copy of a vote. // Adjust so that we're always pointing at an actual vote. // If the vote isn't found in VoteStorage, just use the one provided. vote = VoteStorage.GetVoteMatching(vote) ?? vote; // Remove the version of the vote we're starting with. VoteStorage.Remove(vote); string originalTask = vote.Task; vote.Task = task; // If there's a conflict with the newly-tasked vote, we need to merge with the existing vote. if (VoteStorage.ContainsKey(vote)) { if (VoteStorage.TryGetValue(vote, out var toSupport)) { foreach (var(supporterName, supporterVote) in supporters) { if (!toSupport.ContainsKey(supporterName)) { supporterVote.Task = task; toSupport.Add(supporterName, supporterVote); } } } else { // Undo the attempt if we couldn't get the conflicting vote data vote.Task = originalTask; VoteStorage.Add(vote, supporters); return(false); } } // If there's no conflict, update the tasks in the supporter votes and add the revised vote. else { foreach (var(_, supporterVote) in supporters) { supporterVote.Task = task; } VoteStorage.Add(vote, supporters); } return(true); }
/// <summary> /// Delete an entire vote and all associated supporters. /// </summary> /// <param name="vote">The vote to delete.</param> /// <returns>Returns true if successfully completed.</returns> public bool Delete(VoteLineBlock vote) { bool removed = false; if (VoteStorage.ContainsKey(vote)) { UndoBuffer.Push(new UndoAction(UndoActionType.Delete, VoteStorage)); removed = VoteStorage.Remove(vote); } if (removed) { OnPropertyChanged("Votes"); OnPropertyChanged("Voters"); OnPropertyChanged(nameof(HasUndoActions)); } else { UndoBuffer.Pop(); } return(removed); }