diff --git a/osu.Game/Screens/Ranking/CollectionButton.cs b/osu.Game/Screens/Ranking/CollectionButton.cs index 4d53125005..804ffe9f75 100644 --- a/osu.Game/Screens/Ranking/CollectionButton.cs +++ b/osu.Game/Screens/Ranking/CollectionButton.cs @@ -1,26 +1,43 @@ // Copyright (c) ppy Pty Ltd . 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 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(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(), updateRealm); + + current.BindValueChanged(_ => updateState(), true); + } + + protected override void Dispose(bool isDisposing) + { + base.Dispose(isDisposing); + + collectionSubscription?.Dispose(); + } + + private void updateRealm(IRealmCollection 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); } }