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
CompetitionViewModel.cs
1using System.Collections.ObjectModel;
2using System.ComponentModel;
3using System.Data;
4using System.Windows.Data;
5using System.Windows.Input;
6using Microsoft.Win32;
7using CommunityToolkit.Mvvm.ComponentModel;
8using CommunityToolkit.Mvvm.Input;
9using MahApps.Metro.Controls.Dialogs;
14
16
20public partial class CompetitionViewModel : ObservableObject, INavigationAware
21{
25 [ObservableProperty]
26 private ObservableCollection<Competition> _competitions;
27
28 private ICollectionView _competitionsCollectionView;
32 public ICollectionView CompetitionsCollectionView
33 {
34 get => _competitionsCollectionView;
35 private set => SetProperty(ref _competitionsCollectionView, value);
36 }
37
41 [ObservableProperty]
43
47 public bool IsAnyDistanceInvalid => Competitions.Any(c => !c.IsDistanceValid);
48
52 public bool HasDuplicateCompetitions => Competitions?.Any(c => c.HasDuplicates) ?? false;
53
54 // ----------------------------------------------------------------------------------------------------------------------------------------------
55
59 [ObservableProperty]
60 private ObservableCollection<CompetitionDistanceRule> _competitionDistanceRules;
61
62 private ICollectionView _competitionDistanceRuleCollectionView;
67 {
68 get => _competitionDistanceRuleCollectionView;
69 private set => SetProperty(ref _competitionDistanceRuleCollectionView, value);
70 }
71
75 public List<CompetitionDistanceRuleValidationIssue> CompetitionDistanceRuleValidationIssues
76 => _competitionDistanceRuleService.ValidateRules();
77
78 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
79
80 private List<SwimmingStyles> _availableSwimmingStyles = Enum.GetValues(typeof(SwimmingStyles)).Cast<SwimmingStyles>().Where(s => s != SwimmingStyles.Unknown).ToList();
84 public List<SwimmingStyles> AvailableSwimmingStyles => _availableSwimmingStyles;
85
86 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
87
88 private ICompetitionService _competitionService;
89 private ICompetitionDistanceRuleService _competitionDistanceRuleService;
90 private IWorkspaceService _workspaceService;
91 private IDialogCoordinator _dialogCoordinator;
92 private ShellViewModel _shellVM;
93
102 public CompetitionViewModel(ICompetitionService competitionService, ICompetitionDistanceRuleService competitionDistanceRuleService, IWorkspaceService workspaceService, IDialogCoordinator dialogCoordinator, ShellViewModel shellVM)
103 {
104 _competitionService = competitionService;
105 _competitionDistanceRuleService = competitionDistanceRuleService;
106 _workspaceService = workspaceService;
107 _dialogCoordinator = dialogCoordinator;
108 _shellVM = shellVM;
109
110 _competitionDistanceRuleService.PropertyChanged += (sender, e) =>
111 {
112 switch(e.PropertyName)
113 {
115 default: break;
116 }
117 };
118 }
119
120 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
121
122 private ICommand _addCompetitionCommand;
126 public ICommand AddCompetitionCommand => _addCompetitionCommand ?? (_addCompetitionCommand = new RelayCommand(() =>
127 {
128 Competition competition = new Competition();
129 _competitionService.AddCompetition(competition);
130 competition.PropertyChanged += Competition_PropertyChanged;
131 SelectedCompetition = competition;
132 OnPropertyChanged(nameof(IsAnyDistanceInvalid));
133 OnPropertyChanged(nameof(HasDuplicateCompetitions));
134 }));
135
136 private ICommand _removeCompetitionCommand;
140 public ICommand RemoveCompetitionCommand => _removeCompetitionCommand ?? (_removeCompetitionCommand = new RelayCommand<Competition>(async (competition) =>
141 {
142 MessageDialogResult result = await _dialogCoordinator.ShowMessageAsync(_shellVM, Resources.RemoveCompetitionString, Resources.RemoveCompetitionConfirmationString, MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings() { NegativeButtonText = Resources.CancelString });
143 if (result == MessageDialogResult.Affirmative)
144 {
145 competition.PropertyChanged -= Competition_PropertyChanged;
146 _competitionService.RemoveCompetition(competition);
147 OnPropertyChanged(nameof(IsAnyDistanceInvalid));
148 OnPropertyChanged(nameof(HasDuplicateCompetitions));
149 }
150 }));
151
152 // ----------------------------------------------------------------------------------------------------------------------------------------------
153
154 private ICommand _addDistanceRuleCommand;
158 public ICommand AddDistanceRuleCommand => _addDistanceRuleCommand ?? (_addDistanceRuleCommand = new RelayCommand(() =>
159 {
161 _competitionDistanceRuleService.AddDistanceRule(rule);
162 }));
163
164 private ICommand _removeDistanceRuleCommand;
168 public ICommand RemoveDistanceRuleCommand => _removeDistanceRuleCommand ?? (_removeDistanceRuleCommand = new RelayCommand<CompetitionDistanceRule>(async (rule) =>
169 {
170 MessageDialogResult result = await _dialogCoordinator.ShowMessageAsync(_shellVM, Resources.RemoveDistanceRuleString, Resources.RemoveDistanceRuleConfirmationString, MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings() { NegativeButtonText = Resources.CancelString });
171 if (result == MessageDialogResult.Affirmative)
172 {
173 _competitionDistanceRuleService.RemoveDistanceRule(rule);
174 }
175 }));
176
177 // ----------------------------------------------------------------------------------------------------------------------------------------------
178
179 private ICommand _updateCompetitionTimesFromRudolphTableCommand;
183 public ICommand UpdateCompetitionTimesFromRudolphTableCommand => _updateCompetitionTimesFromRudolphTableCommand ?? (_updateCompetitionTimesFromRudolphTableCommand = new RelayCommand(async () =>
184 {
185 OpenFileDialog fileDialog = new OpenFileDialog();
186 fileDialog.InitialDirectory = _workspaceService.PersistentPath;
187 fileDialog.Filter = Resources.FileDialogCsvFilterString;
188 fileDialog.Title = Resources.SelectRudolphTableString;
189 if (fileDialog.ShowDialog() == true)
190 {
191 string inputStr = await _dialogCoordinator.ShowInputAsync(_shellVM, Resources.RudolphTableString, Resources.EnterRudolphScoreString, new MetroDialogSettings() { NegativeButtonText = Resources.CancelString });
192 if (inputStr != null)
193 {
194 byte rudolphScore = 0;
195 if (byte.TryParse(inputStr, out rudolphScore) && rudolphScore >= 1 && rudolphScore <= 20)
196 {
197 _competitionService.UpdateAllCompetitionTimesFromRudolphTable(fileDialog.FileName, rudolphScore);
198 await _dialogCoordinator.ShowMessageAsync(_shellVM, Resources.RudolphTableString, Resources.FinishedUpdateFromRudolphTableString);
199 }
200 else
201 {
202 await _dialogCoordinator.ShowMessageAsync(_shellVM, Resources.ErrorString, Resources.ErrorUpdatingFromRudolphTableString);
203 }
204 }
205 else
206 {
207 // User canceled
208 }
209 }
210 }));
211
212 private ICommand _createCompetitionsFromRudolphTableCommand;
216 public ICommand CreateCompetitionsFromRudolphTableCommand => _createCompetitionsFromRudolphTableCommand ?? (_createCompetitionsFromRudolphTableCommand = new RelayCommand(async () =>
217 {
218 MessageDialogResult result = await _dialogCoordinator.ShowMessageAsync(_shellVM, Resources.RudolphTableString, Resources.RudolphTableCompetitionDeletionConfirmationString, MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings() { NegativeButtonText = Resources.CancelString });
219 if (result == MessageDialogResult.Affirmative)
220 {
221 OpenFileDialog fileDialog = new OpenFileDialog();
222 fileDialog.InitialDirectory = _workspaceService.PersistentPath;
223 fileDialog.Filter = Resources.FileDialogCsvFilterString;
224 fileDialog.Title = Resources.SelectRudolphTableString;
225 if (fileDialog.ShowDialog() == true)
226 {
227 string inputStr = await _dialogCoordinator.ShowInputAsync(_shellVM, Resources.RudolphTableString, Resources.EnterRudolphScoreString, new MetroDialogSettings() { NegativeButtonText = Resources.CancelString });
228 if (inputStr != null)
229 {
230 byte rudolphScore = 0;
231 if (byte.TryParse(inputStr, out rudolphScore) && rudolphScore >= 1 && rudolphScore <= 20)
232 {
233 _competitionService.CreateCompetitionsFromRudolphTable(fileDialog.FileName, rudolphScore);
234 await _dialogCoordinator.ShowMessageAsync(_shellVM, Resources.RudolphTableString, Resources.FinishedUpdateFromRudolphTableString);
235 OnPropertyChanged(nameof(IsAnyDistanceInvalid));
236 }
237 else
238 {
239 await _dialogCoordinator.ShowMessageAsync(_shellVM, Resources.ErrorString, Resources.ErrorUpdatingFromRudolphTableString);
240 }
241 }
242 else
243 {
244 // User canceled
245 }
246 }
247 }
248 }));
249
250 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
251
253 public void OnNavigatedTo(object parameter)
254 {
255 Competitions = _competitionService?.GetCompetitions();
256 CompetitionsCollectionView = CollectionViewSource.GetDefaultView(Competitions);
257 SelectedCompetition = Competitions?.FirstOrDefault();
258
259 CompetitionDistanceRules = _competitionDistanceRuleService?.GetCompetitionDistanceRules();
260 CompetitionDistanceRuleCollectionView = CollectionViewSource.GetDefaultView(CompetitionDistanceRules);
261
262 foreach (Competition competition in Competitions)
263 {
264 // Unsubscribe from and resubscribe to the event to avoid multiple subscriptions
265 competition.PropertyChanged -= Competition_PropertyChanged;
266 competition.PropertyChanged += Competition_PropertyChanged;
267 }
268
269 OnPropertyChanged(nameof(IsAnyDistanceInvalid));
270 OnPropertyChanged(nameof(HasDuplicateCompetitions));
271 OnPropertyChanged(nameof(CompetitionDistanceRuleValidationIssues));
272 }
273
275 public void OnNavigatedFrom()
276 {
277 // Unsubscribe from the event to avoid raising this event on another page (not necessary there)
278 foreach (Competition competition in Competitions)
279 {
280 competition.PropertyChanged -= Competition_PropertyChanged;
281 }
282 }
283
284 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
285
286 #region Property changed event handler
287
288 private void Competition_PropertyChanged(object sender, PropertyChangedEventArgs e)
289 {
290 switch (e.PropertyName)
291 {
292 case nameof(Competition.HasDuplicates): OnPropertyChanged(nameof(HasDuplicateCompetitions)); break;
293 case nameof(Competition.IsDistanceValid): OnPropertyChanged(nameof(IsAnyDistanceInvalid)); break;
294 default: break;
295 }
296 }
297
298 #endregion
299
300}
Class describing a competition.
bool HasDuplicates
This flag indicates if there are duplicates of this in the competition list.
bool IsDistanceValid
Only true when the Distance is greater 0.
Eine stark typisierte Ressourcenklasse zum Suchen von lokalisierten Zeichenfolgen usw.
static string RemoveCompetitionString
Sucht eine lokalisierte Zeichenfolge, die Remove Competition ähnelt.
static string CancelString
Sucht eine lokalisierte Zeichenfolge, die Cancel ähnelt.
static string RemoveCompetitionConfirmationString
Sucht eine lokalisierte Zeichenfolge, die Really remove this competition?
static string FileDialogCsvFilterString
Sucht eine lokalisierte Zeichenfolge, die CSV Files (.csv)|.csv ähnelt.
static string ErrorUpdatingFromRudolphTableString
Sucht eine lokalisierte Zeichenfolge, die Error while updating from the Rudolph Table (e....
static string SelectRudolphTableString
Sucht eine lokalisierte Zeichenfolge, die Select a Rudolph Table (.csv) ähnelt.
static string RudolphTableCompetitionDeletionConfirmationString
Sucht eine lokalisierte Zeichenfolge, die CAUTION: When the Rudolph Table is used to create the compe...
static string RemoveDistanceRuleConfirmationString
Sucht eine lokalisierte Zeichenfolge, die Really remove this distance rule?
static string ErrorString
Sucht eine lokalisierte Zeichenfolge, die Error ähnelt.
static string RudolphTableString
Sucht eine lokalisierte Zeichenfolge, die Rudolph Table ähnelt.
static string RemoveDistanceRuleString
Sucht eine lokalisierte Zeichenfolge, die Remove Distance Rule ähnelt.
static string EnterRudolphScoreString
Sucht eine lokalisierte Zeichenfolge, die Please enter the rudolph score (1 - 20) that should be used...
static string FinishedUpdateFromRudolphTableString
Sucht eine lokalisierte Zeichenfolge, die Finished updating from the Rudolph Table.
List< SwimmingStyles > AvailableSwimmingStyles
List with all available SwimmingStyles
ObservableCollection< Competition > _competitions
List of competitions shown on the competition page.
bool HasDuplicateCompetitions
True if there are duplicate Competition
List< CompetitionDistanceRuleValidationIssue > CompetitionDistanceRuleValidationIssues
CompetitionDistanceRuleValidationIssue objects that contain all issues of the validations of the Comp...
ICommand RemoveDistanceRuleCommand
Command to remove a distance rule from the list.
ICommand RemoveCompetitionCommand
Command to remove a competition from the list.
ICommand AddDistanceRuleCommand
Command to add a new distance rule.
void OnNavigatedTo(object parameter)
OnNavigatedTo method to handle navigation to this object.
ICollectionView CompetitionsCollectionView
CollectionView used to display the list of competitions.
ICollectionView CompetitionDistanceRuleCollectionView
CollectionView used to display the list of competition distance rules.
ICommand UpdateCompetitionTimesFromRudolphTableCommand
Command to update the competition times from a rudolph table.
ICommand CreateCompetitionsFromRudolphTableCommand
Command to create the competitions from a rudolph table.
ICommand AddCompetitionCommand
Command to add a new competition.
CompetitionViewModel(ICompetitionService competitionService, ICompetitionDistanceRuleService competitionDistanceRuleService, IWorkspaceService workspaceService, IDialogCoordinator dialogCoordinator, ShellViewModel shellVM)
Constructor of the competitions view model.
Competition _selectedCompetition
Currently selected Competition
ObservableCollection< CompetitionDistanceRule > _competitionDistanceRules
List of competitions distance rules shown on the competition page.
void OnNavigatedFrom()
OnNavigatedFrom method to handle navigation away from this object.
bool IsAnyDistanceInvalid
True, if at least one Competition has an invalid distance.
ViewModel for the main shell of the application.
Interface for objects that need to handle navigation events.
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.
bool HasUnsavedChanges
Check if there are unsave changes.
Definition ISaveable.cs:44
Interface for a service used to manage a workspace.
SwimmingStyles
Available swimming styles.