mirror of
https://github.com/ppy/osu.git
synced 2024-12-17 12:22:55 +08:00
Update favorite button
This commit is contained in:
parent
3296beb003
commit
c16b7c5c70
@ -9,6 +9,7 @@ using osu.Framework.Graphics;
|
|||||||
using osu.Framework.Graphics.Shapes;
|
using osu.Framework.Graphics.Shapes;
|
||||||
using osu.Framework.Graphics.Sprites;
|
using osu.Framework.Graphics.Sprites;
|
||||||
using osu.Framework.Logging;
|
using osu.Framework.Logging;
|
||||||
|
using osu.Game.Beatmaps;
|
||||||
using osu.Game.Beatmaps.Drawables.Cards;
|
using osu.Game.Beatmaps.Drawables.Cards;
|
||||||
using osu.Game.Graphics;
|
using osu.Game.Graphics;
|
||||||
using osu.Game.Graphics.UserInterface;
|
using osu.Game.Graphics.UserInterface;
|
||||||
@ -24,15 +25,10 @@ namespace osu.Game.Screens.Ranking
|
|||||||
{
|
{
|
||||||
private readonly Box background;
|
private readonly Box background;
|
||||||
private readonly SpriteIcon icon;
|
private readonly SpriteIcon icon;
|
||||||
private readonly BindableWithCurrent<BeatmapSetFavouriteState> current;
|
|
||||||
|
|
||||||
public Bindable<BeatmapSetFavouriteState> Current
|
private readonly BeatmapSetInfo beatmapSetInfo;
|
||||||
{
|
private APIBeatmapSet beatmapSet;
|
||||||
get => current.Current;
|
private Bindable<BeatmapSetFavouriteState> current;
|
||||||
set => current.Current = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly APIBeatmapSet beatmapSet;
|
|
||||||
|
|
||||||
private PostBeatmapFavouriteRequest favouriteRequest;
|
private PostBeatmapFavouriteRequest favouriteRequest;
|
||||||
private LoadingLayer loading;
|
private LoadingLayer loading;
|
||||||
@ -45,10 +41,9 @@ namespace osu.Game.Screens.Ranking
|
|||||||
[Resolved]
|
[Resolved]
|
||||||
private OsuColour colours { get; set; }
|
private OsuColour colours { get; set; }
|
||||||
|
|
||||||
public FavouriteButton(APIBeatmapSet beatmapSet)
|
public FavouriteButton(BeatmapSetInfo beatmapSetInfo)
|
||||||
{
|
{
|
||||||
this.beatmapSet = beatmapSet;
|
this.beatmapSetInfo = beatmapSetInfo;
|
||||||
current = new BindableWithCurrent<BeatmapSetFavouriteState>(new BeatmapSetFavouriteState(this.beatmapSet.HasFavourited, this.beatmapSet.FavouriteCount));
|
|
||||||
|
|
||||||
Size = new Vector2(50, 30);
|
Size = new Vector2(50, 30);
|
||||||
|
|
||||||
@ -68,24 +63,42 @@ namespace osu.Game.Screens.Ranking
|
|||||||
},
|
},
|
||||||
loading = new LoadingLayer(true, false),
|
loading = new LoadingLayer(true, false),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Action = toggleFavouriteStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
[BackgroundDependencyLoader]
|
[BackgroundDependencyLoader]
|
||||||
private void load()
|
private void load()
|
||||||
{
|
{
|
||||||
updateState();
|
current = new BindableWithCurrent<BeatmapSetFavouriteState>(new BeatmapSetFavouriteState(false, 0));
|
||||||
|
current.BindValueChanged(_ => updateState(), true);
|
||||||
|
|
||||||
localUser.BindTo(api.LocalUser);
|
localUser.BindTo(api.LocalUser);
|
||||||
localUser.BindValueChanged(_ => updateEnabled());
|
localUser.BindValueChanged(_ => updateUser(), true);
|
||||||
|
|
||||||
Action = toggleFavouriteStatus;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void LoadComplete()
|
private void getBeatmapSet()
|
||||||
{
|
{
|
||||||
base.LoadComplete();
|
GetBeatmapSetRequest beatmapSetRequest;
|
||||||
|
beatmapSetRequest = new GetBeatmapSetRequest(beatmapSetInfo.OnlineID);
|
||||||
|
|
||||||
current.BindValueChanged(_ => updateState(), true);
|
loading.Show();
|
||||||
|
beatmapSetRequest.Success += beatmapSet =>
|
||||||
|
{
|
||||||
|
this.beatmapSet = beatmapSet;
|
||||||
|
current.Value = new BeatmapSetFavouriteState(this.beatmapSet.HasFavourited, this.beatmapSet.FavouriteCount);
|
||||||
|
|
||||||
|
loading.Hide();
|
||||||
|
Enabled.Value = true;
|
||||||
|
};
|
||||||
|
beatmapSetRequest.Failure += e =>
|
||||||
|
{
|
||||||
|
Logger.Error(e, $"Failed to fetch beatmap info: {e.Message}");
|
||||||
|
|
||||||
|
loading.Hide();
|
||||||
|
Enabled.Value = false;
|
||||||
|
};
|
||||||
|
api.Queue(beatmapSetRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void toggleFavouriteStatus()
|
private void toggleFavouriteStatus()
|
||||||
@ -118,7 +131,18 @@ namespace osu.Game.Screens.Ranking
|
|||||||
api.Queue(favouriteRequest);
|
api.Queue(favouriteRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateEnabled() => Enabled.Value = !(localUser.Value is GuestUser) && beatmapSet.OnlineID > 0;
|
private void updateUser()
|
||||||
|
{
|
||||||
|
if (!(localUser.Value is GuestUser) && beatmapSetInfo.OnlineID > 0)
|
||||||
|
getBeatmapSet();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Enabled.Value = false;
|
||||||
|
current.Value = new BeatmapSetFavouriteState(false, 0);
|
||||||
|
updateState();
|
||||||
|
TooltipText = BeatmapsetsStrings.ShowDetailsFavouriteLogin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void updateState()
|
private void updateState()
|
||||||
{
|
{
|
||||||
|
@ -16,7 +16,6 @@ using osu.Framework.Graphics.Cursor;
|
|||||||
using osu.Framework.Graphics.Shapes;
|
using osu.Framework.Graphics.Shapes;
|
||||||
using osu.Framework.Input.Bindings;
|
using osu.Framework.Input.Bindings;
|
||||||
using osu.Framework.Input.Events;
|
using osu.Framework.Input.Events;
|
||||||
using osu.Framework.Logging;
|
|
||||||
using osu.Framework.Screens;
|
using osu.Framework.Screens;
|
||||||
using osu.Game.Graphics;
|
using osu.Game.Graphics;
|
||||||
using osu.Game.Graphics.Containers;
|
using osu.Game.Graphics.Containers;
|
||||||
@ -24,7 +23,6 @@ using osu.Game.Graphics.UserInterface;
|
|||||||
using osu.Game.Input.Bindings;
|
using osu.Game.Input.Bindings;
|
||||||
using osu.Game.Localisation;
|
using osu.Game.Localisation;
|
||||||
using osu.Game.Online.API;
|
using osu.Game.Online.API;
|
||||||
using osu.Game.Online.API.Requests;
|
|
||||||
using osu.Game.Online.Placeholders;
|
using osu.Game.Online.Placeholders;
|
||||||
using osu.Game.Overlays;
|
using osu.Game.Overlays;
|
||||||
using osu.Game.Scoring;
|
using osu.Game.Scoring;
|
||||||
@ -217,25 +215,12 @@ namespace osu.Game.Screens.Ranking
|
|||||||
buttons.Add(new CollectionButton(Score.BeatmapInfo) { Width = 75 });
|
buttons.Add(new CollectionButton(Score.BeatmapInfo) { Width = 75 });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do not render if user is not logged in or the mapset does not have a valid online ID.
|
if (Score?.BeatmapInfo?.BeatmapSet != null && Score.BeatmapInfo.BeatmapSet.OnlineID > 0)
|
||||||
if (api.IsLoggedIn && Score?.BeatmapInfo?.BeatmapSet != null && Score.BeatmapInfo.BeatmapSet.OnlineID > 0)
|
|
||||||
{
|
{
|
||||||
GetBeatmapSetRequest beatmapSetRequest;
|
buttons.Add(new FavouriteButton(Score.BeatmapInfo.BeatmapSet)
|
||||||
beatmapSetRequest = new GetBeatmapSetRequest(Score.BeatmapInfo.BeatmapSet.OnlineID);
|
|
||||||
|
|
||||||
beatmapSetRequest.Success += (beatmapSet) =>
|
|
||||||
{
|
{
|
||||||
buttons.Add(new FavouriteButton(beatmapSet)
|
Width = 75
|
||||||
{
|
});
|
||||||
Width = 75
|
|
||||||
});
|
|
||||||
};
|
|
||||||
beatmapSetRequest.Failure += e =>
|
|
||||||
{
|
|
||||||
Logger.Error(e, $"Failed to fetch beatmap info: {e.Message}");
|
|
||||||
};
|
|
||||||
|
|
||||||
api.Queue(beatmapSetRequest);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user