/// <summary> /// 更新头条文章的磁贴tile /// </summary> /// <param name="t"></param> private static void UpdateTile(LatestStories t) { //头5条 循环更新到tile中 List<string> list = t.Top_Stories.ToList().Select(s => s.Title).ToList(); List<string> list_image = t.Top_Stories.ToList().Select(s => s.Image).ToList(); var tileUpdater = TileUpdateManager.CreateTileUpdaterForApplication(); var plannedNotifications = tileUpdater.GetScheduledTileNotifications(); DateTime now = DateTime.Now; DateTime updateTime = now; DateTime planTill = now.AddMinutes(60); //每次制定1小时的tile更新计划 //删除还未执行的计划 if (plannedNotifications.Count > 0) { plannedNotifications.ToList().ForEach(s => tileUpdater.RemoveFromSchedule(s)); } int index = 0; int count = list.Count; //制定tile更新的计划 for (var startPlanning = updateTime; startPlanning < planTill; startPlanning = startPlanning.AddSeconds(10)) { try { var tilexml = TileContentFactory.CreateTileSquare150x150PeekImageAndText02(); tilexml.Image.Src = list_image[index % count]; tilexml.TextHeading.Text = "知乎日报UWP"; tilexml.TextBodyWrap.Text = list[index % count]; index++; ScheduledTileNotification stn = new ScheduledTileNotification(tilexml.GetXml(), startPlanning); stn.ExpirationTime = startPlanning.AddSeconds(8); tileUpdater.AddToSchedule(stn); } catch { } } }
/// <summary> /// 更新未读头条文章的徽章badge /// </summary> /// <param name="t"></param> private static void UpdateBadge(LatestStories t) { //将当天未读TOP文章更新到 badge int un_readed = 0; t.Top_Stories.ToList().ForEach(s => { if (!DataShareManager.Current.ReadedList.Contains(s.ID)) un_readed++; }); var updater = BadgeUpdateManager.CreateBadgeUpdaterForApplication(); if (un_readed != 0) { var badgexml = new BadgeNumericNotificationContent((uint)un_readed); var n = badgexml.CreateNotification(); n.ExpirationTime = DateTime.Now.AddDays(7); updater.Update(n); } else { updater.Clear(); } }
/// <summary> /// 主页最近文章 /// </summary> /// <returns></returns> public async Task<LatestStories> GetLatestStories() { try { if (NetworkManager.Current.Network == 4) { LatestStories l = await FileHelper.Current.ReadObjectAsync<LatestStories>("latest_stories.json"); return l; } else { JsonObject json = await GetJson(ServiceURL.LatestStories); if (json != null) { ObservableCollection<Story> stories_list = new ObservableCollection<Story>(); ObservableCollection<Story> top_stories_list = new ObservableCollection<Story>(); Story tmp; var stories = json["stories"]; WriteableBitmap wb = null; string image_ext = "jpg";string[] sitem = null; if (stories != null) { JsonArray ja = stories.GetArray(); JsonObject jo; string image; bool multiPic = false; foreach (var j in ja) { jo = j.GetObject(); multiPic = jo.ContainsKey("multipic") && (jo["multipic"].GetBoolean()); image = jo["images"].GetArray()[0].GetString(); tmp = new Story { Date = json["date"].GetString(), ID = jo["id"].GetNumber().ToString(), Image = image, Title = jo["title"].GetString(), MultiPic = multiPic }; if (!await FileHelper.Current.CacheExist(tmp.ID + "_story_image." + image_ext)) //没有缓存 { wb = await GetImage(tmp.Image); //下载图片 if (!tmp.Image.Equals("")) { sitem = tmp.Image.Split('.'); image_ext = sitem[sitem.Count() - 1]; } await FileHelper.Current.SaveImageAsync(wb, tmp.ID + "_story_image." + image_ext); //保存图片 } if (!tmp.Image.Equals("")) { tmp.Image = _local_path + "\\images_cache\\" + tmp.ID + "_story_image." + image_ext; //图片路径重定向 } //tmp.Favorite = DataShareManager.Current.FavoriteList.Contains(tmp.ID) ? true : false; //是否收藏 //tmp.Readed = DataShareManager.Current.ReadedList.Contains(tmp.ID) ? true : false; //是否已读 stories_list.Add(tmp); } } var top_stories = json["top_stories"]; if (top_stories != null) { JsonArray ja = top_stories.GetArray(); JsonObject jo; foreach (var j in ja) { jo = j.GetObject(); tmp = new Story { Date = json["date"].GetString(), Favorite = false, ID = jo["id"].GetNumber().ToString(), Image = jo["image"].GetString(), Title = jo["title"].GetString() }; if (!await FileHelper.Current.CacheExist(tmp.ID + "_story_top_image." + image_ext)) //没有缓存 { wb = await GetImage(tmp.Image); //下载图片 if (!tmp.Image.Equals("")) { sitem = tmp.Image.Split('.'); image_ext = sitem[sitem.Count() - 1]; } await FileHelper.Current.SaveImageAsync(wb, tmp.ID + "_story_top_image." + image_ext); //保存图片 } if (!tmp.Image.Equals("")) { tmp.Image = _local_path + "\\images_cache\\" + tmp.ID + "_story_top_image." + image_ext; //图片路径重定向 } top_stories_list.Add(tmp); } } LatestStories ls = new LatestStories { Date = json["date"].GetString(), Stories = stories_list, Top_Stories = top_stories_list }; await FileHelper.Current.WriteObjectAsync<LatestStories>(ls, "latest_stories.json"); return ls; } else { return null; } } } catch { return null; } }
/// <summary> /// 显示未读头条文章的toast /// </summary> /// <param name="t"></param> private static void ShowToast(LatestStories t) { if (!DataShareManager.Current.ShowToast) return; //将还未阅读的头条文章使用toast方式通知给用户 var toast_updater = ToastNotificationManager.CreateToastNotifier(); string xml = "<toast lang=\"zh-CN\" launch='111' >" + "<visual>" + "<binding template=\"ToastGeneric\">" + "<text>知乎日报UWP 头条文章</text>" + "<text>{0}</text>" + "</binding>" + "</visual>" + "</toast>"; t.Top_Stories.ToList().ForEach(s => { if (!DataShareManager.Current.ReadedList.Contains(s.ID) && !IsAlreadyToast(s.ID)) //未阅读 { // 加载XML文档 XmlDocument doc = new XmlDocument(); doc.LoadXml(string.Format(xml, s.Title)); // 显示通知 ToastNotification notification = new ToastNotification(doc); toast_updater.Show(notification); } }); }