Beispiel #1
0
        async void OnAddPersonsDataButtonClicked(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(nameTextBox.Text))
            {
                await DisplayAlert(ConfigurationManager.AppSettings["SystemErrorTitle"], ConfigurationManager.AppSettings["AddPersonInvalidNoName"], ConfigurationManager.AppSettings["MessageBoxClosingBtnText"]);

                return;
            }

            if (_PersonSelected == null && PersonVM.Faces.Count < 1)
            {
                await DisplayAlert(ConfigurationManager.AppSettings["SystemErrorTitle"], ConfigurationManager.AppSettings["AddPersonInvalidNo"], ConfigurationManager.AppSettings["MessageBoxClosingBtnText"]);

                return;
            }

            addPerson.IsEnabled  = false;
            editPerson.IsEnabled = false;
            //deletePerson.IsEnabled = false;

            PersonVM.Username       = nameTextBox.Text;
            PersonVM.BirthDate      = birthdatePicker.Date;
            PersonVM.MissingDate    = missingdatePicker.Date;
            PersonVM.AdditionalInfo = addInfoTextBox.Text;

            if (string.IsNullOrEmpty(PersonVM.AdditionalInfo))
            {
                PersonVM.AdditionalInfo = "";
            }

            try
            {
                IList <byte[]> newPhotos = new List <byte[]>();

                foreach (Face face in PersonVM.Faces)
                {
                    if (face.PhotoByteArr != null && string.IsNullOrEmpty(face.BlobURI))
                    {
                        newPhotos.Add(face.PhotoByteArr);
                    }
                }

                if (_PersonSelected == null)
                {
                    var addPerson = await _personsService.AddNewPersonAsync(
                        new Person
                    {
                        Name           = PersonVM.Username,
                        BirthDate      = PersonVM.BirthDate.ToString(),
                        MissingDate    = PersonVM.MissingDate.ToString(),
                        AdditionalInfo = PersonVM.AdditionalInfo,
                        CreatorId      = App.CurrentUser,
                        Photos         = newPhotos
                    }
                        );

                    if (addPerson.Success)
                    {
                        await DisplayAlert("Success", "Person added", "Close");

                        ClearData();
                    }
                    else
                    {
                        await DisplayAlert("Adding error" + ConfigurationManager.AppSettings["SystemErrorTitle"], addPerson.Error, ConfigurationManager.AppSettings["MessageBoxClosingBtnText"]);

                        ErrorLogger.Instance.LogError(addPerson.Error);
                    }
                }
                else
                {
                    var updatePerson = await _personsService.UpdatePersonAsync(
                        new Person
                    {
                        Id             = PersonVM.Id,
                        Name           = PersonVM.Username,
                        BirthDate      = PersonVM.BirthDate.ToString(),
                        MissingDate    = PersonVM.MissingDate.ToString(),
                        AdditionalInfo = PersonVM.AdditionalInfo,
                        CreatorId      = App.CurrentUser,
                        Photos         = newPhotos
                    },
                        _PersonSelected.Id
                        );

                    if (updatePerson.Success)
                    {
                        await DisplayAlert("Success", "Person updated", "Close");
                    }
                    else
                    {
                        await DisplayAlert(ConfigurationManager.AppSettings["SystemErrorTitle"], updatePerson.Error, ConfigurationManager.AppSettings["MessageBoxClosingBtnText"]);

                        ErrorLogger.Instance.LogError(updatePerson.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.Instance.LogException(ex);
                await DisplayAlert(ConfigurationManager.AppSettings["SystemErrorTitle"], ConfigurationManager.AppSettings["SystemErrorMessage"], ConfigurationManager.AppSettings["MessageBoxClosingBtnText"]);
            }
            finally
            {
                addPerson.IsEnabled  = true;
                editPerson.IsEnabled = true;
                //deletePerson.IsEnabled = true;
            }
        }