From 6d60aa7d9cc45799c40c72f119db2c0936aa606f Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 17 Dec 2021 19:09:35 +0900 Subject: [PATCH] Replace `TopLocalRank` event flow with realm subscriptions --- .../Screens/Select/Carousel/TopLocalRank.cs | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/osu.Game/Screens/Select/Carousel/TopLocalRank.cs b/osu.Game/Screens/Select/Carousel/TopLocalRank.cs index a83cb08e1f..2ade213e8b 100644 --- a/osu.Game/Screens/Select/Carousel/TopLocalRank.cs +++ b/osu.Game/Screens/Select/Carousel/TopLocalRank.cs @@ -1,6 +1,7 @@ // 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; @@ -19,9 +20,6 @@ namespace osu.Game.Screens.Select.Carousel { private readonly BeatmapInfo beatmapInfo; - [Resolved] - private ScoreManager scores { get; set; } - [Resolved] private IBindable ruleset { get; set; } @@ -40,26 +38,34 @@ namespace osu.Game.Screens.Select.Carousel [BackgroundDependencyLoader] private void load() { - scores.ItemUpdated += scoreChanged; - scores.ItemRemoved += scoreChanged; - ruleset.ValueChanged += _ => fetchAndLoadTopScore(); fetchAndLoadTopScore(); } - private void scoreChanged(ScoreInfo score) + protected override void LoadComplete() { - if (score.BeatmapInfoID == beatmapInfo.ID) - fetchAndLoadTopScore(); + base.LoadComplete(); + + scoreSubscription = realmFactory.Context.All().Where(s => s.BeatmapInfo.ID == beatmapInfo.ID).QueryAsyncWithNotifications((_, changes, ___) => + { + if (changes == null) + return; + + fetchTopScoreRank(); + }); } + private IDisposable scoreSubscription; + private ScheduledDelegate scheduledRankUpdate; private void fetchAndLoadTopScore() { + // TODO: this lookup likely isn't required, we can use the results of the subscription directly. var rank = fetchTopScoreRank(); - scheduledRankUpdate = Schedule(() => + + scheduledRankUpdate = Scheduler.Add(() => { Rank = rank; @@ -89,11 +95,7 @@ namespace osu.Game.Screens.Select.Carousel { base.Dispose(isDisposing); - if (scores != null) - { - scores.ItemUpdated -= scoreChanged; - scores.ItemRemoved -= scoreChanged; - } + scoreSubscription?.Dispose(); } } }