Vereinsmeisterschaften  22aa7800eae54b428d40e835886cefe1fdefdfdf
This is a software that can be used to manage the internal competition of the swimming club Illertissen called "Vereinsmeisterschaften".
Loading...
Searching...
No Matches
Race.cs
1using CommunityToolkit.Mvvm.ComponentModel;
2using System.Collections.ObjectModel;
3using System.Xml.Linq;
5
7{
11 public class Race : ObservableObject, IEquatable<Race>, ICloneable
12 {
13 #region Constructors
14
18 public Race()
19 {
20 Starts = new ObservableCollection<PersonStart>();
21 RegisterEvents();
22 }
23
28 public Race(List<PersonStart> starts)
29 {
30 Starts = starts == null ? null : new ObservableCollection<PersonStart>(starts);
31 RegisterEvents();
32 }
33
38 public Race(ObservableCollection<PersonStart> starts)
39 {
40 Starts = starts;
41 RegisterEvents();
42 }
43
49 public Race(Race other, bool deepClone = true) : this()
50 {
51 if (other == null) { return; }
52 if (deepClone)
53 {
54 // Create a deep copy of the list
55 Starts = new ObservableCollection<PersonStart>(other.Starts.Select(item => (PersonStart)item.Clone()));
56 }
57 else
58 {
59 // Create a new list but keep the references to the <see cref="PersonStart"/> objects
60 Starts = new ObservableCollection<PersonStart>(other.Starts);
61 }
62 RegisterEvents();
63 RaceID = other.RaceID;
64 }
65
66 #endregion
67
68 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
69
70 #region Basic properties
71
75 [FileServiceOrder]
76 public SwimmingStyles Style => Starts?.FirstOrDefault()?.Style ?? SwimmingStyles.Unknown;
77
81 [FileServiceOrder]
82 public int Distance => Starts?.FirstOrDefault()?.CompetitionObj?.Distance ?? 0;
83
84 private ObservableCollection<PersonStart> _starts;
88 [FileServiceOrder]
89 public ObservableCollection<PersonStart> Starts
90 {
91 get => _starts;
92 set => SetProperty(ref _starts, value);
93 }
94
95 #endregion
96
97 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
98
99 #region Validation properties
100
104 [FileServiceIgnore]
105 public bool IsValid_SameDistances => Starts?.GroupBy(s => s?.CompetitionObj?.Distance).Count() <= 1;
106
110 [FileServiceIgnore]
111 public bool IsValid_SameStyles => Starts?.GroupBy(s => s?.Style).Count() <= 1;
112
116 [FileServiceIgnore]
117 public bool IsValid_StartsAvailable => Starts?.Count > 0;
118
125 [FileServiceIgnore]
127
128 #endregion
129
130 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
131
132 #region Other properties
133
134 private int _raceID;
138 [FileServiceIgnore]
139 public int RaceID
140 {
141 get => _raceID;
142 set => SetProperty(ref _raceID, value);
143 }
144
145 #endregion
146
147 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
148
149 #region Event Handling
150
151 private void RegisterEvents()
152 {
153 if (Starts != null)
154 {
155 Starts.CollectionChanged += Starts_CollectionChanged;
156
157 foreach(PersonStart start in Starts)
158 {
159 start.PropertyChanged += Start_PropertyChanged;
160 start.CompetitionObj?.PropertyChanged += Competition_PropertyChanged;
161 }
162 }
163 }
164
165 private void Competition_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
166 => raiseAllPropertyChanged();
167
168 private void Start_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
169 => raiseAllPropertyChanged();
170
171 private void Starts_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
172 => raiseAllPropertyChanged();
173
174 private void raiseAllPropertyChanged()
175 {
176 OnPropertyChanged(nameof(Style));
177 OnPropertyChanged(nameof(Distance));
178 OnPropertyChanged(nameof(IsValid_SameStyles));
179 OnPropertyChanged(nameof(IsValid_SameDistances));
180 OnPropertyChanged(nameof(IsValid_StartsAvailable));
181 OnPropertyChanged(nameof(IsValid));
182 }
183
184 #endregion
185
186 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
187
188 #region Equality, HashCode, ToString, Clone
189
195 public override bool Equals(object obj)
196 => obj is Race r && r.Starts.SequenceEqual(Starts, new PersonStartWithoutIsActiveEqualityComparer());
197
203 public bool Equals(Race other)
204 => Equals((object)other);
205
210 public override int GetHashCode()
211 => Starts.GetHashCode();
212
217 public object Clone()
218 => new Race(this, true);
219
220 #endregion
221 }
222}
Class describing a start of a person.
Definition PersonStart.cs:9
Competition CompetitionObj
Reference to the competition object to which the start belongs.
Comparer that compares a PersonStart without regarding the PersonStart.IsActive
bool Equals(Race other)
Indicates wheather the current object is equal to another object of the same type.
int RaceID
Number for this Race
Definition Race.cs:140
bool IsValid_SameDistances
True when all PersonStart in the Starts collection have the same distance.
Definition Race.cs:105
int Distance
Distance for this Race.
Definition Race.cs:82
ObservableCollection< PersonStart > Starts
List with all starts of this Race
Definition Race.cs:90
Race(List< PersonStart > starts)
Constructor for the Race class (copy an Starts collection).
Definition Race.cs:28
object Clone()
Create a new object that has the same property values than this one.
bool IsValid_StartsAvailable
True when the starts are not empty.
Definition Race.cs:117
SwimmingStyles Style
for this .
Definition Race.cs:76
Race(ObservableCollection< PersonStart > starts)
Constructor for the Race class (copy an Starts collection).
Definition Race.cs:38
Race()
Constructor for the Race class (create an empty Starts collection).
Definition Race.cs:18
bool IsValid
A Race is considered as valid, when:
Definition Race.cs:126
Race(Race other, bool deepClone=true)
Create a new object that has the same property values than this one.
Definition Race.cs:49
override bool Equals(object obj)
Compare if two Race are equal.
bool IsValid_SameStyles
True when all PersonStart in the Starts collection have the same SwimmingStyles
Definition Race.cs:111
override int GetHashCode()
Serves as the default hash function.
SwimmingStyles
Available swimming styles.