diff --git a/Library.eCommerce/Models/Item.cs b/Library.eCommerce/Models/Item.cs index cb3caf4..dbb9a21 100644 --- a/Library.eCommerce/Models/Item.cs +++ b/Library.eCommerce/Models/Item.cs @@ -16,6 +16,8 @@ public class Item public ProductDTO Product { get; set; } public int? Quantity { get; set; } + public int Price { get; set; } + public ICommand? AddCommand { get; set; } public override string ToString() @@ -26,7 +28,7 @@ public override string ToString() public string Display { get { - return $"{Product?.Display ?? string.Empty} {Quantity}"; + return $"{Product?.Display ?? string.Empty} x{Quantity} ${Price}"; } } @@ -48,6 +50,7 @@ public Item(Item i) Product = new ProductDTO(i.Product); Quantity = i.Quantity; Id = i.Id; + Price = i.Price; AddCommand = new Command(DoAdd); } diff --git a/Library.eCommerce/Services/ProductServiceProxy.cs b/Library.eCommerce/Services/ProductServiceProxy.cs index a2594e3..1c1b0eb 100644 --- a/Library.eCommerce/Services/ProductServiceProxy.cs +++ b/Library.eCommerce/Services/ProductServiceProxy.cs @@ -15,9 +15,9 @@ private ProductServiceProxy() { Products = new List { - new Item{ Product = new ProductDTO{Id = 1, Name ="Product 1"}, Id = 1, Quantity = 1 }, - new Item{ Product = new ProductDTO{Id = 2, Name ="Product 2"}, Id = 2 , Quantity = 2 }, - new Item{ Product = new ProductDTO{Id = 3, Name ="Product 3"}, Id=3 , Quantity = 3 } + new Item{ Product = new ProductDTO{Id = 1, Name ="Product 1"}, Id = 1, Quantity = 1, Price = 5 }, + new Item{ Product = new ProductDTO{Id = 2, Name ="Product 2"}, Id = 2 , Quantity = 2,Price = 50 }, + new Item{ Product = new ProductDTO{Id = 3, Name ="Product 3"}, Id=3 , Quantity = 3, Price = 500 } }; } diff --git a/Library.eCommerce/Services/ShoppingCartService.cs b/Library.eCommerce/Services/ShoppingCartService.cs index c3a6f93..474f64d 100644 --- a/Library.eCommerce/Services/ShoppingCartService.cs +++ b/Library.eCommerce/Services/ShoppingCartService.cs @@ -6,6 +6,7 @@ public class ShoppingCartService { private ProductServiceProxy _prodSvc = ProductServiceProxy.Current; private List items; + public double CheckoutPrice; public List CartItems { get @@ -29,6 +30,9 @@ private ShoppingCartService() { items = new List(); } + public void ClearList() { items = new List(); } + //Sets the list to an empty, new, list + public Item? AddOrUpdate(Item item) { var existingInvItem = _prodSvc.GetById(item.Id); diff --git a/Maui.eCommerce/AppShell.xaml b/Maui.eCommerce/AppShell.xaml index 71666d7..38fae71 100644 --- a/Maui.eCommerce/AppShell.xaml +++ b/Maui.eCommerce/AppShell.xaml @@ -24,4 +24,8 @@ Title="Shop" ContentTemplate="{DataTemplate views:ShoppingManagementView}" Route="ShoppingManagement" /> + diff --git a/Maui.eCommerce/Maui.eCommerce.csproj b/Maui.eCommerce/Maui.eCommerce.csproj index a3d3c3d..d8b082c 100644 --- a/Maui.eCommerce/Maui.eCommerce.csproj +++ b/Maui.eCommerce/Maui.eCommerce.csproj @@ -57,8 +57,8 @@ - - + + @@ -67,6 +67,9 @@ + + MSBuild:Compile + MSBuild:Compile diff --git a/Maui.eCommerce/ViewModels/ProductViewModel.cs b/Maui.eCommerce/ViewModels/ProductViewModel.cs index c08fa64..60a6bb5 100644 --- a/Maui.eCommerce/ViewModels/ProductViewModel.cs +++ b/Maui.eCommerce/ViewModels/ProductViewModel.cs @@ -43,6 +43,20 @@ public int? Quantity } } + public int? Price + { + get + { + return Model?.Price; + } + set + { + if (Model != null && Model.Price != value) + { + Model.Price = value ?? 0; + } + } + } public Item? Model { get; set; } public void AddOrUpdate() diff --git a/Maui.eCommerce/ViewModels/ShoppingManagementViewModel.cs b/Maui.eCommerce/ViewModels/ShoppingManagementViewModel.cs index 0d39f8e..3ab37e3 100644 --- a/Maui.eCommerce/ViewModels/ShoppingManagementViewModel.cs +++ b/Maui.eCommerce/ViewModels/ShoppingManagementViewModel.cs @@ -13,8 +13,8 @@ namespace Maui.eCommerce.ViewModels { public class ShoppingManagementViewModel : INotifyPropertyChanged { - private ProductServiceProxy _invSvc = ProductServiceProxy.Current; - private ShoppingCartService _cartSvc = ShoppingCartService.Current; + private ProductServiceProxy _invSvc = ProductServiceProxy.Current; + private ShoppingCartService _cartSvc = ShoppingCartService.Current; public Item? SelectedItem { get; set; } public Item? SelectedCartItem { get; set; } @@ -84,5 +84,27 @@ public void ReturnItem() } } } + //Do stuff on list, then the next page will be a readout + public void Checkout() + { + /* int totalCost = 0; + foreach (var item in ShoppingCart) + { + totalCost += item?.Price ?? 0; + } + // _cartSvc.ClearList(); + + if (totalCost > 0) + { + NotifyPropertyChanged(nameof(ShoppingCart)); + } + _cartSvc.CheckoutPrice = totalCost; + NotifyPropertyChanged(nameof(_cartSvc.CheckoutPrice)); + */ + // _cartSvc.ClearList(); + NotifyPropertyChanged(nameof(ShoppingCart)); + + } + } } diff --git a/Maui.eCommerce/ViewModels/TotalCostViewModel.cs b/Maui.eCommerce/ViewModels/TotalCostViewModel.cs new file mode 100644 index 0000000..89f381f --- /dev/null +++ b/Maui.eCommerce/ViewModels/TotalCostViewModel.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; +using Library.eCommerce.Models; +using Library.eCommerce.Services; + +namespace Maui.eCommerce.ViewModels +{ + public class TotalCostViewModel : INotifyPropertyChanged + { + private ShoppingCartService __svc = ShoppingCartService.Current; + + public event PropertyChangedEventHandler? PropertyChanged; + + + public double CheckoutCost + { + get { + var rounded = Math.Round(__svc.CheckoutPrice + (__svc.CheckoutPrice * .07), 2); + return rounded; + } + set + { + __svc.CheckoutPrice = value; + + } + } + + public ObservableCollection ShoppingCart + { + get + { + int totalCost = 0; + foreach (var item in __svc.CartItems) + { + totalCost += item?.Price ?? 0; + } + CheckoutCost = totalCost; + NotifyPropertyChanged(nameof(CheckoutCost)); + + var toRet = new ObservableCollection(__svc.CartItems + .Where(i => i?.Quantity > 0) + ); + // __svc.CartItems.Clear(); + + + + return toRet; + } + } + + public void NotifyPropertyChanged([CallerMemberName] string propertyName = "") + { + if (propertyName is null) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + + public void RefreshUI() + { + NotifyPropertyChanged(nameof(CheckoutCost)); + NotifyPropertyChanged(nameof(ShoppingCart)); + } + public void ClearOnLeave() + { + __svc.CartItems.Clear(); + CheckoutCost = 0; + } + } +} diff --git a/Maui.eCommerce/Views/CheckoutView.xaml b/Maui.eCommerce/Views/CheckoutView.xaml new file mode 100644 index 0000000..f8e62b1 --- /dev/null +++ b/Maui.eCommerce/Views/CheckoutView.xaml @@ -0,0 +1,43 @@ + + + +