1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 07:32:55 +08:00

Implement PostBeatmapFavouriteRequest

This commit is contained in:
Andrei Zavatski 2019-10-07 15:36:23 +03:00
parent 84c13b93fc
commit 2d707b2b65
2 changed files with 59 additions and 1 deletions

View File

@ -0,0 +1,36 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.IO.Network;
using System.Net.Http;
namespace osu.Game.Online.API.Requests
{
public class PostBeatmapFavouriteRequest : APIRequest
{
private readonly int id;
private readonly BeatmapFavouriteAction action;
public PostBeatmapFavouriteRequest(int id, BeatmapFavouriteAction action)
{
this.id = id;
this.action = action;
}
protected override WebRequest CreateWebRequest()
{
var req = base.CreateWebRequest();
req.Method = HttpMethod.Post;
req.AddParameter(@"action", action.ToString().ToLowerInvariant());
return req;
}
protected override string Target => $@"beatmapsets/{id}/favourites";
}
public enum BeatmapFavouriteAction
{
Favourite,
UnFavourite
}
}

View File

@ -10,6 +10,9 @@ using osu.Framework.Graphics.Sprites;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.Backgrounds;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osuTK;
namespace osu.Game.Overlays.BeatmapSet.Buttons
@ -20,8 +23,11 @@ namespace osu.Game.Overlays.BeatmapSet.Buttons
private readonly Bindable<bool> favourited = new Bindable<bool>();
private PostBeatmapFavouriteRequest request;
private DimmedLoadingLayer loading;
[BackgroundDependencyLoader]
private void load()
private void load(IAPIProvider api)
{
Container pink;
SpriteIcon icon;
@ -55,6 +61,7 @@ namespace osu.Game.Overlays.BeatmapSet.Buttons
Size = new Vector2(18),
Shadow = false,
},
loading = new DimmedLoadingLayer(),
});
BeatmapSet.BindValueChanged(setInfo =>
@ -67,6 +74,8 @@ namespace osu.Game.Overlays.BeatmapSet.Buttons
favourited.ValueChanged += favourited =>
{
loading.Hide();
if (favourited.NewValue)
{
pink.FadeIn(200);
@ -78,6 +87,19 @@ namespace osu.Game.Overlays.BeatmapSet.Buttons
icon.Icon = FontAwesome.Regular.Heart;
}
};
Action = () =>
{
if (loading.State.Value == Visibility.Visible)
return;
loading.Show();
request?.Cancel();
request = new PostBeatmapFavouriteRequest(BeatmapSet.Value?.OnlineBeatmapSetID ?? 0, favourited.Value ? BeatmapFavouriteAction.UnFavourite : BeatmapFavouriteAction.Favourite);
request.Success += () => favourited.Value = !favourited.Value;
api.Queue(request);
};
}
protected override void UpdateAfterChildren()