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
WorkspaceViewModel.cs
1using System.Collections.ObjectModel;
3using System.Windows.Input;
4using CommunityToolkit.Mvvm.ComponentModel;
5using CommunityToolkit.Mvvm.Input;
9
11
15public partial class WorkspaceViewModel : ObservableObject, INavigationAware
16{
17 #region Properties
18
22 public string CurrentWorkspaceFolder => _workspaceService.PersistentPath;
23
27 public bool IsCurrentWorkspaceOpen => _workspaceService.IsWorkspaceOpen;
28
32 [ObservableProperty]
34
35 // ----------------------------------------------------------------------------------------------------------------------------------------------
36
40 public bool HasUnsavedChanges => _workspaceService.HasUnsavedChanges;
41
45 public bool HasUnsavedChanges_Persons => _workspaceService?.HasUnsavedChanges_Persons ?? false;
46
50 public bool HasUnsavedChanges_Competitions => _workspaceService?.HasUnsavedChanges_Competitions ?? false;
51
55 public bool HasUnsavedChanges_Races => _workspaceService?.HasUnsavedChanges_Races ?? false;
56
60 public bool HasUnsavedChanges_Settings => _workspaceService?.HasUnsavedChanges_Settings ?? false;
61
62 // ----------------------------------------------------------------------------------------------------------------------------------------------
63
67 public WorkspaceSettings Settings => _workspaceService?.Settings;
68
72 public WorkspaceSettings SettingsPersistedInFile => _workspaceService?.SettingsPersistedInFile;
73
74 #endregion
75
76 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
77
78 #region Reset Commands
79
80 private ICommand _resetCompetitionsCommand;
84 public ICommand ResetCompetitionsCommand => _resetCompetitionsCommand ?? (_resetCompetitionsCommand = new RelayCommand(() =>
85 {
86 _workspaceService.ResetCompetitionsToLoadedState();
87 OnPropertyChanged(nameof(HasUnsavedChanges_Competitions));
88 }));
89
90 // ----------------------------------------------------------------------------------------------------------------------------------------------
91
92 private ICommand _resetPersonsCommand;
96 public ICommand ResetPersonsCommand => _resetPersonsCommand ?? (_resetPersonsCommand = new RelayCommand(() =>
97 {
98 _workspaceService.ResetPersonsToLoadedState();
99 OnPropertyChanged(nameof(HasUnsavedChanges_Persons));
100 }));
101
102 // ----------------------------------------------------------------------------------------------------------------------------------------------
103
104 private ICommand _resetRacesCommand;
108 public ICommand ResetRacesCommand => _resetRacesCommand ?? (_resetRacesCommand = new RelayCommand(() =>
109 {
110 _workspaceService.ResetRacesToLoadedState();
111 OnPropertyChanged(nameof(HasUnsavedChanges_Races));
112 }));
113
114 // ----------------------------------------------------------------------------------------------------------------------------------------------
115
116 private ICommand _resetSettingsCommand;
120 public ICommand ResetSettingsCommand => _resetSettingsCommand ?? (_resetSettingsCommand = new RelayCommand(() =>
121 {
122 _workspaceService.ResetSettingsToLoadedState();
123 OnPropertyChanged(nameof(HasUnsavedChanges_Settings));
124 initSettingsGroups(_workspaceService.Settings);
125 }));
126
127 #endregion
128
129 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
130
131 #region Settings Groups Handling
132
133 private ObservableCollection<WorkspaceSettingsGroupViewModel> _settingsGroups;
137 public ObservableCollection<WorkspaceSettingsGroupViewModel> SettingsGroups
138 {
139 get => _settingsGroups;
140 private set => SetProperty(ref _settingsGroups, value);
141 }
142
143 private void initSettingsGroups(WorkspaceSettings model)
144 {
145 if(model == null) { return; }
146 SettingsGroups = new ObservableCollection<WorkspaceSettingsGroupViewModel>();
147
148 ResourceDictionary settingEditorTemplatesResourceDictionary = new ResourceDictionary();
149 settingEditorTemplatesResourceDictionary.Source = new Uri("pack://application:,,,/Views/WorkspaceSettingEditorTemplates.xaml", UriKind.RelativeOrAbsolute);
150
151 foreach (KeyValuePair<string, string> groupViewConfig in WorkspaceSettingViewConfigs.GroupKeyLabelsDict)
152 {
153 // Get the group from the model. If no group with this key exists, skip it.
154 WorkspaceSettingsGroup groupModel = model?.Groups?.FirstOrDefault(g => g.GroupKey == groupViewConfig.Key);
155 if (groupModel == null) { continue; }
156
157 List<IWorkspaceSettingViewModel> settingsVms = new List<IWorkspaceSettingViewModel>();
158
159 // Iterate over the setting view configs
160 foreach (KeyValuePair<(string, string), WorkspaceSettingViewConfig> settingViewConfig in WorkspaceSettingViewConfigs.SettingKeyConfigDict)
161 {
162 (string groupKey, string settingKey) = settingViewConfig.Key;
163
164 // Only consider settings that belong to the current group
165 if (groupKey != groupViewConfig.Key) { continue; }
166
167 // Get the setting from the model. If no setting with this key exists, skip it.
168 IWorkspaceSetting settingModel = groupModel?.Settings?.FirstOrDefault(s => s.Key == settingKey);
169 if (settingModel == null) { continue; }
170
171 WorkspaceSettingEditorTypes editorType = settingViewConfig.Value?.Editor ?? WorkspaceSettingEditorTypes.String;
172 DataTemplate editorTemplate = settingEditorTemplatesResourceDictionary[editorType.ToString() + "EditorTemplate"] as DataTemplate;
173
174 // Create the view model for this setting
175 Type genericType = typeof(WorkspaceSettingViewModel<>).MakeGenericType(settingModel.ValueType);
176 IWorkspaceSettingViewModel settingVm = (IWorkspaceSettingViewModel)Activator.CreateInstance(genericType,
177 settingModel,
178 settingViewConfig.Value.Label,
179 settingViewConfig.Value.Tooltip,
180 settingViewConfig.Value.Icon,
181 settingViewConfig.Value.IconGeometry,
182 editorTemplate,
183 settingViewConfig.Value.SupportResetToDefault)!;
184 settingsVms.Add(settingVm);
185 }
186
187 WorkspaceSettingsGroupViewModel groupVm = new WorkspaceSettingsGroupViewModel(groupModel, groupViewConfig.Value, settingsVms);
188 SettingsGroups.Add(groupVm);
189 }
190 }
191
192 #endregion
193
194 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
195
196 private IWorkspaceService _workspaceService;
197 private IWorkspaceManagerViewModel _workspaceManagerViewModel;
198
204 public WorkspaceViewModel(IWorkspaceService workspaceService, IWorkspaceManagerViewModel workspaceManagerViewModel)
205 {
206 _workspaceService = workspaceService;
207 _workspaceService.PropertyChanged += _workspaceService_PropertyChanged;
208
209 _workspaceManagerViewModel = workspaceManagerViewModel;
210 _workspaceManagerViewModel.OnWorkspaceLoaded += (sender, path) => initSettingsGroups(_workspaceService.Settings);
211 IsWorkspaceManagerExpanded = !IsCurrentWorkspaceOpen;
212 }
213
214 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
215
216 private void _workspaceService_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
217 {
218 switch (e.PropertyName)
219 {
220 case nameof(IWorkspaceService.HasUnsavedChanges): OnPropertyChanged(nameof(HasUnsavedChanges)); break;
221 case nameof(IWorkspaceService.HasUnsavedChanges_Persons): OnPropertyChanged(nameof(HasUnsavedChanges_Persons)); break;
222 case nameof(IWorkspaceService.HasUnsavedChanges_Competitions): OnPropertyChanged(nameof(HasUnsavedChanges_Competitions)); break;
223 case nameof(IWorkspaceService.HasUnsavedChanges_Races): OnPropertyChanged(nameof(HasUnsavedChanges_Races)); break;
225 {
226 OnPropertyChanged(nameof(HasUnsavedChanges_Settings));
227 OnPropertyChanged(nameof(SettingsGroups));
228 break;
229 }
230 case nameof(IWorkspaceService.PersistentPath): OnPropertyChanged(nameof(CurrentWorkspaceFolder)); break;
232 {
233 OnPropertyChanged(nameof(CurrentWorkspaceFolder));
234 OnPropertyChanged(nameof(IsCurrentWorkspaceOpen));
235 OnPropertyChanged(nameof(Settings));
236 IsWorkspaceManagerExpanded = !IsCurrentWorkspaceOpen;
237 break;
238 }
239 default: break;
240 }
241 }
242
244 public void OnNavigatedTo(object parameter)
245 {
246 OnPropertyChanged(nameof(CurrentWorkspaceFolder));
247 OnPropertyChanged(nameof(IsCurrentWorkspaceOpen));
248 OnPropertyChanged(nameof(Settings));
249 OnPropertyChanged(nameof(HasUnsavedChanges));
250 OnPropertyChanged(nameof(HasUnsavedChanges_Competitions));
251 OnPropertyChanged(nameof(HasUnsavedChanges_Persons));
252 OnPropertyChanged(nameof(HasUnsavedChanges_Races));
253 OnPropertyChanged(nameof(HasUnsavedChanges_Settings));
254
255 initSettingsGroups(_workspaceService.Settings);
256 }
257
259 public void OnNavigatedFrom()
260 {
261 }
262}
List< IWorkspaceSetting > Settings
List with IWorkspaceSetting instances belonging to this group.
List< WorkspaceSettingsGroup > Groups
List with WorkspaceSettingsGroup instances.
ICommand ResetPersonsCommand
Command to reset the persons.
WorkspaceSettings SettingsPersistedInFile
Settings for the current workspace that are persisted in the file.
WorkspaceViewModel(IWorkspaceService workspaceService, IWorkspaceManagerViewModel workspaceManagerViewModel)
Constructor of the workspace view model.
ICommand ResetRacesCommand
Command to reset the races.
bool HasUnsavedChanges
True if the workspace has unsaved changes, false otherwise.
ICommand ResetSettingsCommand
Command to reset the settings.
ObservableCollection< WorkspaceSettingsGroupViewModel > SettingsGroups
List of WorkspaceSettingsGroupViewModel instances representing the settings groups in the workspace.
bool IsCurrentWorkspaceOpen
Is a workspace opened at the moment?
bool HasUnsavedChanges_Races
True if the workspace has unsaved changes in races.
bool HasUnsavedChanges_Competitions
True if the workspace has unsaved changes in competitions.
bool _isWorkspaceManagerExpanded
True, when the WorkspaceManagerUserControl should be visible.
string CurrentWorkspaceFolder
Current workspace folder path.
void OnNavigatedTo(object parameter)
OnNavigatedTo method to handle navigation to this object.
WorkspaceSettings Settings
Settings for the current workspace.
bool HasUnsavedChanges_Settings
True if the workspace has unsaved changes in settings.
void OnNavigatedFrom()
OnNavigatedFrom method to handle navigation away from this object.
ICommand ResetCompetitionsCommand
Command to reset the competitions.
bool HasUnsavedChanges_Persons
True if the workspace has unsaved changes in persons.
Interface for objects that need to handle navigation events.
bool HasUnsavedChanges
Check if there are unsave changes.
Definition ISaveable.cs:44
Interface for a service used to manage a workspace.
bool HasUnsavedChanges_Settings
Unsaved changes exist in the Settings.
bool HasUnsavedChanges_Persons
Unsaved changes exist in the PersonService
bool IsWorkspaceOpen
If true, a workspace is loaded; if false, not workspace is loaded.
bool HasUnsavedChanges_Races
Unsaved changes exist in the RaceService
bool HasUnsavedChanges_Competitions
Unsaved changes exist in the <see cref="CompetitionService" or CompetitionDistanceRuleService/>
WorkspaceSettingEditorTypes
Enum with available editor types for workspace settings.