From 72489b32f922bc38f5306e5556411f32c5c582d1 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 8 Nov 2021 21:39:16 +0900 Subject: [PATCH] Move toggle code into own method for readability --- .../Cards/Buttons/FavouriteButton.cs | 59 ++++++++++--------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/osu.Game/Beatmaps/Drawables/Cards/Buttons/FavouriteButton.cs b/osu.Game/Beatmaps/Drawables/Cards/Buttons/FavouriteButton.cs index 0b43b1c619..a61065b468 100644 --- a/osu.Game/Beatmaps/Drawables/Cards/Buttons/FavouriteButton.cs +++ b/osu.Game/Beatmaps/Drawables/Cards/Buttons/FavouriteButton.cs @@ -27,47 +27,48 @@ namespace osu.Game.Beatmaps.Drawables.Cards.Buttons private PostBeatmapFavouriteRequest favouriteRequest; + [Resolved] + private IAPIProvider api { get; set; } + public FavouriteButton(APIBeatmapSet beatmapSet) { current = new BindableWithCurrent(new BeatmapSetFavouriteState(beatmapSet.HasFavourited, beatmapSet.FavouriteCount)); onlineBeatmapID = beatmapSet.OnlineID; } - [BackgroundDependencyLoader] - private void load(IAPIProvider api) - { - Action = () => - { - var actionType = current.Value.Favourited ? BeatmapFavouriteAction.UnFavourite : BeatmapFavouriteAction.Favourite; - - favouriteRequest?.Cancel(); - favouriteRequest = new PostBeatmapFavouriteRequest(onlineBeatmapID, actionType); - - Enabled.Value = false; - favouriteRequest.Success += () => - { - bool favourited = actionType == BeatmapFavouriteAction.Favourite; - - current.Value = new BeatmapSetFavouriteState(favourited, current.Value.FavouriteCount + (favourited ? 1 : -1)); - - Enabled.Value = true; - }; - favouriteRequest.Failure += e => - { - Logger.Error(e, $"Failed to {actionType.ToString().ToLower()} beatmap: {e.Message}"); - Enabled.Value = true; - }; - - api.Queue(favouriteRequest); - }; - } - protected override void LoadComplete() { base.LoadComplete(); + + Action = toggleFavouriteStatus; current.BindValueChanged(_ => updateState(), true); } + private void toggleFavouriteStatus() + { + var actionType = current.Value.Favourited ? BeatmapFavouriteAction.UnFavourite : BeatmapFavouriteAction.Favourite; + + favouriteRequest?.Cancel(); + favouriteRequest = new PostBeatmapFavouriteRequest(onlineBeatmapID, actionType); + + Enabled.Value = false; + favouriteRequest.Success += () => + { + bool favourited = actionType == BeatmapFavouriteAction.Favourite; + + current.Value = new BeatmapSetFavouriteState(favourited, current.Value.FavouriteCount + (favourited ? 1 : -1)); + + Enabled.Value = true; + }; + favouriteRequest.Failure += e => + { + Logger.Error(e, $"Failed to {actionType.ToString().ToLower()} beatmap: {e.Message}"); + Enabled.Value = true; + }; + + api.Queue(favouriteRequest); + } + private void updateState() { if (current.Value.Favourited)