# HG changeset patch # User Jason Maltzen # Date 2021-09-11 03:40:21 # Node ID a78dba5bed0e96d9e55c3f67fd6ad65008b067cb # Parent ccf67c142d6588a8873aba27b89b2761043afc91 Add stub view model code for recipe library (future feature work) diff --git a/ViewModels/RecipeLibraryViewModel.cs b/ViewModels/RecipeLibraryViewModel.cs new file mode 100644 --- /dev/null +++ b/ViewModels/RecipeLibraryViewModel.cs @@ -0,0 +1,51 @@ +using System.Collections.Generic; +using System.Reactive; +using System.Reactive.Linq; +using DesertPaintCodex.Services; +using ReactiveUI; + + +namespace DesertPaintCodex.ViewModels +{ + public class RecipeLibraryViewModel : ViewModelBase + { + private string _profileName = string.Empty; + public string ProfileName { get => _profileName; set => this.RaiseAndSetIfChanged(ref _profileName, value); } + + private readonly ObservableAsPropertyHelper _duplicateWarning; + public bool DuplicateWarning => _duplicateWarning.Value; + + public RecipeLibraryViewModel() + { + List profiles = ProfileManager.GetProfileList(); + + // TODO ..... + + // Duplicate warning display logic. + this.WhenAnyValue(x => x.ProfileName) + .Select(profileName => + (!string.IsNullOrWhiteSpace(profileName) && + profiles.Contains(profileName))) + .ToProperty(this, + x => x.DuplicateWarning, + out _duplicateWarning); + + // TODO: Use proper validation to decorate the TextBox, as well. + + // OK button enabling logic. + var okEnabled = this.WhenAnyValue( + x => x.ProfileName, + x => !string.IsNullOrWhiteSpace(x) && + !profiles.Contains(x)); + + // Button commands. + Cancel = ReactiveCommand.Create(() => { }); + Ok = ReactiveCommand.Create(() => { + ProfileManager.CreateNewProfile(_profileName); + }, okEnabled); + } + + public ReactiveCommand Cancel { get; } + public ReactiveCommand Ok { get; } + } +}