Files @ 40eaee10ae56
Branch filter:

Location: ATITD-Tools/Desert-Paint-Codex/ViewModels/ReactionTestViewModel.cs

Malkyne
First commit. New UI.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading.Tasks;
using DesertPaintCodex.Models;
using DesertPaintCodex.Services;
using DesertPaintCodex.Util;
using ReactiveUI;
using DynamicData;

namespace DesertPaintCodex.ViewModels
{
    public class ReactionTestViewModel : ViewModelBase
    {
        private ReactionTest _reactionTest = Constants.StubReactionTest;
        public ReactionTest ReactionTest
        {
            get => _reactionTest;
            set => this.RaiseAndSetIfChanged(ref _reactionTest, value);
        }

        private readonly List<Reagent> _allPigmentList = new();
        public ObservableCollection<Reagent> BufferPigmentList { get; } = new();


        public ReactionTestViewModel()
        {
            List<string> reagentNames = ReagentService.Names;
            
            foreach (var reagent in reagentNames.Select(ReagentService.GetReagent).Where(x => !x.IsCatalyst))
            {
                _allPigmentList.Add(reagent);
            }

            this.WhenAnyValue(x => x.ReactionTest)
                .Subscribe(_ => UpdateDerivedState());
            
            ShowScreenSettingsDialog = new Interaction<ScreenSettingsViewModel, Unit>();
            SaveReaction = ReactiveCommand.Create(() => ReactionTest.SaveReaction());
            ClearReaction = ReactiveCommand.Create(() => ReactionTest.ClearReaction());
        }

        private void UpdateDerivedState()
        {
            // There are more "reactive" ways to pull this off, but I don't have time to figure out all those recipes
            // right now.
            SetupTest();
        }
            
        
        private void SetupTest()
        {
            if (ReactionTest.Requires3Way)
            {
                FilterBufferPigments();
            }
        }

        private void FilterBufferPigments()
        {
            // Avalonia doesn't really have proper filtering for listy controls yet.
            BufferPigmentList.Clear();

            BufferPigmentList.AddRange(_allPigmentList.Where(pigment =>
                    pigment != ReactionTest.Reagent1 && pigment != ReactionTest.Reagent2));
        }

        public async void Analyze()
        {
            Debug.WriteLine("Analyze");
            try
            {
                await ReactionTest.StartScan();
            }
            catch (OperationCanceledException)
            {
                Debug.WriteLine("Scan canceled.");
            }
        }

        public void MarkInert()
        {
            ReactionTest.MarkInert();
        }

        public void CancelScan()
        {
            ReactionTest.CancelScan();
        }

        public async Task OpenScreenSettings()
        {
            await ShowScreenSettingsDialog.Handle(new ScreenSettingsViewModel());
        }

        public async Task ShowClipInfo()
        {
            await ShowInfoBox("Clipped Reactions", "The Pigment Lab is only capable of displaying color channel values in the 0-255 range. However, sometimes, your reactions will push one or more channels outside of that range. When that happens, the value will be clamped either to 0 or 255. In this case, we say that the reaction was \"Clipped.\"\n\nThis is nothing to panic about, though. We solve this issue by using a third (buffer) reagent to offset the extreme value, so that it falls within measurable range, similar to how we test Catalyst+Catalyst reactions. Desert Paint Codex will automatically do the math for this, but it does move clipped reactions towards the end of your test list, because you'll want any reactions between the buffer reagent and your two test reagents already known, prior to running your buffered test.");
        }
        
        public Interaction<ScreenSettingsViewModel, Unit> ShowScreenSettingsDialog { get; }

        public ReactiveCommand<Unit, Unit> ClearReaction { get; }
        public ReactiveCommand<Unit, Unit> SaveReaction { get; }
    }
}