Пример #1
0
        private async void BtnRegister_OnClicked(object sender, EventArgs e)
        {
            Overlay.IsVisible = true;

            if (_file == null)
            {
                Overlay.IsVisible = false;
                await DisplayAlert("Image Upload", "Upload your image", "Ok");

                return;
            }

            if (!ChkBlood.IsChecked && !ChkOrgan.IsChecked)
            {
                Overlay.IsVisible = false;
                await DisplayAlert("Donate Option", "Select at least one donation option", "Ok");

                return;
            }

            if (EntBloodGroup.SelectedIndex == -1 && _organList.Count == 0)
            {
                Overlay.IsVisible = false;
                await DisplayAlert("", "Select at least a blood group or an organ", "OK");
            }


            var imageArray = FilesHelper.ReadFully(_file.GetStream());

            _file.Dispose();

            var request  = new GeolocationRequest(GeolocationAccuracy.High);
            var location = await Geolocation.GetLocationAsync(request);

            var placemarks = await Geocoding.GetPlacemarksAsync(location.Latitude, location.Longitude);

            var placemark = placemarks?.FirstOrDefault();
            var donor     = new Donor()
            {
                Id         = Settings.Id,
                FirstName  = EntFirstname.Text,
                LastName   = EntLastname.Text,
                Phone      = EntPhone.Text,
                Gender     = RdGender.SelectedItem.ToString(),
                RegionId   = ((RegionModel)Reg.SelectedItem).Id,
                Location   = placemark.Locality,
                Latitude   = location.Latitude,
                Longitude  = location.Longitude,
                Email      = Settings.Email,
                ImageArray = imageArray
            };


            ApiService apiService = new ApiService();
            var        response   = await apiService.BecomeADonor(donor);

            if (!response)
            {
                Overlay.IsVisible = false;
                await DisplayAlert("Oops", "Something went wrong", "Cancel");
            }

            else
            {
                var bloodOption = new BloodOrganOption();
                if (EntBloodType.SelectedIndex != -1)
                {
                    bloodOption.UserId      = Settings.Id;
                    bloodOption.BloodTypeId = ((BloodType)EntBloodType.SelectedItem).Id;
                    //bloodOption.OrganListId = (int?)null;
                    await apiService.AddBloodOrganOption(bloodOption);
                }

                if (_organList.Count > 0)
                {
                    var organOption = new OrganOption();
                    foreach (var option in _organList)
                    {
                        organOption.UserId = Settings.Id;
                        // organOption.BloodTypeId = int ?;
                        organOption.OrganListId = option;
                        await apiService.AddOrganOption(organOption);
                    }
                }

                Overlay.IsVisible = false;
                //  var result = await DisplayAlert("Success", "You are now a registered donor", "Ok");
                // if (result == true)
                // {
                await DisplayAlert("Success", "You are now a registered donor", "Ok");

                // await Navigation.PushAsync(new HomePage());
                Application.Current.MainPage = new MasterPage();
                // }
            }
        }