1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-15 00:13:19 +08:00

Replace BeatmapDownloadTracker event flow with realm subscriptions

This commit is contained in:
Dean Herbert 2021-12-17 18:53:46 +09:00
parent 5dc497e949
commit 00e9f0d41e
2 changed files with 30 additions and 33 deletions

View File

@ -2,8 +2,10 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Online.API; using osu.Game.Online.API;
#nullable enable #nullable enable
@ -12,37 +14,47 @@ namespace osu.Game.Online
{ {
public class BeatmapDownloadTracker : DownloadTracker<IBeatmapSetInfo> public class BeatmapDownloadTracker : DownloadTracker<IBeatmapSetInfo>
{ {
[Resolved(CanBeNull = true)]
protected BeatmapManager? Manager { get; private set; }
[Resolved(CanBeNull = true)] [Resolved(CanBeNull = true)]
protected BeatmapModelDownloader? Downloader { get; private set; } protected BeatmapModelDownloader? Downloader { get; private set; }
private ArchiveDownloadRequest<IBeatmapSetInfo>? attachedRequest; private ArchiveDownloadRequest<IBeatmapSetInfo>? attachedRequest;
private IDisposable? realmSubscription;
[Resolved]
private RealmContextFactory realmContextFactory { get; set; } = null!;
public BeatmapDownloadTracker(IBeatmapSetInfo trackedItem) public BeatmapDownloadTracker(IBeatmapSetInfo trackedItem)
: base(trackedItem) : base(trackedItem)
{ {
} }
[BackgroundDependencyLoader(true)] protected override void LoadComplete()
private void load()
{ {
if (Manager == null || Downloader == null) base.LoadComplete();
if (Downloader == null)
return; return;
Downloader.DownloadBegan += downloadBegan;
Downloader.DownloadFailed += downloadFailed;
// Used to interact with manager classes that don't support interface types. Will eventually be replaced. // Used to interact with manager classes that don't support interface types. Will eventually be replaced.
var beatmapSetInfo = new BeatmapSetInfo { OnlineID = TrackedItem.OnlineID }; var beatmapSetInfo = new BeatmapSetInfo { OnlineID = TrackedItem.OnlineID };
if (Manager.IsAvailableLocally(beatmapSetInfo)) realmSubscription = realmContextFactory.Context.All<BeatmapSetInfo>().Where(s => s.OnlineID == TrackedItem.OnlineID).QueryAsyncWithNotifications((items, changes, ___) =>
UpdateState(DownloadState.LocallyAvailable); {
else if (items.Any())
attachDownload(Downloader.GetExistingDownload(beatmapSetInfo)); Schedule(() => UpdateState(DownloadState.LocallyAvailable));
else
Downloader.DownloadBegan += downloadBegan; {
Downloader.DownloadFailed += downloadFailed; Schedule(() =>
Manager.ItemUpdated += itemUpdated; {
Manager.ItemRemoved += itemRemoved; UpdateState(DownloadState.NotDownloaded);
attachDownload(Downloader.GetExistingDownload(beatmapSetInfo));
});
}
});
} }
private void downloadBegan(ArchiveDownloadRequest<IBeatmapSetInfo> request) => Schedule(() => private void downloadBegan(ArchiveDownloadRequest<IBeatmapSetInfo> request) => Schedule(() =>
@ -97,18 +109,6 @@ namespace osu.Game.Online
private void onRequestFailure(Exception e) => Schedule(() => attachDownload(null)); private void onRequestFailure(Exception e) => Schedule(() => attachDownload(null));
private void itemUpdated(BeatmapSetInfo item) => Schedule(() =>
{
if (checkEquality(item, TrackedItem))
UpdateState(DownloadState.LocallyAvailable);
});
private void itemRemoved(BeatmapSetInfo item) => Schedule(() =>
{
if (checkEquality(item, TrackedItem))
UpdateState(DownloadState.NotDownloaded);
});
private bool checkEquality(IBeatmapSetInfo x, IBeatmapSetInfo y) => x.OnlineID == y.OnlineID; private bool checkEquality(IBeatmapSetInfo x, IBeatmapSetInfo y) => x.OnlineID == y.OnlineID;
#region Disposal #region Disposal
@ -118,17 +118,13 @@ namespace osu.Game.Online
base.Dispose(isDisposing); base.Dispose(isDisposing);
attachDownload(null); attachDownload(null);
realmSubscription?.Dispose();
if (Downloader != null) if (Downloader != null)
{ {
Downloader.DownloadBegan -= downloadBegan; Downloader.DownloadBegan -= downloadBegan;
Downloader.DownloadFailed -= downloadFailed; Downloader.DownloadFailed -= downloadFailed;
} }
if (Manager != null)
{
Manager.ItemUpdated -= itemUpdated;
Manager.ItemRemoved -= itemRemoved;
}
} }
#endregion #endregion

View File

@ -187,6 +187,7 @@ namespace osu.Game.Stores
item.Realm.Write(r => item.DeletePending = false); item.Realm.Write(r => item.DeletePending = false);
} }
// TODO: delete or abstract
public virtual bool IsAvailableLocally(TModel model) => false; // Not relevant for skins since they can't be downloaded yet. public virtual bool IsAvailableLocally(TModel model) => false; // Not relevant for skins since they can't be downloaded yet.
public void Update(TModel skin) public void Update(TModel skin)