예제 #1
0
        private async void DeleteExercise()
        {
            var answer = await Application.Current.MainPage.DisplayAlert(
                Languages.Confirm,
                Languages.DeleteConfirmation,
                Languages.Yes,
                Languages.No);

            if (!answer)
            {
                return;
            }
            //eliminamos el producto
            //comprobamos si hay conexión
            var connection = await this.apiService.CheckConnection();

            //si la conexion a internet no ha sido exitosa
            if (!connection.IsSuccess)
            {
                await Application.Current.MainPage.DisplayAlert(Languages.Error, connection.Message, Languages.Accept);

                return;
            }

            //vamos a la api
            var url = Application.Current.Resources["UrlAPI"].ToString();

            var prefix = Application.Current.Resources["UrlPrefix"].ToString();

            var controller = Application.Current.Resources["UrlExercisesController"].ToString();

            var response = await this.apiService.Delete(url, prefix, controller, this.ExerciseId, Settings.TokenType, Settings.AccessToken);

            if (!response.IsSuccess)
            {
                await Application.Current.MainPage.DisplayAlert(Languages.Error, connection.Message, Languages.Accept);

                return;
            }

            //hay que actualizar la lista, llamamos a  SINGLETON
            var exercisesViewModel = ExercisesViewModel.GetInstance();

            //buscamos el producto en la lista y lo eliminamos
            var deletedExercise = exercisesViewModel.MyExercises.Where(p => p.ExerciseId == this.ExerciseId).FirstOrDefault();

            //si encontramos el producto
            if (deletedExercise != null)
            {
                exercisesViewModel.MyExercises.Remove(deletedExercise);
            }
            exercisesViewModel.RefreshList();
        }
예제 #2
0
        private async void Save()
        {
            if (string.IsNullOrEmpty(this.Name))
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.NameError,
                    Languages.Accept);

                return;
            }

            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;

            //chekea la conexion
            var connection = await this.apiService.CheckConnection();

            //si la conexion a internet no ha sido exitosa
            if (!connection.IsSuccess)
            {
                this.IsRunning = false;
                this.IsEnabled = true;
                await Application.Current.MainPage.DisplayAlert(Languages.Error, connection.Message, Languages.Accept);

                return;
            }

            //para saber si se cogió o no foto

            byte[] imageArray = null;
            if (this.file != null)
            {
                imageArray = FilesHelper.ReadFully(this.file.GetStream());
            }

            // var location = await this.GetLocation();

            //metemos lo que mandemos al post como un producto

            var exercise = new Exercise
            {
                Name        = this.Name,
                Description = this.Description,
                //Price = price,
                //Remarks = this.Remarks,
                ImageArray = imageArray,
                CategoryId = this.Category.CategoryId,
                UserId     = MainViewModel.GetInstance().UserASP.Id,
                //    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["UrlExercisesController"].ToString();

            //invocamos el metodo post del apiservice
            var response = await this.apiService.Post(url, prefix, controller, exercise, Settings.TokenType, Settings.AccessToken);

            //preguntamos si lo grabó de manera exitosa
            if (!response.IsSuccess)
            {
                this.IsRunning = false;
                this.IsEnabled = true;
                await Application.Current.MainPage.DisplayAlert(Languages.Error, response.Message, Languages.Accept);

                return;
            }

            var newExercise = (Exercise)response.Result;

            //adicionamos el producto a la colección
            var exercisesViewModel = ExercisesViewModel.GetInstance();

            exercisesViewModel.MyExercises.Add(newExercise);
            exercisesViewModel.RefreshList();
            // la ordenamos
            //viewModel.Exercises = viewModel.Exercises.OrderBy(p => p.Description).ToList();


            //si lo hizo de manera exitosa hacemos el back
            this.IsRunning = false;
            this.IsEnabled = true;
            //Desapilamos
            await App.Navigator.PopAsync();

            // await App.Navigator.PopAsync();
        }
예제 #3
0
        private async void Save()
        {
            if (string.IsNullOrEmpty(this.Exercise.Name))
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.NameError,
                    Languages.Accept);

                return;
            }

            if (string.IsNullOrEmpty(this.Exercise.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;

            //chekea la conexion
            var connection = await this.apiService.CheckConnection();

            //si la conexion a internet no ha sido exitosa
            if (!connection.IsSuccess)
            {
                this.IsRunning = false;
                this.IsEnabled = true;
                await Application.Current.MainPage.DisplayAlert(Languages.Error, connection.Message, Languages.Accept);

                return;
            }

            //para saber si se cogió o no foto

            byte[] imageArray = null;
            if (this.file != null)
            {
                imageArray = FilesHelper.ReadFully(this.file.GetStream());
                this.Exercise.ImageArray = imageArray;
            }

            // var location = await this.GetLocation();

            //por si el usuario cambia la categoría
            this.Exercise.CategoryId = this.Category.CategoryId;

            var url = Application.Current.Resources["UrlAPI"].ToString();

            var prefix = Application.Current.Resources["UrlPrefix"].ToString();

            var controller = Application.Current.Resources["UrlExercisesController"].ToString();

            //invocamos el metodo put del apiservice
            var response = await this.apiService.Put(url, prefix, controller, this.Exercise, this.Exercise.ExerciseId, Settings.TokenType, Settings.AccessToken);

            //preguntamos si lo grabó de manera exitosa
            if (!response.IsSuccess)
            {
                this.IsRunning = false;
                this.IsEnabled = true;
                await Application.Current.MainPage.DisplayAlert(Languages.Error, response.Message, Languages.Accept);

                return;
            }

            var newExercise = (Exercise)response.Result;

            //adicionamos el producto a la colección
            var exercisesViewModel = ExercisesViewModel.GetInstance();

            //buscamos ejercicio lo eliminamos y lo añadimos
            var oldExercise = exercisesViewModel.MyExercises.Where(p => p.ExerciseId == this.Exercise.ExerciseId).FirstOrDefault();

            if (oldExercise != null)
            {
                exercisesViewModel.MyExercises.Remove(oldExercise);
            }

            exercisesViewModel.MyExercises.Add(newExercise);
            exercisesViewModel.RefreshList();
            // la ordenamos
            //viewModel.Exercises = viewModel.Exercises.OrderBy(p => p.Description).ToList();


            //si lo hizo de manera exitosa hacemos el back
            this.IsRunning = false;
            this.IsEnabled = true;
            //Desapilamos
            await App.Navigator.PopAsync();

            // await App.Navigator.PopAsync();
        }