1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 04:57:27 +08:00
osu-lazer/osu.Game/Screens/Ranking/FavouriteButton.cs

165 lines
5.2 KiB
C#
Raw Normal View History

2024-07-18 03:45: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.
#nullable disable
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Logging;
2024-07-22 01:01:06 +08:00
using osu.Game.Beatmaps;
2024-07-18 03:45:20 +08:00
using osu.Game.Beatmaps.Drawables.Cards;
using osu.Game.Graphics;
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.Screens.Ranking
{
public partial class FavouriteButton : OsuAnimatedButton
{
private readonly Box background;
private readonly SpriteIcon icon;
2024-07-22 07:14:26 +08:00
public readonly BeatmapSetInfo BeatmapSetInfo;
2024-07-22 01:01:06 +08:00
private APIBeatmapSet beatmapSet;
2024-07-22 07:14:26 +08:00
private readonly Bindable<BeatmapSetFavouriteState> current;
2024-07-18 03:45:20 +08:00
private PostBeatmapFavouriteRequest favouriteRequest;
2024-07-22 07:14:26 +08:00
private readonly LoadingLayer loading;
2024-07-18 03:45:20 +08:00
private readonly IBindable<APIUser> localUser = new Bindable<APIUser>();
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private OsuColour colours { get; set; }
2024-07-22 01:01:06 +08:00
public FavouriteButton(BeatmapSetInfo beatmapSetInfo)
2024-07-18 03:45:20 +08:00
{
2024-07-22 07:14:26 +08:00
BeatmapSetInfo = beatmapSetInfo;
current = new BindableWithCurrent<BeatmapSetFavouriteState>(new BeatmapSetFavouriteState(false, 0));
2024-07-18 03:45:20 +08:00
Size = new Vector2(50, 30);
Children = new Drawable[]
{
background = new Box
{
RelativeSizeAxes = Axes.Both,
Depth = float.MaxValue
},
icon = new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(13),
Icon = FontAwesome.Regular.Heart,
},
loading = new LoadingLayer(true, false),
};
2024-07-22 01:01:06 +08:00
Action = toggleFavouriteStatus;
2024-07-18 03:45:20 +08:00
}
[BackgroundDependencyLoader]
private void load()
2024-07-18 03:45:20 +08:00
{
2024-07-22 01:01:06 +08:00
current.BindValueChanged(_ => updateState(), true);
2024-07-18 03:45:20 +08:00
localUser.BindTo(api.LocalUser);
2024-07-22 01:01:06 +08:00
localUser.BindValueChanged(_ => updateUser(), true);
2024-07-18 03:45:20 +08:00
}
2024-07-22 01:01:06 +08:00
private void getBeatmapSet()
2024-07-18 03:45:20 +08:00
{
2024-07-22 08:32:48 +08:00
GetBeatmapSetRequest beatmapSetRequest = new GetBeatmapSetRequest(BeatmapSetInfo.OnlineID);
2024-07-18 03:45:20 +08:00
2024-07-22 01:01:06 +08:00
loading.Show();
beatmapSetRequest.Success += beatmapSet =>
{
this.beatmapSet = beatmapSet;
current.Value = new BeatmapSetFavouriteState(this.beatmapSet.HasFavourited, this.beatmapSet.FavouriteCount);
loading.Hide();
Enabled.Value = true;
};
beatmapSetRequest.Failure += e =>
{
Logger.Error(e, $"Failed to fetch beatmap info: {e.Message}");
loading.Hide();
Enabled.Value = false;
};
api.Queue(beatmapSetRequest);
2024-07-18 03:45:20 +08:00
}
private void toggleFavouriteStatus()
{
Enabled.Value = false;
loading.Show();
var actionType = current.Value.Favourited ? BeatmapFavouriteAction.UnFavourite : BeatmapFavouriteAction.Favourite;
favouriteRequest?.Cancel();
favouriteRequest = new PostBeatmapFavouriteRequest(beatmapSet.OnlineID, actionType);
favouriteRequest.Success += () =>
{
bool favourited = actionType == BeatmapFavouriteAction.Favourite;
current.Value = new BeatmapSetFavouriteState(favourited, current.Value.FavouriteCount + (favourited ? 1 : -1));
Enabled.Value = true;
loading.Hide();
};
favouriteRequest.Failure += e =>
{
Logger.Error(e, $"Failed to {actionType.ToString().ToLowerInvariant()} beatmap: {e.Message}");
Enabled.Value = true;
loading.Hide();
};
api.Queue(favouriteRequest);
}
2024-07-22 01:01:06 +08:00
private void updateUser()
{
2024-07-22 07:14:26 +08:00
if (!(localUser.Value is GuestUser) && BeatmapSetInfo.OnlineID > 0)
2024-07-22 01:01:06 +08:00
getBeatmapSet();
else
{
Enabled.Value = false;
current.Value = new BeatmapSetFavouriteState(false, 0);
updateState();
TooltipText = BeatmapsetsStrings.ShowDetailsFavouriteLogin;
}
}
2024-07-18 03:45:20 +08:00
private void updateState()
{
if (current?.Value == null)
return;
if (current.Value.Favourited)
{
background.Colour = colours.Green;
icon.Icon = FontAwesome.Solid.Heart;
TooltipText = BeatmapsetsStrings.ShowDetailsUnfavourite;
}
else
{
background.Colour = colours.Gray4;
icon.Icon = FontAwesome.Regular.Heart;
TooltipText = BeatmapsetsStrings.ShowDetailsFavourite;
}
}
}
}