using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Artemis.Core.Modules; using ReactiveUI; namespace Artemis.UI.Shared.DataModelVisualization; /// /// Represents a input view model /// /// The type of the data model public abstract class DataModelInputViewModel : DataModelInputViewModel { private bool _closed; [AllowNull] private T _inputValue; /// /// Creates a new instance of the class /// /// The description of the property this input VM is representing /// The initial value to set the input value to protected DataModelInputViewModel(DataModelPropertyAttribute targetDescription, T initialValue) { TargetDescription = targetDescription; InputValue = initialValue; } /// /// Gets or sets the value shown in the input /// [AllowNull] public T InputValue { get => _inputValue; set => this.RaiseAndSetIfChanged(ref _inputValue, value); } /// /// Gets the description of the property this input VM is representing /// public DataModelPropertyAttribute TargetDescription { get; } internal override object InternalGuard { get; } = new(); /// public sealed override void Submit() { if (_closed) return; _closed = true; OnSubmit(); UpdateCallback(InputValue, true); } /// public sealed override void Cancel() { if (_closed) return; _closed = true; OnCancel(); UpdateCallback(InputValue, false); } } /// /// For internal use only, implement instead. /// public abstract class DataModelInputViewModel : ReactiveObject { /// /// Prevents this type being implemented directly, implement instead. /// // ReSharper disable once UnusedMember.Global internal abstract object InternalGuard { get; } internal Action UpdateCallback { get; set; } = null!; // Set right after construction /// /// Gets the types this input view model can support through type conversion. This list is defined when registering the /// view model. /// internal IReadOnlyCollection? CompatibleConversionTypes { get; set; } /// /// Submits the input value and removes this view model. /// This is called automatically when the user presses enter or clicks outside the view /// public abstract void Submit(); /// /// Discards changes to the input value and removes this view model. /// This is called automatically when the user presses escape /// public abstract void Cancel(); /// /// Called before the current value is submitted /// protected virtual void OnSubmit() { } /// /// Called before the current value is discarded /// protected virtual void OnCancel() { } }