private async void DeleteProduct() { var answer = await Application.Current.MainPage.DisplayAlert(Languages.Confirm, Languages.DeleteConfirmation, Languages.Yes, Languages.No); if (!answer) { return; } this.IsRunning = true; this.IsEnabled = false; var connection = await this.apiService.CheckConnection(); if (!connection.IsSuccess) { await Application.Current.MainPage.DisplayAlert(Languages.Error, connection.Message, Languages.Accept); return; } var url = Application.Current.Resources["UrlAPI"].ToString(); var prefix = Application.Current.Resources["UrlPrefix"].ToString(); var controller = Application.Current.Resources["UrlProductsController"].ToString(); var response = await this.apiService.Delete(url, prefix, controller, this.Product.IdProduct); if (!response.IsSuccess) { this.IsRunning = false; this.IsEnabled = true; await Application.Current.MainPage.DisplayAlert(Languages.Error, response.Message, Languages.Accept); return; } var productsViewModel = ProductsViewModel.GetInstance(); var deletedProduct = productsViewModel.MyProducts.Where(c => c.IdProduct == this.Product.IdProduct).FirstOrDefault(); if (deletedProduct != null) { productsViewModel.MyProducts.Remove(deletedProduct); } productsViewModel.RefreshList(); this.IsRunning = false; this.IsEnabled = true; //await App.Navigator.PopAsync(); await Application.Current.MainPage.Navigation.PopAsync(); }
private async void SaveProduct() { if (string.IsNullOrEmpty(this.Product.Description)) { await Application.Current.MainPage.DisplayAlert( Languages.Error, Languages.DescriptionError, Languages.Accept); return; } if (Product.Price <= 0) { await Application.Current.MainPage.DisplayAlert( Languages.Error, Languages.PriceError, Languages.Accept); return; } this.IsRunning = true; this.IsEnabled = false; var connection = await this.apiService.CheckConnection(); if (!connection.IsSuccess) { this.IsRunning = false; this.IsEnabled = true; await Application.Current.MainPage.DisplayAlert( Languages.Error, connection.Message, Languages.Accept); return; } byte[] imageArray = null; if (this.file != null) { imageArray = FilesHelper.ReadFully(this.file.GetStream()); this.Product.ImageArray = imageArray; } var url = Application.Current.Resources["UrlAPI"].ToString(); var prefix = Application.Current.Resources["UrlPrefix"].ToString(); var controller = Application.Current.Resources["UrlProductsController"].ToString(); var response = await this.apiService.Put(url, prefix, controller, this.Product, this.Product.IdProduct); if (!response.IsSuccess) { this.IsRunning = false; this.IsEnabled = true; await Application.Current.MainPage.DisplayAlert( Languages.Error, response.Message, Languages.Accept); return; } var newProduct = (Product)response.Result; var productsViewModel = ProductsViewModel.GetInstance(); var oldCensus = productsViewModel.MyProducts.Where(c => c.IdProduct == this.Product.IdProduct).FirstOrDefault(); if (oldCensus != null) { productsViewModel.MyProducts.Remove(oldCensus); } productsViewModel.MyProducts.Add(newProduct); productsViewModel.RefreshList(); this.IsRunning = false; this.IsEnabled = true; await Application.Current.MainPage.Navigation.PopAsync(); }
private async void Save() { if (string.IsNullOrEmpty(this.Description)) { await Application.Current.MainPage.DisplayAlert( Languages.Error, Languages.DescriptionError, Languages.Accept); return; } if (string.IsNullOrEmpty(this.Price)) { await Application.Current.MainPage.DisplayAlert( Languages.Error, Languages.Price, Languages.Accept); return; } var price = int.Parse(this.Price); if (price <= 0) { await Application.Current.MainPage.DisplayAlert( Languages.Error, Languages.PriceError, Languages.Accept); return; } this.IsRunning = true; this.IsEnabled = false; var connection = await this.apiService.CheckConnection(); if (!connection.IsSuccess) { this.IsRunning = false; this.IsEnabled = true; await Application.Current.MainPage.DisplayAlert( Languages.Error, connection.Message, Languages.Accept); return; } byte[] imageArray = null; if (this.file != null) { imageArray = FilesHelper.ReadFully(this.file.GetStream()); } var product = new Product { Description = this.Description, Price = price, Remarks = this.Remarks, ImageArray = imageArray, }; var url = Application.Current.Resources["UrlAPI"].ToString(); var prefix = Application.Current.Resources["UrlPrefix"].ToString(); var controller = Application.Current.Resources["UrlProductsController"].ToString(); var response = await this.apiService.Post(url, prefix, controller, product, Settings.TokenType, Settings.AccessToken); if (!response.IsSuccess) { this.IsRunning = false; this.IsEnabled = true; await Application.Current.MainPage.DisplayAlert( Languages.Error, response.Message, Languages.Accept); return; } var newProduct = (Product)response.Result; var productsViewModel = ProductsViewModel.GetInstance(); productsViewModel.MyProducts.Add(newProduct); productsViewModel.RefreshList(); this.IsRunning = false; this.IsEnabled = true; await Application.Current.MainPage.Navigation.PopAsync(); /* * await Application.Current.MainPage.DisplayAlert("e", "Agregamos el pdto con id: " + newProduct.IdProduct, "ok"); * * MainViewModel.GetInstance().Res = new ResViewModel(newProduct); * await Application.Current.MainPage.Navigation.PushAsync(new Views.ResPage()); */ }