1 using System.Globalization;
2 using Reactive.Bindings;
3 using Reactive.Bindings.Extensions;
4 using TrainEditor2.Extensions;
5 using TrainEditor2.Models.Trains;
6 
7 namespace TrainEditor2.ViewModels.Trains
8 {
9 	internal class PerformanceViewModel : BaseViewModel
10 	{
11 		internal ReactiveProperty<string> Deceleration
12 		{
13 			get;
14 		}
15 
16 		internal ReactiveProperty<string> CoefficientOfStaticFriction
17 		{
18 			get;
19 		}
20 
21 		internal ReactiveProperty<string> CoefficientOfRollingResistance
22 		{
23 			get;
24 		}
25 
26 		internal ReactiveProperty<string> AerodynamicDragCoefficient
27 		{
28 			get;
29 		}
30 
PerformanceViewModel(Performance performance)31 		internal PerformanceViewModel(Performance performance)
32 		{
33 			CultureInfo culture = CultureInfo.InvariantCulture;
34 
35 			Deceleration = performance
36 				.ToReactivePropertyAsSynchronized(
37 					x => x.Deceleration,
38 					x => x.ToString(culture),
39 					x => double.Parse(x, NumberStyles.Float, culture),
40 					ignoreValidationErrorValue: true
41 				)
42 				.SetValidateNotifyError(x =>
43 				{
44 					double result;
45 					string message;
46 
47 					Utilities.TryParse(x, NumberRange.NonNegative, out result, out message);
48 
49 					return message;
50 				})
51 				.AddTo(disposable);
52 
53 			CoefficientOfStaticFriction = performance
54 				.ToReactivePropertyAsSynchronized(
55 					x => x.CoefficientOfStaticFriction,
56 					x => x.ToString(culture),
57 					x => double.Parse(x, NumberStyles.Float, culture),
58 					ignoreValidationErrorValue: true
59 				)
60 				.SetValidateNotifyError(x =>
61 				{
62 					double result;
63 					string message;
64 
65 					Utilities.TryParse(x, NumberRange.NonNegative, out result, out message);
66 
67 					return message;
68 				})
69 				.AddTo(disposable);
70 
71 			CoefficientOfRollingResistance = performance
72 				.ToReactivePropertyAsSynchronized(
73 					x => x.CoefficientOfRollingResistance,
74 					x => x.ToString(culture),
75 					x => double.Parse(x, NumberStyles.Float, culture),
76 					ignoreValidationErrorValue: true
77 				)
78 				.SetValidateNotifyError(x =>
79 				{
80 					double result;
81 					string message;
82 
83 					Utilities.TryParse(x, NumberRange.NonNegative, out result, out message);
84 
85 					return message;
86 				})
87 				.AddTo(disposable);
88 
89 			AerodynamicDragCoefficient = performance
90 				.ToReactivePropertyAsSynchronized(
91 					x => x.AerodynamicDragCoefficient,
92 					x => x.ToString(culture),
93 					x => double.Parse(x, NumberStyles.Float, culture),
94 					ignoreValidationErrorValue: true
95 				)
96 				.SetValidateNotifyError(x =>
97 				{
98 					double result;
99 					string message;
100 
101 					Utilities.TryParse(x, NumberRange.NonNegative, out result, out message);
102 
103 					return message;
104 				})
105 				.AddTo(disposable);
106 		}
107 	}
108 }
109