Advertisement
Guest User

OrderViewModel.cs

a guest
Jul 12th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Collections.Specialized;
  5. using System.ComponentModel;
  6. using System.Data.Entity;
  7. using System.Linq;
  8. using System.Windows.Controls;
  9. using DbContext;
  10. using GalaSoft.MvvmLight.Command;
  11. using GalaSoft.MvvmLight.Messaging;
  12. using SalesApp.Logic.Ui.Model;
  13.  
  14. namespace SalesApp.Logic.Ui.ViewModel
  15. {
  16.   public class OrderViewModel : ViewModelBase
  17.   {
  18.     private decimal _totalPrice;
  19.     public decimal TotalPrice
  20.     {
  21.       get => this._totalPrice;
  22.       set
  23.       {
  24.         if (this._totalPrice == value)
  25.           return;
  26.         this._totalPrice = value;
  27.  
  28.         RaisePropertyChanged();
  29.       }
  30.     }
  31.  
  32.     private ObservableCollection<Product> _products;
  33.     public ObservableCollection<Product> Products
  34.     {
  35.       get => this._products;
  36.       set
  37.       {
  38.         if (this.Products != null)
  39.         {
  40.           this.Products.CollectionChanged -= OnCollectionChanged;
  41.           UnsubscribeFromItemPropertyChanged(this.Products);
  42.         }
  43.  
  44.         if (this.Products == value)
  45.           return;
  46.  
  47.         this._products = value;
  48.  
  49.         this.Products.CollectionChanged += OnCollectionChanged;
  50.         if (this.Products.Any())
  51.         {
  52.           SubscribeToItemPropertyChanged(this.Products);
  53.         }
  54.  
  55.         RaisePropertyChanged();
  56.       }
  57.     }
  58.  
  59.     private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  60.     {
  61.       if (e.Action.Equals(NotifyCollectionChangedAction.Reset))
  62.       {
  63.         UnsubscribeFromItemPropertyChanged(sender as IEnumerable);
  64.         return;
  65.       }
  66.  
  67.       if (!e.Action.Equals(NotifyCollectionChangedAction.Move))
  68.       {
  69.         UnsubscribeFromItemPropertyChanged(e.OldItems);
  70.         SubscribeToItemPropertyChanged(e.NewItems);
  71.       }
  72.  
  73.       CalculateTotalPrice();
  74.     }
  75.  
  76.     private void ProductChanged(object sender, PropertyChangedEventArgs e) => CalculateTotalPrice();
  77.  
  78.     private void SubscribeToItemPropertyChanged(IEnumerable newItems)
  79.     {
  80.       if (newItems != null)
  81.       {
  82.         foreach (INotifyPropertyChanged item in newItems.OfType<INotifyPropertyChanged>())
  83.           item.PropertyChanged += ProductChanged;
  84.       }
  85.     }
  86.  
  87.     private void UnsubscribeFromItemPropertyChanged(IEnumerable oldItems)
  88.     {
  89.       if (oldItems != null)
  90.       {
  91.         foreach (INotifyPropertyChanged item in oldItems.OfType<INotifyPropertyChanged>())
  92.           item.PropertyChanged -= ProductChanged;
  93.       }
  94.     }
  95.  
  96.     private void CalculateTotalPrice() => this.TotalPrice = this.Products.Sum(item => item.total_price);
  97.  
  98.     private void GetProducts()
  99.     {
  100.       using (var context = new mainEntities())
  101.       {
  102.         var result = context.product.Include(c => c.brand);
  103.         this.Products = new ObservableCollection<Product>(
  104.           result.Select(item => new Product(item.name, item.mass, item.ean, item.brand.name, item.price)));
  105.       }
  106.     }
  107.  
  108.     public OrderViewModel()
  109.     {
  110.       SetView("Dodaj zamówienie");
  111.       GetProducts();
  112.     }
  113.   }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement