Ejemplo n.º 1
0
        /// <summary>
        /// 更新布局,获取最新资讯
        /// </summary>
        /// <param name="channel">频道数据</param>
        /// <returns></returns>
        public async Task UpdateLayout(Channel channel, bool isForceRefresh = false)
        {
            AllFeeds.Clear();
            LastCacheTimeContainer.Visibility = Visibility.Collapsed;
            LoadingRing.IsActive           = true;
            AllReadButton.Visibility       = Visibility.Collapsed;
            JustNoReadSwitch.IsEnabled     = false;
            NoDataTipContainer.Visibility  = Visibility.Collapsed;
            AllReadTipContainer.Visibility = Visibility.Collapsed;
            _sourceData = channel;
            ChannelDescriptionTextBlock.Text = _sourceData.Description;
            ChannelNameTextBlock.Text        = _sourceData.Name;
            FeedCollection.Clear();
            var feed = new List <RssSchema>();

            if (NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable)
            {
                gg : if (!isForceRefresh)
                {
                    if (MainPage.Current.TempCache.Count > 0 && MainPage.Current.TempCache.Any(c => c.Channel.Id == channel.Id))
                    {
                        feed = MainPage.Current.TempCache.Where(c => c.Channel.Id == channel.Id).FirstOrDefault()?.Feeds;
                        if (feed.Count == 0)
                        {
                            isForceRefresh = true;
                            goto gg;
                        }
                    }
                    else
                    {
                        var data = await IOTools.GetLocalCache(channel);

                        feed = data.Item1;
                        int cacheTime = data.Item2;
                        int now       = AppTools.DateToTimeStamp(DateTime.Now.ToLocalTime());
                        if (feed.Count == 0 || now > cacheTime + 1200)
                        {
                            isForceRefresh = true;
                            goto gg;
                        }
                        else
                        {
                            if (cacheTime > 0)
                            {
                                LastCacheTimeContainer.Visibility = Visibility.Visible;
                                LastCacheTimeBlock.Text           = AppTools.TimeStampToDate(cacheTime).ToString("HH:mm");
                            }
                        }
                    }
                }
                else
                {
                    feed = await AppTools.GetFeedsFromUrl(_sourceData.Link);

                    bool isAutoCache = Convert.ToBoolean(AppTools.GetLocalSetting(AppSettings.AutoCacheWhenOpenChannel, "False"));
                    if (isAutoCache && feed.Count > 0)
                    {
                        await IOTools.AddCacheChannel(null, channel);
                    }
                }
            }
            else
            {
                if (MainPage.Current._isCacheAlert)
                {
                    new PopupToast(AppTools.GetReswLanguage("Tip_WatchingCache")).ShowPopup();
                    MainPage.Current._isCacheAlert = false;
                }
                var data = await IOTools.GetLocalCache(channel);

                feed = data.Item1;
                int cacheTime = data.Item2;
                if (cacheTime > 0)
                {
                    LastCacheTimeContainer.Visibility = Visibility.Visible;
                    LastCacheTimeBlock.Text           = AppTools.TimeStampToDate(cacheTime).ToString("HH:mm");
                }
            }
            if (feed != null && feed.Count > 0)
            {
                AllFeeds = feed;
                await FeedInit();
            }
            else
            {
                NoDataTipContainer.Visibility = Visibility.Visible;
            }
            JustNoReadSwitch.IsEnabled = true;
            LoadingRing.IsActive       = false;
        }