osu/osu.Game/Overlays/Comments/CommentsContainer.cs

138 lines
4.2 KiB
C#
Raw Normal View History

2019-10-07 14:49:20 +00: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.Graphics;
using osu.Game.Online.API.Requests.Responses;
2019-10-13 09:10:01 +00:00
using System.Threading;
2019-10-07 14:49:20 +00:00
namespace osu.Game.Overlays.Comments
2019-10-07 14:49:20 +00:00
{
public class CommentsContainer : CompositeDrawable
{
private readonly CommentableType type;
private readonly long id;
2019-10-13 08:23:49 +00:00
public readonly Bindable<CommentsSortCriteria> Sort = new Bindable<CommentsSortCriteria>();
2019-10-09 09:18:49 +00:00
public readonly BindableBool ShowDeleted = new BindableBool();
2019-10-07 14:49:20 +00:00
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private OsuColour colours { get; set; }
private GetCommentsRequest request;
2019-10-13 09:10:01 +00:00
private CancellationTokenSource loadCancellation;
2019-10-07 14:49:20 +00:00
private readonly Box background;
private readonly FillFlowContainer content;
2019-10-07 14:49:20 +00:00
public CommentsContainer(CommentableType type, long id)
{
this.type = type;
this.id = id;
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
AddRangeInternal(new Drawable[]
{
background = new Box
{
RelativeSizeAxes = Axes.Both,
},
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
2019-10-07 15:45:22 +00:00
new CommentsHeader
{
2019-10-09 09:18:49 +00:00
Sort = { BindTarget = Sort },
ShowDeleted = { BindTarget = ShowDeleted }
},
content = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
}
}
}
});
}
protected override void LoadComplete()
{
Sort.BindValueChanged(onSortChanged, true);
base.LoadComplete();
}
2019-10-13 08:23:49 +00:00
private void onSortChanged(ValueChangedEvent<CommentsSortCriteria> sort) => getComments();
private void getComments()
{
request?.Cancel();
2019-10-13 09:10:01 +00:00
loadCancellation?.Cancel();
request = new GetCommentsRequest(type, id, Sort.Value);
request.Success += onSuccess;
api.Queue(request);
}
private void onSuccess(APICommentsController response)
{
2019-10-13 09:10:01 +00:00
loadCancellation = new CancellationTokenSource();
FillFlowContainer page = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
};
foreach (var c in response.Comments)
{
2019-10-09 09:18:49 +00:00
if (c.IsTopLevel)
2019-10-13 09:10:01 +00:00
page.Add(new DrawableComment(c)
2019-10-10 08:43:45 +00:00
{
ShowDeleted = { BindTarget = ShowDeleted }
});
}
2019-10-13 09:10:01 +00:00
LoadComponentAsync(page, loaded =>
{
2019-10-13 09:10:01 +00:00
content.Clear();
2019-10-13 09:10:01 +00:00
content.Add(loaded);
int deletedComments = 0;
response.Comments.ForEach(comment =>
{
if (comment.IsDeleted && comment.IsTopLevel)
deletedComments++;
});
content.Add(new DeletedChildsPlaceholder(deletedComments)
{
ShowDeleted = { BindTarget = ShowDeleted }
});
}, loadCancellation.Token);
2019-10-07 14:49:20 +00:00
}
[BackgroundDependencyLoader]
private void load()
2019-10-07 14:49:20 +00:00
{
2019-10-13 08:50:27 +00:00
background.Colour = colours.Gray2;
2019-10-07 14:49:20 +00:00
}
}
}