Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using Catsier.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Text;
  6. using System.Windows;
  7. using System.Windows.Input;
  8.  
  9. namespace Catsier.ViewModels {
  10. class ListProductViewModel : ViewModelBase {
  11.  
  12. public ObservableCollection<ListProductItemViewModel> productList;
  13.  
  14. public IEnumerable<ListProductItemViewModel> ProductList {
  15. get {
  16. return productList;
  17. }
  18. }
  19.  
  20. public ICommand BackCommand {
  21. get {
  22. return new RelayCommand(x => Mediator.Invoke("Change View To Dashboard", null));
  23. }
  24. }
  25.  
  26. private ProductRepository repo;
  27.  
  28. public ListProductViewModel(ProductRepository repo) {
  29. this.repo = repo;
  30. repo.OnProductListChange += UpdateList;
  31. productList = new ObservableCollection<ListProductItemViewModel>();
  32. UpdateList();
  33. }
  34.  
  35. private void UpdateList() {
  36. productList.Clear();
  37. Product[] products = repo.GetProducts();
  38. foreach (Product product in products) {
  39. productList.Add(new ListProductItemViewModel(product));
  40. }
  41. UpdateNumbering();
  42. }
  43.  
  44. private ICommand editCommand;
  45. private ICommand deleteCommand;
  46. private ICommand createCommand;
  47.  
  48. public ICommand EditCommand {
  49. get {
  50. return editCommand ?? (editCommand = new RelayCommand<ListProductItemViewModel>(x => Edit(x)));
  51. }
  52. }
  53.  
  54. public ICommand DeleteCommand {
  55. get {
  56. return deleteCommand ?? (deleteCommand = new RelayCommand<ListProductItemViewModel>(x => Delete(x)));
  57. }
  58. }
  59.  
  60. public ICommand CreateCommand {
  61. get {
  62. return createCommand ?? (createCommand = new RelayCommand(x => GoToCreateProduct()));
  63. }
  64. }
  65.  
  66. private void Edit(ListProductItemViewModel viewModel) {
  67. Mediator.Invoke("Change View To Edit Product", viewModel.Product);
  68. }
  69.  
  70. private void Delete(ListProductItemViewModel viewModel) {
  71. var res = MessageBox.Show("Apakah kamu yakin untuk menghapus produk ini ?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
  72. if(res == MessageBoxResult.No) {
  73. return;
  74. }
  75. repo.RemoveProduct(viewModel.Kode);
  76. }
  77.  
  78. private void UpdateNumbering() {
  79. for(int i = 0; i < productList.Count; i++) {
  80. productList[i].No = i + 1;
  81. }
  82. }
  83.  
  84. private void GoToCreateProduct() {
  85. Mediator.Invoke("Change View To Create Product", null);
  86. }
  87. }