241 lines
7.3 KiB
C#
241 lines
7.3 KiB
C#
using BeWo.ServiceProxy;
|
|
using BeWo.Services;
|
|
using BeWo.ViewModel.ListViewModel;
|
|
using BS.Shared;
|
|
using BS.Shared.DataContracts.Feature.AI;
|
|
using BS.Shared.Extensions.CustomInterfaces;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Input;
|
|
using DelegateCommand = DevExpress.Mvvm.DelegateCommand;
|
|
|
|
namespace BeWo.ViewModel.View.Administration
|
|
{
|
|
public class AiPromptbausteinTreeViewModel : WindowViewModel
|
|
{
|
|
private IWindowService WindowService { get; set; }
|
|
|
|
public AiPromptbausteinTreeViewModel()
|
|
{
|
|
if (BeWoApp.IsInDesignMode)
|
|
{
|
|
ListVM = new AiPromptbausteinTreeListVM();
|
|
}
|
|
|
|
Func<bool> canAddPrompt = () => SelectedNode.IsOrdner() || SelectedNode.IsKette();
|
|
Func<bool> canAddOrdner = () => SelectedNode.IsOrdner() || true;
|
|
Func<bool> canAddKette = () => SelectedNode.IsOrdner() && false;
|
|
Func<bool> canEdit = () => SelectedNode is object;
|
|
Func<bool> canCopy = () => SelectedNode.IsKette() || SelectedNode.IsOrdner();
|
|
Func<bool> canCopySingle = () => SelectedNode is object;
|
|
Func<bool> canPaste = () => HasCopiedTreeNode && !Contains(CopyTreeNode, SelectedNode) &&
|
|
(!CopyTreeNode.IsOrdner() || canAddOrdner()) &&
|
|
(!CopyTreeNode.IsKette() || canAddKette()) &&
|
|
(!CopyTreeNode.IsPrompt() || canAddPrompt());
|
|
Func<bool> canDelete = () => SelectedNode is object;
|
|
|
|
AddPromptCommand = new DelegateCommand(() => Add(AiPromptbausteinType.Prompt), canAddPrompt);
|
|
AddOrdnerCommand = new DelegateCommand(() => Add(AiPromptbausteinType.Ordner), canAddOrdner);
|
|
AddKetteCommand = new DelegateCommand(() => Add(AiPromptbausteinType.Kette), canAddKette);
|
|
EditPromptbausteinCommand = new DelegateCommand(EditPromptbaustein, canEdit);
|
|
CopyPromptbausteinCommand = new DelegateCommand(CopyPromptbaustein, canCopy);
|
|
CopySinglePromptbausteinCommand = new DelegateCommand(CopySinglePromptbaustein, canCopySingle);
|
|
PastePromptbausteinCommand = new DelegateCommand(PastePromptbaustein, canPaste);
|
|
DeletePromptbausteinCommand = new DelegateCommand(DeletePromptbaustein, canDelete);
|
|
}
|
|
|
|
public AiPromptbausteinTreeViewModel(IWindowService windowService) : this()
|
|
{
|
|
WindowService = windowService;
|
|
}
|
|
|
|
public AiPromptbausteinTreeVM SelectedNode { get; set; }
|
|
public AiPromptbausteinTreeListVM ListVM { get; private set; }
|
|
|
|
public bool CopySingleTreeNode { get; set; }
|
|
public AiPromptbausteinTreeVM CopyTreeNode { get; set; }
|
|
public bool HasCopiedTreeNode => CopyTreeNode != null;
|
|
|
|
public bool IsEditingMode { get; set; }
|
|
|
|
public ICommand AddPromptCommand { get; set; }
|
|
public ICommand AddOrdnerCommand { get; set; }
|
|
public ICommand AddKetteCommand { get; set; }
|
|
public ICommand EditPromptbausteinCommand { get; set; }
|
|
public ICommand CopyPromptbausteinCommand { get; set; }
|
|
public ICommand CopySinglePromptbausteinCommand { get; set; }
|
|
public ICommand PastePromptbausteinCommand { get; set; }
|
|
public ICommand DeletePromptbausteinCommand { get; set; }
|
|
|
|
public override bool IsDirty
|
|
{
|
|
get
|
|
{
|
|
if (ListVM is null)
|
|
return false;
|
|
|
|
return ListVM.ContainsDirtyNode();
|
|
}
|
|
}
|
|
|
|
public void SaveVMList()
|
|
{
|
|
if (!IsDirty)
|
|
return;
|
|
|
|
// DFS -> 1. Sobald Knoten gefunden wird mit IsNew=true: Teilbaum als neu markieren
|
|
// 2. Sobald Knoten gefunden wird mit IsNew=false && IsDirty=true: Knoten als update markieren
|
|
// 3. Checken, welche Knoten gelöscht wurden
|
|
|
|
var toadd = new List<AiPromptbausteinTreeVM>();
|
|
var toupdate = new List<AiPromptbausteinTreeVM>();
|
|
DFS_Search_AddUpdate(ListVM, ref toadd, ref toupdate);
|
|
|
|
var todelete = new List<AiPromptbausteinTreeDC>();
|
|
DFS_Search_Delete(ListVM, ref todelete);
|
|
|
|
var toadd_dcs = toadd.Select(x => x.CommitToDataContract());
|
|
var toupdate_dcs = toupdate.Select(x => x.CommitToDataContract());
|
|
|
|
var actions = new List<Action<IAiEnhancedService>>();
|
|
|
|
if (toadd_dcs.Any())
|
|
actions.Add(x => x.CreateAiPromptbausteineTree(toadd_dcs.ToList()));
|
|
|
|
if (toupdate_dcs.Any())
|
|
actions.Add(x => x.UpdateAiPromptbausteineTree(toupdate_dcs.ToList()));
|
|
|
|
if (todelete.Any())
|
|
actions.Add(x => x.DeleteAiPromptbausteineTree(todelete.ToDictionary(x1 => x1.Oid.Value, x2 => x2.Version.Value)));
|
|
|
|
ServiceFacade.DoMultipleAiEnhancedServicesAsync(actions, () =>
|
|
{
|
|
ReloadVMList();
|
|
});
|
|
}
|
|
|
|
internal void ReloadVMList()
|
|
{
|
|
VMFactory.CreateAiPromptbausteinTreeListAsync(vm => UpdateVMList(vm));
|
|
}
|
|
public void UpdateVMList(AiPromptbausteinTreeListVM new_list)
|
|
{
|
|
ListVM = new_list;
|
|
FirePropertyChanged(nameof(ListVM));
|
|
}
|
|
|
|
public void Add(AiPromptbausteinType type)
|
|
{
|
|
var parent = SelectedNode?.Children ?? ListVM;
|
|
parent.NewVM = null;
|
|
parent.NewVM.PromptbausteinType = type;
|
|
parent.NewVM.Parent = SelectedNode;
|
|
|
|
WindowService.ShowAiPromptbausteinDetailWindow(AiPromptbausteinDetailViewModel.CreateAdd(this, parent.NewVM, parent));
|
|
}
|
|
public void EditPromptbaustein()
|
|
{
|
|
WindowService.ShowAiPromptbausteinDetailWindow(AiPromptbausteinDetailViewModel.CreateEdit(this, SelectedNode));
|
|
}
|
|
public void CopyPromptbaustein()
|
|
{
|
|
CopySingleTreeNode = false;
|
|
CopyTreeNode = SelectedNode;
|
|
}
|
|
public void CopySinglePromptbaustein()
|
|
{
|
|
CopySingleTreeNode = true;
|
|
CopyTreeNode = SelectedNode;
|
|
}
|
|
public void PastePromptbaustein()
|
|
{
|
|
CopyPastePromptbaustein(!CopySingleTreeNode, CopyTreeNode, SelectedNode);
|
|
}
|
|
private void CopyPastePromptbaustein(bool rec, AiPromptbausteinTreeVM old_node, AiPromptbausteinTreeVM new_parent)
|
|
{
|
|
var old_parent = old_node?.Parent;
|
|
var new_node = old_node.CopyToNewViewModel();
|
|
|
|
if (new_parent is null)
|
|
{
|
|
ListVM.VMList.Add(new_node);
|
|
}
|
|
else
|
|
{
|
|
new_parent.Children.VMList.Add(new_node);
|
|
new_node.Parent = new_parent;
|
|
}
|
|
|
|
if (!rec)
|
|
return;
|
|
|
|
foreach (var old_child in old_node.Children.VMList)
|
|
{
|
|
CopyPastePromptbaustein(rec, old_child, new_node);
|
|
}
|
|
}
|
|
public void DeletePromptbaustein()
|
|
{
|
|
var node = SelectedNode;
|
|
var parent_source = node.Parent?.Children ?? ListVM;
|
|
|
|
parent_source.VMList.Remove(node);
|
|
}
|
|
|
|
private bool Contains(AiPromptbausteinTreeVM current_node, AiPromptbausteinTreeVM search)
|
|
{
|
|
if (current_node == search)
|
|
return true;
|
|
|
|
if (current_node.Children is null || !current_node.Children.VMList.Any())
|
|
return false;
|
|
|
|
foreach (var node in current_node.Children.VMList)
|
|
{
|
|
if (Contains(node, search))
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private void DFS_Search_AddUpdate(AiPromptbausteinTreeListVM search, ref List<AiPromptbausteinTreeVM> toAdd, ref List<AiPromptbausteinTreeVM> toUpdate)
|
|
{
|
|
foreach (var node in search.VMList)
|
|
{
|
|
if (node.IsNew)
|
|
{
|
|
toAdd.Add(node);
|
|
}
|
|
else
|
|
{
|
|
if (node.IsDirty)
|
|
toUpdate.Add(node);
|
|
|
|
if (node.HasChildren)
|
|
DFS_Search_AddUpdate(node.Children, ref toAdd, ref toUpdate);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DFS_Search_Delete(AiPromptbausteinTreeListVM search, ref List<AiPromptbausteinTreeDC> toDelete)
|
|
{
|
|
foreach(var node_dc in search.DCBackup)
|
|
{
|
|
if (search.VMList.FirstOrDefault(vm => vm.DataContract == node_dc) is AiPromptbausteinTreeVM node_vm)
|
|
{
|
|
if (node_vm.Children.DCBackup is object && node_vm.Children.DCBackup.Count > 0)
|
|
DFS_Search_Delete(node_vm.Children, ref toDelete);
|
|
}
|
|
else
|
|
{
|
|
toDelete.Add(node_dc);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|