1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 00:27:25 +08:00
osu-lazer/osu.Game/Overlays/Comments/CommentsPage.cs

145 lines
5.0 KiB
C#
Raw Normal View History

2020-01-29 11:22:08 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Allocation;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics;
using osu.Framework.Bindables;
using osu.Game.Online.API.Requests.Responses;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics.Sprites;
using System.Linq;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API;
using System.Collections.Generic;
using osu.Framework.Extensions.IEnumerableExtensions;
2020-01-29 11:22:08 +08:00
namespace osu.Game.Overlays.Comments
{
2020-01-29 11:58:53 +08:00
public class CommentsPage : CompositeDrawable
2020-01-29 11:22:08 +08:00
{
public readonly BindableBool ShowDeleted = new BindableBool();
public readonly Bindable<CommentsSortCriteria> Sort = new Bindable<CommentsSortCriteria>();
public readonly Bindable<CommentableType> Type = new Bindable<CommentableType>();
public readonly BindableLong CommentableId = new BindableLong();
[Resolved]
private IAPIProvider api { get; set; }
2020-01-29 11:22:08 +08:00
private readonly CommentBundle commentBundle;
public CommentsPage(CommentBundle commentBundle)
{
this.commentBundle = commentBundle;
}
[BackgroundDependencyLoader]
2020-01-29 11:58:53 +08:00
private void load(OverlayColourProvider colourProvider)
2020-01-29 11:22:08 +08:00
{
2020-01-29 11:58:53 +08:00
FillFlowContainer flow;
2020-01-29 11:22:08 +08:00
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
2020-01-29 11:58:53 +08:00
AddRangeInternal(new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background5
},
flow = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
}
});
2020-01-29 11:22:08 +08:00
if (!commentBundle.Comments.Any())
{
2020-01-29 11:58:53 +08:00
flow.Add(new NoCommentsPlaceholder());
2020-01-29 11:22:08 +08:00
return;
}
commentBundle.Comments.ForEach(c =>
2020-01-29 11:22:08 +08:00
{
if (c.IsTopLevel)
flow.Add(createCommentWithReplies(c, commentBundle));
});
}
private DrawableComment createCommentWithReplies(Comment comment, CommentBundle commentBundle)
{
var drawableComment = createDrawableComment(comment);
var replies = commentBundle.Comments.Where(c => c.ParentId == comment.Id);
if (replies.Any())
{
replies.ForEach(c => c.ParentComment = comment);
drawableComment.OnLoadComplete += _ => drawableComment.AddReplies(replies.Select(reply => createCommentWithReplies(reply, commentBundle)));
2020-01-29 11:22:08 +08:00
}
return drawableComment;
2020-01-29 11:22:08 +08:00
}
private void onCommentRepliesRequested(DrawableComment drawableComment, int page)
{
var request = new GetCommentRepliesRequest(drawableComment.Comment.Id, Type.Value, CommentableId.Value, Sort.Value, page);
request.Success += response => onCommentRepliesReceived(response, drawableComment);
api.PerformAsync(request);
}
private void onCommentRepliesReceived(CommentBundle response, DrawableComment drawableComment)
{
var receivedComments = response.Comments;
var uniqueComments = new List<Comment>();
// We may receive already loaded comments
receivedComments.ForEach(c =>
{
if (drawableComment.LoadedReplies.All(loadedReply => loadedReply.Id != c.Id))
uniqueComments.Add(c);
});
uniqueComments.ForEach(c => c.ParentComment = drawableComment.Comment);
2020-02-10 21:08:58 +08:00
drawableComment.AddReplies(uniqueComments.Select(createDrawableComment));
}
private DrawableComment createDrawableComment(Comment comment) => new DrawableComment(comment)
{
ShowDeleted = { BindTarget = ShowDeleted },
Sort = { BindTarget = Sort },
RepliesRequested = onCommentRepliesRequested
};
2020-01-29 11:22:08 +08:00
private class NoCommentsPlaceholder : CompositeDrawable
{
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
Height = 80;
RelativeSizeAxes = Axes.X;
AddRangeInternal(new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background4
},
new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Margin = new MarginPadding { Left = 50 },
Text = @"No comments yet."
}
});
}
}
}
}