Esempio n. 1
0
        public void UpdatePrice()
        {
            var price = RestaurantMenuItemModifierPage.UpdatePrice(MenuItemView.RestaurantMenuItem.MenuItemVM, ListRMenuGroupModifierVM, this.Quantity);

            if (price.HasValue)
            {
                mPrice = price.Value;
            }
            else
            {
                mPrice = 0;
            }
            OnPropertyChanged(nameof(Price));
            OnPropertyChanged(nameof(DisplayPrice));
        }
        public MyCartPage(RestaurantDetailPage restaurantDetailPage)
        {
            RestaurantDetailPage = restaurantDetailPage;

            InitializeComponent();

            NavigationPage.SetBackButtonTitle(this, AppResources.Back);

            var listMenuItemViews = RestaurantDetailPage.ListMenuItemViews.Values.SelectMany(t => t);

            listMenuGroupModifierItemSelected = listMenuItemViews
                                                .SelectMany(t => t.ListMenuGroupModifierItemSelected)
                                                .ToList();

            if (listMenuGroupModifierItemSelected.Count == 0)
            {
                ButtonCheckout.IsEnabled = false;
            }

            var cartItemsSource = new ObservableCollection <CartMenuGroupModifierItemViewModel>();
            var deleteAction    = new Action <CartMenuGroupModifierItemViewModel>((t) =>
            {
                cartItemsSource.Remove(t);
                listMenuGroupModifierItemSelected.Remove(t.Model);
                foreach (var listMenuItemView in listMenuItemViews)
                {
                    listMenuItemView.ListMenuGroupModifierItemSelected.Remove(t.Model);
                    listMenuItemView.IsSelected = listMenuItemView.ListMenuGroupModifierItemSelected.Count > 0;
                }
                UpdatePrice();

                if (listMenuGroupModifierItemSelected.Count == 0)
                {
                    ButtonCheckout.IsEnabled = false;
                }
            });
            var cartItems = listMenuGroupModifierItemSelected.Select(t => new CartMenuGroupModifierItemViewModel(t, deleteAction)).ToList();

            foreach (var cartItem in cartItems)
            {
                cartItemsSource.Add(cartItem);
            }
            ListViewCarts.ItemsSource = cartItemsSource;
            UpdatePrice();

            ListViewCarts.ItemTapped += (sender, e) =>
            {
                CartMenuGroupModifierItemViewModel cartMenuGroupModifierItem = e.Item as CartMenuGroupModifierItemViewModel;
                ListViewCarts.SelectedItem = null;
                RestaurantMenuItemModifierPage restaurantMenuItemModifierPage = new RestaurantMenuItemModifierPage(RestaurantDetailPage.RestaurantVM, cartMenuGroupModifierItem.Model.MenuItemView, cartMenuGroupModifierItem.Model);
                restaurantMenuItemModifierPage.Saved += (obj) =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        obj.RaiseAllOnChanged();
                        UpdatePrice();
                    });
                };
                Utils.PushAsync(Navigation, restaurantMenuItemModifierPage, true);
            };

            ButtonCheckout.Clicked += async(sender, e) =>
            {
                UserFacade userFacade = new UserFacade();
                try
                {
                    var isSucceed = false;
                    this.IsBusy = true;
                    if (restaurantDetailPage.GroupedDeliveryDestination != null)
                    {
                        Shared.LocalAddress = restaurantDetailPage.GroupedDeliveryDestination.Address;
                    }

                    var serviceAddress = new AddressFacade().GetUserAddress();
                    var myCartPage     = new PlaceOrderPage(String.Empty, RestaurantDetailPage, serviceAddress);
                    await Utils.PushAsync(Navigation, myCartPage, true);

                    this.IsBusy = false;
                }
                catch (Exception ex)
                {
                    this.IsBusy = false;
                    Device.BeginInvokeOnMainThread(async() =>
                    {
                        var message = string.IsNullOrEmpty(ex.Message) ? AppResources.SomethingWentWrong : ex.Message;
                        await Utils.ShowErrorMessage(message, 5);
                    });
                }
            };
        }