private void RestructureViewModels(ModifyModel modifyModel, CommunityViewModel changingViewModel) { AgentViewModel[] agents = changingViewModel.FindItemsByType <AgentViewModel>().ToArray(); MessageViewModel[] messages = changingViewModel.FindItemsByType <MessageViewModel>().ToArray(); foreach (AgentViewModel agent in agents) { changingViewModel.RemoveItem(agent); } foreach (MessageViewModel message in messages) { changingViewModel.RemoveItem(message); } string oldNamespace = modifyModel.Modification.OldValue.AssertTypeOf <string>() ?? string.Empty; string newNamespace = modifyModel.Modification.NewValue.AssertTypeOf <string>() ?? string.Empty; UpdateRelativeRootFolder(changingViewModel, oldNamespace, newNamespace); foreach (AgentViewModel agent in agents) { agent.FullName = UpdateFullName(agent.FullName, agent.RelativeNamespace); changingViewModel.AddItem(agent); } foreach (MessageViewModel message in messages) { message.FullName = UpdateFullName(message.FullName, message.RelativeNamespace); changingViewModel.AddItem(message); } string UpdateFullName(string oldFullName, string relativeNamespace) { if (!relativeNamespace.StartsWith(".")) { return(oldFullName); } string relativeFullName = oldFullName.Substring(oldNamespace.Length); if (!relativeFullName.StartsWith(".", StringComparison.Ordinal)) { relativeFullName = $".{relativeFullName}"; } return(string.IsNullOrEmpty(newNamespace) ? relativeFullName.Substring(1) : newNamespace + relativeFullName); } }
private static void UpdateRelativeRootFolder(CommunityViewModel changingViewModel, string oldNamespace, string newNamespace) { if (!string.IsNullOrEmpty(oldNamespace)) { FolderViewModel rootFolder = changingViewModel.Items.OfType <FolderViewModel>() .First(f => f.IsRelativeRoot); if (!string.IsNullOrEmpty(newNamespace)) { rootFolder.Name = newNamespace; } else { changingViewModel.RemoveItem(rootFolder); } } else if (!string.IsNullOrEmpty(newNamespace)) { FolderViewModel rootFolder = new() { Name = newNamespace, IsRelativeRoot = true }; changingViewModel.Items.Add(rootFolder); } }
private void ChangeMessages(ModifyModel modifyModel, CommunityViewModel changingViewModel) { switch (modifyModel.Modification.ModificationType) { case ModificationType.Add: AddMessage(); break; case ModificationType.Remove: RemoveMessage(); break; case ModificationType.Change: RemoveMessage(); AddMessage(); break; } void AddMessage() { MessageModel messageModel = modifyModel.Modification.NewValue.AssertTypeOf <MessageModel>(); MessageViewModel viewModel = messageModel.CreateViewModel(changingViewModel); changingViewModel.AddItem(viewModel); (changingViewModel.FindItemByType <AgentViewModel>()?.AvailableItems ?? changingViewModel.FindItemByType <MessageViewModel>()?.AvailableItems)?.AvailableMessages.Add(viewModel); } void RemoveMessage() { MessageModel messageModel = modifyModel.Modification.OldValue.AssertTypeOf <MessageModel>(); MessageViewModel viewModel = (MessageViewModel)changingViewModel.FindViewItemById(messageModel.Id); changingViewModel.RemoveItem(viewModel); (changingViewModel.FindItemByType <AgentViewModel>()?.AvailableItems ?? changingViewModel.FindItemByType <MessageViewModel>()?.AvailableItems)?.AvailableMessages.Remove(viewModel); } }