Example #1
0
        private async void ReportButton_Clicked(object sender, EventArgs e)
        {
            if (_image is null)
            {
                await DisplayAlert("Please select an image", "Please select an image from camera or gallery.", "Ok");

                return;
            }

            var missingFields = new List <string>();

            if (string.IsNullOrEmpty(nameEntry.Text))
            {
                missingFields.Add("Name");
            }

            if (string.IsNullOrEmpty(phoneEntry.Text))
            {
                missingFields.Add("Phone number");
            }

            if (maleRadioButton.IsChecked == false && femaleRadioButton.IsChecked == false)
            {
                missingFields.Add("Missing person's sex");
            }

            if (missingFields.Any())
            {
                await DisplayAlert("Required Fields", $"Please fill in the required fields: {string.Join(", ", missingFields)}", "Ok");

                return;
            }

            try
            {
                SetBusy();

                var stream = _image.GetStream();
                var uri    = await ImageUploadHelper.UploadFileAsync(stream);

                var input = new Models.Person
                {
                    ContactPhone = phoneEntry.Text,
                    Gender       = maleRadioButton.IsChecked ? MissingPeople.Models.Gender.Male : MissingPeople.Models.Gender.Female,
                    ImageURL     = uri.AbsoluteUri,
                    Name         = nameEntry.Text,
                };

                var task = _state == ReportPageState.Found ?
                           ImageUploadHelper.ReportFoundPerson(input) :
                           ImageUploadHelper.ReportMissingPerson(input);

                var person = await task;

                if (person is null)
                {
                    await DisplayAlert("Could not scan image!", "Could not scan image, please try again later.", "Okay");

                    return;
                }

                var matches = person.SimilarPeople?.Select(p => new MatchViewModel
                {
                    Confidence  = p.Similarity * 100,
                    CreatedAt   = p.CreatedAt,
                    Name        = p.Name,
                    PhoneNumber = p.ContactPhone,
                    Thumbnail   = ImageSource.FromUri(new Uri(p.ImageURL)),
                });

                string error = null;

                if (matches is null || !matches.Any())
                {
                    error = _state == ReportPageState.Found ?
                            "This person has not been registered as a missing person." :
                            "This person has not been found by anyone yet. Please check again at a later time.";
                }

                var page = error is null ? new Matches(matches) : new Matches(error);

                await(App.Current.MainPage as NavigationPage).PushAsync(page);
                (App.Current.MainPage as NavigationPage).Navigation.RemovePage(this);
            }
            catch (Exception ex)
            {
                await DisplayAlert("Error", ex.Message, "OK");
            }
            finally
            {
                SetNotBusy();
            }
        }