From 5110ae82a1cd76a970ef11bfebf7559dea0c9514 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 12 Nov 2019 21:38:08 +0900 Subject: [PATCH] Tidy up implementation --- .../BeatmapSet/Buttons/FavouriteButton.cs | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/osu.Game/Overlays/BeatmapSet/Buttons/FavouriteButton.cs b/osu.Game/Overlays/BeatmapSet/Buttons/FavouriteButton.cs index f059e06214..33df1fa485 100644 --- a/osu.Game/Overlays/BeatmapSet/Buttons/FavouriteButton.cs +++ b/osu.Game/Overlays/BeatmapSet/Buttons/FavouriteButton.cs @@ -1,6 +1,7 @@ -// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System.Diagnostics; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Graphics; @@ -20,7 +21,7 @@ namespace osu.Game.Overlays.BeatmapSet.Buttons { public readonly Bindable BeatmapSet = new Bindable(); - private readonly Bindable favourited = new Bindable(); + private readonly BindableBool favourited = new BindableBool(); private PostBeatmapFavouriteRequest request; private DimmedLoadingLayer loading; @@ -44,40 +45,45 @@ namespace osu.Game.Overlays.BeatmapSet.Buttons loading = new DimmedLoadingLayer(), }); + favourited.ValueChanged += favourited => icon.Icon = favourited.NewValue ? FontAwesome.Solid.Heart : FontAwesome.Regular.Heart; + BeatmapSet.BindValueChanged(setInfo => { - if (setInfo.NewValue?.OnlineInfo?.HasFavourited == null) - return; - - favourited.Value = setInfo.NewValue.OnlineInfo.HasFavourited; - }); - - favourited.ValueChanged += favourited => - { - loading.Hide(); - - icon.Icon = favourited.NewValue ? FontAwesome.Solid.Heart : FontAwesome.Regular.Heart; - }; + Enabled.Value = BeatmapSet.Value?.OnlineBeatmapSetID > 0; + favourited.Value = setInfo.NewValue?.OnlineInfo?.HasFavourited ?? false; + }, true); Action = () => { if (loading.State.Value == Visibility.Visible) return; + // guaranteed by disabled state abvove. + Debug.Assert(BeatmapSet.Value.OnlineBeatmapSetID != null); + loading.Show(); request?.Cancel(); - request = new PostBeatmapFavouriteRequest(BeatmapSet.Value?.OnlineBeatmapSetID ?? 0, favourited.Value ? BeatmapFavouriteAction.UnFavourite : BeatmapFavouriteAction.Favourite); - request.Success += () => favourited.Value = !favourited.Value; - request.Failure += exception => + + request = new PostBeatmapFavouriteRequest(BeatmapSet.Value.OnlineBeatmapSetID.Value, favourited.Value ? BeatmapFavouriteAction.UnFavourite : BeatmapFavouriteAction.Favourite); + + request.Success += () => + { + favourited.Toggle(); + loading.Hide(); + }; + + request.Failure += e => { notifications.Post(new SimpleNotification { - Text = exception.Message, + Text = e.Message, Icon = FontAwesome.Solid.Times, }); + loading.Hide(); }; + api.Queue(request); }; }