2019-11-12 20:38:16 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
2019-01-24 16:43:03 +08:00
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-11-12 20:38:08 +08:00
|
|
|
|
using System.Diagnostics;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2019-10-07 20:41:05 +08:00
|
|
|
|
using osu.Framework.Graphics.Cursor;
|
2019-03-27 18:29:27 +08:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2019-07-22 19:35:18 +08:00
|
|
|
|
using osu.Game.Beatmaps;
|
2019-10-07 20:36:23 +08:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
|
using osu.Game.Online.API.Requests;
|
2019-10-16 18:58:29 +08:00
|
|
|
|
using osu.Game.Overlays.Notifications;
|
2019-11-13 11:13:33 +08:00
|
|
|
|
using osu.Game.Users;
|
2018-11-20 15:51:59 +08:00
|
|
|
|
using osuTK;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-04-18 15:04:02 +08:00
|
|
|
|
namespace osu.Game.Overlays.BeatmapSet.Buttons
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-10-07 20:41:05 +08:00
|
|
|
|
public class FavouriteButton : HeaderButton, IHasTooltip
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-07-22 19:35:18 +08:00
|
|
|
|
public readonly Bindable<BeatmapSetInfo> BeatmapSet = new Bindable<BeatmapSetInfo>();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-11-12 20:38:08 +08:00
|
|
|
|
private readonly BindableBool favourited = new BindableBool();
|
2019-07-22 21:14:09 +08:00
|
|
|
|
|
2019-10-07 20:36:23 +08:00
|
|
|
|
private PostBeatmapFavouriteRequest request;
|
2020-02-21 14:31:40 +08:00
|
|
|
|
private LoadingLayer loading;
|
2019-10-07 20:36:23 +08:00
|
|
|
|
|
2020-12-18 14:16:36 +08:00
|
|
|
|
private readonly IBindable<User> localUser = new Bindable<User>();
|
2019-11-13 11:13:33 +08:00
|
|
|
|
|
|
|
|
|
public string TooltipText
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (!Enabled.Value) return string.Empty;
|
|
|
|
|
|
|
|
|
|
return (favourited.Value ? "Unfavourite" : "Favourite") + " this beatmapset";
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-07 20:41:05 +08:00
|
|
|
|
|
2019-10-16 18:58:29 +08:00
|
|
|
|
[BackgroundDependencyLoader(true)]
|
|
|
|
|
private void load(IAPIProvider api, NotificationOverlay notifications)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
SpriteIcon icon;
|
2019-11-13 11:13:33 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
AddRange(new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
icon = new SpriteIcon
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
2019-04-02 18:55:24 +08:00
|
|
|
|
Icon = FontAwesome.Regular.Heart,
|
2018-04-13 17:19:50 +08:00
|
|
|
|
Size = new Vector2(18),
|
|
|
|
|
Shadow = false,
|
|
|
|
|
},
|
2021-01-04 21:42:39 +08:00
|
|
|
|
loading = new LoadingLayer(true, false),
|
2018-04-13 17:19:50 +08:00
|
|
|
|
});
|
|
|
|
|
|
2019-10-07 20:36:23 +08:00
|
|
|
|
Action = () =>
|
|
|
|
|
{
|
2019-11-13 05:00:08 +08:00
|
|
|
|
// guaranteed by disabled state above.
|
2019-11-12 20:38:08 +08:00
|
|
|
|
Debug.Assert(BeatmapSet.Value.OnlineBeatmapSetID != null);
|
|
|
|
|
|
2019-10-07 20:36:23 +08:00
|
|
|
|
loading.Show();
|
|
|
|
|
|
|
|
|
|
request?.Cancel();
|
2019-11-12 20:38:08 +08:00
|
|
|
|
|
|
|
|
|
request = new PostBeatmapFavouriteRequest(BeatmapSet.Value.OnlineBeatmapSetID.Value, favourited.Value ? BeatmapFavouriteAction.UnFavourite : BeatmapFavouriteAction.Favourite);
|
|
|
|
|
|
|
|
|
|
request.Success += () =>
|
|
|
|
|
{
|
|
|
|
|
favourited.Toggle();
|
|
|
|
|
loading.Hide();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
request.Failure += e =>
|
2019-10-16 18:58:29 +08:00
|
|
|
|
{
|
2019-11-13 05:01:13 +08:00
|
|
|
|
notifications?.Post(new SimpleNotification
|
2019-10-16 18:58:29 +08:00
|
|
|
|
{
|
2019-11-12 20:38:08 +08:00
|
|
|
|
Text = e.Message,
|
2019-11-12 18:34:20 +08:00
|
|
|
|
Icon = FontAwesome.Solid.Times,
|
|
|
|
|
});
|
2019-11-12 20:38:08 +08:00
|
|
|
|
|
2019-11-12 18:34:20 +08:00
|
|
|
|
loading.Hide();
|
2019-10-16 18:58:29 +08:00
|
|
|
|
};
|
2019-11-12 20:38:08 +08:00
|
|
|
|
|
2019-10-07 20:36:23 +08:00
|
|
|
|
api.Queue(request);
|
|
|
|
|
};
|
2019-11-13 11:13:33 +08:00
|
|
|
|
|
|
|
|
|
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);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-13 11:13:33 +08:00
|
|
|
|
private void updateEnabled() => Enabled.Value = !(localUser.Value is GuestUser) && BeatmapSet.Value?.OnlineBeatmapSetID > 0;
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
protected override void UpdateAfterChildren()
|
|
|
|
|
{
|
|
|
|
|
base.UpdateAfterChildren();
|
|
|
|
|
|
|
|
|
|
Width = DrawHeight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|