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
|
|
|
|
|
{
|
2024-07-28 08:20:22 +08:00
|
|
|
|
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;
|
2024-07-28 08:20:22 +08:00
|
|
|
|
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)
|
2024-07-28 08:20:22 +08:00
|
|
|
|
: 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]
|
2024-07-21 02:49:46 +08:00
|
|
|
|
private void load()
|
2024-07-18 03:45:20 +08:00
|
|
|
|
{
|
2024-07-28 08:20:22 +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}");
|
|
|
|
|
|
2024-08-21 18:27:56 +08:00
|
|
|
|
Schedule(() =>
|
|
|
|
|
{
|
|
|
|
|
loading.Hide();
|
|
|
|
|
Enabled.Value = false;
|
|
|
|
|
});
|
2024-07-22 01:01:06 +08:00
|
|
|
|
};
|
|
|
|
|
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}");
|
2024-08-21 18:27:56 +08:00
|
|
|
|
|
|
|
|
|
Schedule(() =>
|
|
|
|
|
{
|
|
|
|
|
Enabled.Value = true;
|
|
|
|
|
loading.Hide();
|
|
|
|
|
});
|
2024-07-18 03:45:20 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2024-07-28 08:20:22 +08:00
|
|
|
|
Background.Colour = colours.Green;
|
|
|
|
|
Icon.Icon = FontAwesome.Solid.Heart;
|
2024-07-18 03:45:20 +08:00
|
|
|
|
TooltipText = BeatmapsetsStrings.ShowDetailsUnfavourite;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-07-28 08:20:22 +08:00
|
|
|
|
Background.Colour = colours.Gray4;
|
|
|
|
|
Icon.Icon = FontAwesome.Regular.Heart;
|
2024-07-18 03:45:20 +08:00
|
|
|
|
TooltipText = BeatmapsetsStrings.ShowDetailsFavourite;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|