mirror of
https://github.com/ppy/osu.git
synced 2025-02-14 02:43:02 +08:00
Move toggle code into own method for readability
This commit is contained in:
parent
9a2425f316
commit
72489b32f9
@ -27,47 +27,48 @@ namespace osu.Game.Beatmaps.Drawables.Cards.Buttons
|
|||||||
|
|
||||||
private PostBeatmapFavouriteRequest favouriteRequest;
|
private PostBeatmapFavouriteRequest favouriteRequest;
|
||||||
|
|
||||||
|
[Resolved]
|
||||||
|
private IAPIProvider api { get; set; }
|
||||||
|
|
||||||
public FavouriteButton(APIBeatmapSet beatmapSet)
|
public FavouriteButton(APIBeatmapSet beatmapSet)
|
||||||
{
|
{
|
||||||
current = new BindableWithCurrent<BeatmapSetFavouriteState>(new BeatmapSetFavouriteState(beatmapSet.HasFavourited, beatmapSet.FavouriteCount));
|
current = new BindableWithCurrent<BeatmapSetFavouriteState>(new BeatmapSetFavouriteState(beatmapSet.HasFavourited, beatmapSet.FavouriteCount));
|
||||||
onlineBeatmapID = beatmapSet.OnlineID;
|
onlineBeatmapID = beatmapSet.OnlineID;
|
||||||
}
|
}
|
||||||
|
|
||||||
[BackgroundDependencyLoader]
|
|
||||||
private void load(IAPIProvider api)
|
|
||||||
{
|
|
||||||
Action = () =>
|
|
||||||
{
|
|
||||||
var actionType = current.Value.Favourited ? BeatmapFavouriteAction.UnFavourite : BeatmapFavouriteAction.Favourite;
|
|
||||||
|
|
||||||
favouriteRequest?.Cancel();
|
|
||||||
favouriteRequest = new PostBeatmapFavouriteRequest(onlineBeatmapID, actionType);
|
|
||||||
|
|
||||||
Enabled.Value = false;
|
|
||||||
favouriteRequest.Success += () =>
|
|
||||||
{
|
|
||||||
bool favourited = actionType == BeatmapFavouriteAction.Favourite;
|
|
||||||
|
|
||||||
current.Value = new BeatmapSetFavouriteState(favourited, current.Value.FavouriteCount + (favourited ? 1 : -1));
|
|
||||||
|
|
||||||
Enabled.Value = true;
|
|
||||||
};
|
|
||||||
favouriteRequest.Failure += e =>
|
|
||||||
{
|
|
||||||
Logger.Error(e, $"Failed to {actionType.ToString().ToLower()} beatmap: {e.Message}");
|
|
||||||
Enabled.Value = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
api.Queue(favouriteRequest);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void LoadComplete()
|
protected override void LoadComplete()
|
||||||
{
|
{
|
||||||
base.LoadComplete();
|
base.LoadComplete();
|
||||||
|
|
||||||
|
Action = toggleFavouriteStatus;
|
||||||
current.BindValueChanged(_ => updateState(), true);
|
current.BindValueChanged(_ => updateState(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void toggleFavouriteStatus()
|
||||||
|
{
|
||||||
|
var actionType = current.Value.Favourited ? BeatmapFavouriteAction.UnFavourite : BeatmapFavouriteAction.Favourite;
|
||||||
|
|
||||||
|
favouriteRequest?.Cancel();
|
||||||
|
favouriteRequest = new PostBeatmapFavouriteRequest(onlineBeatmapID, actionType);
|
||||||
|
|
||||||
|
Enabled.Value = false;
|
||||||
|
favouriteRequest.Success += () =>
|
||||||
|
{
|
||||||
|
bool favourited = actionType == BeatmapFavouriteAction.Favourite;
|
||||||
|
|
||||||
|
current.Value = new BeatmapSetFavouriteState(favourited, current.Value.FavouriteCount + (favourited ? 1 : -1));
|
||||||
|
|
||||||
|
Enabled.Value = true;
|
||||||
|
};
|
||||||
|
favouriteRequest.Failure += e =>
|
||||||
|
{
|
||||||
|
Logger.Error(e, $"Failed to {actionType.ToString().ToLower()} beatmap: {e.Message}");
|
||||||
|
Enabled.Value = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
api.Queue(favouriteRequest);
|
||||||
|
}
|
||||||
|
|
||||||
private void updateState()
|
private void updateState()
|
||||||
{
|
{
|
||||||
if (current.Value.Favourited)
|
if (current.Value.Favourited)
|
||||||
|
Loading…
Reference in New Issue
Block a user