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 Sep 29, 2019
1 parent 4abd047 commit 5bfdc60
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 17 deletions.
51 changes: 51 additions & 0 deletions PixivFSUWP/Data/CommentAvatarLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PixivFSUWP.Data
{
public class CommentAvatarLoader
{
private bool isBusy = false;
private bool emergencyStop = false;
private int currentIndex = 0;
private ObservableCollection<ViewModels.CommentViewModel> collection;

public CommentAvatarLoader(ObservableCollection<ViewModels.CommentViewModel> Collection) => collection = Collection;

public async Task LoadAvatars()
{
if (isBusy) return;
isBusy = true;
try
{
while (currentIndex + 1 <= collection.Count)
{
if (emergencyStop)
{
collection = null;
return;
}
await collection[currentIndex].LoadAvatarAsync();
if (collection[currentIndex].ChildrenComments != null)
{
foreach (var child in collection[currentIndex].ChildrenComments)
{
await child.LoadAvatarAsync();
}
}
currentIndex++;
}
}
finally
{
isBusy = false;
}
}

public void EmergencyStop() => emergencyStop = true;
}
}
10 changes: 8 additions & 2 deletions PixivFSUWP/Data/CommentsCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ public class CommentsCollection : ObservableCollection<ViewModels.CommentViewMod
EventWaitHandle pause = new ManualResetEvent(true);
readonly string illustid;
List<ViewModels.CommentViewModel> ChildrenComments = new List<ViewModels.CommentViewModel>();
public CommentAvatarLoader AvatarLoader;

public CommentsCollection(string IllustID) => illustid = IllustID;
public CommentsCollection(string IllustID)
{
illustid = IllustID;
AvatarLoader = new CommentAvatarLoader(this);
}

public bool HasMoreItems
{
Expand Down Expand Up @@ -99,7 +104,6 @@ protected async Task<LoadMoreItemsResult> LoadMoreItemsAsync(CancellationToken c
}
Data.IllustCommentItem recommendi = Data.IllustCommentItem.FromJsonValue(recillust.GetObject());
var recommendmodel = ViewModels.CommentViewModel.FromItem(recommendi);
await recommendmodel.LoadAvatarAsync();
//查找是否存在子回复
var children = from item
in ChildrenComments
Expand Down Expand Up @@ -146,8 +150,10 @@ in ChildrenComments
finally
{
_busy = false;
_ = AvatarLoader.LoadAvatars();
if (_emergencyStop)
{
AvatarLoader.EmergencyStop();
nexturl = "";
Clear();
GC.Collect();
Expand Down
53 changes: 38 additions & 15 deletions PixivFSUWP/IllustDetailPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,38 +137,61 @@
<Border.BorderBrush>
<RevealBorderBrush Color="Transparent" FallbackColor="LightGray"/>
</Border.BorderBrush>
<Grid Margin="10">
<Grid Margin="15,15,15,10">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Avatar}" Height="32" Width="32"/>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding UserName}" FontWeight="Bold"/>
<TextBlock Text="{Binding UserAccount}" Foreground="Gray"/>
</StackPanel>
</StackPanel>
<TextBlock Grid.Row="1" Text="{Binding Comment}" TextWrapping="WrapWholeWords"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Ellipse Grid.RowSpan="2" Width="40" Height="40">
<Ellipse.Fill>
<ImageBrush ImageSource="{Binding Avatar}"/>
</Ellipse.Fill>
</Ellipse>
<TextBlock Margin="5,0" Grid.Column="1" FontSize="14" FontWeight="Bold" VerticalAlignment="Bottom" Text="{Binding UserName}"/>
<TextBlock Margin="5,0" Grid.Row="1" Grid.Column="1" FontSize="12" Foreground="Gray" VerticalAlignment="Top" Text="{Binding UserAccount}"/>
</Grid>
<TextBlock Grid.Row="1" Margin="0,5,0,0" Text="{Binding Comment}" TextWrapping="WrapWholeWords"/>
<TextBlock Grid.Row="2" Margin="0,3,0,0" Text="{Binding DateTime}" FontSize="12" Foreground="Gray"/>
<ListView Grid.Row="3" Margin="0,5,0,0" Visibility="{Binding ChildrenComments,Converter={StaticResource ChildrenCommentVisibilityConverter}}"
<ListView Grid.Row="3" Margin="10,5,0,0" Visibility="{Binding ChildrenComments,Converter={StaticResource ChildrenCommentVisibilityConverter}}"
ItemsSource="{Binding ChildrenComments}" SelectionMode="None"
ScrollViewer.VerticalScrollMode="Disabled" ScrollViewer.HorizontalScrollMode="Disabled">
<ListView.ItemTemplate>
<DataTemplate x:DataType="viewmodels:CommentViewModel">
<Border Margin="0,-5" BorderThickness="0">
<Grid Margin="10">
<Grid Margin="15">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding UserName}" FontWeight="Bold"/>
<TextBlock Text="{Binding UserAccount}" Foreground="Gray"/>
</StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Ellipse Grid.RowSpan="2" Width="40" Height="40">
<Ellipse.Fill>
<ImageBrush ImageSource="{Binding Avatar}"/>
</Ellipse.Fill>
</Ellipse>
<TextBlock Margin="5,0" Grid.Column="1" FontSize="14" FontWeight="Bold" VerticalAlignment="Bottom" Text="{Binding UserName}"/>
<TextBlock Margin="5,0" Grid.Row="1" Grid.Column="1" FontSize="12" Foreground="Gray" VerticalAlignment="Top" Text="{Binding UserAccount}"/>
</Grid>
<TextBlock Grid.Row="1" Text="{Binding Comment}" TextWrapping="WrapWholeWords"/>
<TextBlock Grid.Row="2" Margin="0,3,0,0" Text="{Binding DateTime}" FontSize="12" Foreground="Gray"/>
</Grid>
Expand Down
3 changes: 3 additions & 0 deletions PixivFSUWP/IllustDetailPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ protected override void OnNavigatedFrom(NavigationEventArgs e)
Data.Backstack.Default.Push(typeof(IllustDetailPage), illustID);
((Frame.Parent as Grid)?.Parent as MainPage)?.UpdateNavButtonState();
}
(listComments.ItemsSource as Data.CommentsCollection)?.AvatarLoader?.EmergencyStop();
(listComments.ItemsSource as Data.CommentsCollection)?.StopLoading();
(listComments.ItemsSource as Data.CommentsCollection)?.Clear();
GC.Collect();
base.OnNavigatedFrom(e);
}
Expand Down
1 change: 1 addition & 0 deletions PixivFSUWP/PixivFSUWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
<Compile Include="Data\Backstack.cs" />
<Compile Include="Data\BigImageDetail.cs" />
<Compile Include="Data\BookmarkIllustsCollection.cs" />
<Compile Include="Data\CommentAvatarLoader.cs" />
<Compile Include="Data\CommentsCollection.cs" />
<Compile Include="Data\CurrentUser.cs" />
<Compile Include="Data\FollowingIllustsCollection.cs" />
Expand Down

0 comments on commit 5bfdc60

Please sign in to comment.