2021-10-18 03:34:31 +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.
|
|
|
|
|
2021-10-23 05:36:27 +08:00
|
|
|
using osu.Framework.Allocation;
|
2021-11-08 00:42:32 +08:00
|
|
|
using osu.Framework.Bindables;
|
2021-10-18 03:34:31 +08:00
|
|
|
using osu.Framework.Graphics.Sprites;
|
2021-11-08 00:42:32 +08:00
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2021-10-18 03:34:31 +08:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2021-10-23 05:36:27 +08:00
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
using osu.Game.Resources.Localisation.Web;
|
2021-10-18 03:34:31 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps.Drawables.Cards.Buttons
|
|
|
|
{
|
2021-11-08 00:42:32 +08:00
|
|
|
public class FavouriteButton : BeatmapCardIconButton, IHasCurrentValue<BeatmapSetFavouriteState>
|
2021-10-18 03:34:31 +08:00
|
|
|
{
|
2021-11-08 00:42:32 +08:00
|
|
|
private readonly BindableWithCurrent<BeatmapSetFavouriteState> current;
|
|
|
|
|
|
|
|
public Bindable<BeatmapSetFavouriteState> Current
|
|
|
|
{
|
|
|
|
get => current.Current;
|
|
|
|
set => current.Current = value;
|
|
|
|
}
|
|
|
|
|
2021-11-08 20:42:56 +08:00
|
|
|
private readonly APIBeatmapSet beatmapSet;
|
2021-10-18 03:34:31 +08:00
|
|
|
|
2021-10-23 05:36:27 +08:00
|
|
|
private PostBeatmapFavouriteRequest favouriteRequest;
|
|
|
|
|
2021-11-08 20:39:16 +08:00
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; }
|
|
|
|
|
2021-10-18 03:34:31 +08:00
|
|
|
public FavouriteButton(APIBeatmapSet beatmapSet)
|
|
|
|
{
|
2021-11-08 00:42:32 +08:00
|
|
|
current = new BindableWithCurrent<BeatmapSetFavouriteState>(new BeatmapSetFavouriteState(beatmapSet.HasFavourited, beatmapSet.FavouriteCount));
|
2021-11-08 20:42:56 +08:00
|
|
|
this.beatmapSet = beatmapSet;
|
2021-10-18 03:34:31 +08:00
|
|
|
}
|
|
|
|
|
2021-11-08 20:39:16 +08:00
|
|
|
protected override void LoadComplete()
|
2021-10-23 05:36:27 +08:00
|
|
|
{
|
2021-11-08 20:39:16 +08:00
|
|
|
base.LoadComplete();
|
2021-10-23 05:36:27 +08:00
|
|
|
|
2021-11-08 20:39:16 +08:00
|
|
|
Action = toggleFavouriteStatus;
|
|
|
|
current.BindValueChanged(_ => updateState(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void toggleFavouriteStatus()
|
|
|
|
{
|
|
|
|
var actionType = current.Value.Favourited ? BeatmapFavouriteAction.UnFavourite : BeatmapFavouriteAction.Favourite;
|
2021-10-23 05:36:27 +08:00
|
|
|
|
2021-11-08 20:39:16 +08:00
|
|
|
favouriteRequest?.Cancel();
|
2021-11-08 20:42:56 +08:00
|
|
|
favouriteRequest = new PostBeatmapFavouriteRequest(beatmapSet.OnlineID, actionType);
|
2021-11-08 00:42:32 +08:00
|
|
|
|
2021-11-08 20:39:16 +08:00
|
|
|
Enabled.Value = false;
|
|
|
|
favouriteRequest.Success += () =>
|
|
|
|
{
|
|
|
|
bool favourited = actionType == BeatmapFavouriteAction.Favourite;
|
2021-11-08 00:42:32 +08:00
|
|
|
|
2021-11-08 20:39:16 +08:00
|
|
|
current.Value = new BeatmapSetFavouriteState(favourited, current.Value.FavouriteCount + (favourited ? 1 : -1));
|
2021-10-23 05:36:27 +08:00
|
|
|
|
2021-11-08 20:39:16 +08:00
|
|
|
Enabled.Value = true;
|
|
|
|
};
|
|
|
|
favouriteRequest.Failure += e =>
|
|
|
|
{
|
|
|
|
Logger.Error(e, $"Failed to {actionType.ToString().ToLower()} beatmap: {e.Message}");
|
|
|
|
Enabled.Value = true;
|
2021-10-23 05:36:27 +08:00
|
|
|
};
|
|
|
|
|
2021-11-08 20:39:16 +08:00
|
|
|
api.Queue(favouriteRequest);
|
2021-11-08 00:42:32 +08:00
|
|
|
}
|
|
|
|
|
2021-10-23 05:36:27 +08:00
|
|
|
private void updateState()
|
|
|
|
{
|
2021-11-08 00:42:32 +08:00
|
|
|
if (current.Value.Favourited)
|
2021-10-23 05:36:27 +08:00
|
|
|
{
|
|
|
|
Icon.Icon = FontAwesome.Solid.Heart;
|
|
|
|
TooltipText = BeatmapsetsStrings.ShowDetailsUnfavourite;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Icon.Icon = FontAwesome.Regular.Heart;
|
|
|
|
TooltipText = BeatmapsetsStrings.ShowDetailsFavourite;
|
|
|
|
}
|
|
|
|
}
|
2021-10-18 03:34:31 +08:00
|
|
|
}
|
|
|
|
}
|