private async Task Init() { foreach (var item in Cities) { if (item.data != null && item.data.Length > 31) { var weather = HeWeatherModel.Generate(item.data, settings.Preferences.DataSource); if (weather == null || weather.DailyForecast == null || weather.HourlyForecast == null) { return; } await itemInit(item, weather); item.Updated = true; item.Succeed = true; } } if (settings.Cities.EnableLocate && settings.Cities.LocatedCity != null) { CurrentIndex = settings.Cities.CurrentIndex + 1; } else { CurrentIndex = settings.Cities.CurrentIndex; } }
public async static Task <HeWeather.Location> ReGeobyIpAsync() { var ipUrl = "http://api.ip138.com/query/?token={0}"; var result = await ApiRequestHelper.RequestWithFormattedUrlAsync(ipUrl, new string[] { Key.ip138 }); if (result == null) { return(null); } var ipRes = JsonHelper.FromJson <IpContract>(result); var city = await Request.RequestbyIpAsync(ipRes.ip); var fetchresult = HeWeatherModel.Generate(city, DataSource.HeWeather); return(fetchresult.Location); }
private async Task Complete(CityViewModel item) { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, new DispatchedHandler(async() => { if (item.data != null && item.data.Length > 31) { var weather = HeWeatherModel.Generate(item.data, settings.Preferences.DataSource); if (weather == null || weather.DailyForecast == null || weather.HourlyForecast == null) { return; } await itemInit(item, weather); item.Updated = true; item.Succeed = true; item.LastUpdate = DateTime.Now; } })); }
internal async void Pin(IList <object> selectedItems) { if (selectedItems == null || selectedItems.Count == 0) { return; } foreach (CityViewModel item in selectedItems) { SecondaryTile s = new SecondaryTile(item.Id, item.City, item.Id, new Uri("ms-appx:///Assets/Square150x150Logo.png"), TileSize.Default); var elem = s.VisualElements; elem.ShowNameOnSquare150x150Logo = true; elem.ShowNameOnSquare310x310Logo = true; elem.ShowNameOnWide310x150Logo = true; elem.Square310x310Logo = new Uri("ms-appx:///Assets/Square310x310Logo.png"); elem.Square44x44Logo = new Uri("ms-appx:///Assets/Square44x44Logo.png"); elem.Square71x71Logo = new Uri("ms-appx:///Assets/Square71x71Logo.png"); elem.Wide310x150Logo = new Uri("ms-appx:///Assets/Wide310x150Logo.png"); await s.RequestCreateAsync(); var index = Array.FindIndex(settings.Cities.SavedCities, x => { return(x.Id == item.Id); }); CitySettingsModel currentCity; if (index == -1 && settings.Cities.LocatedCity != null) { settings.Cities.LocatedCity.IsPinned = true; currentCity = settings.Cities.LocatedCity; } else { settings.Cities.SavedCities[index].IsPinned = true; currentCity = settings.Cities.SavedCities[index]; } var t = ThreadPool.RunAsync(async(work) => { string resstr; try { resstr = await settings.Cities.ReadDataAsync(item.Id.IsNullorEmpty() ? item.City : item.Id, settings.Preferences.DataSource); } catch (Exception) { resstr = await Request.GetRequestAsync(settings, item.Id, item.City, item.longitude, item.latitude, item.zmw); } if (!resstr.IsNullorEmpty()) { var weather = HeWeatherModel.Generate(resstr, settings.Preferences.DataSource); if (weather == null || weather.DailyForecast == null || weather.HourlyForecast == null) { return; } var utcOffset = weather.Location.UpdateTime - weather.Location.UtcTime; var current = DateTimeHelper.ReviseLoc(utcOffset); var todayIndex = Array.FindIndex(weather.DailyForecast, x => { return(x.Date.Date == current.Date); }); var hourIndex = Array.FindIndex(weather.HourlyForecast, x => { return((x.DateTime - current).TotalSeconds > 0); }); if (todayIndex < 0) { todayIndex = 0; } if (hourIndex < 0) { hourIndex = 0; } var isNight = Generator.CalcIsNight(weather.Location.UpdateTime, weather.DailyForecast[todayIndex].SunRise, weather.DailyForecast[todayIndex].SunSet, new Models.Location(currentCity.Latitude, currentCity.Longitude)); var glanceFull = Glance.GenerateGlanceDescription(weather, isNight, settings.Preferences.TemperatureParameter, DateTime.Now); var glance = Glance.GenerateShortDescription(weather, isNight); var uri = await settings.Immersive.GetCurrentBackgroundAsync(weather.NowWeather.Now.Condition, isNight); Sender.CreateSubTileNotification(await Generator.CreateAll(currentCity, weather, current), item.Id); } }); } }
private async Task Init(SettingsModel settings, CitySettingsModel currentCity) { string resstr = await Request.GetRequestAsync(settings, currentCity); if (!resstr.IsNullorEmpty()) { var fetchresult = HeWeatherModel.Generate(resstr, settings.Preferences.DataSource); if (fetchresult == null || fetchresult.DailyForecast == null || fetchresult.HourlyForecast == null) { return; } var utcOffset = fetchresult.Location.UpdateTime - fetchresult.Location.UtcTime; var current = DateTimeHelper.ReviseLoc(utcOffset); try { Sender.CreateMainTileQueue(await Generator.CreateAll(currentCity, fetchresult, current)); } catch (Exception) { } try { if (UserProfilePersonalizationSettings.IsSupported() && settings.Preferences.SetWallPaper) { var todayIndex = Array.FindIndex(fetchresult.DailyForecast, x => { return(x.Date.Date == current.Date); }); if (current.Hour <= 7) { todayIndex--; } if (todayIndex < 0) { todayIndex = 0; } TimeSpan sunRise, sunSet; if (fetchresult.DailyForecast[todayIndex].SunRise == default(TimeSpan) || fetchresult.DailyForecast[todayIndex].SunSet == default(TimeSpan)) { sunRise = Core.LunarCalendar.SunRiseSet.GetRise(new Models.Location(currentCity.Latitude, currentCity.Longitude), current); sunSet = Core.LunarCalendar.SunRiseSet.GetSet(new Models.Location(currentCity.Latitude, currentCity.Longitude), current); } else { sunRise = fetchresult.DailyForecast[todayIndex].SunRise; sunSet = fetchresult.DailyForecast[todayIndex].SunSet; } var file = await FileIOHelper.GetFilebyUriAsync(await settings.Immersive.GetCurrentBackgroundAsync(fetchresult.NowWeather.Now.Condition, WeatherModel.CalculateIsNight(current, sunRise, sunSet))); var lFile = await FileIOHelper.CreateWallPaperFileAsync(Guid.NewGuid().ToString() + ".png"); var d = PointerDevice.GetPointerDevices(); var m = d.ToArray(); var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; var size = new Size(m[0].PhysicalDeviceRect.Width, m[0].PhysicalDeviceRect.Height); var ratio = size.Height / size.Width; var cropSize = new Size(); double scale; var startPoint = new Point(); using (var stream = await file.OpenReadAsync()) { var bitmap = new BitmapImage(); await bitmap.SetSourceAsync(stream); var width = bitmap.PixelWidth; var height = bitmap.PixelHeight; var center = new Point(width / 2, height / 2); if (width * ratio >= height) { cropSize.Width = height / ratio; cropSize.Height = height; scale = size.Height / height; } else { cropSize.Width = width; cropSize.Height = width * ratio; scale = size.Width / width; } startPoint.X = center.X - cropSize.Width / 2; startPoint.Y = center.Y - cropSize.Height / 2; } await ImagingHelper.CropandScaleAsync(file, lFile, startPoint, cropSize, scale); var uc = await ImagingHelper.SetWallpaperAsync(lFile); } } catch (Exception) { } if (settings.Preferences.EnableMorning) { var shu1 = settings.Preferences.MorningNoteTime.TotalHours; var tomorrow8 = DateTime.Now.Hour > shu1 ? (DateTime.Today.AddDays(1)).AddHours(shu1) : (DateTime.Today.AddHours(shu1)); try { Sender.CreateScheduledToastNotification(await(Generator.CreateToast(fetchresult, currentCity, settings, DateTimeHelper.ReviseLoc(tomorrow8, utcOffset))), tomorrow8, "MorningToast"); } catch (Exception) { } } if (settings.Preferences.EnableEvening) { var shu2 = settings.Preferences.EveningNoteTime.TotalHours; var tomorrow20 = DateTime.Now.Hour > shu2 ? (DateTime.Today.AddDays(1)).AddHours(shu2) : (DateTime.Today.AddHours(shu2)); try { Sender.CreateScheduledToastNotification(await(Generator.CreateToast(fetchresult, currentCity, settings, DateTimeHelper.ReviseLoc(tomorrow20, utcOffset))), tomorrow20, "EveningToast"); } catch (Exception) { } } if (settings.Preferences.EnableAlarm) { if (!fetchresult.Alarms.IsNullorEmpty() && (DateTime.Now - settings.Preferences.LastAlertTime).TotalDays > 1) { Sender.CreateBadge(Generator.GenerateAlertBadge()); Sender.CreateToast(Generator.CreateAlertToast(fetchresult, currentCity).GetXml()); } else { Sender.ClearBadge(); } var str = Generator.CalculateWeatherAlarm(fetchresult, currentCity, settings, DateTimeHelper.ReviseLoc(DateTime.Now, utcOffset)); if (!str.IsNullorEmpty() && str.Length > 1 && (DateTime.Now - settings.Preferences.LastAlarmTime).TotalDays > 1) { Sender.CreateToast(Generator.CreateAlarmToast(str, currentCity).GetXml()); } } await settings.Cities.SaveDataAsync(currentCity.Id.IsNullorEmpty()?currentCity.City : currentCity.Id, resstr, settings.Preferences.DataSource); currentCity.Update(); if (settings.Cities.CurrentIndex != -1) { settings.Cities.SavedCities[settings.Cities.CurrentIndex] = currentCity; } else { settings.Cities.LocatedCity = currentCity; } settings.Cities.Save(); } }
private async Task FetchDataAsync() { await SearchExistingDataAsync(); string resstr; if (currentCityModel.Id != null) { try { if (!storedDatas.IsNullorEmpty()) { resstr = storedDatas; fetchresult = HeWeatherModel.Generate(resstr, settings.Preferences.DataSource); return; } resstr = await Core.Models.Request.GetRequestAsync(settings, currentCityModel); if (resstr.IsNullorEmpty()) { await Task.Delay(1000); this.OnFetchDataFailed(this, new FetchDataFailedEventArgs("Network_Error")); return; } fetchresult = HeWeatherModel.Generate(resstr, settings.Preferences.DataSource); if (fetchresult.Status != HeWeatherStatus.ok) { await Task.Delay(1000); this.OnFetchDataFailed(this, new FetchDataFailedEventArgs("Service_Unavailable")); return; } var task = ThreadPool.RunAsync(async(work) => { await settings.Cities.SaveDataAsync(currentCityModel.Id.IsNullorEmpty() ? currentCityModel.City : currentCityModel.Id, resstr, settings.Preferences.DataSource); currentCityModel.Update(); if (settings.Cities.CurrentIndex != -1) { settings.Cities.SavedCities[settings.Cities.CurrentIndex] = currentCityModel; } else { settings.Cities.LocatedCity = currentCityModel; } settings.Cities.Save(); }); return; } catch (Exception) { await Task.Delay(1000); this.OnFetchDataFailed(this, new FetchDataFailedEventArgs("Service_Unavailable")); return; } } else { throw new NullReferenceException(); } }