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.
|
2019-01-17 20:10:34 +08:00
|
|
|
|
2019-01-18 13:28:06 +08:00
|
|
|
using System;
|
|
|
|
using Microsoft.EntityFrameworkCore.Internal;
|
2019-01-17 20:10:34 +08:00
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
using osu.Framework.Bindables;
|
2019-01-17 20:10:34 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Direct
|
|
|
|
{
|
2019-02-14 15:21:01 +08:00
|
|
|
/// <summary>
|
|
|
|
/// A component which tracks a beatmap through potential download/import/deletion.
|
|
|
|
/// </summary>
|
2019-01-18 13:28:06 +08:00
|
|
|
public abstract class DownloadTrackingComposite : CompositeDrawable
|
2019-01-17 20:10:34 +08:00
|
|
|
{
|
2019-01-18 13:28:06 +08:00
|
|
|
public readonly Bindable<BeatmapSetInfo> BeatmapSet = new Bindable<BeatmapSetInfo>();
|
|
|
|
|
2019-01-17 20:10:34 +08:00
|
|
|
private BeatmapManager beatmaps;
|
|
|
|
|
2019-01-18 13:28:06 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Holds the current download state of the beatmap, whether is has already been downloaded, is in progress, or is not downloaded.
|
|
|
|
/// </summary>
|
|
|
|
protected readonly Bindable<DownloadState> State = new Bindable<DownloadState>();
|
2019-01-17 20:10:34 +08:00
|
|
|
|
2019-01-18 13:28:06 +08:00
|
|
|
protected readonly Bindable<double> Progress = new Bindable<double>();
|
2019-01-17 20:10:34 +08:00
|
|
|
|
2019-01-18 13:28:06 +08:00
|
|
|
protected DownloadTrackingComposite(BeatmapSetInfo beatmapSet = null)
|
2019-01-17 20:10:34 +08:00
|
|
|
{
|
2019-01-18 13:28:06 +08:00
|
|
|
BeatmapSet.Value = beatmapSet;
|
2019-01-17 20:10:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader(true)]
|
|
|
|
private void load(BeatmapManager beatmaps)
|
|
|
|
{
|
|
|
|
this.beatmaps = beatmaps;
|
|
|
|
|
2019-02-22 19:13:38 +08:00
|
|
|
BeatmapSet.BindValueChanged(setInfo =>
|
2019-02-14 03:04:49 +08:00
|
|
|
{
|
2019-02-22 19:13:38 +08:00
|
|
|
if (setInfo.NewValue == null)
|
2019-02-14 03:04:49 +08:00
|
|
|
attachDownload(null);
|
2019-02-22 19:13:38 +08:00
|
|
|
else if (beatmaps.QueryBeatmapSets(s => s.OnlineBeatmapSetID == setInfo.NewValue.OnlineBeatmapSetID).Any())
|
2019-02-14 03:04:49 +08:00
|
|
|
State.Value = DownloadState.LocallyAvailable;
|
|
|
|
else
|
2019-02-22 19:13:38 +08:00
|
|
|
attachDownload(beatmaps.GetExistingDownload(setInfo.NewValue));
|
2019-02-14 03:04:49 +08:00
|
|
|
}, true);
|
2019-01-18 13:28:06 +08:00
|
|
|
|
|
|
|
beatmaps.BeatmapDownloadBegan += download =>
|
|
|
|
{
|
|
|
|
if (download.BeatmapSet.OnlineBeatmapSetID == BeatmapSet.Value?.OnlineBeatmapSetID)
|
|
|
|
attachDownload(download);
|
|
|
|
};
|
2019-01-17 20:10:34 +08:00
|
|
|
|
|
|
|
beatmaps.ItemAdded += setAdded;
|
2019-02-14 03:11:46 +08:00
|
|
|
beatmaps.ItemRemoved += setRemoved;
|
2019-01-17 20:10:34 +08:00
|
|
|
}
|
|
|
|
|
2019-01-18 13:28:06 +08:00
|
|
|
#region Disposal
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
2019-01-17 20:10:34 +08:00
|
|
|
{
|
2019-01-18 13:28:06 +08:00
|
|
|
base.Dispose(isDisposing);
|
2019-01-31 18:08:54 +08:00
|
|
|
|
2019-01-18 13:28:06 +08:00
|
|
|
beatmaps.BeatmapDownloadBegan -= attachDownload;
|
|
|
|
beatmaps.ItemAdded -= setAdded;
|
2019-01-31 18:08:54 +08:00
|
|
|
|
|
|
|
State.UnbindAll();
|
|
|
|
|
|
|
|
attachDownload(null);
|
2019-01-18 13:28:06 +08:00
|
|
|
}
|
2019-01-17 20:10:34 +08:00
|
|
|
|
2019-01-18 13:28:06 +08:00
|
|
|
#endregion
|
2019-01-17 20:10:34 +08:00
|
|
|
|
2019-01-18 13:28:06 +08:00
|
|
|
private DownloadBeatmapSetRequest attachedRequest;
|
2019-01-17 20:10:34 +08:00
|
|
|
|
2019-01-18 13:28:06 +08:00
|
|
|
private void attachDownload(DownloadBeatmapSetRequest 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;
|
|
|
|
}
|
2019-01-18 13:28:06 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
State.Value = DownloadState.NotDownloaded;
|
|
|
|
}
|
2019-01-17 20:10:34 +08:00
|
|
|
}
|
|
|
|
|
2019-02-14 15:21:01 +08:00
|
|
|
private void onRequestSuccess(string _) => Schedule(() => State.Value = DownloadState.Downloaded);
|
2019-01-17 20:10:34 +08:00
|
|
|
|
2019-02-14 15:21:01 +08:00
|
|
|
private void onRequestProgress(float progress) => Schedule(() => Progress.Value = progress);
|
2019-01-17 20:10:34 +08:00
|
|
|
|
2019-02-14 15:21:01 +08:00
|
|
|
private void onRequestFailure(Exception e) => Schedule(() => attachDownload(null));
|
2019-01-17 20:10:34 +08:00
|
|
|
|
2019-02-25 17:24:06 +08:00
|
|
|
private void setAdded(BeatmapSetInfo s, bool existing) => setDownloadStateFromManager(s, DownloadState.LocallyAvailable);
|
2019-02-14 03:11:46 +08:00
|
|
|
|
2019-02-14 15:21:01 +08:00
|
|
|
private void setRemoved(BeatmapSetInfo s) => setDownloadStateFromManager(s, DownloadState.NotDownloaded);
|
2019-02-14 03:11:46 +08:00
|
|
|
|
2019-02-14 15:21:01 +08:00
|
|
|
private void setDownloadStateFromManager(BeatmapSetInfo s, DownloadState state) => Schedule(() =>
|
2019-01-17 20:10:34 +08:00
|
|
|
{
|
2019-01-18 13:28:06 +08:00
|
|
|
if (s.OnlineBeatmapSetID != BeatmapSet.Value?.OnlineBeatmapSetID)
|
2019-01-17 20:10:34 +08:00
|
|
|
return;
|
|
|
|
|
2019-02-14 15:21:01 +08:00
|
|
|
State.Value = state;
|
|
|
|
});
|
2019-01-17 20:10:34 +08:00
|
|
|
}
|
2019-01-18 13:28:06 +08:00
|
|
|
}
|