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(); } }
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; } }