Skip to content

Commit

Permalink
Bump v0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
ProJend committed Apr 10, 2024
1 parent 458b582 commit 5e75ba3
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 61 deletions.
51 changes: 22 additions & 29 deletions TrueLove.Lib/Models/Code/CommentCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,12 +19,28 @@ public class CommentItem
public class CommentCollection : ObservableCollection<CommentItem>, ISupportIncrementalLoading
{
public ObservableCollection<CommentItem> CommentColl;
int _pageNum = 1;

public async void LoadMoreItemsManuallyAsync()
public async Task<bool> 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);
Expand All @@ -37,7 +52,7 @@ public async void LoadMoreItemsManuallyAsync()

public Windows.Foundation.IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count) => AsyncInfo.Run(c => LoadMoreItemsAsyncCore(c, count));

public bool HasMoreItems => false;
public bool HasMoreItems => Count < 100;

async Task<LoadMoreItemsResult> LoadMoreItemsAsyncCore(CancellationToken cancel, uint count)
{
Expand All @@ -52,30 +67,9 @@ async Task<LoadMoreItemsResult> 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);
Expand All @@ -92,6 +86,5 @@ async Task<LoadMoreItemsResult> LoadMoreItemsAsyncCore(CancellationToken cancel,
public event EventHandler LoadMoreEnd;

int _pageNumber = 0;
int _times = 0;
}
}
4 changes: 2 additions & 2 deletions TrueLove.Lib/Models/Code/ImageCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down Expand Up @@ -75,7 +75,7 @@ async Task<LoadMoreItemsResult> 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++);
Expand Down
16 changes: 11 additions & 5 deletions TrueLove.Lib/Spider/RefineStream.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<CommentItem> 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";
Expand Down
22 changes: 10 additions & 12 deletions TrueLove.Lib/Spider/ReviewStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,19 +25,17 @@ public string GetStream(string path, bool isFirstLanuch = false)
/// </summary>
/// <param name="path">Use URL address</param>
/// <param name="isLocalFile">switch to use website address or local document address</param>
public async Task<string> GetStreamAsync(string path, bool isLocalFile = false)
public async Task<string> 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)
{
Expand Down
2 changes: 1 addition & 1 deletion TrueLove.UWP/Package.appxmanifest
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" IgnorableNamespaces="uap mp">
<Identity Name="AviciiUWP" Publisher="CN=Admin" Version="0.5.0.0" />
<Identity Name="AviciiUWP" Publisher="CN=Admin" Version="0.6.0.0" />
<mp:PhoneIdentity PhoneProductId="cf0148d5-01bb-4b60-b53b-da3346bf8e5e" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
<DisplayName>ms-resource:pPackageDisplayName</DisplayName>
Expand Down
6 changes: 4 additions & 2 deletions TrueLove.UWP/Views/CommentsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<Grid.Background>
<ImageBrush
ImageSource="/Assets/Instagram/4.jpg"
Expand All @@ -88,6 +89,7 @@
Margin="0,0,19,13"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Opacity="0"
Style="{StaticResource GhostButton}">
Leave a Memory
</Button>
Expand Down Expand Up @@ -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}">
<GridView.ItemsPanel>
Expand Down
11 changes: 7 additions & 4 deletions TrueLove.UWP/Views/CommentsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -38,7 +38,7 @@ private void OnWindowActivated(object sender, WindowActivatedEventArgs e) => Vis
/// 检查工具栏相关的按钮可用状态。
/// 下滑隐藏工具栏 https://www.cnblogs.com/lonelyxmas/p/9919869.html
/// </summary>
private void Scroller_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
private async void Scroller_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
scrlocation = Scroller.VerticalOffset;

Expand All @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions TrueLove.UWP/Views/ExtendedSplash.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
HorizontalAlignment="Center" />
</Canvas>
<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Margin=" 0 60">
<TextBlock Style="{ThemeResource SubheaderTextBlockStyle}" TextAlignment="Center">Initiation...</TextBlock>
<TextBlock Style="{ThemeResource BodyTextBlockStyle}" TextAlignment="Center">Make Sure Network Connecting</TextBlock>
<TextBlock Style="{ThemeResource SubheaderTextBlockStyle}" TextAlignment="Center">Preparing...</TextBlock>
<TextBlock Style="{ThemeResource BodyTextBlockStyle}" TextAlignment="Center">Make sure the network is connected.</TextBlock>
</StackPanel>
</Grid>
</Page>
10 changes: 6 additions & 4 deletions TrueLove.UWP/Views/ExtendedSplash.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 5e75ba3

Please sign in to comment.