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

117 lines
3.6 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-07 14:49:20 +00:00
namespace osu.Game.Overlays.Comments
2019-10-07 14:49:20 +00:00
{
public class CommentsContainer : CompositeDrawable
{
2019-10-08 12:39:03 +00:00
private const float separator_height = 1.5f;
2019-10-07 14:49:20 +00:00
private readonly CommentableType type;
private readonly long id;
public readonly Bindable<SortCommentsBy> Sort = new Bindable<SortCommentsBy>();
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private OsuColour colours { get; set; }
private GetCommentsRequest request;
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
{
Sort = { BindTarget = Sort }
},
content = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
}
}
}
});
}
protected override void LoadComplete()
{
Sort.BindValueChanged(onSortChanged, true);
base.LoadComplete();
}
private void onSortChanged(ValueChangedEvent<SortCommentsBy> sort) => getComments();
private void getComments()
{
request?.Cancel();
2019-10-08 19:46:42 +00:00
content.Clear();
request = new GetCommentsRequest(type, id, Sort.Value);
request.Success += onSuccess;
api.Queue(request);
}
private void onSuccess(APICommentsController response)
{
foreach (var c in response.Comments)
{
if (!c.IsDeleted && c.IsTopLevel)
content.AddRange(new Drawable[]
{
2019-10-09 08:07:56 +00:00
new DrawableComment(c),
new Container
{
RelativeSizeAxes = Axes.X,
2019-10-08 12:39:03 +00:00
Height = separator_height,
Child = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colours.Gray1,
}
}
});
}
2019-10-07 14:49:20 +00:00
}
[BackgroundDependencyLoader]
private void load()
2019-10-07 14:49:20 +00:00
{
background.Colour = colours.Gray3;
}
}
}