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
PeopleViewModel.cs
1using System.Collections.ObjectModel;
2using System.ComponentModel;
3using System.Windows.Data;
4using System.Windows.Input;
5using CommunityToolkit.Mvvm.ComponentModel;
6using CommunityToolkit.Mvvm.Input;
7using MahApps.Metro.Controls.Dialogs;
12
14
18public partial class PeopleViewModel : ObservableObject, INavigationAware
19{
23 [ObservableProperty]
24 private ObservableCollection<Person> _people;
25
29 public bool HasEmptyPersons => People?.Any(p => !p.HasStarts) ?? false;
30
34 public bool HasDuplicatePersons => People?.Any(p => p.HasDuplicates) ?? false;
35
36 private ICollectionView _peopleCollectionView;
40 public ICollectionView PeopleCollectionView
41 {
42 get => _peopleCollectionView;
43 private set => SetProperty(ref _peopleCollectionView, value);
44 }
45
49 [ObservableProperty]
51
52 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
53
54 private string _filterText = "";
58 public string FilterText
59 {
60 get => _filterText;
61 set
62 {
63 if (SetProperty(ref _filterText, value))
64 {
65 PeopleCollectionView.Refresh();
66 }
67 }
68 }
69
73 public Predicate<object> PersonFilterPredicate
74 {
75 get
76 {
77 return (item) =>
78 {
79 Person person = item as Person;
80
81 bool filterResult = person.Name.ToLower().Contains(FilterText?.ToLower()) ||
82 person.FirstName.ToLower().Contains(FilterText?.ToLower()) ||
83 (person.FirstName.ToLower() + " " + person.Name.ToLower()).Contains(FilterText?.ToLower()) ||
84 (person.Name.ToLower() + " " + person.FirstName.ToLower()).Contains(FilterText?.ToLower()) ||
85 (person.FirstName.ToLower() + ", " + person.Name.ToLower()).Contains(FilterText?.ToLower()) ||
86 (person.Name.ToLower() + ", " + person.FirstName.ToLower()).Contains(FilterText?.ToLower()) ||
87 person.BirthYear.ToString().Contains(FilterText ?? "");
88 return filterResult;
89 };
90 }
91 }
92
93 private ICommand _clearFilterCommand;
97 public ICommand ClearFilterCommand => _clearFilterCommand ?? (_clearFilterCommand = new RelayCommand(() =>
98 {
99 FilterText = string.Empty;
100 }));
101
102 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
103
104 private IPersonService _personService;
105 private ICompetitionService _competitionService;
106 private IDialogCoordinator _dialogCoordinator;
107 private ShellViewModel _shellVM;
108
116 public PeopleViewModel(IPersonService personService, ICompetitionService competitionService, IDialogCoordinator dialogCoordinator, ShellViewModel shellVM)
117 {
118 _personService = personService;
119 _competitionService = competitionService;
120 _dialogCoordinator = dialogCoordinator;
121 _shellVM = shellVM;
122 }
123
124 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
125
126 private ICommand _addPersonCommand;
130 public ICommand AddPersonCommand => _addPersonCommand ?? (_addPersonCommand = new RelayCommand(() =>
131 {
132 Person person = new Person();
133 person.PropertyChanged += Person_PropertyChanged;
134 _personService.AddPerson(person);
135 _competitionService.UpdateAllCompetitionsForPerson(person);
136 SelectedPerson = person;
137 OnPropertyChanged(nameof(HasDuplicatePersons));
138 OnPropertyChanged(nameof(HasEmptyPersons));
139 }));
140
141 private ICommand _removePersonCommand;
145 public ICommand RemovePersonCommand => _removePersonCommand ?? (_removePersonCommand = new RelayCommand<Person>(async (person) =>
146 {
147 MessageDialogResult result = await _dialogCoordinator.ShowMessageAsync(_shellVM, Resources.RemovePersonString, Resources.RemovePersonConfirmationString, MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings() { NegativeButtonText = Resources.CancelString });
148 if (result == MessageDialogResult.Affirmative)
149 {
150 person.PropertyChanged -= Person_PropertyChanged;
151 _personService.RemovePerson(person);
152 OnPropertyChanged(nameof(HasDuplicatePersons));
153 OnPropertyChanged(nameof(HasEmptyPersons));
154 }
155 }));
156
157 private ICommand _personActiveInactiveCommand;
161 public ICommand PersonActiveInactiveCommand => _personActiveInactiveCommand ?? (_personActiveInactiveCommand = new RelayCommand<Person>((person) =>
162 {
163 person.IsActive = !person.IsActive;
164 }));
165
166 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
167
168 #region Friend Groups Handling
169
173 [ObservableProperty]
174 private ObservableCollection<FriendGroupViewModel> _friendGroups = new ObservableCollection<FriendGroupViewModel>();
175
176 [ICommand]
180 public void AddNewFriendGroup()
181 {
182 int newGroupId = 0;
183 if (FriendGroups?.Count > 0) { newGroupId = FriendGroups?.Max(g => g.GroupId) + 1 ?? 0; }
185 {
186 GroupId = newGroupId,
187 AvailableFriends = People,
188 Friends = new ObservableCollection<Person>()
189 };
190 newGroup.Friends.CollectionChanged += (s, e) => Friends_CollectionChanged(s, e, newGroup);
191 FriendGroups.Add(newGroup);
192 }
193
198 [ICommand]
200 {
201 foreach (Person person in group.Friends)
202 {
203 if (person.FriendGroupIDs.Contains(group.GroupId))
204 {
205 person.FriendGroupIDs.Remove(group.GroupId);
206 }
207 }
208 FriendGroups.Remove(group);
209 _personService.UpdateAllFriendReferencesFromFriendGroupIDs();
210 }
211
216 {
217 _pauseFriendsCollectionChangedEvent = true;
218 FriendGroups.Clear();
219 _pauseFriendsCollectionChangedEvent = false;
220
221 // Collect all unique group IDs from all persons
222 List<int> allGroupIds = People.Select(p => p.FriendGroupIDs ?? Enumerable.Empty<int>()).Aggregate(new List<int>(), (acc, innerList) =>
223 {
224 acc.AddRange(innerList);
225 return acc;
226 }).Distinct().OrderBy(id => id).ToList();
227
228 foreach (int groupId in allGroupIds)
229 {
230 // Create view model entry for the group
232 {
233 GroupId = groupId,
234 AvailableFriends = People,
235 // Get all persons belonging to this group
236 Friends = new ObservableCollection<Person>(People.Where(p => p.FriendGroupIDs.Contains(groupId)))
237 };
238 group.Friends.CollectionChanged += (s, e) => Friends_CollectionChanged(s, e, group);
239 FriendGroups.Add(group);
240 }
241 }
242
243 private bool _pauseFriendsCollectionChangedEvent = false;
244 private void Friends_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e, FriendGroupViewModel groupViewModel)
245 {
246 if (_pauseFriendsCollectionChangedEvent) { return; }
247
248 int groupId = groupViewModel.GroupId;
249 switch (e.Action)
250 {
251 case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
252 {
253 foreach (Person newPerson in e.NewItems)
254 {
255 if (!newPerson.FriendGroupIDs.Contains(groupId))
256 {
257 newPerson.FriendGroupIDs.Add(groupId);
258 }
259 }
260 break;
261 }
262 case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
263 {
264 foreach (Person oldPerson in e.OldItems)
265 {
266 if (oldPerson.FriendGroupIDs.Contains(groupId))
267 {
268 oldPerson.FriendGroupIDs.Remove(groupId);
269 }
270 }
271 break;
272 }
273 default: break;
274 }
275 _personService.UpdateAllFriendReferencesFromFriendGroupIDs();
276 }
277
278 #endregion
279
280 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
281
282
284 public void OnNavigatedTo(object parameter)
285 {
286 People = _personService?.GetPersons();
287 PeopleCollectionView = CollectionViewSource.GetDefaultView(People);
288 PeopleCollectionView.Filter += PersonFilterPredicate;
289 SelectedPerson = People?.FirstOrDefault();
290
291 foreach(Person person in People)
292 {
293 // Unsubscribe from and resubscribe to the event to avoid multiple subscriptions
294 person.PropertyChanged -= Person_PropertyChanged;
295 person.PropertyChanged += Person_PropertyChanged;
296 }
297
299
300 OnPropertyChanged(nameof(HasDuplicatePersons));
301 OnPropertyChanged(nameof(HasEmptyPersons));
302 }
303
305 public void OnNavigatedFrom()
306 {
307 // Unsubscribe from the event to avoid raising this event on another page (not necessary there)
308 foreach (Person person in People)
309 {
310 person.PropertyChanged -= Person_PropertyChanged;
311 }
312 }
313
314 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
315
316 #region Property changed event handler
317
318 private void Person_PropertyChanged(object sender, PropertyChangedEventArgs e)
319 {
320 switch (e.PropertyName)
321 {
322 case nameof(Person.HasDuplicates): OnPropertyChanged(nameof(HasDuplicatePersons)); break;
323 case nameof(Person.HasStarts): OnPropertyChanged(nameof(HasEmptyPersons)); break;
324 default: break;
325 }
326 }
327
328 #endregion
329}
Class describing a person.
Definition Person.cs:12
string FirstName
First name of the person.
Definition Person.cs:77
ObservableCollection< int > FriendGroupIDs
IDs of the friend groups the person belongs to.
Definition Person.cs:497
UInt16 BirthYear
Birth year of the person.
Definition Person.cs:99
string Name
Last name of the person.
Definition Person.cs:66
Eine stark typisierte Ressourcenklasse zum Suchen von lokalisierten Zeichenfolgen usw.
static string RemovePersonString
Sucht eine lokalisierte Zeichenfolge, die Remove Person ähnelt.
static string CancelString
Sucht eine lokalisierte Zeichenfolge, die Cancel ähnelt.
static string RemovePersonConfirmationString
Sucht eine lokalisierte Zeichenfolge, die Really remove this person?
ICommand ClearFilterCommand
Command to clear the filter.
PeopleViewModel(IPersonService personService, ICompetitionService competitionService, IDialogCoordinator dialogCoordinator, ShellViewModel shellVM)
Constructor of the people view model.
void RemoveFriendGroup(FriendGroupViewModel group)
Remove the given friend group and remove the group ID from all persons belonging to this group.
ICollectionView PeopleCollectionView
CollectionView used to display the list of people and filter.
ICommand PersonActiveInactiveCommand
Command to set a person active or inactive.
bool HasEmptyPersons
True if there are persons with empty starts.
string FilterText
Text used to filter the person list.
Predicate< object > PersonFilterPredicate
Function used when filtering the person list.
ICommand RemovePersonCommand
Command to remove a person from the list.
void OnNavigatedTo(object parameter)
OnNavigatedTo method to handle navigation to this object.
ICommand AddPersonCommand
Command to add a new person.
bool HasDuplicatePersons
True if there are duplicate persons.
Person _selectedPerson
Currently selected Person
void CreateFriendGroupViewModel()
Creates the friend group view model based on the persons' friend group IDs and friends.
void OnNavigatedFrom()
OnNavigatedFrom method to handle navigation away from this object.
ObservableCollection< Person > _people
List of people shown on the person overview page.
ObservableCollection< FriendGroupViewModel > _friendGroups
List with all friend group view models.
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 objects.
Interface for a service used to get and store a list of Person objects.
@ Person
Highlight all PersonStart objects that have the same Person object.