-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResultViewModel.cs
138 lines (113 loc) · 4.21 KB
/
ResultViewModel.cs
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
using DiffPatch;
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace PatchReviewer
{
public class ResultViewModel : INotifyPropertyChanged
{
public FilePatcherViewModel File { get; }
private Patcher.Result Result { get; }
private int origIndex = 0;
public ResultViewModel(FilePatcherViewModel file, Patcher.Result result, int origIndex) {
File = file;
Result = result;
this.origIndex = origIndex;
}
private Patch _editingPatch;
public Patch EditingPatch {
get => _editingPatch;
set {
var start1 = Start1;
_editingPatch = value;
if (start1 != Start1)
OnPropertyChanged(nameof(Start1));
}
}
// Patch which is active in the editor
public Patch ViewPatch => EditingPatch ?? Result.appliedPatch;
public int Start1 => ViewPatch?.start1 ?? (Result.patch.start1 + Result.searchOffset);
public int Start2 => ViewPatch.start2;
public int End1 => Start1 + (ViewPatch ?? Result.patch).length1;
public int End2 => Start2 + ViewPatch.length2;
public int SearchOffset => Result.searchOffset;
// should be a 'friend method' of FilePatcherViewModel
// FilePatcherViewModel is responsible for making sure that Start1 and Start2 actually line up with the patch list
public void MoveTo(int start2) {
if (Result.appliedPatch == null)
throw new NullReferenceException(nameof(Result.appliedPatch));
Result.appliedPatch.start2 = start2;
OnPropertyChanged(nameof(Start2));
}
public bool IsRemoved => Result.success && Result.appliedPatch == null;
public ResultStatus Status {
get {
if (!Result.success)
return ResultStatus.FAILED;
if (Result.mode == Patcher.Mode.FUZZY && Result.fuzzyQuality < 0.5f)
return ResultStatus.BAD;
if (Result.offsetWarning || Result.mode == Patcher.Mode.FUZZY && Result.fuzzyQuality < 0.85f)
return ResultStatus.WARNING;
if (Result.mode == Patcher.Mode.FUZZY)
return ResultStatus.GOOD;
if (Result.mode == Patcher.Mode.OFFSET)
return ResultStatus.OFFSET;
return ResultStatus.EXACT;
}
}
// shouldn't slow things down much, and worth offering some 'immutability'
public Patch OriginalPatch => Result.patch;
public Patch AppliedPatch => Result.appliedPatch == null ? null : Result.appliedPatch;
public Patch ApprovedPatch => Status >= ResultStatus.OFFSET ? AppliedPatch : OriginalPatch;
private bool _modifiedInEditor;
public bool ModifiedInEditor {
get => _modifiedInEditor;
set {
if (_modifiedInEditor == value) return;
_modifiedInEditor = value;
OnPropertyChanged();
OnPropertyChanged(nameof(LabelWithModifiedIndicator));
}
}
public string LabelWithModifiedIndicator => Label + (ModifiedInEditor ? " *" : "");
public string Label => IsRemoved ? $"REMOVED: {Result.patch.Header}" : Result.Summary();
public string Title => $"{File.Label} {Label}";
public string MovedPatchCountText { get; private set; } = "";
private int _appliedIndex = -1;
public int AppliedIndex {
get => _appliedIndex;
set {
if (_appliedIndex == value)
return;
_appliedIndex = value;
int moved = AppliedIndex - origIndex;
MovedPatchCountText = moved > 0 ? $"▼{moved}" : moved < 0 ? $"▲{-moved}" : "";
OnPropertyChanged(nameof(MovedPatchCountText));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
internal void Approve() {
Result.success = true;
Result.mode = Patcher.Mode.EXACT;
Result.offsetWarning = false;
Result.appliedPatch = EditingPatch;
ModifiedInEditor = false;
// trigger reordering in the collection view
OnPropertyChanged(nameof(Start1));
OnPropertyChanged(nameof(Status));
OnPropertyChanged(nameof(Label));
OnPropertyChanged(nameof(LabelWithModifiedIndicator));
OnPropertyChanged(nameof(Title));
File.ResultsModified = true;
File.RecalculateOffsets();
}
internal void UndoRemove() {
Result.success = false; //convert to FAILED
OnPropertyChanged(nameof(Status));
OnPropertyChanged(nameof(Label));
OnPropertyChanged(nameof(Title));
OnPropertyChanged(nameof(LabelWithModifiedIndicator));
}
}
}