1
0
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:
Layendan 2024-07-21 10:01:06 -07:00 committed by Dean Herbert
parent 3296beb003
commit c16b7c5c70
No known key found for this signature in database
2 changed files with 47 additions and 38 deletions

View File

@ -9,6 +9,7 @@ using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Logging;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables.Cards;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
@ -24,15 +25,10 @@ namespace osu.Game.Screens.Ranking
{
private readonly Box background;
private readonly SpriteIcon icon;
private readonly BindableWithCurrent<BeatmapSetFavouriteState> current;
public Bindable<BeatmapSetFavouriteState> Current
{
get => current.Current;
set => current.Current = value;
}
private readonly APIBeatmapSet beatmapSet;
private readonly BeatmapSetInfo beatmapSetInfo;
private APIBeatmapSet beatmapSet;
private Bindable<BeatmapSetFavouriteState> current;
private PostBeatmapFavouriteRequest favouriteRequest;
private LoadingLayer loading;
@ -45,10 +41,9 @@ namespace osu.Game.Screens.Ranking
[Resolved]
private OsuColour colours { get; set; }
public FavouriteButton(APIBeatmapSet beatmapSet)
public FavouriteButton(BeatmapSetInfo beatmapSetInfo)
{
this.beatmapSet = beatmapSet;
current = new BindableWithCurrent<BeatmapSetFavouriteState>(new BeatmapSetFavouriteState(this.beatmapSet.HasFavourited, this.beatmapSet.FavouriteCount));
this.beatmapSetInfo = beatmapSetInfo;
Size = new Vector2(50, 30);
@ -68,24 +63,42 @@ namespace osu.Game.Screens.Ranking
},
loading = new LoadingLayer(true, false),
};
Action = toggleFavouriteStatus;
}
[BackgroundDependencyLoader]
private void load()
{
updateState();
current = new BindableWithCurrent<BeatmapSetFavouriteState>(new BeatmapSetFavouriteState(false, 0));
current.BindValueChanged(_ => updateState(), true);
localUser.BindTo(api.LocalUser);
localUser.BindValueChanged(_ => updateEnabled());
Action = toggleFavouriteStatus;
localUser.BindValueChanged(_ => updateUser(), true);
}
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()
@ -118,7 +131,18 @@ namespace osu.Game.Screens.Ranking
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()
{

View File

@ -16,7 +16,6 @@ using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Logging;
using osu.Framework.Screens;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
@ -24,7 +23,6 @@ using osu.Game.Graphics.UserInterface;
using osu.Game.Input.Bindings;
using osu.Game.Localisation;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.Placeholders;
using osu.Game.Overlays;
using osu.Game.Scoring;
@ -217,25 +215,12 @@ namespace osu.Game.Screens.Ranking
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 (api.IsLoggedIn && Score?.BeatmapInfo?.BeatmapSet != null && Score.BeatmapInfo.BeatmapSet.OnlineID > 0)
if (Score?.BeatmapInfo?.BeatmapSet != null && Score.BeatmapInfo.BeatmapSet.OnlineID > 0)
{
GetBeatmapSetRequest beatmapSetRequest;
beatmapSetRequest = new GetBeatmapSetRequest(Score.BeatmapInfo.BeatmapSet.OnlineID);
beatmapSetRequest.Success += (beatmapSet) =>
buttons.Add(new FavouriteButton(Score.BeatmapInfo.BeatmapSet)
{
buttons.Add(new FavouriteButton(beatmapSet)
{
Width = 75
});
};
beatmapSetRequest.Failure += e =>
{
Logger.Error(e, $"Failed to fetch beatmap info: {e.Message}");
};
api.Queue(beatmapSetRequest);
Width = 75
});
}
}