diff --git a/TrueLove.Lib/Models/Code/CommentCollection.cs b/TrueLove.Lib/Models/Code/CommentCollection.cs index f322f43..75d5557 100644 --- a/TrueLove.Lib/Models/Code/CommentCollection.cs +++ b/TrueLove.Lib/Models/Code/CommentCollection.cs @@ -5,7 +5,6 @@ using System.Threading; using System.Threading.Tasks; using TrueLove.Lib.Spider; -using Windows.Storage; using Windows.UI.Xaml.Data; namespace TrueLove.Lib.Models.Code @@ -20,12 +19,28 @@ public class CommentItem public class CommentCollection : ObservableCollection, ISupportIncrementalLoading { public ObservableCollection CommentColl; + int _pageNum = 1; - public async void LoadMoreItemsManuallyAsync() + public async Task LoadMoreItemsManuallyAsync() { try { - var refineStream = new RefineStream(); + var refineStream = new RefineStream(_pageNum++); + for (int i = 1; i <= 99; i++) + { + var singleItme = await refineStream.RefineComment(i); + Add(singleItme); + } + } + catch { }; + return false; + } + + public async void LoadMoreItemsManually() + { + try + { + var refineStream = new RefineStream(_pageNum++); for (int i = 1; i <= 99; i++) { var singleItme = await refineStream.RefineComment(i); @@ -37,7 +52,7 @@ public async void LoadMoreItemsManuallyAsync() public Windows.Foundation.IAsyncOperation LoadMoreItemsAsync(uint count) => AsyncInfo.Run(c => LoadMoreItemsAsyncCore(c, count)); - public bool HasMoreItems => false; + public bool HasMoreItems => Count < 100; async Task LoadMoreItemsAsyncCore(CancellationToken cancel, uint count) { @@ -52,30 +67,9 @@ async Task LoadMoreItemsAsyncCore(CancellationToken cancel, else { // 向集合中添加指定项 - for (uint n = 0; n < count; n++) - { - var reviewStream = new ReviewStream(); - string _src = "null"; - if (_pageNumber == 0) - { - _src = await reviewStream.GetStreamAsync(ApplicationData.Current.LocalFolder.Path + $"/OfflineData.txt"); - } - else if (NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable) - { - _src = await reviewStream.GetStreamAsync($"https://avicii.com/page/{_pageNumber}"); - } - var refineStream = new RefineStream(); - try - { - var singleItme = await refineStream.RefineComment(_times++); - Add(singleItme); - } - catch (Exception) - { - _pageNumber++; - _times = 0; - } - } + var refineStream = new RefineStream(_pageNumber++); + var singleItme = await refineStream.RefineComment(1); + Add(singleItme); } // 完成加载 LoadMoreEnd?.Invoke(this, EventArgs.Empty); @@ -92,6 +86,5 @@ async Task LoadMoreItemsAsyncCore(CancellationToken cancel, public event EventHandler LoadMoreEnd; int _pageNumber = 0; - int _times = 0; } } diff --git a/TrueLove.Lib/Models/Code/ImageCollection.cs b/TrueLove.Lib/Models/Code/ImageCollection.cs index f22f699..9f53763 100644 --- a/TrueLove.Lib/Models/Code/ImageCollection.cs +++ b/TrueLove.Lib/Models/Code/ImageCollection.cs @@ -34,7 +34,7 @@ public async void LoadMoreItemsManuallyAsync(bool isRefreshing = false) var reviewStream = new ReviewStream(); _src = await reviewStream.GetStreamAsync($"https://avicii.com/page/{_pageNumber}"); } - var refineStream = new RefineStream(); + var refineStream = new RefineStream(_pageNumber++); try { for (int i = 1; i <= _times; i++) @@ -75,7 +75,7 @@ async Task LoadMoreItemsAsyncCore(CancellationToken cancel, { _src = await reviewStream.GetStreamAsync($"https://avicii.com/page/{_pageNumber}"); } - var refineStream = new RefineStream(); + var refineStream = new RefineStream(_pageNumber++); try { var singleItme = await refineStream.RefineImage(_src, _times++); diff --git a/TrueLove.Lib/Spider/RefineStream.cs b/TrueLove.Lib/Spider/RefineStream.cs index 610f94d..bb12378 100644 --- a/TrueLove.Lib/Spider/RefineStream.cs +++ b/TrueLove.Lib/Spider/RefineStream.cs @@ -1,6 +1,7 @@ using HtmlAgilityPack; using Microsoft.Toolkit.Uwp.Connectivity; using System; +using System.Diagnostics; using System.Threading.Tasks; using TrueLove.Lib.Models.Code; using Windows.Storage; @@ -11,19 +12,24 @@ namespace TrueLove.Lib.Spider public class RefineStream { string _src; - int _pageNumber = 1; - HtmlDocument htmlDocument = new HtmlDocument(); - public RefineStream() + + public RefineStream(int pageNumber) => RefineStreamAsync(pageNumber); + + private async void RefineStreamAsync(int _pageNumber) { var reviewStream = new ReviewStream(); - _src = reviewStream.GetStream(ApplicationData.Current.LocalFolder.Path + @"/OfflineData.txt", _pageNumber == 1); - htmlDocument.LoadHtml(_src); + _src = reviewStream.GetStream(ApplicationData.Current.LocalFolder.Path + @"/OfflineData.txt"); + Debug.WriteLine(ApplicationData.Current.LocalFolder.Path + @"/OfflineData.txt"); + if (NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable) + await reviewStream.GetStreamAsync($"https://avicii.com/page/{_pageNumber++}"); } public Task RefineComment(int ID) { var singleComment = new CommentItem(); + var htmlDocument = new HtmlDocument(); + htmlDocument.LoadHtml(_src); string namePath = $"//*[@id=\"comments\"]/ul[2]/li[{ID}]/div/strong"; string comPath = $"//*[@id=\"comments\"]/ul[2]/li[{ID}]/div/p"; diff --git a/TrueLove.Lib/Spider/ReviewStream.cs b/TrueLove.Lib/Spider/ReviewStream.cs index 71b890b..02058ba 100644 --- a/TrueLove.Lib/Spider/ReviewStream.cs +++ b/TrueLove.Lib/Spider/ReviewStream.cs @@ -10,11 +10,11 @@ namespace TrueLove.Lib.Spider { public class ReviewStream { - public string GetStream(string path, bool isFirstLanuch = false) + public string GetStream(string path) { - stream = File.ReadAllText(path); - if (isFirstLanuch) + if (File.Exists(path)) { + stream = File.ReadAllText(path); TimeToNow(); } return stream; @@ -25,19 +25,17 @@ public string GetStream(string path, bool isFirstLanuch = false) /// /// Use URL address /// switch to use website address or local document address - public async Task GetStreamAsync(string path, bool isLocalFile = false) + public async Task GetStreamAsync(string path) { try { - using (var httpClient = new HttpClient()) - { - stream = await httpClient.GetStringAsync(new Uri(path)); + using var httpClient = new HttpClient(); + stream = await httpClient.GetStringAsync(new Uri(path)); - var localFolder = ApplicationData.Current.LocalFolder; - var file = await localFolder.CreateFileAsync("OfflineData.txt", - CreationCollisionOption.ReplaceExisting); - await FileIO.AppendTextAsync(file, stream); - } + var localFolder = ApplicationData.Current.LocalFolder; + var file = await localFolder.CreateFileAsync("OfflineData.txt", + CreationCollisionOption.ReplaceExisting); + await FileIO.AppendTextAsync(file, stream); } catch (HttpRequestException) { diff --git a/TrueLove.UWP/Package.appxmanifest b/TrueLove.UWP/Package.appxmanifest index 039d53a..7a02b51 100644 --- a/TrueLove.UWP/Package.appxmanifest +++ b/TrueLove.UWP/Package.appxmanifest @@ -1,6 +1,6 @@  - + ms-resource:pPackageDisplayName diff --git a/TrueLove.UWP/Views/CommentsPage.xaml b/TrueLove.UWP/Views/CommentsPage.xaml index df1c748..bc5bad7 100644 --- a/TrueLove.UWP/Views/CommentsPage.xaml +++ b/TrueLove.UWP/Views/CommentsPage.xaml @@ -70,7 +70,8 @@ x:Name="Forebody" Padding="0,40,0,0" VerticalAlignment="Top" - DoubleTapped="Forebody_DoubleTapped"> + DoubleTapped="Forebody_DoubleTapped" + ToolTipService.ToolTip="Double-click to back to top of page"> Leave a Memory @@ -119,7 +121,7 @@ x:Name="CommentView" Margin="0,60,0,0" animations:ReorderGridAnimation.Duration="250" - ItemContainerStyle="{StaticResource GridViewItemNoneStyle}" + ItemContainerStyle="{StaticResource GridViewItemStretchStyle}" ItemTemplate="{StaticResource CommentItemTemplate}" ItemsSource="{x:Bind CommentCollection}"> diff --git a/TrueLove.UWP/Views/CommentsPage.xaml.cs b/TrueLove.UWP/Views/CommentsPage.xaml.cs index b3c4349..79112e3 100644 --- a/TrueLove.UWP/Views/CommentsPage.xaml.cs +++ b/TrueLove.UWP/Views/CommentsPage.xaml.cs @@ -26,7 +26,7 @@ public CommentsPage() protected override void OnNavigatedFrom(NavigationEventArgs e) => Window.Current.Activated -= OnWindowActivated; private void CompositionTarget_Rendered(object sender, RenderedEventArgs e) { - CommentCollection.LoadMoreItemsManuallyAsync(); + CommentCollection.LoadMoreItemsManually(); CompositionTarget.Rendered -= CompositionTarget_Rendered; } @@ -38,7 +38,7 @@ private void OnWindowActivated(object sender, WindowActivatedEventArgs e) => Vis /// 检查工具栏相关的按钮可用状态。 /// 下滑隐藏工具栏 https://www.cnblogs.com/lonelyxmas/p/9919869.html /// - private void Scroller_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e) + private async void Scroller_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e) { scrlocation = Scroller.VerticalOffset; @@ -58,11 +58,14 @@ private void Scroller_ViewChanged(object sender, ScrollViewerViewChangedEventArg BackSubTitle.Opacity = 1; } - if ((Scroller.ScrollableHeight - scrlocation) is >= 800 and <= 810) + if (Scroller.ScrollableHeight - Scroller.VerticalOffset <= 500) { - CommentCollection.LoadMoreItemsManuallyAsync(); + if (_isLoading) return; + _isLoading = true; + _isLoading = await CommentCollection.LoadMoreItemsManuallyAsync(); } } + bool _isLoading; CommentCollection CommentCollection = new CommentCollection(); diff --git a/TrueLove.UWP/Views/ExtendedSplash.xaml b/TrueLove.UWP/Views/ExtendedSplash.xaml index b92045a..8b9ff86 100644 --- a/TrueLove.UWP/Views/ExtendedSplash.xaml +++ b/TrueLove.UWP/Views/ExtendedSplash.xaml @@ -18,8 +18,8 @@ HorizontalAlignment="Center" /> - Initiation... - Make Sure Network Connecting + Preparing... + Make sure the network is connected. diff --git a/TrueLove.UWP/Views/ExtendedSplash.xaml.cs b/TrueLove.UWP/Views/ExtendedSplash.xaml.cs index 76ca586..04cab23 100644 --- a/TrueLove.UWP/Views/ExtendedSplash.xaml.cs +++ b/TrueLove.UWP/Views/ExtendedSplash.xaml.cs @@ -5,7 +5,6 @@ using Windows.ApplicationModel.Activation; using Windows.UI.Core; using Microsoft.Toolkit.Uwp.Connectivity; -using TrueLove.Lib.Spider; using Windows.Storage; using System.Threading.Tasks; using System.IO; @@ -14,6 +13,7 @@ using Windows.ApplicationModel.Core; using Windows.UI.ViewManagement; using Windows.UI; +using System.Net.Http; // The Blank Page item template is documented at https://go.microsoft.com/fwlink/p/?LinkID=234238 @@ -113,17 +113,19 @@ async void CatchStream() var path = ApplicationData.Current.LocalFolder.Path + @"/OfflineData.txt"; do { - await Task.Delay(0); // 使初始画面活动起来 + await Task.Delay(1); // 使初始画面活动起来 var isInternetAvailable = NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable; if (isInternetAvailable) { splashProgressRing.IsActive = true; + using var httpClient = new HttpClient(); + var stream = await httpClient.GetStringAsync("https://avicii.com/page/11"); + var localFolder = ApplicationData.Current.LocalFolder; var file = await localFolder.CreateFileAsync("OfflineData.txt", CreationCollisionOption.ReplaceExisting); - var _src = await new ReviewStream().GetStreamAsync($"https://avicii.com/page/11"); - await FileIO.AppendTextAsync(file, _src); + await FileIO.AppendTextAsync(file, stream); } } while (!File.Exists(path));