39 private IDialogCoordinator _dialogCoordinator;
46 private const string SETTING_KEY_LAST_WORKSPACE_PATHS =
"LastWorkspacePaths";
47 private const string SETTING_KEY_LAST_WORKSPACE_FOLDER =
"LastWorkspaceFolder";
53 #region Hamburger menu items
70 public ObservableCollection<HamburgerMenuItem>
MenuItems {
get; } =
new ObservableCollection<HamburgerMenuItem>()
73 new HamburgerMenuGlyphItem() { Label =
Resources.
ShellWorkspacePage, Glyph =
"\uE821", TargetPageType = typeof(WorkspaceViewModel) },
74 new HamburgerMenuGlyphItem() { Label = Resources.ShellCompetitionPage, Glyph =
"\uF0E3", TargetPageType = typeof(CompetitionViewModel) },
75 new HamburgerMenuGlyphItem() { Label = Resources.ShellPeoplePage, Glyph =
"\uE77B", TargetPageType = typeof(PeopleViewModel) },
76 new HamburgerMenuGlyphItem() { Label = Resources.ShellPrepareRacesPage, Glyph =
"\uE7C1", TargetPageType = typeof(PrepareRacesViewModel) },
77 new HamburgerMenuGlyphItem() { Label = Resources.ShellTimeInputPage, Glyph =
"\uE916", TargetPageType = typeof(TimeInputViewModel) },
78 new HamburgerMenuGlyphItem() { Label = Resources.ShellResultsPage, Glyph =
"\uE9F9", TargetPageType = typeof(ResultsViewModel) },
79 new HamburgerMenuGlyphItem() { Label = Resources.ShellAnalyticsPage, Glyph =
"\uE9D2", TargetPageType = typeof(AnalyticsViewModel) },
80 new HamburgerMenuGlyphItem() { Label = Resources.ShellCreateDocumentsPage, Glyph =
"\uE8A5", TargetPageType = typeof(CreateDocumentsViewModel) },
86 public ObservableCollection<HamburgerMenuItem>
OptionMenuItems {
get; } =
new ObservableCollection<HamburgerMenuItem>()
97 private RelayCommand _goBackCommand;
98 private ICommand _menuItemInvokedCommand;
99 private ICommand _optionsMenuItemInvokedCommand;
100 private ICommand _loadedCommand;
101 private ICommand _unloadedCommand;
102 private ICommand _closingCommand;
103 private ICommand _saveWorkspaceCommand;
104 private ICommand _openWorkspaceFlyoutCommand;
109 public RelayCommand
GoBackCommand => _goBackCommand ?? (_goBackCommand =
new RelayCommand(() => _navigationService.GoBack(), () => _navigationService.CanGoBack));
114 public ICommand
MenuItemInvokedCommand => _menuItemInvokedCommand ?? (_menuItemInvokedCommand =
new RelayCommand(() => NavigateTo(SelectedMenuItem.TargetPageType)));
119 public ICommand
OptionsMenuItemInvokedCommand => _optionsMenuItemInvokedCommand ?? (_optionsMenuItemInvokedCommand =
new RelayCommand(() => NavigateTo(SelectedOptionsMenuItem.TargetPageType)));
124 public ICommand
LoadedCommand => _loadedCommand ?? (_loadedCommand =
new RelayCommand(OnLoaded));
129 public ICommand
UnloadedCommand => _unloadedCommand ?? (_unloadedCommand =
new RelayCommand(OnUnloaded));
134 public ICommand
ClosingCommand => _closingCommand ?? (_closingCommand =
new RelayCommand<CancelEventArgs>(OnClosing));
139 public ICommand
SaveWorkspaceCommand => _saveWorkspaceCommand ?? (_saveWorkspaceCommand =
new RelayCommand(async () => await _workspaceService?.Save(CancellationToken.None)));
144 public ICommand
OpenWorkspaceFlyoutCommand => _openWorkspaceFlyoutCommand ?? (_openWorkspaceFlyoutCommand =
new RelayCommand(() => IsWorkspaceFlyoutOpen = !IsWorkspaceFlyoutOpen));
158 _navigationService = navigationService;
159 _dialogCoordinator = dialogCoordinator;
160 _workspaceService = workspaceService;
165 #region Load and unload
167 private async
void OnLoaded()
169 _navigationService.Navigated += OnNavigated;
170 _workspaceService.PropertyChanged += _workspaceService_PropertyChanged;
173 object lastWorkspacePaths =
App.Current.Properties[SETTING_KEY_LAST_WORKSPACE_PATHS];
174 if (lastWorkspacePaths !=
null)
176 Newtonsoft.Json.Linq.JArray lastWorkspacePathsJArray = lastWorkspacePaths as Newtonsoft.Json.Linq.JArray;
177 List<string> lastWorkspacePathsList = lastWorkspacePathsJArray.ToObject<List<string>>();
178 _workspaceService.LastWorkspacePaths =
new ObservableCollection<string>(lastWorkspacePathsList);
184 await _workspaceService.
Load(
App.Current.Properties[SETTING_KEY_LAST_WORKSPACE_FOLDER]?.ToString(), CancellationToken.None);
188 await _dialogCoordinator.ShowMessageAsync(
this, Resources.ErrorString, ex.Message);
192 private void OnUnloaded()
194 _navigationService.Navigated -= OnNavigated;
195 _workspaceService.PropertyChanged -= _workspaceService_PropertyChanged;
198 private void _workspaceService_PropertyChanged(
object sender, System.ComponentModel.PropertyChangedEventArgs e)
200 switch (e.PropertyName)
202 case nameof(IWorkspaceService.HasUnsavedChanges): OnPropertyChanged(nameof(
HasUnsavedChanges));
break;
204 case nameof(IWorkspaceService.IsWorkspaceOpen):
219 foreach (HamburgerMenuItem menuItem
in MenuItems)
223 menuItem.IsEnabled = _workspaceService.IsWorkspaceOpen;
234 private bool _forceClose =
false;
240 protected void OnClosing(CancelEventArgs e)
253 Dispatcher.CurrentDispatcher.InvokeAsync(async () =>
255 bool save =
false, cancel =
false;
263 await _workspaceService.Save(CancellationToken.None);
271 App.Current.Properties[SETTING_KEY_LAST_WORKSPACE_PATHS] = _workspaceService.LastWorkspacePaths.ToList();
275 }, DispatcherPriority.Normal);
284 bool save =
false, cancel =
false;
287 MetroDialogSettings dialogButtonSettings =
new MetroDialogSettings()
292 DefaultButtonFocus = MessageDialogResult.Affirmative
295 MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary, dialogButtonSettings);
296 switch (dialogResult)
298 case MessageDialogResult.Affirmative: save =
true; cancel =
false;
break;
299 case MessageDialogResult.Negative: save =
false; cancel =
false;
break;
300 case MessageDialogResult.FirstAuxiliary: save =
false; cancel =
true;
break;
304 return (save, cancel);
311 private void NavigateTo(Type targetViewModel)
313 if (targetViewModel !=
null)
315 _navigationService.NavigateTo(targetViewModel.FullName);
319 private void OnNavigated(
object sender,
string viewModelName)
321 var item =
MenuItems.OfType<HamburgerMenuItem>().FirstOrDefault(i => viewModelName == i.TargetPageType?.FullName);
324 SelectedMenuItem = item;
328 SelectedOptionsMenuItem =
OptionMenuItems.OfType<HamburgerMenuItem>().FirstOrDefault(i => viewModelName == i.TargetPageType?.FullName);
View model for the main view of the application.