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

212 lines
7.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.Online.API.Requests.Responses;
2019-10-13 09:10:01 +00:00
using System.Threading;
using System.Linq;
using osu.Framework.Extensions.IEnumerableExtensions;
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 CommentableType type;
2020-01-07 09:29:21 +00:00
private long? id;
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
[Resolved]
private IAPIProvider api { get; set; }
private GetCommentsRequest request;
2019-10-13 09:10:01 +00:00
private CancellationTokenSource loadCancellation;
2019-10-13 11:43:30 +00:00
private int currentPage;
private FillFlowContainer content;
private DeletedChildrenPlaceholder deletedChildrenPlaceholder;
private CommentsShowMoreButton moreButton;
private TotalCommentsCounter commentCounter;
2019-10-07 14:49:20 +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[]
{
new Box
2019-10-07 14:49:20 +00:00
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background5
2019-10-07 14:49:20 +00:00
},
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
commentCounter = new TotalCommentsCounter(),
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,
2019-10-13 09:38:50 +00:00
},
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
new Box
2019-10-13 09:38:50 +00:00
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background4
2019-10-13 09:38:50 +00:00
},
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,
Children = new Drawable[]
{
2019-10-14 21:32:21 +00:00
deletedChildrenPlaceholder = new DeletedChildrenPlaceholder
2019-10-13 09:38:50 +00:00
{
ShowDeleted = { BindTarget = ShowDeleted }
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,
2019-10-14 13:43:43 +00:00
Margin = new MarginPadding(5),
Action = getComments,
IsLoading = true,
2019-10-13 11:43:30 +00:00
}
2019-10-13 09:38:50 +00:00
}
}
}
}
}
}
}
});
2019-10-15 08:26:58 +00:00
}
protected override void LoadComplete()
{
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 = type;
this.id = 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 09:30:06 +00:00
private void refetchComments()
{
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-01-07 09:29:21 +00:00
if (!id.HasValue)
return;
request?.Cancel();
2019-10-13 09:10:01 +00:00
loadCancellation?.Cancel();
2020-01-07 09:29:21 +00:00
request = new GetCommentsRequest(type, id.Value, Sort.Value, currentPage++);
2019-10-15 09:27:32 +00:00
request.Success += onSuccess;
api.Queue(request);
}
2019-10-15 08:25:58 +00:00
private void clearComments()
{
currentPage = 1;
deletedChildrenPlaceholder.DeletedCount.Value = 0;
moreButton.IsLoading = true;
content.Clear();
}
private void onSuccess(CommentBundle response)
{
2019-10-13 09:10:01 +00:00
loadCancellation = new CancellationTokenSource();
var page = new FillFlowContainer
2019-10-13 09:10:01 +00:00
{
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-11-11 11:53:22 +00:00
{
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-11-11 11:53:22 +00:00
}
}
2019-10-13 09:10:01 +00:00
LoadComponentAsync(page, loaded =>
{
2019-10-13 09:10:01 +00:00
content.Add(loaded);
2019-10-15 08:25:58 +00:00
deletedChildrenPlaceholder.DeletedCount.Value += response.Comments.Count(c => c.IsDeleted && c.IsTopLevel);
2019-10-13 11:43:30 +00:00
if (response.HasMore)
{
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;
}
2019-10-14 14:33:14 +00:00
commentCounter.Current.Value = response.Total;
2019-10-13 11:43:30 +00:00
moreButton.FadeTo(response.HasMore ? 1 : 0);
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
}
}