Exemple #1
0
        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.PriceError,
                                                                Languages.Accept);

                return;
            }

            var price = decimal.Parse(this.Price);

            if (price < 0)
            {
                await Application.Current.MainPage.DisplayAlert(Languages.Error,
                                                                Languages.PriceError,
                                                                Languages.Accept);

                return;
            }

            if (this.Category == null)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.CategoryError,
                    Languages.Accept);

                return;
            }



            this.IsRunning = true;

            this.IsEnabled = false;



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



            if (!connection.IsSuccess)
            {
                this.IsRunning = true;

                this.IsEnabled = true;

                await Application.Current.MainPage.DisplayAlert(Languages.Error, connection.Message, Languages.Accept);

                return;
            }

            byte[] imageArray = null;

            if (this.file != null)
            {
                imageArray = FileHelper.ReadFully(this.file.GetStream());
            }



            var location = await this.GetLocation();

            var product = new Product
            {
                description = this.Description,
                price       = price,
                remark      = this.Remark,
                imageArray  = imageArray,
                category    = this.Category,
                user        = MainViewModel.GetInstance().UserRequest,
                latitude    = location == null? 0 : location.Latitude,
                longitude   = location == null? 0 : location.Longitude
            };

            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, Setting.AccessToken);

            if (!response.IsSuccess)
            {
                this.IsRunning = true;

                this.IsEnabled = true;

                await Application.Current.MainPage.DisplayAlert(Languages.Error, response.Message, Languages.Accept);

                return;
            }

            var productResult = (Product)response.Result;

            var viewModel = ProductViewModel.GetInstance();

            viewModel.MyProducts.Add(productResult);

            viewModel.RefreshList();

            this.IsRunning = true;

            this.IsEnabled = true;

            await App.Navigator.PopAsync();
        }