Beispiel #1
0
        public void OnWeatherLoaded(LocationData location, Weather weather)
        {
            Activity?.RunOnUiThread(() =>
            {
                if (weather?.IsValid() == true)
                {
                    wm.UpdateWeather(weather);
                    weatherView.UpdateView(weather);
                    SetView(weatherView);
                    mCallback?.OnWeatherViewUpdated(weatherView);

                    // Update complications if they haven't been already
                    WeatherComplicationIntentService.EnqueueWork(Activity,
                                                                 new Intent(Activity, typeof(WeatherComplicationIntentService))
                                                                 .SetAction(WeatherComplicationIntentService.ACTION_UPDATECOMPLICATIONS));

                    if (!loaded)
                    {
                        TimeSpan span = DateTimeOffset.Now - weather.update_time;
                        if (Settings.DataSync != WearableDataSync.Off && span.TotalMinutes > Settings.DefaultInterval)
                        {
                            // send request to refresh data on connected device
                            Activity?.StartService(new Intent(Activity, typeof(WearableDataListenerService))
                                                   .SetAction(WearableDataListenerService.ACTION_REQUESTWEATHERUPDATE)
                                                   .PutExtra(WearableDataListenerService.EXTRA_FORCEUPDATE, true));
                        }

                        loaded = true;
                    }
                }

                refreshLayout.Refreshing = false;
            });
        }
Beispiel #2
0
        private async Task UpdateWeather(DataMap dataMap)
        {
            if (dataMap != null && !dataMap.IsEmpty)
            {
                var update_time = dataMap.GetLong("update_time", 0);
                if (update_time != 0)
                {
                    if (Settings.HomeData is WeatherData.LocationData location)
                    {
                        var upDateTime = new DateTime(update_time, DateTimeKind.Utc);

                        /*
                         *  DateTime < 0 - This instance is earlier than value.
                         *  DateTime == 0 - This instance is the same as value.
                         *  DateTime > 0 - This instance is later than value.
                         */
                        if (Settings.UpdateTime.CompareTo(upDateTime) >= 0)
                        {
                            // Send callback to receiver
                            LocalBroadcastManager.GetInstance(this).SendBroadcast(
                                new Intent(WearableHelper.WeatherPath));
                            return;
                        }
                    }
                }

                var weatherJSON = dataMap.GetString("weatherData", String.Empty);
                if (!String.IsNullOrWhiteSpace(weatherJSON))
                {
                    using (var weatherTextReader = new Newtonsoft.Json.JsonTextReader(
                               new System.IO.StringReader(weatherJSON)))
                    {
                        var weatherData = WeatherData.Weather.FromJson(weatherTextReader);
                        var alerts      = dataMap.GetStringArrayList("weatherAlerts");

                        if (weatherData != null && weatherData.IsValid())
                        {
                            if (alerts.Count > 0)
                            {
                                weatherData.weather_alerts = new List <WeatherData.WeatherAlert>();
                                foreach (String alertJSON in alerts)
                                {
                                    using (var alertTextReader = new Newtonsoft.Json.JsonTextReader(
                                               new System.IO.StringReader(alertJSON)))
                                    {
                                        var alert = WeatherData.WeatherAlert.FromJson(alertTextReader);

                                        if (alert != null)
                                        {
                                            weatherData.weather_alerts.Add(alert);
                                        }
                                    }
                                }
                            }

                            await Settings.SaveWeatherAlerts(Settings.HomeData, weatherData.weather_alerts);

                            await Settings.SaveWeatherData(weatherData);

                            Settings.UpdateTime = weatherData.update_time.UtcDateTime;

                            // Send callback to receiver
                            LocalBroadcastManager.GetInstance(this).SendBroadcast(
                                new Intent(WearableHelper.WeatherPath));

                            // Update complications
                            WeatherComplicationIntentService.EnqueueWork(this,
                                                                         new Intent(this, typeof(WeatherComplicationIntentService))
                                                                         .SetAction(WeatherComplicationIntentService.ACTION_UPDATECOMPLICATIONS)
                                                                         .PutExtra(WeatherComplicationIntentService.EXTRA_FORCEUPDATE, true));
                        }
                    }
                }
            }
        }
            public void OnSharedPreferenceChanged(ISharedPreferences sharedPreferences, string key)
            {
                if (String.IsNullOrWhiteSpace(key))
                {
                    return;
                }

                var context = Application.Context;

                switch (key)
                {
                // Weather Provider changed
                case KEY_API:
                    WeatherData.WeatherManager.GetInstance().UpdateAPI();
#if !__ANDROID_WEAR__
                    context.StartService(
                        new Intent(context, typeof(WearableDataListenerService))
                        .SetAction(WearableDataListenerService.ACTION_SENDSETTINGSUPDATE));
#endif
                    goto case KEY_USECELSIUS;

                // FollowGPS changed
                case KEY_FOLLOWGPS:
#if !__ANDROID_WEAR__
                    context.StartService(
                        new Intent(context, typeof(WearableDataListenerService))
                        .SetAction(WearableDataListenerService.ACTION_SENDSETTINGSUPDATE));
                    context.StartService(
                        new Intent(context, typeof(WearableDataListenerService))
                        .SetAction(WearableDataListenerService.ACTION_SENDLOCATIONUPDATE));
#endif
                    goto case KEY_USECELSIUS;

                // Settings unit changed
                case KEY_USECELSIUS:
#if __ANDROID_WEAR__
                    WeatherComplicationIntentService.EnqueueWork(context,
                                                                 new Intent(context, typeof(WeatherComplicationIntentService))
                                                                 .SetAction(WeatherComplicationIntentService.ACTION_UPDATECOMPLICATIONS)
                                                                 .PutExtra(WeatherComplicationIntentService.EXTRA_FORCEUPDATE, true));
                    break;
#else
                    WeatherWidgetService.EnqueueWork(context, new Intent(context, typeof(WeatherWidgetService))
                                                     .SetAction(WeatherWidgetService.ACTION_UPDATEWEATHER));
                    break;

                // Refresh interval changed
                case KEY_REFRESHINTERVAL:
                    WeatherWidgetService.EnqueueWork(context, new Intent(context, typeof(WeatherWidgetService))
                                                     .SetAction(WeatherWidgetService.ACTION_UPDATEALARM));
                    break;
#endif
#if __ANDROID_WEAR__
                // Data sync setting changed
                case KEY_DATASYNC:
                    // Reset UpdateTime value to force a refresh
                    UpdateTime = DateTime.MinValue;
                    break;
#endif
                default:
                    break;
                }
            }