コード例 #1
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();
        }
コード例 #2
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();
            }
        }