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.Graphics;
|
2019-10-07 23:26:07 +08:00
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2019-10-13 17:10:01 +08:00
|
|
|
|
using System.Threading;
|
2019-10-15 05:55:33 +08:00
|
|
|
|
using System.Linq;
|
2019-10-15 17:26:16 +08:00
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
2019-10-07 22:49:20 +08:00
|
|
|
|
|
2019-10-08 19:51:12 +08:00
|
|
|
|
namespace osu.Game.Overlays.Comments
|
2019-10-07 22:49:20 +08:00
|
|
|
|
{
|
|
|
|
|
public class CommentsContainer : CompositeDrawable
|
|
|
|
|
{
|
|
|
|
|
private readonly CommentableType type;
|
|
|
|
|
private readonly long id;
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
|
private IAPIProvider api { get; set; }
|
|
|
|
|
|
2019-10-07 23:26:07 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private OsuColour colours { get; set; }
|
|
|
|
|
|
|
|
|
|
private GetCommentsRequest request;
|
2019-10-13 17:10:01 +08:00
|
|
|
|
private CancellationTokenSource loadCancellation;
|
2019-10-13 19:43:30 +08:00
|
|
|
|
private int currentPage;
|
2019-10-07 23:26:07 +08:00
|
|
|
|
|
2019-10-07 22:49:20 +08:00
|
|
|
|
private readonly Box background;
|
2019-10-07 23:26:07 +08:00
|
|
|
|
private readonly FillFlowContainer content;
|
2019-10-15 05:32:21 +08:00
|
|
|
|
private readonly DeletedChildrenPlaceholder deletedChildrenPlaceholder;
|
2019-10-13 19:43:30 +08:00
|
|
|
|
private readonly CommentsShowMoreButton moreButton;
|
2019-10-07 22:49:20 +08: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,
|
|
|
|
|
},
|
2019-10-07 23:26:07 +08:00
|
|
|
|
new FillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2019-10-07 23:45:22 +08:00
|
|
|
|
new CommentsHeader
|
2019-10-07 23:26:07 +08:00
|
|
|
|
{
|
2019-10-09 17:18:49 +08:00
|
|
|
|
Sort = { BindTarget = Sort },
|
|
|
|
|
ShowDeleted = { BindTarget = ShowDeleted }
|
2019-10-07 23:26:07 +08:00
|
|
|
|
},
|
|
|
|
|
content = new FillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
Direction = FillDirection.Vertical,
|
2019-10-13 17:38:50 +08:00
|
|
|
|
},
|
|
|
|
|
new Container
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Colour = OsuColour.Gray(0.2f)
|
|
|
|
|
},
|
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[]
|
|
|
|
|
{
|
2019-10-15 05:32:21 +08:00
|
|
|
|
deletedChildrenPlaceholder = new DeletedChildrenPlaceholder
|
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),
|
2019-10-15 16:25:58 +08:00
|
|
|
|
Action = getComments
|
2019-10-13 19:43:30 +08:00
|
|
|
|
}
|
2019-10-13 17:38:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-07 23:26:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-15 16:26:58 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
|
|
|
|
{
|
|
|
|
|
background.Colour = colours.Gray2;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-07 23:26:07 +08:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
Sort.BindValueChanged(onSortChanged, true);
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-15 16:25:58 +08:00
|
|
|
|
private void onSortChanged(ValueChangedEvent<CommentsSortCriteria> sort)
|
2019-10-07 23:26:07 +08:00
|
|
|
|
{
|
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()
|
|
|
|
|
{
|
2019-10-07 23:26:07 +08:00
|
|
|
|
request?.Cancel();
|
2019-10-13 17:10:01 +08:00
|
|
|
|
loadCancellation?.Cancel();
|
2019-10-13 19:43:30 +08:00
|
|
|
|
request = new GetCommentsRequest(type, id, Sort.Value, currentPage++);
|
2019-10-15 17:27:32 +08:00
|
|
|
|
request.Success += onSuccess;
|
2019-10-07 23:26:07 +08:00
|
|
|
|
api.Queue(request);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-15 16:25:58 +08:00
|
|
|
|
private void clearComments()
|
|
|
|
|
{
|
|
|
|
|
currentPage = 1;
|
|
|
|
|
deletedChildrenPlaceholder.DeletedCount.Value = 0;
|
|
|
|
|
moreButton.IsLoading = true;
|
|
|
|
|
content.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onSuccess(CommentBundle response)
|
2019-10-07 23:26:07 +08:00
|
|
|
|
{
|
2019-10-13 17:10:01 +08:00
|
|
|
|
loadCancellation = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
FillFlowContainer page = new FillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-07 23:26:07 +08:00
|
|
|
|
foreach (var c in response.Comments)
|
|
|
|
|
{
|
2019-10-09 17:18:49 +08:00
|
|
|
|
if (c.IsTopLevel)
|
2019-11-11 19:53:22 +08:00
|
|
|
|
{
|
2019-10-13 17:10:01 +08:00
|
|
|
|
page.Add(new DrawableComment(c)
|
2019-10-10 16:43:45 +08:00
|
|
|
|
{
|
|
|
|
|
ShowDeleted = { BindTarget = ShowDeleted }
|
|
|
|
|
});
|
2019-11-11 19:53:22 +08:00
|
|
|
|
}
|
2019-10-08 19:51:12 +08:00
|
|
|
|
}
|
2019-10-09 19:10:05 +08:00
|
|
|
|
|
2019-10-13 17:10:01 +08:00
|
|
|
|
LoadComponentAsync(page, loaded =>
|
2019-10-09 19:10:05 +08:00
|
|
|
|
{
|
2019-10-13 17:10:01 +08:00
|
|
|
|
content.Add(loaded);
|
|
|
|
|
|
2019-10-15 16:25:58 +08:00
|
|
|
|
deletedChildrenPlaceholder.DeletedCount.Value += response.Comments.Count(c => c.IsDeleted && c.IsTopLevel);
|
2019-10-13 19:43:30 +08:00
|
|
|
|
|
|
|
|
|
if (response.HasMore)
|
|
|
|
|
{
|
2019-10-15 17:26:16 +08:00
|
|
|
|
int loadedTopLevelComments = 0;
|
|
|
|
|
content.Children.OfType<FillFlowContainer>().ForEach(p => loadedTopLevelComments += p.Children.OfType<DrawableComment>().Count());
|
|
|
|
|
|
2019-10-13 19:43:30 +08:00
|
|
|
|
moreButton.Current.Value = response.TopLevelCount - loadedTopLevelComments;
|
|
|
|
|
moreButton.IsLoading = false;
|
|
|
|
|
}
|
2019-10-14 22:33:14 +08:00
|
|
|
|
|
2019-10-13 19:43:30 +08:00
|
|
|
|
moreButton.FadeTo(response.HasMore ? 1 : 0);
|
2019-10-13 17:10:01 +08:00
|
|
|
|
}, loadCancellation.Token);
|
2019-10-07 22:49:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-13 21:22:10 +08:00
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
request?.Cancel();
|
|
|
|
|
loadCancellation?.Cancel();
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
}
|
2019-10-07 22:49:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|