Esempio n. 1
0
        private async void CacheAllButton_Click(object sender, RoutedEventArgs e)
        {
            if (!NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable)
            {
                new PopupToast(AppTools.GetReswLanguage("Tip_FailedWithoutInternet"), AppTools.GetThemeSolidColorBrush(ColorType.ErrorColor)).ShowPopup();
                return;
            }
            CacheAllButton.IsEnabled    = false;
            CacheProgressBar.Visibility = Visibility.Visible;

            var list = new List <Channel>();

            foreach (var item in MainPage.Current.Categories)
            {
                foreach (var cha in item.Channels)
                {
                    list.Add(cha);
                }
            }
            var pageList = MainPage.Current.CustomPages;

            if (list.Count > 0)
            {
                try
                {
                    CacheProgressBar.Maximum = list.Count + pageList.Count;
                    int channelCount = 0;
                    int pageCount    = 0;
                    var tasks        = new Task[2];
                    tasks[0] = Task.Run(async() => {
                        await IOTools.AddCacheChannel(async(count) =>
                        {
                            await DispatcherHelper.ExecuteOnUIThreadAsync(() =>
                            {
                                channelCount           = count;
                                CacheProgressBar.Value = channelCount + pageCount;
                            });
                        }, list.ToArray());
                    });
                    tasks[1] = Task.Run(async() => {
                        await IOTools.AddCachePage(async(count) =>
                        {
                            await DispatcherHelper.ExecuteOnUIThreadAsync(() =>
                            {
                                pageCount = count;
                                CacheProgressBar.Value = channelCount + pageCount;
                            });
                        }, pageList.ToArray());
                    });
                    await Task.WhenAll(tasks);

                    new PopupToast(AppTools.GetReswLanguage("Tip_CacheSuccess")).ShowPopup();
                }
                catch (Exception ex)
                {
                    new PopupToast(ex.Message).ShowPopup();
                }
            }
            CacheSizeTextBlock.Text = await IOTools.GetCacheSize();

            CacheAllButton.IsEnabled    = true;
            CacheProgressBar.Visibility = Visibility.Collapsed;
        }
Esempio n. 2
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;
        }