mirror of
https://github.com/ppy/osu.git
synced 2025-02-12 15:23:21 +08:00
Replace BeatmapDownloadTracker
event flow with realm subscriptions
This commit is contained in:
parent
5dc497e949
commit
00e9f0d41e
@ -2,8 +2,10 @@
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Game.Beatmaps;
|
||||
using osu.Game.Database;
|
||||
using osu.Game.Online.API;
|
||||
|
||||
#nullable enable
|
||||
@ -12,37 +14,47 @@ namespace osu.Game.Online
|
||||
{
|
||||
public class BeatmapDownloadTracker : DownloadTracker<IBeatmapSetInfo>
|
||||
{
|
||||
[Resolved(CanBeNull = true)]
|
||||
protected BeatmapManager? Manager { get; private set; }
|
||||
|
||||
[Resolved(CanBeNull = true)]
|
||||
protected BeatmapModelDownloader? Downloader { get; private set; }
|
||||
|
||||
private ArchiveDownloadRequest<IBeatmapSetInfo>? attachedRequest;
|
||||
|
||||
private IDisposable? realmSubscription;
|
||||
|
||||
[Resolved]
|
||||
private RealmContextFactory realmContextFactory { get; set; } = null!;
|
||||
|
||||
public BeatmapDownloadTracker(IBeatmapSetInfo trackedItem)
|
||||
: base(trackedItem)
|
||||
{
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader(true)]
|
||||
private void load()
|
||||
protected override void LoadComplete()
|
||||
{
|
||||
if (Manager == null || Downloader == null)
|
||||
base.LoadComplete();
|
||||
|
||||
if (Downloader == null)
|
||||
return;
|
||||
|
||||
Downloader.DownloadBegan += downloadBegan;
|
||||
Downloader.DownloadFailed += downloadFailed;
|
||||
|
||||
// Used to interact with manager classes that don't support interface types. Will eventually be replaced.
|
||||
var beatmapSetInfo = new BeatmapSetInfo { OnlineID = TrackedItem.OnlineID };
|
||||
|
||||
if (Manager.IsAvailableLocally(beatmapSetInfo))
|
||||
UpdateState(DownloadState.LocallyAvailable);
|
||||
else
|
||||
attachDownload(Downloader.GetExistingDownload(beatmapSetInfo));
|
||||
|
||||
Downloader.DownloadBegan += downloadBegan;
|
||||
Downloader.DownloadFailed += downloadFailed;
|
||||
Manager.ItemUpdated += itemUpdated;
|
||||
Manager.ItemRemoved += itemRemoved;
|
||||
realmSubscription = realmContextFactory.Context.All<BeatmapSetInfo>().Where(s => s.OnlineID == TrackedItem.OnlineID).QueryAsyncWithNotifications((items, changes, ___) =>
|
||||
{
|
||||
if (items.Any())
|
||||
Schedule(() => UpdateState(DownloadState.LocallyAvailable));
|
||||
else
|
||||
{
|
||||
Schedule(() =>
|
||||
{
|
||||
UpdateState(DownloadState.NotDownloaded);
|
||||
attachDownload(Downloader.GetExistingDownload(beatmapSetInfo));
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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 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;
|
||||
|
||||
#region Disposal
|
||||
@ -118,17 +118,13 @@ namespace osu.Game.Online
|
||||
base.Dispose(isDisposing);
|
||||
attachDownload(null);
|
||||
|
||||
realmSubscription?.Dispose();
|
||||
|
||||
if (Downloader != null)
|
||||
{
|
||||
Downloader.DownloadBegan -= downloadBegan;
|
||||
Downloader.DownloadFailed -= downloadFailed;
|
||||
}
|
||||
|
||||
if (Manager != null)
|
||||
{
|
||||
Manager.ItemUpdated -= itemUpdated;
|
||||
Manager.ItemRemoved -= itemRemoved;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
@ -187,6 +187,7 @@ namespace osu.Game.Stores
|
||||
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 void Update(TModel skin)
|
||||
|
Loading…
Reference in New Issue
Block a user