1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-14 04:02:59 +08:00

Simplify tracking of beatmap sets in BeatmapCarousel

This commit is contained in:
Dean Herbert 2024-07-08 18:31:36 +09:00
parent e51d510ea3
commit 151c448535
No known key found for this signature in database

View File

@ -197,7 +197,6 @@ namespace osu.Game.Screens.Select
private CarouselRoot root; private CarouselRoot root;
private IDisposable? subscriptionSets; private IDisposable? subscriptionSets;
private IDisposable? subscriptionDeletedSets;
private IDisposable? subscriptionBeatmaps; private IDisposable? subscriptionBeatmaps;
private IDisposable? subscriptionHiddenBeatmaps; private IDisposable? subscriptionHiddenBeatmaps;
@ -253,6 +252,11 @@ namespace osu.Game.Screens.Select
[Resolved] [Resolved]
private RealmAccess realm { get; set; } = null!; private RealmAccess realm { get; set; } = null!;
/// <summary>
/// Track GUIDs of all sets in realm to allow handling deletions.
/// </summary>
private readonly List<Guid> realmBeatmapSets = new List<Guid>();
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
@ -262,45 +266,9 @@ namespace osu.Game.Screens.Select
// Can't use main subscriptions because we can't lookup deleted indices. // Can't use main subscriptions because we can't lookup deleted indices.
// https://github.com/realm/realm-dotnet/discussions/2634#discussioncomment-1605595. // https://github.com/realm/realm-dotnet/discussions/2634#discussioncomment-1605595.
subscriptionDeletedSets = realm.RegisterForNotifications(r => r.All<BeatmapSetInfo>().Where(s => s.DeletePending && !s.Protected), deletedBeatmapSetsChanged);
subscriptionHiddenBeatmaps = realm.RegisterForNotifications(r => r.All<BeatmapInfo>().Where(b => b.Hidden), beatmapsChanged); subscriptionHiddenBeatmaps = realm.RegisterForNotifications(r => r.All<BeatmapInfo>().Where(b => b.Hidden), beatmapsChanged);
} }
private void deletedBeatmapSetsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet? changes)
{
// If loading test beatmaps, avoid overwriting with realm subscription callbacks.
if (loadedTestBeatmaps)
return;
if (changes == null)
return;
var removeableSets = changes.InsertedIndices.Select(i => sender[i].ID).ToHashSet();
// This schedule is required to retain selection of beatmaps over an ImportAsUpdate operation.
// This is covered by TestPlaySongSelect.TestSelectionRetainedOnBeatmapUpdate.
//
// In short, we have specialised logic in `beatmapSetsChanged` (directly below) to infer that an
// update operation has occurred. For this to work, we need to confirm the `DeletePending` flag
// of the current selection.
//
// If we don't schedule the following code, it is possible for the `deleteBeatmapSetsChanged` handler
// to be invoked before the `beatmapSetsChanged` handler (realm call order seems non-deterministic)
// which will lead to the currently selected beatmap changing via `CarouselGroupEagerSelect`.
//
// We need a better path forward here. A few ideas:
// - Avoid the necessity of having realm subscriptions on deleted/hidden items, maybe by storing all guids in realm
// to a local list so we can better look them up on receiving `DeletedIndices`.
// - Add a new property on `BeatmapSetInfo` to link to the pre-update set, and use that to handle the update case.
Schedule(() =>
{
foreach (var set in removeableSets)
removeBeatmapSet(set);
invalidateAfterChange();
});
}
private void beatmapSetsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet? changes) private void beatmapSetsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet? changes)
{ {
// If loading test beatmaps, avoid overwriting with realm subscription callbacks. // If loading test beatmaps, avoid overwriting with realm subscription callbacks.
@ -312,32 +280,30 @@ namespace osu.Game.Screens.Select
if (changes == null) if (changes == null)
{ {
// During initial population, we must manually account for the fact that our original query was done on an async thread. realmBeatmapSets.Clear();
// Since then, there may have been imports or deletions. realmBeatmapSets.AddRange(sender.Select(r => r.ID));
// Here we manually catch up on any changes.
var realmSets = new HashSet<Guid>();
for (int i = 0; i < sender.Count; i++) foreach (var id in realmBeatmapSets)
realmSets.Add(sender[i].ID);
foreach (var id in realmSets)
{ {
if (!root.BeatmapSetsByID.ContainsKey(id)) if (!root.BeatmapSetsByID.ContainsKey(id))
setsRequiringUpdate.Add(realm.Realm.Find<BeatmapSetInfo>(id)!.Detach()); setsRequiringUpdate.Add(realm.Realm.Find<BeatmapSetInfo>(id)!.Detach());
} }
foreach (var id in root.BeatmapSetsByID.Keys)
{
if (!realmSets.Contains(id))
setsRequiringRemoval.Add(id);
}
} }
else else
{ {
foreach (int i in changes.NewModifiedIndices) foreach (int i in changes.DeletedIndices.OrderDescending())
setsRequiringUpdate.Add(sender[i].Detach()); {
setsRequiringRemoval.Add(realmBeatmapSets[i]);
realmBeatmapSets.RemoveAt(i);
}
foreach (int i in changes.InsertedIndices) foreach (int i in changes.InsertedIndices)
{
realmBeatmapSets.Insert(i, sender[i].ID);
setsRequiringUpdate.Add(sender[i].Detach());
}
foreach (int i in changes.NewModifiedIndices)
setsRequiringUpdate.Add(sender[i].Detach()); setsRequiringUpdate.Add(sender[i].Detach());
} }
@ -1312,7 +1278,6 @@ namespace osu.Game.Screens.Select
base.Dispose(isDisposing); base.Dispose(isDisposing);
subscriptionSets?.Dispose(); subscriptionSets?.Dispose();
subscriptionDeletedSets?.Dispose();
subscriptionBeatmaps?.Dispose(); subscriptionBeatmaps?.Dispose();
subscriptionHiddenBeatmaps?.Dispose(); subscriptionHiddenBeatmaps?.Dispose();
} }