-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathGroupEndpointEditorViewModel.cs
More file actions
84 lines (78 loc) · 3.26 KB
/
GroupEndpointEditorViewModel.cs
File metadata and controls
84 lines (78 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Text;
using DynamicData;
using ExampleCodeGenApp.Views;
using ExampleCodeGenApp.Views.Editors;
using MoreLinq.Extensions;
using NodeNetwork.Toolkit.Group;
using NodeNetwork.Toolkit.ValueNode;
using NodeNetwork.ViewModels;
using ReactiveUI;
namespace ExampleCodeGenApp.ViewModels.Nodes
{
/// <summary>
/// A non-generic interface that provides access to the data in GroupEndpointEditorViewModel.
/// Mapping a view onto a generic viewmodel is problematic because the generic type often isn't known in the view,
/// and generic views are often not allowed.
/// </summary>
public interface IGroupEndpointEditorViewModel
{
public Endpoint Endpoint { get; }
public ReactiveCommand<Unit, Unit> MoveUp { get; }
public ReactiveCommand<Unit, Unit> MoveDown { get; }
public ReactiveCommand<Unit, Unit> Delete { get; }
}
public class GroupEndpointEditorViewModel<T> : ValueEditorViewModel<T>, IGroupEndpointEditorViewModel
{
public Endpoint Endpoint => Parent;
public ReactiveCommand<Unit, Unit> MoveUp { get; }
public ReactiveCommand<Unit, Unit> MoveDown { get; }
public ReactiveCommand<Unit, Unit> Delete { get; }
static GroupEndpointEditorViewModel()
{
Splat.Locator.CurrentMutable.Register(() => new GroupEndpointEditorView(), typeof(IViewFor<GroupEndpointEditorViewModel<T>>));
}
public GroupEndpointEditorViewModel(CodeNodeGroupIOBinding nodeGroupBinding)
{
MoveUp = ReactiveCommand.Create(() =>
{
bool isInput = Parent is NodeInputViewModel;
IEnumerable<Endpoint> endpoints = isInput ? (IEnumerable<Endpoint>)Parent.Parent.InputItems : Parent.Parent.OutputItems;
// Swap SortIndex of this endpoint with the SortIndex of the previous endpoint in the list, if any.
var prevElement = endpoints
.Where(e => e.SortIndex < Parent.SortIndex)
.MaxBy(e => e.SortIndex)
.FirstOrDefault();
if (prevElement != null)
{
var idx = prevElement.SortIndex;
prevElement.SortIndex = Parent.SortIndex;
Parent.SortIndex = idx;
}
});
MoveDown = ReactiveCommand.Create(() =>
{
bool isInput = Parent is NodeInputViewModel;
IEnumerable<Endpoint> endpoints = isInput ? (IEnumerable<Endpoint>)Parent.Parent.InputItems : Parent.Parent.OutputItems;
var nextElement = endpoints
.Where(e => e.SortIndex > Parent.SortIndex)
.MinBy(e => e.SortIndex)
.FirstOrDefault();
if (nextElement != null)
{
var idx = nextElement.SortIndex;
nextElement.SortIndex = Parent.SortIndex;
Parent.SortIndex = idx;
}
});
Delete = ReactiveCommand.Create(() =>
{
nodeGroupBinding.DeleteEndpoint(Parent);
});
}
}
}