1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 16:47:24 +08:00
osu-lazer/osu.Game/Overlays/Comments/CommentsContainer.cs

313 lines
12 KiB
C#
Raw Normal View History

2019-10-07 22:49:20 +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.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Framework.Graphics;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Shapes;
using osu.Game.Online.API.Requests.Responses;
2020-02-26 23:00:48 +08:00
using System.Threading;
using System.Linq;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Game.Users;
2020-02-26 22:38:50 +08:00
using System.Collections.Generic;
using JetBrains.Annotations;
using osu.Game.Graphics.Sprites;
2019-10-07 22:49:20 +08:00
namespace osu.Game.Overlays.Comments
2019-10-07 22:49:20 +08:00
{
public class CommentsContainer : CompositeDrawable
{
private readonly Bindable<CommentableType> type = new Bindable<CommentableType>();
private readonly BindableLong id = new BindableLong();
2019-10-07 22:49:20 +08:00
2019-10-13 16:23:49 +08:00
public readonly Bindable<CommentsSortCriteria> Sort = new Bindable<CommentsSortCriteria>();
2019-10-09 17:18:49 +08:00
public readonly BindableBool ShowDeleted = new BindableBool();
2019-10-07 22:49:20 +08:00
protected readonly Bindable<User> User = new Bindable<User>();
2019-10-07 22:49:20 +08:00
[Resolved]
private IAPIProvider api { get; set; }
private GetCommentsRequest request;
2020-02-26 23:00:48 +08:00
private CancellationTokenSource loadCancellation;
2019-10-13 19:43:30 +08:00
private int currentPage;
private FillFlowContainer content;
private DeletedCommentsCounter deletedCommentsCounter;
private CommentsShowMoreButton moreButton;
private TotalCommentsCounter commentCounter;
2019-10-07 22:49:20 +08:00
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
2019-10-07 22:49:20 +08:00
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
AddRangeInternal(new Drawable[]
{
new Box
2019-10-07 22:49:20 +08:00
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background5
2019-10-07 22:49:20 +08:00
},
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
commentCounter = new TotalCommentsCounter(),
2019-10-07 23:45:22 +08:00
new CommentsHeader
{
2019-10-09 17:18:49 +08:00
Sort = { BindTarget = Sort },
ShowDeleted = { BindTarget = ShowDeleted }
},
content = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
2019-10-13 17:38:50 +08:00
},
new Container
{
Name = @"Footer",
2019-10-13 17:38:50 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
new Box
2019-10-13 17:38:50 +08:00
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background4
2019-10-13 17:38:50 +08:00
},
2019-10-13 19:43:30 +08:00
new FillFlowContainer
2019-10-13 17:38:50 +08:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
deletedCommentsCounter = new DeletedCommentsCounter
2019-10-13 17:38:50 +08:00
{
ShowDeleted = { BindTarget = ShowDeleted }
2019-10-13 19:43:30 +08:00
},
new Container
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Child = moreButton = new CommentsShowMoreButton
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2019-10-14 21:43:43 +08:00
Margin = new MarginPadding(5),
Action = getComments,
IsLoading = true,
2019-10-13 19:43:30 +08:00
}
2019-10-13 17:38:50 +08:00
}
}
}
}
}
}
}
});
User.BindTo(api.LocalUser);
2019-10-15 16:26:58 +08:00
}
protected override void LoadComplete()
{
User.BindValueChanged(_ => refetchComments());
Sort.BindValueChanged(_ => refetchComments(), true);
base.LoadComplete();
}
/// <param name="type">The type of resource to get comments for.</param>
/// <param name="id">The id of the resource to get comments for.</param>
public void ShowComments(CommentableType type, long id)
{
this.type.Value = type;
this.id.Value = id;
if (!IsLoaded)
return;
// only reset when changing ID/type. other refetch ops are generally just changing sort order.
commentCounter.Current.Value = 0;
refetchComments();
}
2020-01-07 17:30:06 +08:00
private void refetchComments()
{
2019-10-15 16:25:58 +08:00
clearComments();
getComments();
}
2019-10-13 19:43:30 +08:00
2019-10-15 16:25:58 +08:00
private void getComments()
{
2020-02-21 18:50:16 +08:00
if (id.Value <= 0)
2020-01-07 17:29:21 +08:00
return;
request?.Cancel();
2020-02-26 23:00:48 +08:00
loadCancellation?.Cancel();
2020-02-21 17:42:11 +08:00
request = new GetCommentsRequest(id.Value, type.Value, Sort.Value, currentPage++, 0);
2020-02-26 22:38:50 +08:00
request.Success += response => Schedule(() => onSuccess(response));
2020-02-05 00:15:23 +08:00
api.PerformAsync(request);
}
2019-10-15 16:25:58 +08:00
private void clearComments()
{
currentPage = 1;
deletedCommentsCounter.Count.Value = 0;
moreButton.Show();
2019-10-15 16:25:58 +08:00
moreButton.IsLoading = true;
content.Clear();
2020-02-26 23:00:48 +08:00
commentDictionary.Clear();
2019-10-15 16:25:58 +08:00
}
2020-02-26 22:38:50 +08:00
private readonly Dictionary<long, DrawableComment> commentDictionary = new Dictionary<long, DrawableComment>();
2019-10-15 16:25:58 +08:00
private void onSuccess(CommentBundle response)
{
2020-02-26 22:38:50 +08:00
if (!response.Comments.Any())
{
2020-02-26 22:38:50 +08:00
content.Add(new NoCommentsPlaceholder());
2020-02-26 23:00:48 +08:00
moreButton.Hide();
2020-02-26 22:38:50 +08:00
return;
}
else
{
2020-02-26 23:00:48 +08:00
var topLevelComments = appendComments(response);
2019-10-13 17:10:01 +08:00
2020-02-26 23:00:48 +08:00
LoadComponentsAsync(topLevelComments, loaded =>
{
content.AddRange(loaded);
2019-10-13 19:43:30 +08:00
2020-02-26 23:00:48 +08:00
deletedCommentsCounter.Count.Value += response.Comments.Count(c => c.IsDeleted && c.IsTopLevel);
commentCounter.Current.Value = response.Total;
2020-02-26 23:00:48 +08:00
if (response.HasMore)
{
int loadedTopLevelComments = 0;
content.Children.OfType<DrawableComment>().ForEach(p => loadedTopLevelComments++);
moreButton.Current.Value = response.TopLevelCount - loadedTopLevelComments;
moreButton.IsLoading = false;
}
else
{
moreButton.Hide();
}
}, (loadCancellation = new CancellationTokenSource()).Token);
2020-02-26 22:38:50 +08:00
}
}
/// <summary>
/// Appends retrieved comments to the subtree rooted of comments in this page.
/// </summary>
/// <param name="bundle">The bundle of comments to add.</param>
2020-02-26 23:00:48 +08:00
private List<DrawableComment> appendComments([NotNull] CommentBundle bundle)
2020-02-26 22:38:50 +08:00
{
2020-02-26 23:00:48 +08:00
var topLevelComments = new List<DrawableComment>();
2020-02-26 22:38:50 +08:00
var orphaned = new List<Comment>();
foreach (var comment in bundle.Comments.Concat(bundle.IncludedComments))
{
// Exclude possible duplicated comments.
if (commentDictionary.ContainsKey(comment.Id))
continue;
addNewComment(comment);
}
// Comments whose parents were seen later than themselves can now be added.
foreach (var o in orphaned)
addNewComment(o);
2020-02-26 23:00:48 +08:00
return topLevelComments;
2020-02-26 22:38:50 +08:00
void addNewComment(Comment comment)
{
var drawableComment = getDrawableComment(comment);
if (comment.ParentId == null)
{
// Comments that have no parent are added as top-level comments to the flow.
2020-02-26 23:00:48 +08:00
topLevelComments.Add(drawableComment);
2020-02-26 22:38:50 +08:00
}
else if (commentDictionary.TryGetValue(comment.ParentId.Value, out var parentDrawable))
{
// The comment's parent has already been seen, so the parent<-> child links can be added.
comment.ParentComment = parentDrawable.Comment;
parentDrawable.Replies.Add(drawableComment);
2019-10-13 19:43:30 +08:00
}
2020-01-31 14:46:35 +08:00
else
{
2020-02-26 22:38:50 +08:00
// The comment's parent has not been seen yet, so keep it orphaned for the time being. This can occur if the comments arrive out of order.
// Since this comment has now been seen, any further children can be added to it without being orphaned themselves.
orphaned.Add(comment);
2020-01-31 14:46:35 +08:00
}
2020-02-26 22:38:50 +08:00
}
}
private DrawableComment getDrawableComment(Comment comment)
{
if (commentDictionary.TryGetValue(comment.Id, out var existing))
return existing;
return commentDictionary[comment.Id] = new DrawableComment(comment)
{
ShowDeleted = { BindTarget = ShowDeleted },
Sort = { BindTarget = Sort },
RepliesRequested = onCommentRepliesRequested
};
}
private void onCommentRepliesRequested(DrawableComment drawableComment, int page)
{
var request = new GetCommentsRequest(id.Value, type.Value, Sort.Value, page, drawableComment.Comment.Id);
2019-10-14 22:33:14 +08:00
2020-02-26 22:38:50 +08:00
request.Success += response => Schedule(() => appendComments(response));
api.PerformAsync(request);
2019-10-07 22:49:20 +08:00
}
2019-10-13 21:22:10 +08:00
protected override void Dispose(bool isDisposing)
{
request?.Cancel();
2020-02-26 23:00:48 +08:00
loadCancellation?.Cancel();
2019-10-13 21:22:10 +08:00
base.Dispose(isDisposing);
}
2020-02-26 22:38:50 +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."
}
});
}
}
2019-10-07 22:49:20 +08:00
}
}