// set starttime based on user location
        public async void ActivateLocationMode()
        {
            //ui
            StackPanelTimePicker.Visibility   = Visibility.Collapsed;
            TextBlockDark.Visibility          = Visibility.Collapsed;
            TextBlockLight.Visibility         = Visibility.Collapsed;
            StackPanelLocationTime.Visibility = Visibility.Visible;
            SetOffsetVisibility(Visibility.Visible);
            locationBlock.Visibility = Visibility.Visible;
            locationBlock.Text       = Properties.Resources.msgSearchLoc;//Searching your location...
            userFeedback.Text        = Properties.Resources.msgSearchLoc;

            LocationHandler locationHandler = new LocationHandler();
            var             accesStatus     = await Geolocator.RequestAccessAsync();

            switch (accesStatus)
            {
            case GeolocationAccessStatus.Allowed:
                //locate user + get sunrise & sunset times
                locationBlock.Text = Properties.Resources.lblCity + ": " + await locationHandler.GetCityName();

                int[] sundate = await locationHandler.CalculateSunTime(false);

                //apply settings
                ApplyTheme(sundate[2], sundate[3], sundate[0], sundate[1]);

                //show time in UI
                if (Properties.Settings.Default.AlterTime)
                {
                    sundate[2] -= 12;
                }

                TimeSpan TimeForUiLight = new TimeSpan(sundate[0], sundate[1], 0);
                TimeSpan TimeForUiDark  = new TimeSpan(sundate[2], sundate[3], 0);
                TextBlockLightTime.Text = Properties.Resources.lblLight + ": " + string.Format("{0:00}:{1:00}", TimeForUiLight.Hours, TimeForUiLight.Minutes); //textblock1
                TextBlockDarkTime.Text  = Properties.Resources.lblDark + ": " + string.Format("{0:00}:{1:00}", TimeForUiDark.Hours, TimeForUiDark.Minutes);    //textblock2

                // ui controls
                lightStartBox.IsEnabled        = false;
                LightStartMinutesBox.IsEnabled = false;
                darkStartBox.IsEnabled         = false;
                DarkStartMinutesBox.IsEnabled  = false;

                applyButton.Visibility = Visibility.Hidden;
                taskSchHandler.CreateLocationTask();
                break;

            case GeolocationAccessStatus.Denied:
                NoLocationAccess();
                break;

            case GeolocationAccessStatus.Unspecified:
                NoLocationAccess();
                break;
            }
            return;
        }
        public async void GetLocation()
        {
            SetOffsetVisibility(Visibility.Visible);
            locationBlock.Visibility = Visibility.Visible;
            locationBlock.Text       = Properties.Resources.msgSearchLoc;//Searching your location...
            LocationHandler locationHandler = new LocationHandler();

            var accesStatus = await Geolocator.RequestAccessAsync();

            switch (accesStatus)
            {
            case GeolocationAccessStatus.Allowed:
                //locate user + get sunrise & sunset times
                locationBlock.Text = Properties.Resources.lblCity + ": " + await locationHandler.GetCityName();

                int[] sundate = await locationHandler.CalculateSunTime(false);

                //apply settings & change UI
                lightStartBox.Text        = sundate[0].ToString();
                LightStartMinutesBox.Text = sundate[1].ToString();
                if (Properties.Settings.Default.AlterTime)
                {
                    sundate[2]       -= 12;
                    darkStartBox.Text = sundate[2].ToString();
                }
                else
                {
                    darkStartBox.Text = sundate[2].ToString();
                }
                DarkStartMinutesBox.Text       = sundate[3].ToString();
                lightStartBox.IsEnabled        = false;
                LightStartMinutesBox.IsEnabled = false;
                darkStartBox.IsEnabled         = false;
                DarkStartMinutesBox.IsEnabled  = false;
                applyButton.IsEnabled          = false;
                ApplyButton_Click(this, null);
                taskShedHandler.CreateLocationTask();
                break;

            case GeolocationAccessStatus.Denied:
                NoLocationAccess();
                break;

            case GeolocationAccessStatus.Unspecified:
                NoLocationAccess();
                break;
            }
            return;
        }
Example #3
0
        // set starttime based on user location
        public async void ActivateLocationMode()
        {
            //ui
            StackPanelTimePicker.Visibility   = Visibility.Collapsed;
            TextBlockDark.Visibility          = Visibility.Collapsed;
            TextBlockLight.Visibility         = Visibility.Collapsed;
            StackPanelLocationTime.Visibility = Visibility.Visible;
            SetOffsetVisibility(Visibility.Visible);
            locationBlock.Visibility = Visibility.Visible;
            locationBlock.Text       = Properties.Resources.msgSearchLoc;//Searching your location...
            userFeedback.Text        = Properties.Resources.msgSearchLoc;

            if (!init)
            {
                try
                {
                    builder.Save();
                }
                catch (Exception ex)
                {
                    ShowErrorMessage(ex);
                }
            }

            int  timeout = 2;
            bool loaded  = false;

            for (int i = 0; i < timeout; i++)
            {
                if (builder.LocationData.LastUpdate == DateTime.MinValue)
                {
                    try
                    {
                        var result = await messagingClient.SendMessageAndGetReplyAsync(Command.Location);

                        if (result == Response.NoLocAccess)
                        {
                            NoLocationAccess();
                            break;
                        }
                        builder.LoadLocationData();
                    }
                    catch (Exception ex)
                    {
                        ShowErrorMessage(ex);
                        loaded = true;
                        break;
                    }
                    await Task.Delay(1000);
                }
                else
                {
                    loaded = true;
                    break;
                }
            }

            LocationHandler locationHandler = new LocationHandler();
            var             accesStatus     = await Geolocator.RequestAccessAsync();

            switch (accesStatus)
            {
            case GeolocationAccessStatus.Allowed:
                //locate user + get sunrise & sunset times
                locationBlock.Text = Properties.Resources.lblCity + ": " + await locationHandler.GetCityName();

                break;

            case GeolocationAccessStatus.Denied:
                NoLocationAccess();
                loaded = false;
                break;

            case GeolocationAccessStatus.Unspecified:
                NoLocationAccess();
                loaded = false;
                break;
            }

            if (!loaded)
            {
                ShowErrorMessage(new TimeoutException("waiting for location data timed out"));
            }

            UpdateSuntimes();

            // ui controls
            lightStartBox.IsEnabled        = false;
            LightStartMinutesBox.IsEnabled = false;
            darkStartBox.IsEnabled         = false;
            DarkStartMinutesBox.IsEnabled  = false;
            userFeedback.Text = Properties.Resources.msgChangesSaved;

            return;
        }
Example #4
0
        private async Task LoadGeolocationData()
        {
            int maxTries = 5;

            for (int i = 0; i < maxTries; i++)
            {
                ApiResponse result = ApiResponse.FromString(await MessageHandler.Client.SendMessageAndGetReplyAsync(Command.GeolocatorIsUpdating));
                if (result.StatusCode == StatusCode.Ok)
                {
                    break;
                }
                await Task.Delay(1000);
            }

            try
            {
                builder.LoadLocationData();
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }

            try
            {
                ApiResponse result = ApiResponse.FromString(await MessageHandler.Client.SendMessageAndGetReplyAsync(Command.LocationAccess));
                if (builder.Config.Location.UseGeolocatorService && result.StatusCode == StatusCode.NoLocAccess)
                {
                    NoLocationAccess();
                    return;
                }
                else if (builder.Config.Location.UseGeolocatorService && result.StatusCode == StatusCode.Ok)
                {
                    locationBlock.Text = AdmProperties.Resources.lblCity + ": " + await LocationHandler.GetCityName();
                }
                else if (!builder.Config.Location.UseGeolocatorService)
                {
                    locationBlock.Text = $"{AdmProperties.Resources.lblPosition}: Lat {Math.Round(builder.LocationData.Lat, 3)} / Lon {Math.Round(builder.LocationData.Lon, 3)}";
                }
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
                return;
            }
        }