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
TimeInputViewModel.cs
1using CommunityToolkit.Mvvm.ComponentModel;
2using System.ComponentModel;
3using System.Windows.Data;
8
10
14public partial class TimeInputViewModel : ObservableObject, INavigationAware
15{
19 public List<PersonStart> AvailablePersonStarts { get; set; }
20
21 private ICollectionView _availablePersonStartsCollectionView;
26 {
27 get => _availablePersonStartsCollectionView;
28 private set => SetProperty(ref _availablePersonStartsCollectionView, value);
29 }
30
34 public RacesVariant PersistedRacesVariant => _raceService?.PersistedRacesVariant;
35
36 private int _timeInputMillisecondDigits;
41 {
42 get => _timeInputMillisecondDigits;
43 set
44 {
45 if (SetProperty(ref _timeInputMillisecondDigits, value))
46 {
47 OnPropertyChanged(nameof(TimeInputFormatString));
48 }
49 }
50 }
51
55 public string TimeInputFormatString => $"mm:ss.{new string('f', TimeInputMillisecondDigits)}";
56
60 public bool IsTimeInputMissing => AvailablePersonStarts.Any(s => s.IsActive && s.CompetitionObj != null && !s.IsTimeSet);
61
62 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
63
64 #region Filter Feature
65
66 private TimeInputPersonStartFilterModes _filterPersonStartMode = TimeInputPersonStartFilterModes.None;
71 {
72 get => _filterPersonStartMode;
73 set
74 {
75 if (SetProperty(ref _filterPersonStartMode, value))
76 {
78 }
79 }
80 }
81
82 // ----------------------------------------------------------------------------------------------------------------------------------------------
83
87 public List<Person> AvailablePersons => _personService?.GetPersons().OrderBy(p => p.Name).ToList();
88
89 private Person _filteredPerson;
94 {
95 get => _filteredPerson;
96 set
97 {
98 if (SetProperty(ref _filteredPerson, value))
99 {
101 }
102 }
103 }
104
105 // ----------------------------------------------------------------------------------------------------------------------------------------------
106
107 private int _filteredRaceID = 1;
111 public int FilteredRaceID
112 {
113 get => _filteredRaceID;
114 set
115 {
116 if (SetProperty(ref _filteredRaceID, value))
117 {
119 }
120 }
121 }
122
123 // ----------------------------------------------------------------------------------------------------------------------------------------------
124
125 private int _filteredCompetitionID = 1;
130 {
131 get => _filteredCompetitionID;
132 set
133 {
134 if (SetProperty(ref _filteredCompetitionID, value))
135 {
137 }
138 }
139 }
140
141 // ----------------------------------------------------------------------------------------------------------------------------------------------
142
147 {
148 get
149 {
150 return (item) =>
151 {
152 PersonStart personStart = item as PersonStart;
153
154 bool filterResult = true;
156 {
158 return personStart?.PersonObj == FilteredPerson;
160 Race race = PersistedRacesVariant?.Races?.Where(r => r.Starts.Contains(personStart)).FirstOrDefault();
161 return race == null ? false : race.RaceID == FilteredRaceID;
162 case TimeInputPersonStartFilterModes.CompetitionID:
163 return (personStart?.CompetitionObj?.Id ?? -1) == FilteredCompetitionID;
165 default:
166 break;
167 }
168 return filterResult;
169 };
170 }
171 }
172
173 #endregion
174
175 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
176
177 private readonly IPersonService _personService;
178 private readonly IRaceService _raceService;
179 private readonly IWorkspaceService _workspaceService;
180
187 public TimeInputViewModel(IPersonService personService, IRaceService raceService, IWorkspaceService workspaceService)
188 {
189 _personService = personService;
190 _raceService = raceService;
191 _workspaceService = workspaceService;
192 }
193
195 public void OnNavigatedTo(object parameter)
196 {
197 AvailablePersonStarts = _personService.GetAllPersonStarts().Where(s => s.IsActive).ToList();
198 AvailablePersonStartsCollectionView = CollectionViewSource.GetDefaultView(AvailablePersonStarts);
199 AvailablePersonStartsCollectionView.Filter += AvailablePersonStartsFilterPredicate;
200
201 TimeInputMillisecondDigits = _workspaceService?.Settings?.GetSettingValue<ushort>(WorkspaceSettings.GROUP_GENERAL, WorkspaceSettings.SETTING_GENERAL_TIMEINPUT_NUMBER_MILLISECOND_DIGITS) ?? 2;
202
203 foreach (PersonStart start in AvailablePersonStarts)
204 {
205 // Unsubscribe from and resubscribe to the event to avoid multiple subscriptions
206 start.PropertyChanged -= Start_PropertyChanged;
207 start.PropertyChanged += Start_PropertyChanged;
208 }
209
210 OnPropertyChanged(nameof(PersistedRacesVariant));
211 OnPropertyChanged(nameof(AvailablePersons));
212 OnPropertyChanged(nameof(IsTimeInputMissing));
213 }
214
216 public void OnNavigatedFrom()
217 {
218 // Unsubscribe from the event to avoid raising this event on another page (not necessary there)
219 foreach (PersonStart start in AvailablePersonStarts)
220 {
221 start.PropertyChanged -= Start_PropertyChanged;
222 }
223 }
224
225 private void Start_PropertyChanged(object sender, PropertyChangedEventArgs e)
226 {
227 switch (e.PropertyName)
228 {
229 case nameof(PersonStart.IsActive):
230 case nameof(PersonStart.IsTimeSet):
231 OnPropertyChanged(nameof(IsTimeInputMissing));
232 break;
233 default: break;
234 }
235 }
236}
Class describing a start of a person.
Definition PersonStart.cs:9
Class that represents a single race.
Definition Race.cs:12
Class that represents a combination variant of all single races.
void OnNavigatedFrom()
OnNavigatedFrom method to handle navigation away from this object.
ICollectionView AvailablePersonStartsCollectionView
CollectionView used to display the list of available PersonStart
void OnNavigatedTo(object parameter)
OnNavigatedTo method to handle navigation to this object.
bool IsTimeInputMissing
True, if at least one active PersonStart is missing a time input.
string TimeInputFormatString
Format string displayed as tooltip in the time input control.
Person FilteredPerson
All PersonStart elements that match this Person will be filtered if the FilterPersonStartMode is Time...
int TimeInputMillisecondDigits
Number of digits used to display milliseconds in the time input control.
TimeInputViewModel(IPersonService personService, IRaceService raceService, IWorkspaceService workspaceService)
Constructor of the time input view model.
TimeInputPersonStartFilterModes FilterPersonStartMode
Currently active filter mode.
List< Person > AvailablePersons
List with all available Person objects.
RacesVariant PersistedRacesVariant
Gets the persisted variant of races as managed by the race service.
List< PersonStart > AvailablePersonStarts
List of all available PersonStart objects.
int FilteredCompetitionID
All PersonStart elements that match this competition ID will be filtered if the FilterPersonStartMode...
Predicate< object > AvailablePersonStartsFilterPredicate
Function used when filtering the PersonStart list.
int FilteredRaceID
All PersonStart elements that are part of the race with the Race.RaceID in the PersistedRacesVariant ...
Interface for objects that need to handle navigation events.
Interface for a service used to get and store a list of Person objects.
Interface for a service used to manage Race and RacesVariant objects.
Interface for a service used to manage a workspace.
@ Person
Highlight all PersonStart objects that have the same Person object.
TimeInputPersonStartFilterModes
Enum to define the different modes to filter a PersonStart