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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

149 lines
4.7 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.
using osu.Framework.Allocation;
using osu.Framework.Bindables;
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 : GrayButton
2024-07-18 03:45:20 +08:00
{
2024-07-22 07:14:26 +08:00
public readonly BeatmapSetInfo BeatmapSetInfo;
2024-07-22 14:46:04 +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
2024-07-22 14:46:04 +08:00
private PostBeatmapFavouriteRequest? favouriteRequest;
private LoadingLayer loading = null!;
2024-07-18 03:45:20 +08:00
private readonly IBindable<APIUser> localUser = new Bindable<APIUser>();
[Resolved]
2024-07-22 14:46:04 +08:00
private IAPIProvider api { get; set; } = null!;
2024-07-18 03:45:20 +08:00
[Resolved]
2024-07-22 14:46:04 +08:00
private OsuColour colours { get; set; } = null!;
2024-07-18 03:45:20 +08:00
2024-07-22 01:01:06 +08:00
public FavouriteButton(BeatmapSetInfo beatmapSetInfo)
: base(FontAwesome.Regular.Heart)
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
2024-07-28 09:32:35 +08:00
Size = new Vector2(75, 30);
2024-07-18 03:45:20 +08:00
}
[BackgroundDependencyLoader]
private void load()
2024-07-18 03:45:20 +08:00
{
Add(loading = new LoadingLayer(true, false));
2024-07-28 08:26:31 +08:00
Action = toggleFavouriteStatus;
}
protected override void LoadComplete()
{
base.LoadComplete();
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}");
Schedule(() => loading.Hide());
2024-07-22 01:01:06 +08:00
Enabled.Value = false;
};
api.Queue(beatmapSetRequest);
2024-07-18 03:45:20 +08:00
}
private void toggleFavouriteStatus()
{
2024-07-22 14:46:04 +08:00
if (beatmapSet == null)
return;
2024-07-18 03:45:20 +08:00
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.Favourited)
{
Background.Colour = colours.Green;
Icon.Icon = FontAwesome.Solid.Heart;
2024-07-18 03:45:20 +08:00
TooltipText = BeatmapsetsStrings.ShowDetailsUnfavourite;
}
else
{
Background.Colour = colours.Gray4;
Icon.Icon = FontAwesome.Regular.Heart;
2024-07-18 03:45:20 +08:00
TooltipText = BeatmapsetsStrings.ShowDetailsFavourite;
}
}
}
}