Exemplo n.º 1
0
        async void ButtonNext_Clicked(object sender, System.EventArgs e)
        {
            (sender as VisualElement).IsEnabled = false;
            this.IsBusy = true;
            try
            {
                var state = string.Empty;
                if (stateEntry.SelectedIndex >= 0)
                {
                    state = stateEntry.Items[stateEntry.SelectedIndex];
                    state = mStates[state];
                }

                string Address = zipcodeEntry.Text + " US";

                GmsDetailsResultItem gmsDetailsResult = await Task.Run(() => { return(mAddressFacade.GetGmsDetails(Address)); });

                if (gmsDetailsResult != null)
                {
                    ExtendedAddress serviceAddress = new ExtendedAddress()
                    {
                        BasicAddress = new ColonyConcierge.APIData.Data.Address()
                    };
                    serviceAddress.BasicAddress.ZipCode   = zipcodeEntry.Text;
                    serviceAddress.BasicAddress.City      = cityEntry.Text;
                    serviceAddress.BasicAddress.StateProv = state;
                    serviceAddress.BasicAddress.Line1     = addressEntry.Text;
                    serviceAddress.BasicAddress.Line2     = apartmentEntry.Text;
                    serviceAddress.Latitude  = (decimal)gmsDetailsResult.Geometry.Location.Latitude;
                    serviceAddress.Longitude = (decimal)gmsDetailsResult.Geometry.Location.Longitude;

                    SerivesFacade serivesService = new SerivesFacade();
                    var           zipCode        = await Task.Run(() => serivesService.CheckAvailableServices(serviceAddress));

                    if (!string.IsNullOrEmpty(zipCode))
                    {
                        var latitude  = mAppServices.LastLatitude;
                        var longitude = mAppServices.LastLongitude;
                        if (!latitude.Equals(0) || !longitude.Equals(0))
                        {
                            var currentAddressDetails = await mAddressFacade.GetGmsDetails(latitude, longitude);

                            ExtendedAddress currentExtendedAddress = mAddressFacade.GetExtendedAddress(currentAddressDetails);
                            if (currentAddressDetails != null && currentExtendedAddress.BasicAddress.ZipCode == zipCode)
                            {
                                serviceAddress.Latitude  = (decimal)latitude;
                                serviceAddress.Longitude = (decimal)longitude;
                            }
                            SaveAddressPage SaveAddressPage = new SaveAddressPage(ParentPage, serviceAddress, Saved);
                            SaveAddressPage.IsBusiness   = IsBusiness;
                            SaveAddressPage.FullName     = FullName;
                            SaveAddressPage.EmailAddress = EmailAddress;

                            await Utils.PushAsync(Navigation, SaveAddressPage, true);
                        }
                        else
                        {
                            mAppServices.CheckLocationPermission((isChecked) =>
                            {
                                Task.Run(() =>
                                {
                                    if (isChecked)
                                    {
                                        IsBusy = true;
                                        Device.BeginInvokeOnMainThread(() =>
                                        {
                                            try
                                            {
                                                mAppServices.GetLocation((isLocation) =>
                                                {
                                                    Task.Run(() =>
                                                    {
                                                        try
                                                        {
                                                            if (isLocation)
                                                            {
                                                                latitude  = mAppServices.LastLatitude;
                                                                longitude = mAppServices.LastLongitude;
                                                                if (!latitude.Equals(0) || !longitude.Equals(0))
                                                                {
                                                                    var currentAddressDetails = mAddressFacade.GetGmsDetails(latitude, longitude).Result;
                                                                    ExtendedAddress currentExtendedAddress = mAddressFacade.GetExtendedAddress(currentAddressDetails);
                                                                    if (currentAddressDetails != null && currentExtendedAddress.BasicAddress.ZipCode == zipCode)
                                                                    {
                                                                        serviceAddress.Latitude  = (decimal)latitude;
                                                                        serviceAddress.Longitude = (decimal)longitude;
                                                                    }
                                                                }
                                                            }
                                                            else
                                                            {
                                                                Device.BeginInvokeOnMainThread(() =>
                                                                {
                                                                    IsBusy = false;
                                                                    SaveAddressPage SaveAddressPage = new SaveAddressPage(ParentPage, serviceAddress, Saved);
                                                                    SaveAddressPage.IsBusiness      = IsBusiness;
                                                                    SaveAddressPage.FullName        = FullName;
                                                                    SaveAddressPage.EmailAddress    = EmailAddress;
                                                                    Utils.PushAsync(Navigation, SaveAddressPage, true);
                                                                });
                                                            }
                                                        }
                                                        catch (Exception)
                                                        {
                                                            Device.BeginInvokeOnMainThread(() =>
                                                            {
                                                                IsBusy = false;
                                                                SaveAddressPage SaveAddressPage = new SaveAddressPage(ParentPage, serviceAddress, Saved);
                                                                SaveAddressPage.IsBusiness      = IsBusiness;
                                                                SaveAddressPage.FullName        = FullName;
                                                                SaveAddressPage.EmailAddress    = EmailAddress;
                                                                Utils.PushAsync(Navigation, SaveAddressPage, true);
                                                            });
                                                        }
                                                        new System.Threading.ManualResetEvent(false).WaitOne(100);
                                                    }).ContinueWith(t =>
                                                    {
                                                        Device.BeginInvokeOnMainThread(() =>
                                                        {
                                                            IsBusy = false;
                                                            var saveAddressPage          = new SaveAddressPage(ParentPage, serviceAddress, Saved);
                                                            saveAddressPage.FullName     = FullName;
                                                            saveAddressPage.EmailAddress = EmailAddress;
                                                            Utils.PushAsync(Navigation, saveAddressPage, true);
                                                        });
                                                    }, TaskScheduler.FromCurrentSynchronizationContext());
                                                });
                                            }
                                            catch (Exception)
                                            {
                                                IsBusy = false;
                                                SaveAddressPage SaveAddressPage = new SaveAddressPage(ParentPage, serviceAddress, Saved);
                                                SaveAddressPage.IsBusiness      = IsBusiness;
                                                SaveAddressPage.FullName        = FullName;
                                                SaveAddressPage.EmailAddress    = EmailAddress;
                                                Utils.PushAsync(Navigation, SaveAddressPage, true);
                                            }
                                        });
                                    }
                                    else
                                    {
                                        new System.Threading.ManualResetEvent(false).WaitOne(100);
                                        Device.BeginInvokeOnMainThread(() =>
                                        {
                                            IsBusy = false;
                                            SaveAddressPage SaveAddressPage = new SaveAddressPage(ParentPage, serviceAddress, Saved);
                                            SaveAddressPage.IsBusiness      = IsBusiness;
                                            SaveAddressPage.FullName        = FullName;
                                            SaveAddressPage.EmailAddress    = EmailAddress;
                                            Utils.PushAsync(Navigation, SaveAddressPage, true);
                                        });
                                    }
                                });
                            });
                        }
                    }
                    else
                    {
                        ExtendedAddress newServiceAddress = new ExtendedAddress()
                        {
                            BasicAddress = new ColonyConcierge.APIData.Data.Address()
                        };
                        newServiceAddress.BasicAddress.ZipCode   = zipcodeEntry.Text;
                        newServiceAddress.BasicAddress.City      = cityEntry.Text;
                        newServiceAddress.BasicAddress.StateProv = state;
                        newServiceAddress.BasicAddress.Line1     = addressEntry.Text;
                        newServiceAddress.BasicAddress.Line2     = apartmentEntry.Text;
                        newServiceAddress.Latitude  = (decimal)gmsDetailsResult.Geometry.Location.Latitude;
                        newServiceAddress.Longitude = (decimal)gmsDetailsResult.Geometry.Location.Longitude;
                        var serviceNotAvailablePage = new ServiceNotAvailablePage(newServiceAddress, FullName, EmailAddress);
                        await Navigation.PushAsync(serviceNotAvailablePage);

                        //var notificator = DependencyService.Get<IToastNotificator>();
                        //await notificator.Notify(ToastNotificationType.Error, "", AppResources.AreaInvalid, TimeSpan.FromSeconds(2));
                    }
                }
                else
                {
                    var notificator = DependencyService.Get <IToastNotificator>();
                    await notificator.Notify(ToastNotificationType.Error, "", AppResources.PullDataFailMessage, TimeSpan.FromSeconds(2));
                }
            }
            catch (Exception ex)
            {
                var notificator = DependencyService.Get <IToastNotificator>();
                await notificator.Notify(ToastNotificationType.Error, "Error", ex.Message, TimeSpan.FromSeconds(2));
            }
            finally
            {
                (sender as VisualElement).IsEnabled = true;
                this.IsBusy = false;
            }
            //throw new NotImplementedException();
        }
        public SaveAddressPage(Page parentPage, ExtendedAddress serviceAddress, Action <ExtendedAddress> saved = null)
        {
            InitializeComponent();

            ParentPage      = parentPage;
            Saved           = saved;
            mServiceAddress = serviceAddress;

            mAppServices = DependencyService.Get <IAppServices>();

            this.ToolbarItems.Add(new ToolbarItem()
            {
                Icon    = "close_white.png",
                Command = new Command(async(t) =>
                {
                    await Navigation.PopToRootAsync(true).ConfigureAwait(false);
                }),
            });

            Position position = new Position((double)mServiceAddress.Latitude, (double)mServiceAddress.Longitude);

            LabelAddress.Text = mServiceAddress.BasicAddress.ToCannonicalString().Replace("\n", " ");

            Map.RegionChanged += (sender, e) =>
            {
                mLatitude  = e.Latitude;
                mLongitude = e.Longitude;
            };

            var appServices = DependencyService.Get <IAppServices>();
            var latitude    = appServices.LastLatitude;
            var longitude   = appServices.LastLongitude;

            if (!latitude.Equals(0) || !longitude.Equals(0))
            {
                Map.CurrentPin = new CustomPin()
                {
                    Pin = new Pin
                    {
                        Label    = AppResources.CurrentLocation,
                        Position = new Position(latitude, longitude),
                    }
                };
            }

            Map.MoveToRegion(MapSpan.FromCenterAndRadius(position, Distance.FromMiles(0.5)));
            Map.Center = position;

            ImagePin.SizeChanged += (sender, e) =>
            {
                if (ImagePin.Height > 0)
                {
                    ImagePin.TranslationY = -ImagePin.Height / 2;
                }
            };
            ImagePin.IsVisible = true;

            //Map.VisibleRegion.Center;
            ButtonSave.Clicked += async(sender, e) =>
            {
                try
                {
                    this.IsBusy = true;

                    var currentAddressDetails = await mAddressFacade.GetGmsDetails(mLatitude, mLongitude);

                    ExtendedAddress currentExtendedAddress = new ExtendedAddress {
                        BasicAddress = new ColonyConcierge.APIData.Data.Address()
                    };
                    mAddressFacade.FillExtendedAddress(currentExtendedAddress, currentAddressDetails);
                    var zipCode = serivesService.CheckAvailableServices(currentExtendedAddress);
                    if (!string.IsNullOrEmpty(zipCode))
                    {
                        mServiceAddress.BasicAddress.ZipCode = zipCode;
                        mServiceAddress.Latitude             = (decimal)mLatitude;
                        mServiceAddress.Longitude            = (decimal)mLongitude;

                        if (!IsBusiness)
                        {
                            Shared.LocalAddress = mServiceAddress;
                        }
                        if (Saved != null)
                        {
                            Saved.Invoke(mServiceAddress);
                        }

                        if (ParentPage != null)
                        {
                            var pages = Navigation.NavigationStack.Reverse().Skip(1).ToList();
                            foreach (var page in pages)
                            {
                                if (page != ParentPage)
                                {
                                    Navigation.RemovePage(page);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            await Navigation.PopAsync(true).ConfigureAwait(false);
                        }
                        else
                        {
                            await Navigation.PopToRootAsync(true).ConfigureAwait(false);
                        }

                        appServices.SetNetworkBar(false);
                    }
                    else
                    {
                        var newserviceAddress = new ExtendedAddress {
                            BasicAddress = new ColonyConcierge.APIData.Data.Address()
                        };
                        mAddressFacade.FillExtendedAddress(newserviceAddress, currentAddressDetails);
                        var serviceNotAvailablePage = new ServiceNotAvailablePage(newserviceAddress, FullName, EmailAddress);
                        await Navigation.PushAsync(serviceNotAvailablePage);

                        //var notificator = DependencyService.Get<IToastNotificator>();
                        //await notificator.Notify(ToastNotificationType.Error, AppResources.Error, AppResources.AreaInvalid, TimeSpan.FromSeconds(2));
                    }
                }
                catch (Exception ex)
                {
                    var notificator = DependencyService.Get <IToastNotificator>();
                    await notificator.Notify(ToastNotificationType.Error, AppResources.Error, ex.Message, TimeSpan.FromSeconds(2));
                }
                finally
                {
                    (sender as VisualElement).IsEnabled = true;
                    this.IsBusy = false;
                }
            };
        }