1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 06:47:24 +08:00

Encapsulate report logic inside button implementation

Avoids complicating the `DrawableComment` class, and allows for isolated
testability.
This commit is contained in:
Salman Ahmed 2022-10-22 02:47:11 +03:00
parent 9b5e35d599
commit 6c82bc36ed
2 changed files with 92 additions and 50 deletions

View File

@ -0,0 +1,91 @@
// 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.Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Resources.Localisation.Web;
using osuTK;
namespace osu.Game.Overlays.Comments
{
public class CommentReportButton : CompositeDrawable, IHasPopover
{
private readonly Comment comment;
private LinkFlowContainer link = null!;
private LoadingSpinner loading = null!;
[Resolved]
private IAPIProvider api { get; set; } = null!;
[Resolved]
private OverlayColourProvider? colourProvider { get; set; }
public CommentReportButton(Comment comment)
{
this.comment = comment;
}
[BackgroundDependencyLoader]
private void load()
{
AutoSizeAxes = Axes.Both;
InternalChildren = new Drawable[]
{
link = new LinkFlowContainer(s => s.Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold))
{
AutoSizeAxes = Axes.Both,
},
loading = new LoadingSpinner
{
Size = new Vector2(12f),
}
};
link.AddLink(UsersStrings.ReportButtonText, this.ShowPopover);
}
private void report(CommentReportReason reason, string comments)
{
var request = new CommentReportRequest(comment.Id, reason, comments);
link.Hide();
loading.Show();
request.Success += () => Schedule(() =>
{
loading.Hide();
link.Clear(true);
link.AddText(UsersStrings.ReportThanks, s => s.Colour = colourProvider?.Content2 ?? Colour4.White);
link.Show();
this.FadeOut(2000, Easing.InQuint).Expire();
});
request.Failure += _ => Schedule(() =>
{
loading.Hide();
link.Show();
});
api.Queue(request);
}
public Popover GetPopover() => new ReportCommentPopover(comment)
{
Action = report
};
}
}

View File

@ -19,8 +19,6 @@ using System;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Extensions.IEnumerableExtensions;
using System.Collections.Specialized;
using osu.Framework.Extensions;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Localisation;
using osu.Framework.Platform;
using osu.Game.Graphics.UserInterface;
@ -64,7 +62,6 @@ namespace osu.Game.Overlays.Comments
private ShowRepliesButton showRepliesButton = null!;
private ChevronButton chevronButton = null!;
private LinkFlowContainer actionsContainer = null!;
private ReportButton? reportButton;
private LoadingSpinner actionsLoading = null!;
private DeletedCommentsCounter deletedCommentsCounter = null!;
private OsuSpriteText deletedLabel = null!;
@ -340,7 +337,7 @@ namespace osu.Game.Overlays.Comments
if (Comment.UserId.HasValue && Comment.UserId.Value == api.LocalUser.Value.Id)
actionsContainer.AddLink("Delete", deleteComment);
else
actionsContainer.AddArbitraryDrawable(reportButton = new ReportButton());
actionsContainer.AddArbitraryDrawable(new CommentReportButton(Comment));
if (Comment.IsTopLevel)
{
@ -417,25 +414,6 @@ namespace osu.Game.Overlays.Comments
api.Queue(request);
}
public void ReportComment(CommentReportReason reason, string comment)
{
actionsContainer.Hide();
actionsLoading.Show();
var request = new CommentReportRequest(Comment.Id, reason, comment);
request.Success += () => Schedule(() =>
{
actionsLoading.Hide();
reportButton?.MarkReported();
actionsContainer.Show();
});
request.Failure += _ => Schedule(() =>
{
actionsLoading.Hide();
actionsContainer.Show();
});
api.Queue(request);
}
private void copyUrl()
{
host.GetClipboard()?.SetText($@"{api.APIEndpointUrl}/comments/{Comment.Id}");
@ -583,32 +561,5 @@ namespace osu.Game.Overlays.Comments
return parentComment.HasMessage ? parentComment.Message : parentComment.IsDeleted ? "deleted" : string.Empty;
}
}
internal class ReportButton : LinkFlowContainer, IHasPopover
{
public ReportButton()
: base(s => s.Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold))
{
}
[Resolved]
private DrawableComment comment { get; set; } = null!;
[BackgroundDependencyLoader]
private void load()
{
AutoSizeAxes = Axes.Both;
AddLink(UsersStrings.ReportButtonText, this.ShowPopover);
}
public void MarkReported()
{
Clear(true);
AddText(UsersStrings.ReportThanks);
this.Delay(3000).Then().FadeOut(2000);
}
public Popover GetPopover() => new ReportCommentPopover(comment);
}
}
}