From c751328665b1c5e1ff2b51f7aa359570518d6f8d Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 13 Nov 2019 12:13:33 +0900 Subject: [PATCH] Disable button when not logged in --- .../BeatmapSet/Buttons/FavouriteButton.cs | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/osu.Game/Overlays/BeatmapSet/Buttons/FavouriteButton.cs b/osu.Game/Overlays/BeatmapSet/Buttons/FavouriteButton.cs index 163251d798..af0987d183 100644 --- a/osu.Game/Overlays/BeatmapSet/Buttons/FavouriteButton.cs +++ b/osu.Game/Overlays/BeatmapSet/Buttons/FavouriteButton.cs @@ -13,6 +13,7 @@ using osu.Game.Online.API; using osu.Game.Online.API.Requests; using osu.Game.Overlays.Notifications; +using osu.Game.Users; using osuTK; namespace osu.Game.Overlays.BeatmapSet.Buttons @@ -26,12 +27,23 @@ public class FavouriteButton : HeaderButton, IHasTooltip private PostBeatmapFavouriteRequest request; private DimmedLoadingLayer loading; - public string TooltipText => (favourited.Value ? "Unfavourite" : "Favourite") + " this beatmapset"; + private readonly Bindable localUser = new Bindable(); + + public string TooltipText + { + get + { + if (!Enabled.Value) return string.Empty; + + return (favourited.Value ? "Unfavourite" : "Favourite") + " this beatmapset"; + } + } [BackgroundDependencyLoader(true)] private void load(IAPIProvider api, NotificationOverlay notifications) { SpriteIcon icon; + AddRange(new Drawable[] { icon = new SpriteIcon @@ -45,14 +57,6 @@ private void load(IAPIProvider api, NotificationOverlay notifications) loading = new DimmedLoadingLayer(0.8f, 0.5f), }); - favourited.ValueChanged += favourited => icon.Icon = favourited.NewValue ? FontAwesome.Solid.Heart : FontAwesome.Regular.Heart; - - BeatmapSet.BindValueChanged(setInfo => - { - Enabled.Value = BeatmapSet.Value?.OnlineBeatmapSetID > 0; - favourited.Value = setInfo.NewValue?.OnlineInfo?.HasFavourited ?? false; - }, true); - Action = () => { if (loading.State.Value == Visibility.Visible) @@ -86,8 +90,22 @@ private void load(IAPIProvider api, NotificationOverlay notifications) api.Queue(request); }; + + favourited.ValueChanged += favourited => icon.Icon = favourited.NewValue ? FontAwesome.Solid.Heart : FontAwesome.Regular.Heart; + + localUser.BindTo(api.LocalUser); + localUser.BindValueChanged(_ => updateEnabled()); + + // must be run after setting the Action to ensure correct enabled state (setting an Action forces a button to be enabled). + BeatmapSet.BindValueChanged(setInfo => + { + updateEnabled(); + favourited.Value = setInfo.NewValue?.OnlineInfo?.HasFavourited ?? false; + }, true); } + private void updateEnabled() => Enabled.Value = !(localUser.Value is GuestUser) && BeatmapSet.Value?.OnlineBeatmapSetID > 0; + protected override void UpdateAfterChildren() { base.UpdateAfterChildren();