-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDriverLapsViewModel.cs
More file actions
105 lines (90 loc) · 3.53 KB
/
DriverLapsViewModel.cs
File metadata and controls
105 lines (90 loc) · 3.53 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
namespace SecondMonitor.Timing.LapTimings.ViewModel
{
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using SecondMonitor.Timing.SessionTiming.Drivers.ViewModel;
using SessionTiming;
using DriverLapsWindow = View.DriverLapsWindow;
public class DriverLapsViewModel : DependencyObject
{
public static readonly DependencyProperty LapsProperty = DependencyProperty.Register("Laps", typeof(ObservableCollection<LapViewModel>), typeof(DriverLapsViewModel));
public static readonly DependencyProperty DriverNameProperty = DependencyProperty.Register("DriverName", typeof(string), typeof(DriverLapsViewModel));
private readonly DriverTiming _driverTiming;
private readonly DriverLapsWindow _gui;
public DriverLapsViewModel()
{
DriverName = "Design Time";
Laps = new ObservableCollection<LapViewModel>();
}
public DriverLapsViewModel(DriverTiming driverTiming, DriverLapsWindow gui)
{
_driverTiming = driverTiming;
Laps = new ObservableCollection<LapViewModel>();
BuildLapsViewModel();
_driverTiming.NewLapStarted += DriverTimingOnNewLapStarted;
DriverName = _driverTiming.Name;
_gui = gui;
_gui.Closed += GuiOnClosed;
_gui.MouseLeave += GuiOnMouseLeave;
_gui.DataContext = this;
}
public DriverTiming DriverTiming => _driverTiming;
public ObservableCollection<LapViewModel> Laps
{
get => (ObservableCollection<LapViewModel>)GetValue(LapsProperty);
private set => SetValue(LapsProperty, value);
}
public string DriverName
{
get => (string)GetValue(DriverNameProperty);
private set => SetValue(DriverNameProperty, value);
}
public void UnRegisterOnGui()
{
_driverTiming.NewLapStarted -= DriverTimingOnNewLapStarted;
_gui.Closed -= GuiOnClosed;
_gui.MouseLeave -= GuiOnMouseLeave;
foreach (var lap in Laps)
{
lap.StopRefresh();
}
}
private void GuiOnMouseLeave(object sender, MouseEventArgs mouseEventArgs)
{
_gui.LapsGrid.SelectedItem = null;
}
private void GuiOnClosed(object sender, EventArgs eventArgs)
{
UnRegisterOnGui();
}
private void DriverTimingOnNewLapStarted(object sender, LapEventArgs e)
{
if (!Application.Current.Dispatcher.CheckAccess())
{
Application.Current.Dispatcher.Invoke(() => DriverTimingOnNewLapStarted(sender, e));
return;
}
var newLapModel = new LapViewModel(e.Lap);
if (Laps.Any() && Laps.Last().LapNumber == newLapModel.LapNumber)
{
Laps.Remove(Laps.Last());
}
Laps.Add(newLapModel);
_gui.LapsGrid.ScrollIntoView(newLapModel);
}
private void BuildLapsViewModel()
{
foreach (var lap in _driverTiming.Laps)
{
if (Laps.Any() && Laps.Last().LapNumber == lap.LapNumber)
{
Laps.Remove(Laps.Last());
}
Laps.Add(new LapViewModel(lap));
}
}
}
}