using System.Collections.ObjectModel; using System.Reactive.Disposables; using Artemis.Core; using Artemis.Core.Modules; using Artemis.Core.Services; using Artemis.Storage.Entities.Profile; using Artemis.UI.Shared.Services.NodeEditor; using Artemis.UI.Shared.Services.NodeEditor.Commands; using Artemis.UI.Shared.VisualScripting; using ReactiveUI; namespace Artemis.VisualScripting.Nodes.DataModel.Screens; public class DataModelEventCycleNodeCustomViewModel : CustomNodeViewModel { private readonly DataModelEventCycleNode _cycleNode; private readonly INodeEditorService _nodeEditorService; private DataModelPath? _dataModelPath; private ObservableCollection? _modules; private bool _updating; public DataModelEventCycleNodeCustomViewModel(DataModelEventCycleNode cycleNode, INodeScript script, ISettingsService settingsService, INodeEditorService nodeEditorService) : base(cycleNode, script) { _cycleNode = cycleNode; _nodeEditorService = nodeEditorService; ShowFullPaths = settingsService.GetSetting("ProfileEditor.ShowFullPaths", true); ShowDataModelValues = settingsService.GetSetting("ProfileEditor.ShowDataModelValues", false); Modules = new ObservableCollection(); this.WhenActivated(d => { // Set up extra modules if (_cycleNode.Script?.Context is Profile scriptProfile && scriptProfile.Configuration.Module != null) Modules = new ObservableCollection {scriptProfile.Configuration.Module}; else if (_cycleNode.Script?.Context is ProfileConfiguration profileConfiguration && profileConfiguration.Module != null) Modules = new ObservableCollection {profileConfiguration.Module}; // Subscribe to node changes _cycleNode.WhenAnyValue(n => n.Storage).Subscribe(UpdateDataModelPath).DisposeWith(d); this.WhenAnyValue(vm => vm.DataModelPath).Subscribe(ApplyDataModelPath).DisposeWith(d); Disposable.Create(() => { _dataModelPath?.Dispose(); _dataModelPath = null; }).DisposeWith(d); }); } public PluginSetting ShowFullPaths { get; } public PluginSetting ShowDataModelValues { get; } public ObservableCollection? Modules { get => _modules; set => this.RaiseAndSetIfChanged(ref _modules, value); } public DataModelPath? DataModelPath { get => _dataModelPath; set => this.RaiseAndSetIfChanged(ref _dataModelPath, value); } private void UpdateDataModelPath(DataModelPathEntity? entity) { try { if (_updating) return; _updating = true; DataModelPath? old = DataModelPath; DataModelPath = entity != null ? new DataModelPath(entity) : null; old?.Dispose(); } finally { _updating = false; } } private void ApplyDataModelPath(DataModelPath? path) { try { if (_updating) return; if (path?.Path == _cycleNode.Storage?.Path) return; _updating = true; path?.Save(); _nodeEditorService.ExecuteCommand(Script, new UpdateStorage(_cycleNode, path?.Entity, "event")); } finally { _updating = false; } } }