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