/// <summary> /// Deletes a specific item. /// </summary> /// <param name="item">The item being deleted.</param> public void DeleteItem(BaseInventoryModel item) { // Remove the item from their respective model collection. if (item is MovieModel movieModel) { MovieModels.Remove(movieModel); } else if (item is BookModel bookModel) { BookModels.Remove(bookModel); } else if (item is VideoGameModel videoGameModel) { VideoGameModels.Remove(videoGameModel); } }
/// <summary> /// Creates a new dialog window that allows the user to find an item. /// </summary> /// <param name="itemType">The type of item is bound to the the tab the user is on (i.e searching from the movie tab returns a movie).</param> public void FindItem(InventoryItemType itemType) { // Try to resolve FindItemViewModel. if (!(App.ServiceProvider.GetService(typeof(FindItemViewModel)) is FindItemViewModel viewModel)) { return; } // Create a new button that finds the item. UICommand findItemCommand = new UICommand() { Caption = "Search", IsDefault = true, // Only allow the user to find an item if there are no errors in the data entry. Command = new DelegateCommand( () => { }, () => !string.IsNullOrWhiteSpace(viewModel.Title) && !string.IsNullOrWhiteSpace(viewModel.Platform) ) }; // Create a new button that cancels and closes the dialog window for finding an item. UICommand cancelCommand = new UICommand() { Caption = "Cancel", IsCancel = true, }; // Display the dialog window. UICommand result = FindItemDialogService?.ShowDialog( dialogCommands: new[] { findItemCommand, cancelCommand }, title: "Find an Item", viewModel: viewModel ); // Check if the user executed the find command (clicked the find button). if (result != findItemCommand) { return; } // Switch based on the tab the user is on. switch (itemType) { case InventoryItemType.Book: // Return the first item that has the requested title and platform of the inventory item. FocusedItemModel = BookModels.FirstOrDefault(x => string.Equals(x.Title, viewModel.Title, StringComparison.InvariantCultureIgnoreCase) && string.Equals(x.Platform, viewModel.Platform, StringComparison.InvariantCultureIgnoreCase)); break; case InventoryItemType.Movie: FocusedItemModel = MovieModels.FirstOrDefault(x => string.Equals(x.Title, viewModel.Title, StringComparison.InvariantCultureIgnoreCase) && string.Equals(x.Platform, viewModel.Platform, StringComparison.InvariantCultureIgnoreCase)); break; case InventoryItemType.VideoGame: FocusedItemModel = VideoGameModels.FirstOrDefault(x => string.Equals(x.Title, viewModel.Title, StringComparison.InvariantCultureIgnoreCase) && string.Equals(x.Platform, viewModel.Platform, StringComparison.InvariantCultureIgnoreCase)); break; // Throw an exception if our item type does not match any of the models. default: throw new ArgumentOutOfRangeException(); } // If a model was not found, display a new message box indicating so to the user. if (FocusedItemModel == null) { MessageBoxService?.ShowMessage($"Could not find \"{viewModel.Title}\" on \"{viewModel.Platform}\""); } }
// NOTE: The DevExpress POCO mechanism generates commands for all public methods without parameters or with a single parameter by convention. // Corresponding delegate command properties are created using the names of public methods with the suffix "Command". /// <summary> /// Creates a new dialog window that allows the user to add an item. /// </summary> /// <param name="itemType">The type of item is bound to the the tab the user is on (i.e adding from the movie tab creates a new movie).</param> public void AddItem(InventoryItemType itemType) { // Get the type of view model we are creating. Type viewModelType; switch (itemType) { case InventoryItemType.Book: viewModelType = typeof(BookModel); break; case InventoryItemType.Movie: viewModelType = typeof(MovieModel); break; case InventoryItemType.VideoGame: viewModelType = typeof(VideoGameModel); break; // Throw an exception if our item type does not match any of the models. default: throw new ArgumentOutOfRangeException(); } // Resolve (create) the view model. if (!(App.ServiceProvider.GetService(viewModelType) is BaseInventoryModel viewModel)) { return; } // Create a new button that adds the item. UICommand addItemCommand = new UICommand() { Caption = "Add", IsDefault = true, // Only allow the user to add the item if there are no errors with the data entry. Command = new DelegateCommand( () => { }, () => string.IsNullOrEmpty(viewModel.Error) ) }; // Create a new button that cancels and closes the dialog window for adding an item. UICommand cancelCommand = new UICommand() { Caption = "Cancel", IsCancel = true, }; // Display the dialog window. UICommand result = AddOrModifyItemDialogService?.ShowDialog( dialogCommands: new[] { addItemCommand, cancelCommand }, title: "Add a New Item", viewModel: viewModel ); // Check if the user executed the add command (clicked the add button). if (result != addItemCommand) { return; } // Add the new model based on the type of item we are adding. switch (itemType) { case InventoryItemType.Book: BookModels.Add(viewModel as BookModel); break; case InventoryItemType.Movie: MovieModels.Add(viewModel as MovieModel); break; case InventoryItemType.VideoGame: VideoGameModels.Add(viewModel as VideoGameModel); break; // Throw an exception if our item type does not match any of the models. default: throw new ArgumentOutOfRangeException(); } // Highlight the newly added item. FocusedItemModel = viewModel; }