From 58e2a265c1aabd02ba86f0b42dba5d836a89704d Mon Sep 17 00:00:00 2001 From: tobiichiamane Date: Sat, 28 Sep 2019 19:14:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=94=AF=E6=8C=81=E5=AD=90?= =?UTF-8?q?=E5=9B=9E=E5=A4=8D=E7=9A=84=E9=9B=86=E5=90=88=EF=BC=88=E6=9C=AA?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ChildrenCommentVisibilityConverter.cs | 27 ++++++++++++ PixivFSUWP/Data/CommentsCollection.cs | 44 +++++++++++++++++-- PixivFSUWP/Data/IllustCommentItem.cs | 1 - PixivFSUWP/PixivFSUWP.csproj | 1 + PixivFSUWP/ViewModels/CommentViewModel.cs | 9 +++- 5 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 PixivFSUWP/Converters/ChildrenCommentVisibilityConverter.cs diff --git a/PixivFSUWP/Converters/ChildrenCommentVisibilityConverter.cs b/PixivFSUWP/Converters/ChildrenCommentVisibilityConverter.cs new file mode 100644 index 0000000..569fcd6 --- /dev/null +++ b/PixivFSUWP/Converters/ChildrenCommentVisibilityConverter.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Data; + +namespace PixivFSUWP.Converters +{ + public class ChildrenCommentVisibilityConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, string language) + { + var childrenComments = value as ObservableCollection; + if (childrenComments == null) return Visibility.Collapsed; + if (childrenComments.Count == 0) return Visibility.Collapsed; + return Visibility.Visible; + } + + public object ConvertBack(object value, Type targetType, object parameter, string language) + { + throw new NotImplementedException(); + } + } +} diff --git a/PixivFSUWP/Data/CommentsCollection.cs b/PixivFSUWP/Data/CommentsCollection.cs index 9bd6e45..18dd8b8 100644 --- a/PixivFSUWP/Data/CommentsCollection.cs +++ b/PixivFSUWP/Data/CommentsCollection.cs @@ -20,6 +20,7 @@ public class CommentsCollection : ObservableCollection ChildrenComments = new List(); public CommentsCollection(string IllustID) => illustid = IllustID; @@ -98,9 +99,46 @@ protected async Task LoadMoreItemsAsync(CancellationToken c } Data.IllustCommentItem recommendi = Data.IllustCommentItem.FromJsonValue(recillust.GetObject()); var recommendmodel = ViewModels.CommentViewModel.FromItem(recommendi); - //await recommendmodel.LoadAvatarAsync(); - Add(recommendmodel); - toret.Count++; + //查找是否存在子回复 + var children = from item + in ChildrenComments + where item.ParentID == recommendmodel.ID + select item; + if (children.Count() > 0) + { + //存在子回复 + recommendmodel.ChildrenComments = new ObservableCollection(); + foreach (var child in children) + { + if (child.ChildrenComments != null) + { + foreach (var childschild in child.ChildrenComments) + { + childschild.Comment = string.Format("RE {0}: {1}", + child.UserName, childschild.Comment); + recommendmodel.ChildrenComments.Add(childschild); + } + child.ChildrenComments.Clear(); + child.ChildrenComments = null; + GC.Collect(); + } + recommendmodel.ChildrenComments.Add(child); + ChildrenComments.Remove(child); + } + } + //检查自己是不是子回复 + if (recommendmodel.ParentID != -1) + { + //自己也是子回复 + ChildrenComments.Add(recommendmodel); + } + else + { + //自己并非子回复 + //await recommendmodel.LoadAvatarAsync(); + Add(recommendmodel); + toret.Count++; + } } return toret; } diff --git a/PixivFSUWP/Data/IllustCommentItem.cs b/PixivFSUWP/Data/IllustCommentItem.cs index 683d06a..b662bd3 100644 --- a/PixivFSUWP/Data/IllustCommentItem.cs +++ b/PixivFSUWP/Data/IllustCommentItem.cs @@ -16,7 +16,6 @@ public class IllustCommentItem public string UserAccount { get; set; } public string AvatarUrl { get; set; } public int ParentCommentID { get; set; } - public List ChildrenComments { get; set; } = null; public static IllustCommentItem FromJsonValue(JsonObject Source) { diff --git a/PixivFSUWP/PixivFSUWP.csproj b/PixivFSUWP/PixivFSUWP.csproj index ce56199..9b32566 100644 --- a/PixivFSUWP/PixivFSUWP.csproj +++ b/PixivFSUWP/PixivFSUWP.csproj @@ -141,6 +141,7 @@ + diff --git a/PixivFSUWP/ViewModels/CommentViewModel.cs b/PixivFSUWP/ViewModels/CommentViewModel.cs index f97fbd4..a5cfe9b 100644 --- a/PixivFSUWP/ViewModels/CommentViewModel.cs +++ b/PixivFSUWP/ViewModels/CommentViewModel.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,12 +10,16 @@ namespace PixivFSUWP.ViewModels { public class CommentViewModel { + public int ID { get; set; } 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 int ParentID { get; set; } + public ObservableCollection ChildrenComments { get; set; } = null; + public string DateTime { get => DateTimeOffset.Parse(_dateTime).LocalDateTime.ToString(); @@ -27,11 +32,13 @@ public static CommentViewModel FromItem(Data.IllustCommentItem Item) { return new CommentViewModel() { + ID = Item.ID, Comment = Item.Comment, UserName = Item.UserName, UserAccount = "@" + Item.UserAccount, _dateTime = Item.DateTime, - AvatarUrl = Item.AvatarUrl + AvatarUrl = Item.AvatarUrl, + ParentID = Item.ParentCommentID }; } }