Exemple #1
0
        private async void DeleteVehicle()
        {
            var answer = await Application.Current.MainPage.DisplayAlert
                             ("Confirmación",
                             "Desea borrar el registro",
                             "Si",
                             "No");

            if (!answer)
            {
                return;
            }

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

            if (!connection.IsSuccess)
            {
                await Application.Current.MainPage.DisplayAlert("Error",
                                                                connection.Message
                                                                , "Acceptar");

                return;
            }

            var url         = Application.Current.Resources["UrlAPI"].ToString();
            var prefix      = Application.Current.Resources["UrlPrefix"].ToString();
            var vController = Application.Current.Resources["UrlVehiclesController"].ToString();
            var response    = await this.apiService.Delete(url, prefix, vController, this.Id);

            if (!response.IsSuccess)
            {
                await Application.Current.MainPage.DisplayAlert("Error", response.Message, "Aceptar");

                return;
            }

            var vehiclesViewModel = VehiclesViewModel.GetInstance();
            var deleteVehicle     = vehiclesViewModel.Vehicles.Where(v => v.Id == this.Id).FirstOrDefault();

            if (deleteVehicle != null)
            {
                vehiclesViewModel.Vehicles.Remove(deleteVehicle);
            }
        }
        private async void Save()
        {
            if (string.IsNullOrEmpty(this.Vehicle.Marca))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar la marca del Vehículo", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.Vehicle.Tipo))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar el Tipo del Vehículo", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.Vehicle.Color))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar el Color del Vehículo", "Aceptar");

                return;
            }
            if (string.IsNullOrWhiteSpace(this.Vehicle.Modelo.ToString()) || this.Vehicle.Modelo == 0 || this.Vehicle.Modelo.ToString().Length < 4)
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar el Modelo correcto del Vehículo", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.Vehicle.NoPlacas))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe el No. de placas del Vehículo", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.Vehicle.NoSerie))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar el No. de serie del Vehículo", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.Vehicle.Resguardante))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar el Nombre Completo del resguardante", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.Vehicle.Cargo))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar el cargo o puesto del Resguardante", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.Vehicle.Adscripcion))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar la Adscripción del resguardante", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.Vehicle.NoAvPrevia))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar No Averiguación Previa", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.Vehicle.NoExpediente))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar No Expendiente DBA", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.Vehicle.Origen))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar el Origen", "Aceptar");

                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("Error", connection.Message, "Aceptar");

                return;
            }

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

            var url         = Application.Current.Resources["UrlAPI"].ToString();
            var prefix      = Application.Current.Resources["UrlPrefix"].ToString();
            var vController = Application.Current.Resources["UrlVehiclesController"].ToString();
            var response    = await this.apiService.Put(url, prefix, vController, this.Vehicle, this.Vehicle.Id);

            if (!response.IsSuccess)
            {
                this.IsRunning = false;
                this.IsEnabled = true;
                await Application.Current.MainPage.DisplayAlert("Error", response.Message, "Aceptar");

                return;
            }

            var newVehicle        = (Vehicle)response.Result;
            var vehiclesViewModel = VehiclesViewModel.GetInstance();

            var oldVehicle = vehiclesViewModel.MyVehicles.Where(v => v.Id == this.Vehicle.Id).FirstOrDefault();

            if (oldVehicle != null)
            {
                vehiclesViewModel.MyVehicles.Remove(oldVehicle);
            }

            vehiclesViewModel.MyVehicles.Add(newVehicle);
            vehiclesViewModel.RefreshList();

            this.IsRunning = false;
            this.IsEnabled = true;
            await Application.Current.MainPage.Navigation.PopAsync();
        }
        private async void Save()
        {
            if (string.IsNullOrEmpty(this.Marca))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar la marca del Vehículo", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.Tipo))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar el Tipo del Vehículo", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.Color))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar el Color del Vehículo", "Aceptar");

                return;
            }
            if (string.IsNullOrWhiteSpace(this.Modelo.ToString()) || this.Modelo == 0 || this.Modelo.ToString().Length < 4)
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar el Modelo correcto del Vehículo", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.NoPlacas))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe el No. de placas del Vehículo", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.NoSerie))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar el No. de serie del Vehículo", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.NombreResguardante))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar el Nombre Completo del resguardante", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.CargoDelResguardante))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar el cargo o puesto del Resguardante", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.AdscripcionResguardante))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar la Adscripción del resguardante", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.NoAvePrevia))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar No Averiguación Previa", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.NoExpdienteDBA))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar No Expendiente DBA", "Aceptar");

                return;
            }
            if (string.IsNullOrEmpty(this.Origen))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Debe ingresar el Origen", "Aceptar");

                return;
            }

            if (FechaTermina <= FechaInicio)
            {
                await Application.Current.MainPage.DisplayAlert("Error", "La Fecha Final no puede ser Menor o Igual a la Fecha de Inicio", "Aceptar");

                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("Error", connection.Message, "Aceptar");

                return;
            }

            byte[] imageArray = null;
            if (this.file != null)
            {
                imageArray = FilesHelpers.ReadFully(this.file.GetStream());
            }
            var vehicle = new Vehicle
            {
                //id = "0000000258", //(apiService.tr + 1).ToString("0000000000"),
                Marca        = this.Marca,
                Tipo         = this.Tipo,
                Color        = this.Color,
                Modelo       = this.Modelo,
                NoPlacas     = this.NoPlacas,
                NoSerie      = this.NoSerie,
                Resguardante = this.NombreResguardante,
                Cargo        = this.CargoDelResguardante,
                Adscripcion  = this.AdscripcionResguardante,
                NoAvPrevia   = this.NoAvePrevia,
                NoExpediente = this.NoExpdienteDBA,
                Origen       = this.Origen,
                FechaInicio  = this.FechaInicio,
                FechaFinal   = this.FechaTermina,
                ImageArray   = imageArray,
            };

            var url         = Application.Current.Resources["UrlAPI"].ToString();
            var prefix      = Application.Current.Resources["UrlPrefix"].ToString();
            var vController = Application.Current.Resources["UrlVehiclesController"].ToString();
            var response    = await this.apiService.Post(url, prefix, vController, vehicle);

            if (!response.IsSuccess)
            {
                this.IsRunning = false;
                this.IsEnabled = true;
                await Application.Current.MainPage.DisplayAlert("Error", response.Message, "Aceptar");

                return;
            }

            var newVehicle        = (Vehicle)response.Result;
            var vehiclesViewModel = VehiclesViewModel.GetInstance();

            vehiclesViewModel.MyVehicles.Add(newVehicle);
            vehiclesViewModel.RefreshList();

            this.IsRunning = false;
            this.IsEnabled = true;
            await Application.Current.MainPage.Navigation.PopAsync();
        }
 public MainViewModel()
 {
     instance      = this;
     this.Vehicles = new VehiclesViewModel();
 }