Disable button when not logged in

This commit is contained in:
Dean Herbert 2019-11-13 12:13:33 +09:00
parent f18263aa70
commit c751328665
1 changed files with 27 additions and 9 deletions

View File

@ -13,6 +13,7 @@
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests;
using osu.Game.Overlays.Notifications; using osu.Game.Overlays.Notifications;
using osu.Game.Users;
using osuTK; using osuTK;
namespace osu.Game.Overlays.BeatmapSet.Buttons namespace osu.Game.Overlays.BeatmapSet.Buttons
@ -26,12 +27,23 @@ public class FavouriteButton : HeaderButton, IHasTooltip
private PostBeatmapFavouriteRequest request; private PostBeatmapFavouriteRequest request;
private DimmedLoadingLayer loading; private DimmedLoadingLayer loading;
public string TooltipText => (favourited.Value ? "Unfavourite" : "Favourite") + " this beatmapset"; private readonly Bindable<User> localUser = new Bindable<User>();
public string TooltipText
{
get
{
if (!Enabled.Value) return string.Empty;
return (favourited.Value ? "Unfavourite" : "Favourite") + " this beatmapset";
}
}
[BackgroundDependencyLoader(true)] [BackgroundDependencyLoader(true)]
private void load(IAPIProvider api, NotificationOverlay notifications) private void load(IAPIProvider api, NotificationOverlay notifications)
{ {
SpriteIcon icon; SpriteIcon icon;
AddRange(new Drawable[] AddRange(new Drawable[]
{ {
icon = new SpriteIcon icon = new SpriteIcon
@ -45,14 +57,6 @@ private void load(IAPIProvider api, NotificationOverlay notifications)
loading = new DimmedLoadingLayer(0.8f, 0.5f), 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 = () => Action = () =>
{ {
if (loading.State.Value == Visibility.Visible) if (loading.State.Value == Visibility.Visible)
@ -86,8 +90,22 @@ private void load(IAPIProvider api, NotificationOverlay notifications)
api.Queue(request); 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() protected override void UpdateAfterChildren()
{ {
base.UpdateAfterChildren(); base.UpdateAfterChildren();