From b5cbdcf9819dff87a57758b4577c80da49f9ed85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Fri, 22 Oct 2021 23:36:27 +0200 Subject: [PATCH] Implement basic behaviour of favourite button --- .../Cards/Buttons/FavouriteButton.cs | 50 ++++++++++++++++++- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/osu.Game/Beatmaps/Drawables/Cards/Buttons/FavouriteButton.cs b/osu.Game/Beatmaps/Drawables/Cards/Buttons/FavouriteButton.cs index 1859a66821..145b3f41b0 100644 --- a/osu.Game/Beatmaps/Drawables/Cards/Buttons/FavouriteButton.cs +++ b/osu.Game/Beatmaps/Drawables/Cards/Buttons/FavouriteButton.cs @@ -1,8 +1,13 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using osu.Framework.Allocation; using osu.Framework.Graphics.Sprites; using osu.Game.Online.API.Requests.Responses; +using osu.Framework.Logging; +using osu.Game.Online.API; +using osu.Game.Online.API.Requests; +using osu.Game.Resources.Localisation.Web; namespace osu.Game.Beatmaps.Drawables.Cards.Buttons { @@ -10,13 +15,54 @@ namespace osu.Game.Beatmaps.Drawables.Cards.Buttons { private readonly APIBeatmapSet beatmapSet; + private PostBeatmapFavouriteRequest favouriteRequest; + public FavouriteButton(APIBeatmapSet beatmapSet) { this.beatmapSet = beatmapSet; - Icon.Icon = FontAwesome.Regular.Heart; + updateState(); } - // TODO: implement behaviour + [BackgroundDependencyLoader] + private void load(IAPIProvider api) + { + Action = () => + { + var actionType = beatmapSet.HasFavourited ? BeatmapFavouriteAction.UnFavourite : BeatmapFavouriteAction.Favourite; + + favouriteRequest?.Cancel(); + favouriteRequest = new PostBeatmapFavouriteRequest(beatmapSet.OnlineID, actionType); + + Enabled.Value = false; + favouriteRequest.Success += () => + { + beatmapSet.HasFavourited = actionType == BeatmapFavouriteAction.Favourite; + Enabled.Value = true; + updateState(); + }; + favouriteRequest.Failure += e => + { + Logger.Error(e, $"Failed to {actionType.ToString().ToLower()} beatmap: {e.Message}"); + Enabled.Value = true; + }; + + api.Queue(favouriteRequest); + }; + } + + private void updateState() + { + if (beatmapSet.HasFavourited) + { + Icon.Icon = FontAwesome.Solid.Heart; + TooltipText = BeatmapsetsStrings.ShowDetailsUnfavourite; + } + else + { + Icon.Icon = FontAwesome.Regular.Heart; + TooltipText = BeatmapsetsStrings.ShowDetailsFavourite; + } + } } }