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;
|
2019-10-07 15:26:07 +00:00
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2019-10-13 09:10:01 +00:00
|
|
|
|
using System.Threading;
|
2019-10-14 21:55:33 +00:00
|
|
|
|
using System.Linq;
|
2019-10-15 09:26:16 +00:00
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
2020-06-13 11:00:05 +00:00
|
|
|
|
using osu.Framework.Threading;
|
2020-02-06 13:54:02 +00:00
|
|
|
|
using osu.Game.Users;
|
2019-10-07 14:49:20 +00:00
|
|
|
|
|
2019-10-08 11:51:12 +00:00
|
|
|
|
namespace osu.Game.Overlays.Comments
|
2019-10-07 14:49:20 +00:00
|
|
|
|
{
|
|
|
|
|
public class CommentsContainer : CompositeDrawable
|
|
|
|
|
{
|
2020-02-10 12:43:11 +00:00
|
|
|
|
private readonly Bindable<CommentableType> type = new Bindable<CommentableType>();
|
|
|
|
|
private readonly BindableLong id = new BindableLong();
|
2019-10-07 14:49:20 +00:00
|
|
|
|
|
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
|
|
|
|
|
2020-12-18 06:16:36 +00:00
|
|
|
|
protected readonly IBindable<User> User = new Bindable<User>();
|
2020-02-06 13:54:02 +00:00
|
|
|
|
|
2019-10-07 14:49:20 +00:00
|
|
|
|
[Resolved]
|
|
|
|
|
private IAPIProvider api { get; set; }
|
|
|
|
|
|
2019-10-07 15:26:07 +00:00
|
|
|
|
private GetCommentsRequest request;
|
2020-06-13 11:00:05 +00:00
|
|
|
|
private ScheduledDelegate scheduledCommentsLoad;
|
2019-10-13 09:10:01 +00:00
|
|
|
|
private CancellationTokenSource loadCancellation;
|
2019-10-13 11:43:30 +00:00
|
|
|
|
private int currentPage;
|
2019-10-07 15:26:07 +00:00
|
|
|
|
|
2020-01-28 07:21:27 +00:00
|
|
|
|
private FillFlowContainer content;
|
2020-01-30 02:17:26 +00:00
|
|
|
|
private DeletedCommentsCounter deletedCommentsCounter;
|
2020-01-28 07:21:27 +00:00
|
|
|
|
private CommentsShowMoreButton moreButton;
|
|
|
|
|
private TotalCommentsCounter commentCounter;
|
2019-10-07 14:49:20 +00:00
|
|
|
|
|
2020-01-28 07:21:27 +00:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OverlayColourProvider colourProvider)
|
2019-10-07 14:49:20 +00:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
|
AddRangeInternal(new Drawable[]
|
|
|
|
|
{
|
2020-01-28 07:21:27 +00:00
|
|
|
|
new Box
|
2019-10-07 14:49:20 +00:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2020-01-28 07:21:27 +00:00
|
|
|
|
Colour = colourProvider.Background5
|
2019-10-07 14:49:20 +00:00
|
|
|
|
},
|
2019-10-07 15:26:07 +00:00
|
|
|
|
new FillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2020-01-23 12:56:01 +00:00
|
|
|
|
commentCounter = new TotalCommentsCounter(),
|
2019-10-07 15:45:22 +00:00
|
|
|
|
new CommentsHeader
|
2019-10-07 15:26:07 +00:00
|
|
|
|
{
|
2019-10-09 09:18:49 +00:00
|
|
|
|
Sort = { BindTarget = Sort },
|
|
|
|
|
ShowDeleted = { BindTarget = ShowDeleted }
|
2019-10-07 15:26:07 +00:00
|
|
|
|
},
|
|
|
|
|
content = new FillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
Direction = FillDirection.Vertical,
|
2019-10-13 09:38:50 +00:00
|
|
|
|
},
|
|
|
|
|
new Container
|
|
|
|
|
{
|
2020-01-28 13:53:22 +00:00
|
|
|
|
Name = @"Footer",
|
2019-10-13 09:38:50 +00:00
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2019-10-13 11:43:30 +00:00
|
|
|
|
new FillFlowContainer
|
2019-10-13 09:38:50 +00:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
Direction = FillDirection.Vertical,
|
2020-07-29 03:39:23 +00:00
|
|
|
|
Margin = new MarginPadding { Bottom = 20 },
|
2019-10-13 09:38:50 +00:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2020-01-30 02:17:26 +00:00
|
|
|
|
deletedCommentsCounter = new DeletedCommentsCounter
|
2019-10-13 09:38:50 +00:00
|
|
|
|
{
|
2020-07-29 03:39:23 +00:00
|
|
|
|
ShowDeleted = { BindTarget = ShowDeleted },
|
|
|
|
|
Margin = new MarginPadding
|
|
|
|
|
{
|
|
|
|
|
Horizontal = 70,
|
|
|
|
|
Vertical = 10
|
|
|
|
|
}
|
2019-10-13 11:43:30 +00:00
|
|
|
|
},
|
|
|
|
|
new Container
|
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
Child = moreButton = new CommentsShowMoreButton
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
2020-07-29 03:39:23 +00:00
|
|
|
|
Margin = new MarginPadding
|
|
|
|
|
{
|
|
|
|
|
Vertical = 10
|
|
|
|
|
},
|
2020-01-06 22:07:50 +00:00
|
|
|
|
Action = getComments,
|
|
|
|
|
IsLoading = true,
|
2019-10-13 11:43:30 +00:00
|
|
|
|
}
|
2019-10-13 09:38:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-07 15:26:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-02-06 13:54:02 +00:00
|
|
|
|
|
|
|
|
|
User.BindTo(api.LocalUser);
|
2019-10-15 08:26:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-07 15:26:07 +00:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
2020-02-06 13:54:02 +00:00
|
|
|
|
User.BindValueChanged(_ => refetchComments());
|
2020-01-23 09:59:53 +00:00
|
|
|
|
Sort.BindValueChanged(_ => refetchComments(), true);
|
2019-10-07 15:26:07 +00:00
|
|
|
|
base.LoadComplete();
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-06 22:07:50 +00:00
|
|
|
|
/// <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)
|
2019-10-07 15:26:07 +00:00
|
|
|
|
{
|
2020-02-10 12:43:11 +00:00
|
|
|
|
this.type.Value = type;
|
|
|
|
|
this.id.Value = id;
|
2020-01-23 09:59:53 +00:00
|
|
|
|
|
|
|
|
|
if (!IsLoaded)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-01-24 02:42:33 +00:00
|
|
|
|
// only reset when changing ID/type. other refetch ops are generally just changing sort order.
|
2020-01-23 12:56:01 +00:00
|
|
|
|
commentCounter.Current.Value = 0;
|
2020-01-24 02:42:33 +00:00
|
|
|
|
|
2020-01-23 09:21:16 +00:00
|
|
|
|
refetchComments();
|
2020-01-06 22:07:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 09:30:06 +00:00
|
|
|
|
private void refetchComments()
|
2020-01-06 22:07:50 +00:00
|
|
|
|
{
|
2019-10-15 08:25:58 +00:00
|
|
|
|
clearComments();
|
|
|
|
|
getComments();
|
|
|
|
|
}
|
2019-10-13 11:43:30 +00:00
|
|
|
|
|
2019-10-15 08:25:58 +00:00
|
|
|
|
private void getComments()
|
|
|
|
|
{
|
2020-02-21 10:50:16 +00:00
|
|
|
|
if (id.Value <= 0)
|
2020-01-07 09:29:21 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2019-10-07 15:26:07 +00:00
|
|
|
|
request?.Cancel();
|
2019-10-13 09:10:01 +00:00
|
|
|
|
loadCancellation?.Cancel();
|
2020-06-13 11:00:05 +00:00
|
|
|
|
scheduledCommentsLoad?.Cancel();
|
2020-02-21 09:42:11 +00:00
|
|
|
|
request = new GetCommentsRequest(id.Value, type.Value, Sort.Value, currentPage++, 0);
|
2020-06-13 11:00:05 +00:00
|
|
|
|
request.Success += res => scheduledCommentsLoad = Schedule(() => onSuccess(res));
|
2020-02-04 16:15:23 +00:00
|
|
|
|
api.PerformAsync(request);
|
2019-10-07 15:26:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-15 08:25:58 +00:00
|
|
|
|
private void clearComments()
|
|
|
|
|
{
|
|
|
|
|
currentPage = 1;
|
2020-01-30 02:17:26 +00:00
|
|
|
|
deletedCommentsCounter.Count.Value = 0;
|
2020-01-28 13:53:22 +00:00
|
|
|
|
moreButton.Show();
|
2019-10-15 08:25:58 +00:00
|
|
|
|
moreButton.IsLoading = true;
|
|
|
|
|
content.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onSuccess(CommentBundle response)
|
2019-10-07 15:26:07 +00:00
|
|
|
|
{
|
2019-10-13 09:10:01 +00:00
|
|
|
|
loadCancellation = new CancellationTokenSource();
|
|
|
|
|
|
2020-01-29 03:22:08 +00:00
|
|
|
|
LoadComponentAsync(new CommentsPage(response)
|
2019-10-07 15:26:07 +00:00
|
|
|
|
{
|
2020-02-10 12:43:11 +00:00
|
|
|
|
ShowDeleted = { BindTarget = ShowDeleted },
|
|
|
|
|
Sort = { BindTarget = Sort },
|
|
|
|
|
Type = { BindTarget = type },
|
|
|
|
|
CommentableId = { BindTarget = id }
|
2020-01-29 03:22:08 +00:00
|
|
|
|
}, loaded =>
|
2019-10-09 11:10:05 +00:00
|
|
|
|
{
|
2019-10-13 09:10:01 +00:00
|
|
|
|
content.Add(loaded);
|
|
|
|
|
|
2020-01-30 02:17:26 +00:00
|
|
|
|
deletedCommentsCounter.Count.Value += response.Comments.Count(c => c.IsDeleted && c.IsTopLevel);
|
2019-10-13 11:43:30 +00:00
|
|
|
|
|
|
|
|
|
if (response.HasMore)
|
|
|
|
|
{
|
2019-10-15 09:26:16 +00:00
|
|
|
|
int loadedTopLevelComments = 0;
|
|
|
|
|
content.Children.OfType<FillFlowContainer>().ForEach(p => loadedTopLevelComments += p.Children.OfType<DrawableComment>().Count());
|
|
|
|
|
|
2019-10-13 11:43:30 +00:00
|
|
|
|
moreButton.Current.Value = response.TopLevelCount - loadedTopLevelComments;
|
|
|
|
|
moreButton.IsLoading = false;
|
|
|
|
|
}
|
2020-01-31 06:46:35 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
moreButton.Hide();
|
|
|
|
|
}
|
2019-10-14 14:33:14 +00:00
|
|
|
|
|
2020-01-23 12:56:01 +00:00
|
|
|
|
commentCounter.Current.Value = response.Total;
|
2019-10-13 09:10:01 +00:00
|
|
|
|
}, loadCancellation.Token);
|
2019-10-07 14:49:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-13 13:22:10 +00:00
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
request?.Cancel();
|
|
|
|
|
loadCancellation?.Cancel();
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
}
|
2019-10-07 14:49:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|