public async Task FetchGeoLocation()
        {
            RunOnUiThread(() =>
            {
                locationButton.Enabled = false;
            });

            if (mLocation != null)
            {
                LocationQueryViewModel view = null;

                // Cancel other tasks
                cts.Cancel();
                cts = new CancellationTokenSource();
                var ctsToken = cts.Token;

                if (ctsToken.IsCancellationRequested)
                {
                    EnableControls(true);
                    return;
                }

                // Show loading bar
                RunOnUiThread(() =>
                {
                    progressBar.Visibility = ViewStates.Visible;
                });

                await Task.Run(async() =>
                {
                    if (ctsToken.IsCancellationRequested)
                    {
                        EnableControls(true);
                        return;
                    }

                    // Get geo location
                    view = await wm.GetLocation(mLocation);

                    if (String.IsNullOrEmpty(view.LocationQuery))
                    {
                        view = new LocationQueryViewModel();
                    }
                });

                if (String.IsNullOrWhiteSpace(view.LocationQuery))
                {
                    // Stop since there is no valid query
                    EnableControls(true);
                    return;
                }

                if (Settings.UsePersonalKey && String.IsNullOrWhiteSpace(Settings.API_KEY) && wm.KeyRequired)
                {
                    RunOnUiThread(() =>
                    {
                        Toast.MakeText(this.ApplicationContext, Resource.String.werror_invalidkey, ToastLength.Short).Show();
                        EnableControls(true);
                    });
                    return;
                }

                if (ctsToken.IsCancellationRequested)
                {
                    EnableControls(true);
                    return;
                }

                // Get Weather Data
                var location = new WeatherData.LocationData(view, mLocation);
                var weather  = await Settings.GetWeatherData(location.query);

                if (weather == null)
                {
                    try
                    {
                        weather = await wm.GetWeather(location);
                    }
                    catch (WeatherException wEx)
                    {
                        weather = null;
                        RunOnUiThread(() =>
                        {
                            Toast.MakeText(App.Context, wEx.Message, ToastLength.Short).Show();
                        });
                    }
                }

                if (weather == null)
                {
                    EnableControls(true);
                    return;
                }

                // We got our data so disable controls just in case
                EnableControls(false);

                // Save weather data
                Settings.SaveHomeData(location);
                if (wm.SupportsAlerts && weather.weather_alerts != null)
                {
                    await Settings.SaveWeatherAlerts(location, weather.weather_alerts);
                }
                await Settings.SaveWeatherData(weather);

                Settings.FollowGPS     = true;
                Settings.WeatherLoaded = true;

                // Start WeatherNow Activity with weather data
                Intent intent = new Intent(this, typeof(MainActivity));
                intent.PutExtra("data", location.ToJson());

                StartActivity(intent);
                FinishAffinity();
            }
            else
            {
                await UpdateLocation();
            }
        }
Пример #2
0
        private async void LocationSearchFragment_clickListener(object sender, RecyclerClickEventArgs e)
        {
            // Get selected query view
            LocationQuery          v        = (LocationQuery)e.View;
            LocationQueryViewModel query_vm = null;

            if (!String.IsNullOrEmpty(mAdapter.Dataset[e.Position].LocationQuery))
            {
                query_vm = mAdapter.Dataset[e.Position];
            }
            else
            {
                query_vm = new LocationQueryViewModel();
            }

            if (String.IsNullOrWhiteSpace(query_vm.LocationQuery))
            {
                // Stop since there is no valid query
                return;
            }

            if (Settings.UsePersonalKey && String.IsNullOrWhiteSpace(Settings.API_KEY) && wm.KeyRequired)
            {
                Toast.MakeText(App.Context, App.Context.GetString(Resource.String.werror_invalidkey), ToastLength.Short).Show();
                return;
            }

            // Cancel pending searches
            cts.Cancel();
            cts = new CancellationTokenSource();
            var ctsToken = cts.Token;

            ShowLoading(true);

            if (ctsToken.IsCancellationRequested)
            {
                ShowLoading(false);
                return;
            }

            // Get Weather Data
            var location = new WeatherData.LocationData(query_vm);
            var weather  = await Settings.GetWeatherData(location.query);

            if (weather == null)
            {
                try
                {
                    weather = await wm.GetWeather(location);
                }
                catch (WeatherException wEx)
                {
                    weather = null;
                    Toast.MakeText(App.Context, wEx.Message, ToastLength.Short).Show();
                }
            }

            if (weather == null)
            {
                ShowLoading(false);
                return;
            }

            // We got our data so disable controls just in case
            mAdapter.Dataset.Clear();
            mAdapter.NotifyDataSetChanged();
            mRecyclerView.Enabled = false;

            // Save weather data
            Settings.SaveHomeData(location);
            if (wm.SupportsAlerts && weather.weather_alerts != null)
            {
                await Settings.SaveWeatherAlerts(location, weather.weather_alerts);
            }
            await Settings.SaveWeatherData(weather);

            // If we're using search
            // make sure gps feature is off
            Settings.FollowGPS     = false;
            Settings.WeatherLoaded = true;

            // Start WeatherNow Activity with weather data
            Intent intent = new Intent(mActivity, typeof(MainActivity));

            intent.PutExtra("data", location.ToJson());

            mActivity.StartActivity(intent);
            mActivity.FinishAffinity();
        }
        public async Task FetchGeoLocation()
        {
            try
            {
                gpsFollowButton.Enabled = false;

                if (mLocation != null)
                {
                    LocationQueryViewModel view = null;

                    // Cancel other tasks
                    cts?.Cancel();
                    cts = new CancellationTokenSource();
                    var ctsToken = cts.Token;

                    ctsToken.ThrowIfCancellationRequested();

                    // Show loading bar
                    RunOnUiThread(() => progressBar.Visibility = ViewStates.Visible);

                    await Task.Run(async() =>
                    {
                        ctsToken.ThrowIfCancellationRequested();

                        // Get geo location
                        view = await wm.GetLocation(mLocation);

                        if (String.IsNullOrEmpty(view.LocationQuery))
                        {
                            view = new LocationQueryViewModel();
                        }
                    });

                    if (String.IsNullOrWhiteSpace(view.LocationQuery))
                    {
                        // Stop since there is no valid query
                        EnableControls(true);
                        return;
                    }

                    if (Settings.UsePersonalKey && String.IsNullOrWhiteSpace(Settings.API_KEY) && wm.KeyRequired)
                    {
                        RunOnUiThread(() =>
                        {
                            Toast.MakeText(this.ApplicationContext, Resource.String.werror_invalidkey, ToastLength.Short).Show();
                        });
                        EnableControls(true);
                        return;
                    }

                    ctsToken.ThrowIfCancellationRequested();

                    // Get Weather Data
                    var location = new WeatherData.LocationData(view, mLocation);
                    if (!location.IsValid())
                    {
                        RunOnUiThread(() =>
                        {
                            Toast.MakeText(App.Context, App.Context.GetString(Resource.String.werror_noweather), ToastLength.Short).Show();
                        });
                        EnableControls(true);
                        return;
                    }

                    ctsToken.ThrowIfCancellationRequested();

                    var weather = await Settings.GetWeatherData(location.query);

                    if (weather == null)
                    {
                        ctsToken.ThrowIfCancellationRequested();

                        try
                        {
                            weather = await Task.Run(() => wm.GetWeather(location), ctsToken);
                        }
                        catch (WeatherException wEx)
                        {
                            weather = null;
                            RunOnUiThread(() =>
                            {
                                Toast.MakeText(App.Context, wEx.Message, ToastLength.Short).Show();
                            });
                        }
                    }

                    if (weather == null)
                    {
                        EnableControls(true);
                        return;
                    }

                    ctsToken.ThrowIfCancellationRequested();

                    // We got our data so disable controls just in case
                    EnableControls(false);

                    // Save weather data
                    Settings.SaveLastGPSLocData(location);
                    await Settings.DeleteLocations();

                    await Settings.AddLocation(new WeatherData.LocationData(view));

                    if (wm.SupportsAlerts && weather.weather_alerts != null)
                    {
                        await Settings.SaveWeatherAlerts(location, weather.weather_alerts);
                    }
                    await Settings.SaveWeatherData(weather);

                    Settings.FollowGPS     = true;
                    Settings.WeatherLoaded = true;

                    // Send data for wearables
                    StartService(new Intent(this, typeof(WearableDataListenerService))
                                 .SetAction(WearableDataListenerService.ACTION_SENDSETTINGSUPDATE));
                    StartService(new Intent(this, typeof(WearableDataListenerService))
                                 .SetAction(WearableDataListenerService.ACTION_SENDLOCATIONUPDATE));
                    StartService(new Intent(this, typeof(WearableDataListenerService))
                                 .SetAction(WearableDataListenerService.ACTION_SENDWEATHERUPDATE));

                    if (mAppWidgetId == AppWidgetManager.InvalidAppwidgetId)
                    {
                        // Start WeatherNow Activity with weather data
                        Intent intent = new Intent(this, typeof(MainActivity));
                        intent.PutExtra("data", location.ToJson());

                        StartActivity(intent);
                        FinishAffinity();
                    }
                    else
                    {
                        // Create return intent
                        Intent resultValue = new Intent();
                        resultValue.PutExtra(AppWidgetManager.ExtraAppwidgetId, mAppWidgetId);
                        resultValue.PutExtra("data", location.ToJson());
                        SetResult(Android.App.Result.Ok, resultValue);
                        Finish();
                    }
                }
                else
                {
                    await UpdateLocation();
                }
            }
            catch (System.OperationCanceledException)
            {
                // Restore controls
                EnableControls(true);
                Settings.FollowGPS     = false;
                Settings.WeatherLoaded = false;
            }
        }
Пример #4
0
        private async void LocationSearchFragment_clickListener(object sender, RecyclerClickEventArgs e)
        {
            // Get selected query view
            LocationQuery          v        = (LocationQuery)e.View;
            LocationQueryViewModel query_vm = null;

            try
            {
                if (!String.IsNullOrEmpty(mAdapter.Dataset[e.Position].LocationQuery))
                {
                    query_vm = mAdapter.Dataset[e.Position];
                }
            }
            catch (ArgumentOutOfRangeException)
            {
                query_vm = null;
            }
            finally
            {
                if (query_vm == null)
                {
                    query_vm = new LocationQueryViewModel();
                }
            }

            if (String.IsNullOrWhiteSpace(query_vm.LocationQuery))
            {
                // Stop since there is no valid query
                return;
            }

            if (Settings.UsePersonalKey && String.IsNullOrWhiteSpace(Settings.API_KEY) && wm.KeyRequired)
            {
                Toast.MakeText(App.Context, App.Context.GetString(Resource.String.werror_invalidkey), ToastLength.Short).Show();
                return;
            }

            // Cancel pending searches
            CtsCancel();
            var ctsToken = cts.Token;

            ShowLoading(true);

            if (ctsToken.IsCancellationRequested)
            {
                ShowLoading(false);
                return;
            }

            // Get Weather Data
            var location = new WeatherData.LocationData(query_vm);

            if (!location.IsValid())
            {
                Toast.MakeText(App.Context, App.Context.GetString(Resource.String.werror_noweather), ToastLength.Short).Show();
                ShowLoading(false);
                return;
            }
            WeatherData.Weather weather = await Settings.GetWeatherData(location.query);

            if (weather == null)
            {
                try
                {
                    weather = await wm.GetWeather(location);
                }
                catch (WeatherException wEx)
                {
                    weather = null;
                    Toast.MakeText(App.Context, wEx.Message, ToastLength.Short).Show();
                }
            }

            if (weather == null)
            {
                ShowLoading(false);
                return;
            }

            // We got our data so disable controls just in case
            mAdapter.Dataset.Clear();
            mAdapter.NotifyDataSetChanged();
            mRecyclerView.Enabled = false;

            // Save weather data
            await Settings.DeleteLocations();

            await Settings.AddLocation(location);

            if (wm.SupportsAlerts && weather.weather_alerts != null)
            {
                await Settings.SaveWeatherAlerts(location, weather.weather_alerts);
            }
            await Settings.SaveWeatherData(weather);

            // If we're using search
            // make sure gps feature is off
            Settings.FollowGPS     = false;
            Settings.WeatherLoaded = true;

            // Send data for wearables
            mActivity.StartService(new Intent(mActivity, typeof(WearableDataListenerService))
                                   .SetAction(WearableDataListenerService.ACTION_SENDSETTINGSUPDATE));
            mActivity.StartService(new Intent(mActivity, typeof(WearableDataListenerService))
                                   .SetAction(WearableDataListenerService.ACTION_SENDLOCATIONUPDATE));
            mActivity.StartService(new Intent(mActivity, typeof(WearableDataListenerService))
                                   .SetAction(WearableDataListenerService.ACTION_SENDWEATHERUPDATE));

            if (mAppWidgetId == AppWidgetManager.InvalidAppwidgetId)
            {
                // Start WeatherNow Activity with weather data
                Intent intent = new Intent(mActivity, typeof(MainActivity));
                intent.PutExtra("data", location.ToJson());

                mActivity.StartActivity(intent);
                mActivity.FinishAffinity();
            }
            else
            {
                // Create return intent
                Intent resultValue = new Intent();
                resultValue.PutExtra(AppWidgetManager.ExtraAppwidgetId, mAppWidgetId);
                resultValue.PutExtra("data", location.ToJson());
                mActivity.SetResult(Android.App.Result.Ok, resultValue);
                mActivity.Finish();
            }
        }