-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainViewModel.cs
More file actions
173 lines (150 loc) · 4.09 KB
/
MainViewModel.cs
File metadata and controls
173 lines (150 loc) · 4.09 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using libspanch;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Linq;
using System.Windows;
namespace ED_Router.UI.Desktop.ViewModel
{
/// <summary>
/// This class contains properties that the main View can data bind to.
/// <para>
/// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
/// </para>
/// <para>
/// You can also use Blend to data bind with the tool's support.
/// </para>
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
public class MainViewModel : ViewModelBase
{
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
////if (IsInDesignMode)
////{
//// // Code runs in Blend --> create design time data.
////}
////else
////{
//// // Code runs "for real"
////}
CalculateCommand = new RelayCommand(CalculateMethod);
NextWaypointCommand = new RelayCommand(NextWaypointMethod);
PrevWaypointCommand = new RelayCommand(PrevWaypointMethod);
CopyToClipboardCommand = new RelayCommand(WaypointToClipboard);
Router = EdRouter.Instance;
Router.PropertyChanged += Router_PropertyChanged;
From = new ObservableCollection<string>();
To = new ObservableCollection<string>();
_fromSearch = Router.Start;
_toSearch = Router.Destination;
}
private void Router_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
RaisePropertyChanged();
}
public ICommand CalculateCommand { get; private set; }
public ICommand NextWaypointCommand { get; private set; }
public ICommand PrevWaypointCommand { get; private set; }
public ICommand CopyToClipboardCommand { get; private set; }
public EdRouter Router { get; private set; }
public ObservableCollection<SystemJump> MyRoute { get; set; }
public ObservableCollection<string> From { get; private set; }
public ObservableCollection<string> To { get; private set; }
private string _fromSearch;
public string FromSearch
{
get { return _fromSearch; }
set
{
_fromSearch = value;
if (_fromSearch.Length > 2)
{
var suggestionSystems = Router.GetSystems(value);
// Add new Systems
foreach (var sys in suggestionSystems)
{
if (From.Contains(sys) == false)
From.Add(sys);
}
// Remove systems which not in suggestion list
foreach (var sys in From.ToList())
{
if (suggestionSystems.Contains(sys) == false)
From.Remove(sys);
}
// pass the Name to the underlying router
if (suggestionSystems.Contains(value))
{
Router.Destination = value;
}
}
}
}
private string _toSearch;
public string ToSearch
{
get { return _toSearch; }
set
{
_toSearch = value;
if (_toSearch.Length > 2)
{
var suggestionSystems = Router.GetSystems(value);
// Add new Systems
foreach (var sys in suggestionSystems)
{
if (To.Contains(sys) == false)
To.Add(sys);
}
// Remove systems which not in suggestion list
foreach (var sys in To.ToList())
{
if (suggestionSystems.Contains(sys) == false)
To.Remove(sys);
}
// pass the Name to the underlying router
if (suggestionSystems.Contains(value))
{
Router.Destination = value;
}
}
}
}
public void CalculateMethod()
{
Router.Start = FromSearch;
Router.Destination = ToSearch;
try
{
Router.CalculateRoute();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
public void NextWaypointMethod()
{
Router.NextWaypoint();
WaypointToClipboard();
RaisePropertyChanged("Router");
}
public void PrevWaypointMethod()
{
Router.PreviousWaypoint();
WaypointToClipboard();
RaisePropertyChanged("Router");
}
public void WaypointToClipboard()
{
Clipboard.SetText(Router.CurrentWaypoint.System);
}
}
}