Files
@ 4778889395e8
Branch filter:
Location: ATITD-Tools/Desert-Paint-Codex/ViewModels/ExperimentLogViewModel.cs
4778889395e8
6.0 KiB
text/x-csharp
Change the displayed permeutation count to display using locale-specific number formatting (with comma / dot separators). Fix a crash when starting a new round of recipe generation after recipe generation completed during a previous run.
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using ReactiveUI;
using DesertPaintCodex.Services;
using DesertPaintCodex.Models;
using DesertPaintCodex.Util;
using DynamicData.Binding;
namespace DesertPaintCodex.ViewModels
{
public class ExperimentLogViewModel : ViewModelBase
{
public ObservableCollection<ReactionTest> RemainingTests { get; } = new();
public ObservableCollection<ReactionTest> CompletedTests { get; } = new();
private int _selectedList = 0;
public int SelectedList { get => _selectedList; private set => this.RaiseAndSetIfChanged(ref _selectedList, value); }
private int _selectedRemainingTest = 0;
public int SelectedRemainingTest { get => _selectedRemainingTest; private set => this.RaiseAndSetIfChanged(ref _selectedRemainingTest, value); }
private int _selectedCompletedTest = 0;
public int SelectedCompletedTest { get => _selectedCompletedTest; private set => this.RaiseAndSetIfChanged(ref _selectedCompletedTest, value); }
private ReactionTestViewModel _testView;
public ReactionTestViewModel TestView { get => _testView; private set => this.RaiseAndSetIfChanged(ref _testView, value); }
private readonly List<ObservableCollection<ReactionTest>> _testLists = new();
public ExperimentLogViewModel()
{
PlayerProfile? profile = ProfileManager.CurrentProfile;
Debug.Assert(profile != null);
_testLists.Add(RemainingTests);
_testLists.Add(CompletedTests);
ReactionTestService.PopulateRemainingTests(RemainingTests);
ReactionTestService.PopulateCompletedTests(CompletedTests);
// If we have no remaining tests, switch to the completed list.
_testView = new ReactionTestViewModel();
if (RemainingTests.Count > 0)
{
SelectedList = 0;
_testView.ReactionTest = RemainingTests[0];
}
else
{
SelectedList = 1;
_testView.ReactionTest = CompletedTests[0];
}
this.WhenAnyPropertyChanged(nameof(SelectedList), nameof(SelectedRemainingTest), nameof(SelectedCompletedTest))
.Subscribe(_ => _testView.ReactionTest = GetSelectedReactionTest());
_testView.ReactionTest = GetSelectedReactionTest();
TestView.SaveReaction.Subscribe(_ => OnSaveReaction());
TestView.ClearReaction.Subscribe(_ => OnClearReaction());
TestView.FinalizeTestResults.Subscribe(_ => OnReactionResults());
}
private ReactionTest GetSelectedReactionTest()
{
int itemIndex = (SelectedList == 0) ? SelectedRemainingTest : SelectedCompletedTest;
if (itemIndex < 0) return Constants.StubReactionTest;
var list = _testLists[SelectedList];
return (itemIndex >= list.Count) ? Constants.StubReactionTest : list[itemIndex];
}
#region Command Handlers
private void OnSaveReaction()
{
ReactionTest test = TestView.ReactionTest;
int oldPos = RemainingTests.IndexOf(test);
// Move test to Completed Tests.
InsertTestIntoList(test, CompletedTests);
RemainingTests.RemoveAt(oldPos);
// Select next Remaining Test.
if (RemainingTests.Count == 0) return;
SelectedRemainingTest = (oldPos == RemainingTests.Count) ? RemainingTests.Count - 1 : oldPos;
// If we have just inserted our first completed test, select it.
if (CompletedTests.Count == 1)
{
SelectedCompletedTest = 0;
}
}
private void OnClearReaction()
{
int oldPos = 0;
ReactionTest test = TestView.ReactionTest;
if (RemainingTests.Contains(test))
{
SelectedRemainingTest = ResortTestInList(test, RemainingTests);
return;
}
oldPos = CompletedTests.IndexOf(test);
// Move test to Remaining Tests.
InsertTestIntoList(test, RemainingTests);
CompletedTests.RemoveAt(oldPos);
// Select next Completed Test.
if (CompletedTests.Count == 0) return;
SelectedCompletedTest = (oldPos == CompletedTests.Count) ? CompletedTests.Count - 1 : oldPos;
// If we have just inserted our first remaining test, select it.
if (RemainingTests.Count == 1)
{
SelectedRemainingTest = 0;
}
}
private void OnReactionResults()
{
int newPos = ResortTestInList(TestView.ReactionTest, RemainingTests);
if (newPos != SelectedRemainingTest)
{
SelectedRemainingTest = newPos;
}
}
#endregion
private static int ResortTestInList(ReactionTest test, IList<ReactionTest> list)
{
int oldPos = list.IndexOf(test);
// If the item is first in the list or the previous item is valued < the test
// And either the item is last in the list or the next item in the list is valued > than the item
// Don't move it
if (((oldPos == 0) || (test.CompareTo(list[oldPos - 1]) > 0))
&& ((oldPos == list.Count - 1) || (test.CompareTo(list[oldPos + 1]) < 0))) return oldPos; // No need to move.
list.RemoveAt(oldPos);
return InsertTestIntoList(test, list);
}
private static int InsertTestIntoList(ReactionTest test, IList<ReactionTest> list)
{
int i;
for (i = 0; i < list.Count; i++)
{
if (test.CompareTo(list[i]) < 0) break;
}
list.Insert(i, test);
return i;
}
}
}
|