1using CommunityToolkit.Mvvm.ComponentModel;
2using System.Collections.ObjectModel;
3using System.ComponentModel;
5using System.Runtime.Intrinsics.X86;
58 _competitionDistanceRules.CollectionChanged += _competitionDistanceRules_CollectionChanged;
59 _fileService = fileService;
65 _competitionService = competitionService;
78 public async Task<bool>
Load(
string path, CancellationToken cancellationToken)
80 bool importingResult =
false;
81 Exception exception =
null;
86 if (!File.Exists(path))
103 _competitionDistanceRules.CollectionChanged += _competitionDistanceRules_CollectionChanged;
109 importingResult =
true;
111 catch (OperationCanceledException)
113 importingResult =
false;
121 if (exception !=
null) {
throw exception; }
123 return importingResult;
134 public async Task<bool>
Save(CancellationToken cancellationToken,
string path =
"")
138 bool saveResult =
false;
139 Exception exception =
null;
146 if (data is Enum dataEnum)
148 return EnumCoreLocalizedStringHelper.Convert(dataEnum);
152 return data.ToString();
163 catch (OperationCanceledException)
173 if (exception !=
null) {
throw exception; }
196 _competitionDistanceRules.CollectionChanged += _competitionDistanceRules_CollectionChanged;
200 rule.PropertyChanged -= DistanceRule_PropertyChanged;
211 _competitionDistanceRules.CollectionChanged -= _competitionDistanceRules_CollectionChanged;
217 _competitionDistanceRules.CollectionChanged += _competitionDistanceRules_CollectionChanged;
226 _competitionDistanceRules.CollectionChanged += _competitionDistanceRules_CollectionChanged;
229 distanceRule.PropertyChanged += DistanceRule_PropertyChanged;
237 distanceRule.PropertyChanged -= DistanceRule_PropertyChanged;
243 private void DistanceRule_PropertyChanged(
object sender, PropertyChangedEventArgs e)
246 OnPropertyChanged(nameof(HasUnsavedChanges));
249 private void _competitionDistanceRules_CollectionChanged(
object sender,
System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
252 OnPropertyChanged(nameof(HasUnsavedChanges));
260 r.SwimmingStyle == swimmingStyle);
275 List<CompetitionDistanceRuleValidationIssue> result =
new List<CompetitionDistanceRuleValidationIssue>();
288 .Select(i => i.Rule1)
291 (unreachableRules.Contains(i.Rule1) || unreachableRules.Contains(i.Rule2)));
304 List<CompetitionDistanceRuleValidationIssue> validationResult =
new List<CompetitionDistanceRuleValidationIssue>();
306 List<CompetitionDistanceRule> orderedAndFilteredRules =
_competitionDistanceRules.Where(r => r.SwimmingStyle == style)?.OrderBy(r => r.MinAge).ToList();
307 for (
int i = 0; i < orderedAndFilteredRules.Count - 1; i++)
318 MinAge = (byte)(currentRule.
MaxAge + 1),
319 MaxAge = (byte)(nextRule.
MinAge - 1),
324 validationResult.Add(issue);
327 return validationResult;
338 List<CompetitionDistanceRuleValidationIssue> validationResult =
new List<CompetitionDistanceRuleValidationIssue>();
340 List<CompetitionDistanceRule> orderedAndFilteredRules =
_competitionDistanceRules.Where(r => r.SwimmingStyle == style)?.OrderBy(r => r.MinAge).ToList();
341 for (
int i = 0; i < orderedAndFilteredRules.Count - 1; i++)
353 MaxAge = (byte)Math.Min(currentRule.
MaxAge, nextRule.
MaxAge),
358 validationResult.Add(issue);
361 return validationResult;
372 List<CompetitionDistanceRuleValidationIssue> validationResult =
new List<CompetitionDistanceRuleValidationIssue>();
376 HashSet<byte> coveredAges =
new HashSet<byte>();
379 bool anyReachable =
false;
383 if (!coveredAges.Contains(age))
398 validationResult.Add(issue);
403 coveredAges.Add(age);
406 return validationResult;
Class describing a competition distance rule.
static void SetPropertyFromString(CompetitionDistanceRule dataObj, string propertyName, string value)
Set the requested property in the CompetitionDistanceRule object by parsing the given string value.
byte MaxAge
Maximum age for this rule (inclusive)
byte MinAge
Minimum age for this rule (inclusive)
ushort Distance
Distance in meters for this rule.
Class describing one issue during the validation of the CompetitionDistanceRule
CompetitionDistanceRuleValidationIssueType
Enum with available issue types.
async Task< bool > Save(CancellationToken cancellationToken, string path="")
Save the list of Competitions to a file.
ObservableCollection< CompetitionDistanceRule > GetCompetitionDistanceRules()
Return all available objects.List of CompetitionDistanceRule objects
void AddDistanceRule(CompetitionDistanceRule distanceRule)
Add a new CompetitionDistanceRule to the CompetitionDistanceRules list.
void SetCompetitionServiceObj(ICompetitionService competitionService)
Save the reference to the ICompetitionService object.Dependency Injection in the constructor can't be...
List< CompetitionDistanceRuleValidationIssue > validateUnreachableRules(SwimmingStyles style)
Check if there are CompetitionDistanceRule objects for the requested SwimmingStyles that are never re...
List< CompetitionDistanceRule > _competitionDistanceRulesOnLoad
List with all objects at the time the method was called.
CompetitionDistanceRuleService(IFileService fileService)
Constructor.
void ClearAll()
Clear all CompetitionDistanceRule
void RemoveDistanceRule(CompetitionDistanceRule distanceRule)
Remove the given CompetitionDistanceRule from the Competition list.
string PersistentPath
Path to the competition distance rule file.
List< CompetitionDistanceRuleValidationIssue > validateOverlaps(SwimmingStyles style)
Check if there are overlaps between the CompetitionDistanceRule objects for the requested SwimmingSty...
ObservableCollection< CompetitionDistanceRule > _competitionDistanceRules
List with all objects.
bool HasUnsavedChanges
Check if the list of CompetitionDistanceRule has not saved changed.
List< CompetitionDistanceRuleValidationIssue > validateAgeGaps(SwimmingStyles style)
Check if there are gaps between the CompetitionDistanceRule objects for the requested SwimmingStyles ...
void ResetToLoadedState()
Reset the list of to the state when the method was called.This will clear all and add the that were...
EventHandler OnFileFinished
Event that is raised when the file operation is finished.
List< CompetitionDistanceRuleValidationIssue > ValidateRules()
Check if all rules are valid and return a list of found issues.CompetitionDistanceRuleValidationIssue...
ushort GetCompetitionDistanceFromRules(byte age, SwimmingStyles swimmingStyle)
Try to find a matching rule for the requested age and swimming style.Found distance or 0 if not found
ProgressDelegate OnFileProgress
Event that is raised when the file operation progress changes.
async Task< bool > Load(string path, CancellationToken cancellationToken)
Load a list of to the .
Interface for a service used to get and store a list of CompetitionDistanceRule objects.
Interface for a service used to get and store a list of objects.
void UpdateAllCompetitionDistancesFromDistanceRules(bool keepRudolphTableFlags=false)
Update the Competition.Distance from the matching CompetitionDistanceRule for all Competition objects...
Interface for a service that handles file operations.
delegate void ProgressDelegate(object sender, float progress, string currentStep="")
Delegate void for progress changes.
SwimmingStyles
Available swimming styles.
@ SwimmingStyle
Filter by the SwimmingStyles