Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
完成评论(目前不加载用户头像)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiichiamane committed Apr 23, 2019
1 parent e61425f commit f543bac
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 16 deletions.
12 changes: 6 additions & 6 deletions PixivFSUWP/Data/BookmarkIllustsCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,26 @@ public void ResumeLoading()

protected async Task<LoadMoreItemsResult> LoadMoreItemsAsync(CancellationToken c, uint count)
{
if (!HasMoreItems) return new LoadMoreItemsResult() { Count = 0 };
try
{
if (!HasMoreItems) return new LoadMoreItemsResult() { Count = 0 };
LoadMoreItemsResult toret = new LoadMoreItemsResult() { Count = 0 };
JsonValue recommendres = null;
JsonValue bookmarkres = null;
if (nexturl == "begin")
recommendres = await Task.Run(() => new PixivFS
bookmarkres = await Task.Run(() => new PixivFS
.PixivAppAPI(OverAll.GlobalBaseAPI)
.csfriendly_user_bookmarks_illust(userID));
else
{
Uri next = new Uri(nexturl);
string getparam(string param) => HttpUtility.ParseQueryString(next.Query).Get(param);
recommendres = await Task.Run(() => new PixivFS
bookmarkres = await Task.Run(() => new PixivFS
.PixivAppAPI(OverAll.GlobalBaseAPI)
.csfriendly_user_bookmarks_illust(userID, getparam("restrict"),
getparam("filter"), getparam("max_bookmark_id")));
}
nexturl = recommendres.Item("next_url").AsString();
foreach (var recillust in recommendres.Item("illusts").AsArray())
nexturl = bookmarkres.Item("next_url").AsString();
foreach (var recillust in bookmarkres.Item("illusts").AsArray())
{
if (_emergencyStop)
{
Expand Down
101 changes: 101 additions & 0 deletions PixivFSUWP/Data/CommentsCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using FSharp.Data;
using PixivFSCS;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Windows.Foundation;
using Windows.UI.Xaml.Data;

namespace PixivFSUWP.Data
{
public class CommentsCollection : ObservableCollection<ViewModels.CommentViewModel>, ISupportIncrementalLoading
{
string nexturl = "begin";
bool _busy = false;
bool _emergencyStop = false;
EventWaitHandle pause = new ManualResetEvent(true);
readonly string illustid;

public CommentsCollection(string IllustID) => illustid = IllustID;

public bool HasMoreItems
{
get => nexturl != "";
}

public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
if (_busy)
throw new InvalidOperationException("Only one operation in flight at a time");
_busy = true;
return AsyncInfo.Run((c) => LoadMoreItemsAsync(c, count));
}

public void StopLoading()
{
if (_busy)
{
_emergencyStop = true;
ResumeLoading();
}
}

public void PauseLoading()
{
pause.Reset();
}

public void ResumeLoading()
{
pause.Set();
}

protected async Task<LoadMoreItemsResult> LoadMoreItemsAsync(CancellationToken c, uint count)
{
try
{
if (!HasMoreItems) return new LoadMoreItemsResult() { Count = 0 };
LoadMoreItemsResult toret = new LoadMoreItemsResult() { Count = 0 };
JsonValue commentres = null;
if (nexturl == "begin")
commentres = await Task.Run(() => new PixivFS
.PixivAppAPI(OverAll.GlobalBaseAPI)
.csfriendly_illust_comments(illustid, include_total_comments: true));
else
{
Uri next = new Uri(nexturl);
string getparam(string param) => HttpUtility.ParseQueryString(next.Query).Get(param);
commentres = await Task.Run(() => new PixivFS
.PixivAppAPI(OverAll.GlobalBaseAPI)
.csfriendly_illust_comments(illustid, getparam("offset"), bool.Parse(getparam("include_total_comments"))));
}
nexturl = commentres.Item("next_url").AsString();
foreach (var recillust in commentres.Item("comments").AsArray())
{
if (_emergencyStop)
{
_emergencyStop = false;
return toret;
}
await Task.Run(() => pause.WaitOne());
Data.IllustCommentItem recommendi = Data.IllustCommentItem.FromJsonValue(recillust);
var recommendmodel = ViewModels.CommentViewModel.FromItem(recommendi);
//await recommendmodel.LoadAvatarAsync();
Add(recommendmodel);
toret.Count++;
}
return toret;
}
finally
{
_busy = false;
}
}
}
}
12 changes: 6 additions & 6 deletions PixivFSUWP/Data/FollowingIllustsCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,25 @@ public void ResumeLoading()

protected async Task<LoadMoreItemsResult> LoadMoreItemsAsync(CancellationToken c, uint count)
{
if (!HasMoreItems) return new LoadMoreItemsResult() { Count = 0 };
try
{
if (!HasMoreItems) return new LoadMoreItemsResult() { Count = 0 };
LoadMoreItemsResult toret = new LoadMoreItemsResult() { Count = 0 };
JsonValue recommendres = null;
JsonValue followingres = null;
if (nexturl == "begin")
recommendres = await Task.Run(() => new PixivFS
followingres = await Task.Run(() => new PixivFS
.PixivAppAPI(OverAll.GlobalBaseAPI)
.csfriendly_illust_follow());
else
{
Uri next = new Uri(nexturl);
string getparam(string param) => HttpUtility.ParseQueryString(next.Query).Get(param);
recommendres = await Task.Run(() => new PixivFS
followingres = await Task.Run(() => new PixivFS
.PixivAppAPI(OverAll.GlobalBaseAPI)
.csfriendly_illust_follow(getparam("restrict"), getparam("offset")));
}
nexturl = recommendres.Item("next_url").AsString();
foreach (var recillust in recommendres.Item("illusts").AsArray())
nexturl = followingres.Item("next_url").AsString();
foreach (var recillust in followingres.Item("illusts").AsArray())
{
if (_emergencyStop)
{
Expand Down
29 changes: 29 additions & 0 deletions PixivFSUWP/Data/IllustCommentItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using FSharp.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PixivFSUWP.Data
{
public class IllustCommentItem
{
public string Comment { get; set; }
public string DateTime { get; set; }
public string UserName { get; set; }
public string UserAccount { get; set; }
public string AvatarUrl { get; set; }

public static IllustCommentItem FromJsonValue(JsonValue Source)
{
IllustCommentItem toret = new IllustCommentItem();
toret.Comment = Source.Item("comment").AsString();
toret.DateTime = Source.Item("date").AsString();
toret.UserName = Source.Item("user").Item("name").AsString();
toret.UserAccount = Source.Item("user").Item("account").AsString();
toret.AvatarUrl = Source.Item("user").Item("profile_image_urls").Item("medium").AsString();
return toret;
}
}
}
2 changes: 1 addition & 1 deletion PixivFSUWP/Data/RecommendIllustsCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public void ResumeLoading()

protected async Task<LoadMoreItemsResult> LoadMoreItemsAsync(CancellationToken c, uint count)
{
if (!HasMoreItems) return new LoadMoreItemsResult() { Count = 0 };
try
{
if (!HasMoreItems) return new LoadMoreItemsResult() { Count = 0 };
LoadMoreItemsResult toret = new LoadMoreItemsResult() { Count = 0 };
JsonValue recommendres = null;
if (nexturl == "begin")
Expand Down
26 changes: 24 additions & 2 deletions PixivFSUWP/IllustDetailPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" SizeChanged="Page_SizeChanged"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ScrollViewer VerticalScrollBarVisibility="Hidden">
<ScrollViewer VerticalScrollBarVisibility="Hidden" ViewChanged="ScrollViewer_ViewChanged">
<ScrollViewer.Background>
<AcrylicBrush BackgroundSource="HostBackdrop" TintColor="White" TintOpacity="0.5" FallbackColor="White"/>
</ScrollViewer.Background>
Expand Down Expand Up @@ -38,7 +38,7 @@
<DataTemplate x:DataType="viewmodels:ImageItemViewModel">
<Grid Margin="-12,0" BorderThickness="1.5">
<Grid.BorderBrush>
<RevealBorderBrush Color="Transparent" FallbackColor="LightGray" />
<RevealBorderBrush Color="Transparent" FallbackColor="LightGray"/>
</Grid.BorderBrush>
<Image Source="{Binding ImageSource}"/>
<Grid>
Expand Down Expand Up @@ -69,6 +69,28 @@
</Grid>
<TextBlock x:Name="txtCaption" Margin="15,15,15,0" Height="Auto" TextWrapping="WrapWholeWords"/>
<TextBlock x:Name="txtCommentTitle" Margin="15,15,15,0" Height="Auto" FontSize="20" FontWeight="Bold"/>
<ListView x:Name="listComments" Margin="0,15" IncrementalLoadingTrigger="None" SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate x:DataType="viewmodels:CommentViewModel">
<Border Background="#99F3F3F3" Margin="0,5" BorderThickness="1.5">
<Border.BorderBrush>
<RevealBorderBrush Color="Transparent" FallbackColor="LightGray"/>
</Border.BorderBrush>
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding UserName}" FontWeight="Bold"/>
<TextBlock Text="{Binding UserAccount}" Foreground="Gray"/>
</StackPanel>
<TextBlock Grid.Row="1" Text="{Binding Comment}"/>
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</ScrollViewer>
</Page>
15 changes: 14 additions & 1 deletion PixivFSUWP/IllustDetailPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ private async Task loadContent()
txtAuthor.Text = illust.Author;
txtAuthorAccount.Text = string.Format("@{0}", illust.AuthorAccount);
txtCaption.Text = (illust.Caption == "") ? "暂无简介" : illust.Caption.Replace("<br />", "\n");
txtCommentTitle.Text = string.Format("评论 - 共{0}条", illust.TotalComments);
txtCommentTitle.Text = "评论";
listComments.ItemsSource = new Data.CommentsCollection(illustID.ToString());
int counter = 0;
foreach (var i in illust.OriginalUrls)
{
Expand Down Expand Up @@ -98,5 +99,17 @@ private async void ImageList_ItemClick(object sender, ItemClickEventArgs e)
Image = await Data.OverAll.ImageToBytes(Item.ImageSource)
});
}

private async void ScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
if ((sender as ScrollViewer).VerticalOffset >= (sender as ScrollViewer).ScrollableHeight - 500)
{
try
{
await (listComments.ItemsSource as ISupportIncrementalLoading)?.LoadMoreItemsAsync(0);
}
catch { }
}
}
}
}
3 changes: 3 additions & 0 deletions PixivFSUWP/PixivFSUWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@
<Compile Include="Controls\WaterfallListView.cs" />
<Compile Include="Data\BigImageDetail.cs" />
<Compile Include="Data\BookmarkIllustsCollection.cs" />
<Compile Include="Data\CommentsCollection.cs" />
<Compile Include="Data\FollowingIllustsCollection.cs" />
<Compile Include="Data\IllustCommentItem.cs" />
<Compile Include="Data\IllustDetail.cs" />
<Compile Include="Data\RecommendIllustsCollection.cs" />
<Compile Include="Data\OverAll.cs" />
Expand All @@ -150,6 +152,7 @@
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ViewModels\CommentViewModel.cs" />
<Compile Include="ViewModels\ImageItemViewModel.cs" />
<Compile Include="ViewModels\WaterfallItemViewModel.cs" />
<Compile Include="WaterfallPage.xaml.cs">
Expand Down
38 changes: 38 additions & 0 deletions PixivFSUWP/ViewModels/CommentViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media.Imaging;

namespace PixivFSUWP.ViewModels
{
public class CommentViewModel
{
public string Comment { get; set; }
public string UserName { get; set; }
public string UserAccount { get; set; }
private string _dateTime { get; set; }
public string AvatarUrl { get; set; }
public BitmapImage Avatar { get; set; }
public string DateTime
{
get => DateTimeOffset.Parse(_dateTime).LocalDateTime.ToString();
}

public async Task LoadAvatarAsync()
=> Avatar = await Data.OverAll.LoadImageAsync(AvatarUrl);

public static CommentViewModel FromItem(Data.IllustCommentItem Item)
{
return new CommentViewModel()
{
Comment = Item.Comment,
UserName = Item.UserName,
UserAccount = "@" + Item.UserAccount,
_dateTime = Item.DateTime,
AvatarUrl = Item.AvatarUrl
};
}
}
}

0 comments on commit f543bac

Please sign in to comment.