mirror of
https://github.com/ppy/osu
synced 2025-01-04 13:22:08 +00:00
Replace ScoreDownloadTracker
event flow with realm subscriptions
This commit is contained in:
parent
00e9f0d41e
commit
8c4836e87d
@ -2,7 +2,9 @@
|
|||||||
// 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.Database;
|
||||||
using osu.Game.Extensions;
|
using osu.Game.Extensions;
|
||||||
using osu.Game.Online.API;
|
using osu.Game.Online.API;
|
||||||
using osu.Game.Scoring;
|
using osu.Game.Scoring;
|
||||||
@ -13,23 +15,26 @@ namespace osu.Game.Online
|
|||||||
{
|
{
|
||||||
public class ScoreDownloadTracker : DownloadTracker<ScoreInfo>
|
public class ScoreDownloadTracker : DownloadTracker<ScoreInfo>
|
||||||
{
|
{
|
||||||
[Resolved(CanBeNull = true)]
|
|
||||||
protected ScoreManager? Manager { get; private set; }
|
|
||||||
|
|
||||||
[Resolved(CanBeNull = true)]
|
[Resolved(CanBeNull = true)]
|
||||||
protected ScoreModelDownloader? Downloader { get; private set; }
|
protected ScoreModelDownloader? Downloader { get; private set; }
|
||||||
|
|
||||||
private ArchiveDownloadRequest<IScoreInfo>? attachedRequest;
|
private ArchiveDownloadRequest<IScoreInfo>? attachedRequest;
|
||||||
|
|
||||||
|
private IDisposable? realmSubscription;
|
||||||
|
|
||||||
|
[Resolved]
|
||||||
|
private RealmContextFactory realmContextFactory { get; set; } = null!;
|
||||||
|
|
||||||
public ScoreDownloadTracker(ScoreInfo trackedItem)
|
public ScoreDownloadTracker(ScoreInfo 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;
|
||||||
|
|
||||||
// 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.
|
||||||
@ -39,15 +44,22 @@ namespace osu.Game.Online
|
|||||||
OnlineID = TrackedItem.OnlineID
|
OnlineID = TrackedItem.OnlineID
|
||||||
};
|
};
|
||||||
|
|
||||||
if (Manager.IsAvailableLocally(scoreInfo))
|
|
||||||
UpdateState(DownloadState.LocallyAvailable);
|
|
||||||
else
|
|
||||||
attachDownload(Downloader.GetExistingDownload(scoreInfo));
|
|
||||||
|
|
||||||
Downloader.DownloadBegan += downloadBegan;
|
Downloader.DownloadBegan += downloadBegan;
|
||||||
Downloader.DownloadFailed += downloadFailed;
|
Downloader.DownloadFailed += downloadFailed;
|
||||||
Manager.ItemUpdated += itemUpdated;
|
|
||||||
Manager.ItemRemoved += itemRemoved;
|
realmSubscription = realmContextFactory.Context.All<ScoreInfo>().Where(s => s.OnlineID == TrackedItem.OnlineID).QueryAsyncWithNotifications((items, changes, ___) =>
|
||||||
|
{
|
||||||
|
if (items.Any())
|
||||||
|
Schedule(() => UpdateState(DownloadState.LocallyAvailable));
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Schedule(() =>
|
||||||
|
{
|
||||||
|
UpdateState(DownloadState.NotDownloaded);
|
||||||
|
attachDownload(Downloader.GetExistingDownload(scoreInfo));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void downloadBegan(ArchiveDownloadRequest<IScoreInfo> request) => Schedule(() =>
|
private void downloadBegan(ArchiveDownloadRequest<IScoreInfo> request) => Schedule(() =>
|
||||||
@ -102,18 +114,6 @@ namespace osu.Game.Online
|
|||||||
|
|
||||||
private void onRequestFailure(Exception e) => Schedule(() => attachDownload(null));
|
private void onRequestFailure(Exception e) => Schedule(() => attachDownload(null));
|
||||||
|
|
||||||
private void itemUpdated(ScoreInfo item) => Schedule(() =>
|
|
||||||
{
|
|
||||||
if (checkEquality(item, TrackedItem))
|
|
||||||
UpdateState(DownloadState.LocallyAvailable);
|
|
||||||
});
|
|
||||||
|
|
||||||
private void itemRemoved(ScoreInfo item) => Schedule(() =>
|
|
||||||
{
|
|
||||||
if (checkEquality(item, TrackedItem))
|
|
||||||
UpdateState(DownloadState.NotDownloaded);
|
|
||||||
});
|
|
||||||
|
|
||||||
private bool checkEquality(IScoreInfo x, IScoreInfo y) => x.MatchesOnlineID(y);
|
private bool checkEquality(IScoreInfo x, IScoreInfo y) => x.MatchesOnlineID(y);
|
||||||
|
|
||||||
#region Disposal
|
#region Disposal
|
||||||
@ -123,17 +123,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
|
||||||
|
Loading…
Reference in New Issue
Block a user