Update collection state when users add/remove from collection

This commit is contained in:
Layendan 2024-07-30 15:22:41 -07:00
parent bc25e5d706
commit 36bd83bb80

View File

@ -1,26 +1,43 @@
// 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 System;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Beatmaps;
using osu.Game.Collections;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
using osuTK;
using Realms;
namespace osu.Game.Screens.Ranking
{
public partial class CollectionButton : GrayButton, IHasPopover
{
private readonly BeatmapInfo beatmapInfo;
private readonly Bindable<bool> current;
[Resolved]
private RealmAccess realmAccess { get; set; } = null!;
private IDisposable? collectionSubscription;
[Resolved]
private OsuColour colours { get; set; } = null!;
public CollectionButton(BeatmapInfo beatmapInfo)
: base(FontAwesome.Solid.Book)
{
this.beatmapInfo = beatmapInfo;
current = new Bindable<bool>(false);
Size = new Vector2(75, 30);
@ -28,13 +45,37 @@ namespace osu.Game.Screens.Ranking
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
private void load()
{
Background.Colour = colours.Green;
Action = this.ShowPopover;
}
protected override void LoadComplete()
{
base.LoadComplete();
collectionSubscription = realmAccess.RegisterForNotifications(r => r.All<BeatmapCollection>(), updateRealm);
current.BindValueChanged(_ => updateState(), true);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
collectionSubscription?.Dispose();
}
private void updateRealm(IRealmCollection<BeatmapCollection> sender, ChangeSet? changes)
{
current.Value = sender.AsEnumerable().Any(c => c.BeatmapMD5Hashes.Contains(beatmapInfo.MD5Hash));
}
private void updateState()
{
Background.FadeColour(current.Value ? colours.Green : colours.Gray4, 500, Easing.InOutExpo);
}
public Popover GetPopover() => new CollectionPopover(beatmapInfo);
}
}