1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 19:22:54 +08:00
osu-lazer/osu.Game/Online/DownloadTrackingComposite.cs

141 lines
4.6 KiB
C#
Raw Normal View History

2019-01-31 18:17:42 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
using osu.Game.Database;
using osu.Game.Online.API;
namespace osu.Game.Online
{
/// <summary>
2019-07-05 10:32:30 +08:00
/// A component which tracks a <see cref="TModel"/> through potential download/import/deletion.
/// </summary>
public abstract class DownloadTrackingComposite<TModel, TModelManager> : CompositeDrawable
2019-06-12 01:53:40 +08:00
where TModel : class, IEquatable<TModel>
2019-06-12 03:11:17 +08:00
where TModelManager : class, IModelDownloader<TModel>
{
2019-06-26 23:29:09 +08:00
protected readonly Bindable<TModel> Model = new Bindable<TModel>();
private TModelManager manager;
/// <summary>
2019-07-05 10:32:30 +08:00
/// Holds the current download state of the <see cref="TModel"/>, whether is has already been downloaded, is in progress, or is not downloaded.
/// </summary>
protected readonly Bindable<DownloadState> State = new Bindable<DownloadState>();
protected readonly Bindable<double> Progress = new Bindable<double>();
protected DownloadTrackingComposite(TModel model = null)
{
2019-06-26 23:29:09 +08:00
Model.Value = model;
}
[BackgroundDependencyLoader(true)]
private void load(TModelManager manager)
{
this.manager = manager;
2019-06-26 23:29:09 +08:00
Model.BindValueChanged(modelInfo =>
{
if (modelInfo.NewValue == null)
attachDownload(null);
else if (manager.IsAvailableLocally(modelInfo.NewValue))
State.Value = DownloadState.LocallyAvailable;
else
attachDownload(manager.GetExistingDownload(modelInfo.NewValue));
}, true);
manager.DownloadBegan += download =>
{
2019-06-26 23:29:09 +08:00
if (download.Model.Equals(Model.Value))
attachDownload(download);
};
2019-06-29 13:25:30 +08:00
manager.DownloadFailed += download =>
{
if (download.Model.Equals(Model.Value))
attachDownload(null);
};
manager.ItemAdded += itemAdded;
manager.ItemRemoved += itemRemoved;
}
2019-06-26 20:52:37 +08:00
private ArchiveDownloadRequest<TModel> attachedRequest;
2019-06-26 20:52:37 +08:00
private void attachDownload(ArchiveDownloadRequest<TModel> request)
{
if (attachedRequest != null)
{
attachedRequest.Failure -= onRequestFailure;
attachedRequest.DownloadProgressed -= onRequestProgress;
attachedRequest.Success -= onRequestSuccess;
}
attachedRequest = request;
if (attachedRequest != null)
{
2019-01-31 18:08:45 +08:00
if (attachedRequest.Progress == 1)
{
State.Value = DownloadState.Downloaded;
Progress.Value = 1;
}
else
{
State.Value = DownloadState.Downloading;
Progress.Value = attachedRequest.Progress;
attachedRequest.Failure += onRequestFailure;
attachedRequest.DownloadProgressed += onRequestProgress;
attachedRequest.Success += onRequestSuccess;
}
}
else
{
State.Value = DownloadState.NotDownloaded;
}
}
private void onRequestSuccess(string _) => Schedule(() => State.Value = DownloadState.Downloaded);
private void onRequestProgress(float progress) => Schedule(() => Progress.Value = progress);
private void onRequestFailure(Exception e) => Schedule(() => attachDownload(null));
2019-06-26 20:52:37 +08:00
private void itemAdded(TModel s) => setDownloadStateFromManager(s, DownloadState.LocallyAvailable);
private void itemRemoved(TModel s) => setDownloadStateFromManager(s, DownloadState.NotDownloaded);
private void setDownloadStateFromManager(TModel s, DownloadState state) => Schedule(() =>
{
2019-06-26 23:29:09 +08:00
if (!s.Equals(Model.Value))
return;
State.Value = state;
});
2019-06-26 23:29:38 +08:00
#region Disposal
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (manager != null)
{
manager.DownloadBegan -= attachDownload;
manager.ItemAdded -= itemAdded;
}
State.UnbindAll();
attachDownload(null);
}
#endregion
}
}