Ejemplo n.º 1
0
        private async void DeleteProduct()
        {
            var anwser = await Application.Current.MainPage.DisplayAlert(Languages.ConfirmLbl, Languages.DeleteConfirmLbl, Languages.YesLbl, Languages.NoLbl);

            if (!anwser)
            {
                return;
            }

            var connection = await this._apiServices.CheckConnection();

            if (!connection.IsSuccess)
            {
                await Application.Current.MainPage.DisplayAlert(Languages.ErrorLbl, connection.Message, Languages.AcceptBtn);

                return;
            }

            var url        = Application.Current.Resources["UrlAPI"].ToString();
            var api        = Application.Current.Resources["Api"].ToString();
            var controller = Application.Current.Resources["ControllerProducts"].ToString();
            var response   = await this._apiServices.Delete(url, api, controller, this.ProductId);

            if (!response.IsSuccess)
            {
                await Application.Current.MainPage.DisplayAlert(Languages.ErrorLbl, response.Message, Languages.AcceptBtn);

                return;
            }

            var productsViewModel = ProductsViewModel.GetInstance();
            var deleteProduct     = productsViewModel.Products.Where(p => p.ProductId == this.ProductId).FirstOrDefault();

            if (deleteProduct != null)
            {
                productsViewModel.Products.Remove(deleteProduct);
            }

            await Application.Current.MainPage.DisplayAlert(Languages.SaveLbl, Languages.DescriptionSaveLbl, Languages.AcceptBtn);
        }
        private async void Save()
        {
            if (string.IsNullOrEmpty(this.Product.Description))
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.ErrorLbl,
                    Languages.DescriptionErrorLbl,
                    Languages.AcceptBtn);

                return;
            }

            if (this.Product.Price < 0)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.ErrorLbl,
                    Languages.PriceErrorLbl,
                    Languages.AcceptBtn);

                return;
            }
            //UserDialogs.Instance.ShowLoading(Languages.LoadingLbl, MaskType.Black);

            this.IsRunning = true;
            this.IsEnabled = false;

            var connection = await this._apiServices.CheckConnection();

            if (!connection.IsSuccess)
            {
                //UserDialogs.Instance.HideLoading();
                this.IsRunning = false;
                this.IsEnabled = true;
                await Application.Current.MainPage.DisplayAlert(Languages.ErrorLbl, connection.Message, Languages.AcceptBtn);

                return;
            }

            byte[] imageArray = null;
            if (this._file != null)
            {
                imageArray = FileHelper.ReadFully(this._file.GetStream());
                this.Product.ImageArray = imageArray;
            }

            var url        = Application.Current.Resources["UrlAPI"].ToString();
            var api        = Application.Current.Resources["Api"].ToString();
            var controller = Application.Current.Resources["ControllerProducts"].ToString();
            var response   = await this._apiServices.Put(url, api, controller, this.Product, this.Product.ProductId);

            if (!response.IsSuccess)
            {
                //UserDialogs.Instance.HideLoading();
                this.IsRunning = false;
                this.IsEnabled = true;
                await Application.Current.MainPage.DisplayAlert(Languages.ErrorLbl, response.Message, Languages.AcceptBtn);

                return;
            }
            //UserDialogs.Instance.HideLoading();


            var newProduct = (Product)response.Result;

            //if (newProduct.ValidationSet.Length > 0 || )
            //{
            //    await Application.Current.MainPage.DisplayAlert(Languages.ErrorLbl, newProduct.ValidationSet, Languages.AcceptBtn);
            //}

            var productsViewModel = ProductsViewModel.GetInstance();

            var oldProduct = productsViewModel.MyProducts.Where(p => p.ProductId == this.Product.ProductId).FirstOrDefault();

            if (oldProduct != null)
            {
                productsViewModel.MyProducts.Remove(oldProduct);
            }

            productsViewModel.MyProducts.Add(newProduct);
            productsViewModel.RefreshList();

            this.IsRunning = false;
            this.IsEnabled = true;

            await Application.Current.MainPage.DisplayAlert(Languages.SaveLbl, Languages.DescriptionSaveLbl, Languages.AcceptBtn);

            await Application.Current.MainPage.Navigation.PopAsync();
        }